Skip to content

Minor Changes to Question Service Backend #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 21 additions & 29 deletions services/question/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,11 @@ uniqueness.

### Responses:

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

### Command Line Example:

Expand All @@ -318,12 +318,9 @@ curl -X POST http://localhost:8081/questions -H "Content-Type: application/json"
"id": 21,
"title": "New Question",
"description": "This is a description for a new question.",
"topics": [
"Data Structures",
"Algorithms"
],
"topics": ["Data Structures", "Algorithms"],
"difficulty": "Medium",
"_id": "66eedf739672ca081e9fd5ff"
"_id": "66f77e7bf9530832bd839239"
}
}
```
Expand All @@ -350,11 +347,12 @@ This endpoint allows updating an existing question. Only the title, description,

### Responses:

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

### Command Line Example:

Expand All @@ -370,14 +368,11 @@ curl -X PUT http://localhost:8081/questions/21 -H "Content-Type: application/jso
"status": "Success",
"message": "Question updated successfully",
"data": {
"_id": "66eedf739672ca081e9fd5ff",
"_id": "66f77e7bf9530832bd839239",
"id": 21,
"title": "Updated Title",
"description": "Updated description for the existing question.",
"topics": [
"Data Structures",
"Algorithms"
],
"title": "Updated Question Title",
"description": "This is the updated description.",
"topics": ["Updated Topic"],
"difficulty": "Hard"
}
}
Expand Down Expand Up @@ -418,14 +413,11 @@ curl -X DELETE http://localhost:8081/questions/21
"status": "Success",
"message": "Question deleted successfully",
"data": {
"_id": "66eedf739672ca081e9fd5ff",
"_id": "66f77e7bf9530832bd839239",
"id": 21,
"title": "Updated Title",
"description": "Updated description for the existing question.",
"topics": [
"Data Structures",
"Algorithms"
],
"title": "Duplicate Title",
"description": "This is the updated description.",
"topics": ["Updated Topic"],
"difficulty": "Hard"
}
}
Expand Down
1 change: 1 addition & 0 deletions services/question/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default tseslint.config({
],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
"@typescript-eslint/no-extraneous-class": "off",

// https://stackoverflow.com/questions/68816664/get-rid-of-error-delete-eslint-prettier-prettier-and-allow-use-double
'prettier/prettier': [
Expand Down
7 changes: 7 additions & 0 deletions services/question/src/controllers/questionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ export const updateQuestion = async (req: Request, res: Response) => {
}

try {
const existingQuestion = await Question.findOne({
$or: [{ title: updates.title }, { description: updates.description }],
}).collation({ locale: 'en', strength: 2 });
if (existingQuestion) {
return handleBadRequest(res, 'A question with the same title or description already exists.');
}

const updatedQuestion = await Question.findOneAndUpdate({ id: parseInt(id, 10) }, updates, {
new: true,
runValidators: true,
Expand Down