Skip to content

Commit c40f85c

Browse files
committed
Add duplicate check for question creation
1 parent 750e89b commit c40f85c

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

backend/question-service/src/controllers/questionController.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,28 @@ export const createQuestion = async (
77
): Promise<void> => {
88
try {
99
const { title, description, complexity, category } = req.body;
10+
11+
const existingQuestion = await Question.findOne({
12+
title: new RegExp(`^${title}$`, "i"),
13+
});
14+
15+
if (existingQuestion) {
16+
res.status(400).json({
17+
message:
18+
"Duplicate question: A question with the same title already exists.",
19+
});
20+
return;
21+
}
22+
1023
const newQuestion = new Question({
1124
title,
1225
description,
1326
complexity,
1427
category,
1528
});
29+
1630
await newQuestion.save();
31+
1732
res.status(201).json({
1833
message: "Question created successfully",
1934
question: newQuestion,

0 commit comments

Comments
 (0)