Skip to content

Commit 755ab89

Browse files
committed
Add duplicate question title check
1 parent 6662392 commit 755ab89

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

backend/question/src/routes/questionRoutes.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ router.post(
2828
}
2929
try {
3030
const { title, description, category, complexity } = req.body;
31+
32+
const existingQuestion = await Question.findOne({ title: title.trim() });
33+
if (existingQuestion) {
34+
return res.status(400).json({
35+
message: "A question with this title already exists",
36+
});
37+
}
38+
3139
const question = { title, description, category, complexity };
3240
const newQuestion = new Question(question);
3341
await newQuestion.save();
@@ -85,6 +93,15 @@ router.post(
8593
const questionId = parseInt(req.params.id);
8694
const updateData: Partial<TQuestion> = {};
8795
if (req.body.title) {
96+
const existingQuestion = await Question.findOne({
97+
title: req.body.title.trim(),
98+
});
99+
if (existingQuestion) {
100+
return res.status(400).json({
101+
message: "A question with this title already exists",
102+
});
103+
}
104+
88105
updateData.title = req.body.title;
89106
}
90107
if (req.body.description) {

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

Lines changed: 51 additions & 9 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

@@ -220,6 +228,20 @@ describe("Test Question API", () => {
220228
expect(res.body.errors[0].msg).toBe("Invalid value");
221229
expect(res.body.errors[0].path).toBe("complexity");
222230
});
231+
232+
// Duplicate question
233+
test("POST /api/create - duplicate question", async () => {
234+
const newQuestion = {
235+
title: "Sample Question",
236+
description: "This is a sample question",
237+
category: ["General"],
238+
complexity: "test",
239+
};
240+
241+
const res = await request.post("/api/create").send(newQuestion);
242+
expect(res.statusCode).toBe(400);
243+
expect(res.body.message).toBe("A question with this title already exists");
244+
});
223245
});
224246

225247
// Test /api/all
@@ -326,7 +348,7 @@ describe("Test Update", () => {
326348
// Update with invalid category
327349
test("POST - empty category", async () => {
328350
const updateQuestion = {
329-
category: []
351+
category: [],
330352
};
331353
const questionId = 1090;
332354
const res = await request
@@ -347,7 +369,9 @@ describe("Test Update", () => {
347369
.post(`/api/${questionId}/update`)
348370
.send(updateQuestion);
349371
expect(res.statusCode).toBe(400);
350-
expect(res.body.errors[0].msg).toBe("Category must contain only non-empty strings");
372+
expect(res.body.errors[0].msg).toBe(
373+
"Category must contain only non-empty strings"
374+
);
351375
});
352376

353377
// Update with invalid category
@@ -362,13 +386,15 @@ describe("Test Update", () => {
362386
.post(`/api/${questionId}/update`)
363387
.send(updateQuestion);
364388
expect(res.statusCode).toBe(400);
365-
expect(res.body.errors[0].msg).toBe("Category must contain only non-empty strings");
389+
expect(res.body.errors[0].msg).toBe(
390+
"Category must contain only non-empty strings"
391+
);
366392
});
367393

368394
// Negative id
369395
test("POST - negative id update", async () => {
370396
const updateQuestion = {
371-
title: "Update Title",
397+
title: "Update Title Again",
372398
description: "Update Description",
373399
category: ["Update Category"],
374400
complexity: "Update Complexity",
@@ -384,7 +410,7 @@ describe("Test Update", () => {
384410
// Non-existent id
385411
test("POST - non-existent id update", async () => {
386412
const updateQuestion = {
387-
title: "Update Title",
413+
title: "Update Title Again",
388414
description: "Update Description",
389415
category: ["Update Category"],
390416
complexity: "Update Complexity",
@@ -396,6 +422,22 @@ describe("Test Update", () => {
396422
expect(res.statusCode).toBe(404);
397423
expect(res.body).toBe("Document not found");
398424
});
425+
426+
// Duplicate question
427+
test("POST - non-existent id update", async () => {
428+
const updateQuestion = {
429+
title: "Update Title",
430+
description: "Update Description",
431+
category: ["Update Category"],
432+
complexity: "Update Complexity",
433+
};
434+
const questionId = 999999;
435+
const res = await request
436+
.post(`/api/${questionId}/update`)
437+
.send(updateQuestion);
438+
expect(res.statusCode).toBe(400);
439+
expect(res.body.message).toBe("A question with this title already exists");
440+
});
399441
});
400442

401443
// Test /api/{id}/delete

0 commit comments

Comments
 (0)