Skip to content

Commit 4efaaca

Browse files
authored
Merge pull request #64 from CS3219-AY2425S1/jehou/questiondb
Add pagination and selection
2 parents be27d77 + 7ec20fd commit 4efaaca

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

backend/question/src/models/Question.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import mongooseAutoIncrement from "mongoose-sequence";
99
export type TQuestion = {
1010
title: string;
1111
description: string;
12-
category: string;
12+
category: [string];
1313
complexity: string;
1414
};
1515

backend/question/src/routes/questionRoutes.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,34 @@ router.post(
5151

5252
// Retrieve all questions
5353
router.get("/all", async (req: Request, res: Response) => {
54+
const pagination = parseInt(req.body.pagination as string, 10) || 1; // Default page is 1
55+
const page_size = parseInt(req.body.page_size as string, 10) || 10; // Default limit is 10
56+
const skip = (pagination - 1) * page_size; // Calculate how many documents to skip
5457
try {
55-
const questions = await Question.find()
58+
const questions = await Question.find(
59+
{},
60+
{
61+
questionid: 1,
62+
title: 1,
63+
description: 1,
64+
complexity: 1,
65+
category: 1,
66+
}
67+
)
5668
.lean()
5769
.sort({ questionid: "ascending" })
70+
.skip(skip)
71+
.limit(page_size)
5872
.exec();
59-
return res.json(questions);
73+
74+
const total = await Question.countDocuments().exec();
75+
76+
return res.json({
77+
questions,
78+
currentPage: pagination,
79+
totalPages: Math.ceil(total / page_size),
80+
totalQuestions: total,
81+
});
6082
} catch (error) {
6183
return res.status(500).send("Internal server error");
6284
}
@@ -70,7 +92,10 @@ router.get("/:id", [...idValidators], async (req: Request, res: Response) => {
7092
}
7193
const questionId = parseInt(req.params.id, 10);
7294
try {
73-
const question = await Question.findOne({ questionid: questionId }).exec();
95+
const question = await Question.findOne(
96+
{ questionid: questionId },
97+
{ questionid: 1, title: 1, description: 1, complexity: 1, category: 1 }
98+
).exec();
7499
if (!question) {
75100
return res.status(404).json({ message: "Question not found" });
76101
}

backend/question/src/tests/app.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,13 @@ describe("Test Get All", () => {
249249
// Get all with questions
250250
test("GET /api/all - should retrieve all questions", async () => {
251251
const res = await request.get("/api/all").send();
252-
const sampleQuestion = res.body[0];
252+
const sampleQuestion = res.body.questions[0];
253253
expect(res.statusCode).toBe(200);
254-
expect(Array.isArray(res.body)).toBe(true);
255-
expect(res.body.length).toBe(1);
254+
expect(Array.isArray(res.body.questions)).toBe(true);
255+
expect(res.body.questions.length).toBe(1);
256+
expect(res.body.currentPage).toBe(1);
257+
expect(res.body.totalPages).toBe(1);
258+
expect(res.body.totalQuestions).toBe(1);
256259
expect(sampleQuestion).toHaveProperty("title", "Sample Question");
257260
expect(sampleQuestion).toHaveProperty(
258261
"description",

0 commit comments

Comments
 (0)