Skip to content

Commit 7ec20fd

Browse files
committed
Add pagination to /all request
Select only necessary fields for /{id} and /all requests Edit test for /all
1 parent 43522eb commit 7ec20fd

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
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
@@ -43,12 +43,34 @@ router.post(
4343

4444
// Retrieve all questions
4545
router.get("/all", async (req: Request, res: Response) => {
46+
const pagination = parseInt(req.body.pagination as string, 10) || 1; // Default page is 1
47+
const page_size = parseInt(req.body.page_size as string, 10) || 10; // Default limit is 10
48+
const skip = (pagination - 1) * page_size; // Calculate how many documents to skip
4649
try {
47-
const questions = await Question.find()
50+
const questions = await Question.find(
51+
{},
52+
{
53+
questionid: 1,
54+
title: 1,
55+
description: 1,
56+
complexity: 1,
57+
category: 1,
58+
}
59+
)
4860
.lean()
4961
.sort({ questionid: "ascending" })
62+
.skip(skip)
63+
.limit(page_size)
5064
.exec();
51-
return res.json(questions);
65+
66+
const total = await Question.countDocuments().exec();
67+
68+
return res.json({
69+
questions,
70+
currentPage: pagination,
71+
totalPages: Math.ceil(total / page_size),
72+
totalQuestions: total,
73+
});
5274
} catch (error) {
5375
return res.status(500).send("Internal server error");
5476
}
@@ -62,7 +84,10 @@ router.get("/:id", [...idValidators], async (req: Request, res: Response) => {
6284
}
6385
const questionId = parseInt(req.params.id, 10);
6486
try {
65-
const question = await Question.findOne({ questionid: questionId }).exec();
87+
const question = await Question.findOne(
88+
{ questionid: questionId },
89+
{ questionid: 1, title: 1, description: 1, complexity: 1, category: 1 }
90+
).exec();
6691
if (!question) {
6792
return res.status(404).json({ message: "Question not found" });
6893
}

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ describe("Test Question API", () => {
9797

9898
const res = await request.post("/api/create").send(newQuestion);
9999
expect(res.statusCode).toBe(400);
100-
expect(res.body.errors[0].msg).toBe("Category must contain only non-empty strings");
100+
expect(res.body.errors[0].msg).toBe(
101+
"Category must contain only non-empty strings"
102+
);
101103
expect(res.body.errors[0].path).toBe("category");
102104
});
103105

@@ -172,7 +174,9 @@ describe("Test Question API", () => {
172174

173175
const res = await request.post("/api/create").send(newQuestion);
174176
expect(res.statusCode).toBe(400);
175-
expect(res.body.errors[0].msg).toBe("Category must contain only non-empty strings");
177+
expect(res.body.errors[0].msg).toBe(
178+
"Category must contain only non-empty strings"
179+
);
176180
expect(res.body.errors[0].path).toBe("category");
177181
});
178182

@@ -187,7 +191,9 @@ describe("Test Question API", () => {
187191

188192
const res = await request.post("/api/create").send(newQuestion);
189193
expect(res.statusCode).toBe(400);
190-
expect(res.body.errors[0].msg).toBe("Category must contain only non-empty strings");
194+
expect(res.body.errors[0].msg).toBe(
195+
"Category must contain only non-empty strings"
196+
);
191197
expect(res.body.errors[0].path).toBe("category");
192198
});
193199

@@ -202,7 +208,9 @@ describe("Test Question API", () => {
202208

203209
const res = await request.post("/api/create").send(newQuestion);
204210
expect(res.statusCode).toBe(400);
205-
expect(res.body.errors[0].msg).toBe("Category must contain only non-empty strings");
211+
expect(res.body.errors[0].msg).toBe(
212+
"Category must contain only non-empty strings"
213+
);
206214
expect(res.body.errors[0].path).toBe("category");
207215
});
208216

@@ -227,10 +235,13 @@ describe("Test Get All", () => {
227235
// Get all with questions
228236
test("GET /api/all - should retrieve all questions", async () => {
229237
const res = await request.get("/api/all").send();
230-
const sampleQuestion = res.body[0];
238+
const sampleQuestion = res.body.questions[0];
231239
expect(res.statusCode).toBe(200);
232-
expect(Array.isArray(res.body)).toBe(true);
233-
expect(res.body.length).toBe(1);
240+
expect(Array.isArray(res.body.questions)).toBe(true);
241+
expect(res.body.questions.length).toBe(1);
242+
expect(res.body.currentPage).toBe(1);
243+
expect(res.body.totalPages).toBe(1);
244+
expect(res.body.totalQuestions).toBe(1);
234245
expect(sampleQuestion).toHaveProperty("title", "Sample Question");
235246
expect(sampleQuestion).toHaveProperty(
236247
"description",
@@ -326,7 +337,7 @@ describe("Test Update", () => {
326337
// Update with invalid category
327338
test("POST - empty category", async () => {
328339
const updateQuestion = {
329-
category: []
340+
category: [],
330341
};
331342
const questionId = 1090;
332343
const res = await request
@@ -347,7 +358,9 @@ describe("Test Update", () => {
347358
.post(`/api/${questionId}/update`)
348359
.send(updateQuestion);
349360
expect(res.statusCode).toBe(400);
350-
expect(res.body.errors[0].msg).toBe("Category must contain only non-empty strings");
361+
expect(res.body.errors[0].msg).toBe(
362+
"Category must contain only non-empty strings"
363+
);
351364
});
352365

353366
// Update with invalid category
@@ -362,7 +375,9 @@ describe("Test Update", () => {
362375
.post(`/api/${questionId}/update`)
363376
.send(updateQuestion);
364377
expect(res.statusCode).toBe(400);
365-
expect(res.body.errors[0].msg).toBe("Category must contain only non-empty strings");
378+
expect(res.body.errors[0].msg).toBe(
379+
"Category must contain only non-empty strings"
380+
);
366381
});
367382

368383
// Negative id

0 commit comments

Comments
 (0)