Skip to content

Commit aaf5883

Browse files
authored
Merge pull request #40 from KhoonSun47/init-question
Minor Changes to Question Service Backend
2 parents c1861bb + 30f8d8f commit aaf5883

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

services/question/README.md

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,11 @@ uniqueness.
295295

296296
### Responses:
297297

298-
| Response Code | Explanation |
299-
|-----------------------------|--------------------------------------------------------------------|
300-
| 201 (Created) | The question is created successfully. |
301-
| 400 (Bad Request) | Required fields are missing or invalid or question already exists. |
302-
| 500 (Internal Server Error) | Unexpected error in the database or server. |
298+
| Response Code | Explanation |
299+
|-----------------------------|---------------------------------------------------------------------|
300+
| 201 (Created) | The question is created successfully. |
301+
| 400 (Bad Request) | Required fields are missing or invalid, or question already exists. |
302+
| 500 (Internal Server Error) | Unexpected error in the database or server. |
303303

304304
### Command Line Example:
305305

@@ -318,12 +318,9 @@ curl -X POST http://localhost:8081/questions -H "Content-Type: application/json"
318318
"id": 21,
319319
"title": "New Question",
320320
"description": "This is a description for a new question.",
321-
"topics": [
322-
"Data Structures",
323-
"Algorithms"
324-
],
321+
"topics": ["Data Structures", "Algorithms"],
325322
"difficulty": "Medium",
326-
"_id": "66eedf739672ca081e9fd5ff"
323+
"_id": "66f77e7bf9530832bd839239"
327324
}
328325
}
329326
```
@@ -350,11 +347,12 @@ This endpoint allows updating an existing question. Only the title, description,
350347

351348
### Responses:
352349

353-
| Response Code | Explanation |
354-
|-----------------------------|------------------------------------------------|
355-
| 200 (OK) | Success, the question is updated successfully. |
356-
| 404 (Not Found) | Question with the specified `id` not found. |
357-
| 500 (Internal Server Error) | Unexpected error in the database or server. |
350+
| Response Code | Explanation |
351+
|-----------------------------|------------------------------------------------------------------------------------|
352+
| 200 (OK) | Success, the question is updated successfully. |
353+
| 400 (Bad Request) | Invalid request body such as including `id` or duplicate `title` or `description`. |
354+
| 404 (Not Found) | Question with the specified `id` not found. |
355+
| 500 (Internal Server Error) | Unexpected error in the database or server. |
358356

359357
### Command Line Example:
360358

@@ -370,14 +368,11 @@ curl -X PUT http://localhost:8081/questions/21 -H "Content-Type: application/jso
370368
"status": "Success",
371369
"message": "Question updated successfully",
372370
"data": {
373-
"_id": "66eedf739672ca081e9fd5ff",
371+
"_id": "66f77e7bf9530832bd839239",
374372
"id": 21,
375-
"title": "Updated Title",
376-
"description": "Updated description for the existing question.",
377-
"topics": [
378-
"Data Structures",
379-
"Algorithms"
380-
],
373+
"title": "Updated Question Title",
374+
"description": "This is the updated description.",
375+
"topics": ["Updated Topic"],
381376
"difficulty": "Hard"
382377
}
383378
}
@@ -418,14 +413,11 @@ curl -X DELETE http://localhost:8081/questions/21
418413
"status": "Success",
419414
"message": "Question deleted successfully",
420415
"data": {
421-
"_id": "66eedf739672ca081e9fd5ff",
416+
"_id": "66f77e7bf9530832bd839239",
422417
"id": 21,
423-
"title": "Updated Title",
424-
"description": "Updated description for the existing question.",
425-
"topics": [
426-
"Data Structures",
427-
"Algorithms"
428-
],
418+
"title": "Duplicate Title",
419+
"description": "This is the updated description.",
420+
"topics": ["Updated Topic"],
429421
"difficulty": "Hard"
430422
}
431423
}

services/question/eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default tseslint.config({
1414
],
1515
rules: {
1616
'@typescript-eslint/no-explicit-any': 'off',
17+
"@typescript-eslint/no-extraneous-class": "off",
1718

1819
// https://stackoverflow.com/questions/68816664/get-rid-of-error-delete-eslint-prettier-prettier-and-allow-use-double
1920
'prettier/prettier': [

services/question/src/controllers/questionController.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ export const updateQuestion = async (req: Request, res: Response) => {
200200
}
201201

202202
try {
203+
const existingQuestion = await Question.findOne({
204+
$or: [{ title: updates.title }, { description: updates.description }],
205+
}).collation({ locale: 'en', strength: 2 });
206+
if (existingQuestion) {
207+
return handleBadRequest(res, 'A question with the same title or description already exists.');
208+
}
209+
203210
const updatedQuestion = await Question.findOneAndUpdate({ id: parseInt(id, 10) }, updates, {
204211
new: true,
205212
runValidators: true,

0 commit comments

Comments
 (0)