Skip to content

Commit 71503b9

Browse files
committed
Add duplicate check for update question
1 parent c40f85c commit 71503b9

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Request, Response } from "express";
22
import Question from "../models/Question.ts";
3+
import { checkIsExistingQuestion } from "../utils/utils.ts";
4+
import { DUPLICATE_QUESTION_RESPONSE_MESSAGE } from "../utils/constants.ts";
35

46
export const createQuestion = async (
57
req: Request,
@@ -8,14 +10,10 @@ export const createQuestion = async (
810
try {
911
const { title, description, complexity, category } = req.body;
1012

11-
const existingQuestion = await Question.findOne({
12-
title: new RegExp(`^${title}$`, "i"),
13-
});
14-
13+
const existingQuestion = await checkIsExistingQuestion(title);
1514
if (existingQuestion) {
1615
res.status(400).json({
17-
message:
18-
"Duplicate question: A question with the same title already exists.",
16+
message: DUPLICATE_QUESTION_RESPONSE_MESSAGE,
1917
});
2018
return;
2119
}
@@ -44,14 +42,26 @@ export const updateQuestion = async (
4442
): Promise<void> => {
4543
try {
4644
const { id } = req.params;
47-
const updatedQuestion = await Question.findByIdAndUpdate(id, req.body, {
48-
new: true,
49-
});
50-
if (!updatedQuestion) {
45+
const { title } = req.body;
46+
47+
const currentQuestion = await Question.findById(id);
48+
if (!currentQuestion) {
5149
res.status(404).json({ message: "Question not found" });
5250
return;
5351
}
5452

53+
const existingQuestion = await checkIsExistingQuestion(title, id);
54+
if (existingQuestion) {
55+
res.status(400).json({
56+
message: DUPLICATE_QUESTION_RESPONSE_MESSAGE,
57+
});
58+
return;
59+
}
60+
61+
const updatedQuestion = await Question.findByIdAndUpdate(id, req.body, {
62+
new: true,
63+
});
64+
5565
res.status(200).json({
5666
message: "Question updated successfully",
5767
question: updatedQuestion,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const DUPLICATE_QUESTION_RESPONSE_MESSAGE =
2+
"Duplicate question: A question with the same title already exists.";
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import mongoose from "mongoose";
2+
3+
import Question from "../models/Question";
4+
5+
export const checkIsExistingQuestion = async (
6+
title: string,
7+
idToExcludeFromCheck = "",
8+
) => {
9+
const objectIdToExclude = idToExcludeFromCheck
10+
? new mongoose.Types.ObjectId(idToExcludeFromCheck)
11+
: null;
12+
13+
return await Question.findOne({
14+
title: new RegExp(`^${title}$`, "i"),
15+
_id: { $ne: objectIdToExclude }, // Exclude current question's ID if provided
16+
});
17+
};

0 commit comments

Comments
 (0)