Skip to content

Commit 05d523b

Browse files
authored
Merge pull request #101 from CS3219-AY2425S1/jehou/questiondb
Enable soft deletes
2 parents 49d30b8 + fd32703 commit 05d523b

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

backend/question/src/models/Question.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type TQuestion = {
1111
description: string;
1212
category: [string];
1313
complexity: string;
14+
deleted: boolean;
1415
};
1516

1617
// Document provides an id field
@@ -34,6 +35,10 @@ const questionSchema: Schema = new Schema(
3435
type: String,
3536
required: true,
3637
},
38+
deleted: {
39+
type: Boolean,
40+
required: true,
41+
},
3742
},
3843
{ collection: "questions" }
3944
);

backend/question/src/routes/questionRoutes.ts

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ router.post(
3636
});
3737
}
3838

39-
const question = { title, description, category, complexity };
39+
const question = {
40+
title,
41+
description,
42+
category,
43+
complexity,
44+
deleted: false,
45+
};
4046
const newQuestion = new Question(question);
4147
await newQuestion.save();
4248
return res.status(200).json({
@@ -56,7 +62,7 @@ router.post("/all", async (req: Request, res: Response) => {
5662
const skip = (pagination - 1) * page_size; // Calculate how many documents to skip
5763
try {
5864
const questions = await Question.find(
59-
{},
65+
{ deleted: false },
6066
{
6167
questionid: 1,
6268
title: 1,
@@ -71,7 +77,7 @@ router.post("/all", async (req: Request, res: Response) => {
7177
.limit(page_size)
7278
.exec();
7379

74-
const total = await Question.countDocuments().exec();
80+
const total = await Question.countDocuments({ deleted: false }).exec();
7581
const totalPages = Math.ceil(total / page_size);
7682
if (totalPages < pagination) pagination = 1;
7783

@@ -96,12 +102,22 @@ router.get("/:id", [...idValidators], async (req: Request, res: Response) => {
96102
try {
97103
const question = await Question.findOne(
98104
{ questionid: questionId },
99-
{ questionid: 1, title: 1, description: 1, complexity: 1, category: 1 }
105+
{
106+
questionid: 1,
107+
title: 1,
108+
description: 1,
109+
complexity: 1,
110+
category: 1,
111+
deleted: 1,
112+
}
100113
).exec();
101-
if (!question) {
114+
if (!question || question.deleted) {
102115
return res.status(404).json({ message: "Question not found" });
103116
}
104-
return res.json(question);
117+
118+
const { deleted, ...responseQuestion } = question.toObject();
119+
120+
return res.json(responseQuestion);
105121
} catch (error) {
106122
return res.status(500).send("Internal server error");
107123
}
@@ -142,6 +158,21 @@ router.post(
142158
}
143159

144160
try {
161+
const question = await Question.findOne(
162+
{ questionid: questionId },
163+
{
164+
questionid: 1,
165+
title: 1,
166+
description: 1,
167+
complexity: 1,
168+
category: 1,
169+
deleted: 1,
170+
}
171+
).exec();
172+
if (!question || question.deleted) {
173+
return res.status(404).json({ message: "Question not found" });
174+
}
175+
145176
const updatedQuestion = await Question.findOneAndUpdate(
146177
{ questionid: questionId },
147178
{ $set: updateData },
@@ -170,9 +201,11 @@ router.post(
170201

171202
const questionId = parseInt(req.params.id);
172203
try {
173-
const deletedQuestion = await Question.findOneAndDelete({
174-
questionid: questionId,
175-
}).exec();
204+
const deletedQuestion = await Question.findOneAndUpdate(
205+
{ questionid: questionId },
206+
{ $set: { deleted: true } },
207+
{ new: true }
208+
).exec();
176209
return res.json(deletedQuestion);
177210
} catch (error) {
178211
//to catch pre-middleware defined error

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ describe("Test Update", () => {
407407
.post(`/api/${questionId}/update`)
408408
.send(updateQuestion);
409409
expect(res.statusCode).toBe(404);
410-
expect(res.body).toBe("Document not found");
410+
expect(res.body.message).toBe("Question not found");
411411
});
412412

413413
// Non-existent id
@@ -423,7 +423,7 @@ describe("Test Update", () => {
423423
.post(`/api/${questionId}/update`)
424424
.send(updateQuestion);
425425
expect(res.statusCode).toBe(404);
426-
expect(res.body).toBe("Document not found");
426+
expect(res.body.message).toBe("Question not found");
427427
});
428428

429429
// Duplicate question

0 commit comments

Comments
 (0)