Skip to content

Commit

Permalink
Add delete endpoint and document the apis
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolelim02 committed Sep 21, 2024
1 parent 2176d08 commit 435a293
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 4 deletions.
1 change: 1 addition & 0 deletions backend/question-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 ."
},
Expand Down
28 changes: 25 additions & 3 deletions backend/question-service/src/controllers/questionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 });
}
};

Expand All @@ -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;
}

Expand Down Expand Up @@ -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<void> => {
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 });
}
};
3 changes: 3 additions & 0 deletions backend/question-service/src/routes/questionRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from "express";
import {
createQuestion,
deleteQuestion,
updateQuestion,
} from "../controllers/questionController.ts";

Expand All @@ -10,4 +11,6 @@ router.post("/questions", createQuestion);

router.put("/questions/:id", updateQuestion);

router.delete("/questions/:id", deleteQuestion);

export default router;
6 changes: 6 additions & 0 deletions backend/question-service/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
181 changes: 180 additions & 1 deletion backend/question-service/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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"

0 comments on commit 435a293

Please sign in to comment.