From 435a2933c5f34affd7526ebabf900f83e7009398 Mon Sep 17 00:00:00 2001 From: Nicole Lim Date: Sat, 21 Sep 2024 12:25:08 +0800 Subject: [PATCH] Add delete endpoint and document the apis --- backend/question-service/package.json | 1 + .../src/controllers/questionController.ts | 28 ++- .../src/routes/questionRoutes.ts | 3 + .../question-service/src/utils/constants.ts | 6 + backend/question-service/swagger.yml | 181 +++++++++++++++++- 5 files changed, 215 insertions(+), 4 deletions(-) diff --git a/backend/question-service/package.json b/backend/question-service/package.json index 5dd69f8341..51152a0df4 100644 --- a/backend/question-service/package.json +++ b/backend/question-service/package.json @@ -5,6 +5,7 @@ "type": "module", "scripts": { "start": "tsx server.ts", + "dev": "tsx watch server.ts", "test": "echo \"Error: no test specified\" && exit 1", "lint": "eslint ." }, diff --git a/backend/question-service/src/controllers/questionController.ts b/backend/question-service/src/controllers/questionController.ts index 632f02ccc6..79ed1b56ff 100644 --- a/backend/question-service/src/controllers/questionController.ts +++ b/backend/question-service/src/controllers/questionController.ts @@ -5,6 +5,9 @@ import { DUPLICATE_QUESTION_RESPONSE_MESSAGE, QN_DESC_EXCEED_CHAR_LIMIT_RESPONSE_MESSAGE, QN_DESC_CHAR_LIMIT, + QN_NOT_FOUND, + QN_DELETED, + SERVER_ERROR, } from "../utils/constants.ts"; export const createQuestion = async ( @@ -43,7 +46,7 @@ export const createQuestion = async ( question: newQuestion, }); } catch (error) { - res.status(500).json({ message: "Server error", error }); + res.status(500).json({ message: SERVER_ERROR, error }); } }; @@ -57,7 +60,7 @@ export const updateQuestion = async ( const currentQuestion = await Question.findById(id); if (!currentQuestion) { - res.status(404).json({ message: "Question not found" }); + res.status(404).json({ message: QN_NOT_FOUND }); return; } @@ -85,6 +88,25 @@ export const updateQuestion = async ( question: updatedQuestion, }); } catch (error) { - res.status(500).json({ message: "Server error", error }); + res.status(500).json({ message: SERVER_ERROR, error }); + } +}; + +export const deleteQuestion = async ( + req: Request, + res: Response, +): Promise => { + try { + const { id } = req.params; + const currentQuestion = await Question.findById(id); + if (!currentQuestion) { + res.status(400).json({ message: QN_NOT_FOUND }); + return; + } + + await Question.findByIdAndDelete(id); + res.status(200).json({ message: QN_DELETED }); + } catch (error) { + res.send(500).json({ message: SERVER_ERROR, error }); } }; diff --git a/backend/question-service/src/routes/questionRoutes.ts b/backend/question-service/src/routes/questionRoutes.ts index 1d63c9824d..c1c514b8bc 100644 --- a/backend/question-service/src/routes/questionRoutes.ts +++ b/backend/question-service/src/routes/questionRoutes.ts @@ -1,6 +1,7 @@ import express from "express"; import { createQuestion, + deleteQuestion, updateQuestion, } from "../controllers/questionController.ts"; @@ -10,4 +11,6 @@ router.post("/questions", createQuestion); router.put("/questions/:id", updateQuestion); +router.delete("/questions/:id", deleteQuestion); + export default router; diff --git a/backend/question-service/src/utils/constants.ts b/backend/question-service/src/utils/constants.ts index 36caf31102..45a2486165 100644 --- a/backend/question-service/src/utils/constants.ts +++ b/backend/question-service/src/utils/constants.ts @@ -5,3 +5,9 @@ export const QN_DESC_EXCEED_CHAR_LIMIT_RESPONSE_MESSAGE = export const DUPLICATE_QUESTION_RESPONSE_MESSAGE = "Duplicate question: A question with the same title already exists."; + +export const QN_NOT_FOUND = "Question not found."; + +export const QN_DELETED = "Question deleted successfully."; + +export const SERVER_ERROR = "Server error."; diff --git a/backend/question-service/swagger.yml b/backend/question-service/swagger.yml index da2a0f8b8c..d9c0ca069a 100644 --- a/backend/question-service/swagger.yml +++ b/backend/question-service/swagger.yml @@ -4,6 +4,71 @@ info: title: Question Service version: 1.0.0 +components: + schemas: + Question: + properties: + title: + type: string + description: Title + description: + type: string + description: Description + complexity: + type: string + description: Complexity - Easy, Medium, Hard + category: + type: array + items: + type: string + description: Categories + +definitions: + Question: + type: object + properties: + _id: + type: string + description: Question id + title: + type: string + description: Title + description: + type: string + description: Description + complexity: + type: string + description: Complexity - Easy, Medium, Hard + category: + type: array + items: + type: string + description: Categories + createdAt: + type: string + description: Date of creation + updatedAt: + type: string + description: Latest update + __v: + type: string + description: Document version + Error: + type: object + properties: + message: + type: string + deescription: Message + ServerError: + type: object + properties: + message: + type: string + description: Message + error: + type: string + description: Error + paths: /: get: @@ -12,7 +77,109 @@ paths: summary: Root description: Ping the server responses: - "200": + 200: + description: Successful Response + content: + application/json: + schema: + type: object + properties: + message: + type: string + description: Message + /api/questions: + post: + tags: + - questions + summary: Creates a question + description: Creates a question + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/Question" + responses: + 201: + description: Created + content: + application/json: + schema: + type: object + properties: + message: + type: string + description: Message + question: + $ref: "#/definitions/Question" + 400: + description: Bad Request + content: + application/json: + schema: + $ref: "#/definitions/Error" + 500: + description: Internal Server Error + content: + application/json: + schema: + $ref: "#/definitions/ServerError" + /api/questions/{id}: + put: + tags: + - questions + summary: Updates a question + description: Updates a question + parameters: + - in: path + name: id + type: string + required: true + description: Question id + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/Question" + responses: + 200: + description: Successful Response + content: + application/json: + schema: + type: object + properties: + message: + type: string + description: Message + question: + $ref: "#/definitions/Question" + 400: + description: Bad Request + content: + application/json: + schema: + $ref: "#/definitions/Error" + 500: + description: Internal Server Error + content: + application/json: + schema: + $ref: "#/definitions/ServerError" + delete: + tags: + - questions + summary: Deletes a question + description: Deletes a question + parameters: + - in: path + name: id + type: string + required: true + description: Question id + responses: + 200: description: Successful Response content: application/json: @@ -22,3 +189,15 @@ paths: message: type: string description: Message + 400: + description: Bad Request + content: + application/json: + schema: + $ref: "#/definitions/Error" + 500: + description: Internal Server Error + content: + application/json: + schema: + $ref: "#/definitions/ServerError"