Skip to content

Commit 9266593

Browse files
authored
Merge pull request #8 from nicolelim02/feat/questions
Add delete endpoint and document the apis
2 parents 793207f + 849af5f commit 9266593

File tree

16 files changed

+337
-184
lines changed

16 files changed

+337
-184
lines changed

backend/question-service/package-lock.json

Lines changed: 0 additions & 146 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/question-service/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"type": "module",
66
"scripts": {
77
"start": "tsx server.ts",
8+
"dev": "tsx watch server.ts",
89
"test": "echo \"Error: no test specified\" && exit 1",
910
"lint": "eslint ."
1011
},
@@ -34,7 +35,6 @@
3435
"@types/swagger-ui-express": "^4.1.6",
3536
"eslint": "^9.10.0",
3637
"globals": "^15.9.0",
37-
"nodemon": "^3.1.4",
3838
"prettier": "^3.3.3",
3939
"ts-node": "^10.9.2",
4040
"tsx": "^4.19.1",

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import {
55
DUPLICATE_QUESTION_RESPONSE_MESSAGE,
66
QN_DESC_EXCEED_CHAR_LIMIT_RESPONSE_MESSAGE,
77
QN_DESC_CHAR_LIMIT,
8+
QN_NOT_FOUND,
9+
QN_DELETED,
10+
SERVER_ERROR,
811
} from "../utils/constants.ts";
912

1013
import { upload } from "../../config/multer";
@@ -46,7 +49,7 @@ export const createQuestion = async (
4649
question: newQuestion,
4750
});
4851
} catch (error) {
49-
res.status(500).json({ message: "Server error", error });
52+
res.status(500).json({ message: SERVER_ERROR, error });
5053
}
5154
};
5255

@@ -89,7 +92,7 @@ export const updateQuestion = async (
8992

9093
const currentQuestion = await Question.findById(id);
9194
if (!currentQuestion) {
92-
res.status(404).json({ message: "Question not found" });
95+
res.status(404).json({ message: QN_NOT_FOUND });
9396
return;
9497
}
9598

@@ -117,7 +120,25 @@ export const updateQuestion = async (
117120
question: updatedQuestion,
118121
});
119122
} catch (error) {
120-
console.log(error);
121-
res.status(500).json({ message: "Server error", error });
123+
res.status(500).json({ message: SERVER_ERROR, error });
124+
}
125+
};
126+
127+
export const deleteQuestion = async (
128+
req: Request,
129+
res: Response,
130+
): Promise<void> => {
131+
try {
132+
const { id } = req.params;
133+
const currentQuestion = await Question.findById(id);
134+
if (!currentQuestion) {
135+
res.status(400).json({ message: QN_NOT_FOUND });
136+
return;
137+
}
138+
139+
await Question.findByIdAndDelete(id);
140+
res.status(200).json({ message: QN_DELETED });
141+
} catch (error) {
142+
res.status(500).json({ message: SERVER_ERROR, error });
122143
}
123144
};

backend/question-service/src/routes/questionRoutes.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import express from "express";
22
import {
33
createQuestion,
4+
deleteQuestion,
45
createImageLink,
56
updateQuestion,
67
} from "../controllers/questionController.ts";
@@ -13,4 +14,6 @@ router.post("/questions/images", createImageLink);
1314

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

17+
router.delete("/questions/:id", deleteQuestion);
18+
1619
export default router;

backend/question-service/src/utils/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ export const QN_DESC_EXCEED_CHAR_LIMIT_RESPONSE_MESSAGE =
55

66
export const DUPLICATE_QUESTION_RESPONSE_MESSAGE =
77
"Duplicate question: A question with the same title already exists.";
8+
9+
export const QN_NOT_FOUND = "Question not found.";
10+
11+
export const QN_DELETED = "Question deleted successfully.";
12+
13+
export const SERVER_ERROR = "Server error.";

0 commit comments

Comments
 (0)