Skip to content

Commit 23baef4

Browse files
committed
Add duplication handling
1 parent fd8573b commit 23baef4

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

question-service/src/routes/questionsController.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ router.put('/:id', async (req: Request, res: Response) => {
4242
}
4343

4444
try {
45+
// Check for duplicate question
46+
const existingQuestion = await questionsCollection.findOne({
47+
title: parsedResult.data.title,
48+
});
49+
50+
if (existingQuestion) {
51+
return res.status(409).json({
52+
error: 'This question title already exist',
53+
});
54+
}
55+
4556
const result = await questionsCollection.updateOne(
4657
{ _id: new ObjectId(id) },
4758
{ $set: parsedResult.data },
@@ -71,6 +82,17 @@ router.post('/', async (req: Request, res: Response) => {
7182
.json({ error: 'Invalid question data. Please check your input.' });
7283
}
7384
try {
85+
// Check for duplicate question
86+
const existingQuestion = await questionsCollection.findOne({
87+
title: parseResult.data.title,
88+
});
89+
90+
if (existingQuestion) {
91+
return res.status(409).json({
92+
error: 'This question title already exist',
93+
});
94+
}
95+
7496
const result = await questionsCollection.insertOne(parseResult.data);
7597
res
7698
.status(201)

0 commit comments

Comments
 (0)