Skip to content

Commit

Permalink
Merge pull request #8 from nicolelim02/feat/questions
Browse files Browse the repository at this point in the history
Add delete endpoint and document the apis
  • Loading branch information
ruiqi7 authored Sep 23, 2024
2 parents 793207f + 849af5f commit 9266593
Show file tree
Hide file tree
Showing 16 changed files with 337 additions and 184 deletions.
146 changes: 0 additions & 146 deletions backend/question-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion 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 Expand Up @@ -34,7 +35,6 @@
"@types/swagger-ui-express": "^4.1.6",
"eslint": "^9.10.0",
"globals": "^15.9.0",
"nodemon": "^3.1.4",
"prettier": "^3.3.3",
"ts-node": "^10.9.2",
"tsx": "^4.19.1",
Expand Down
29 changes: 25 additions & 4 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";

import { upload } from "../../config/multer";
Expand Down Expand Up @@ -46,7 +49,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 Down Expand Up @@ -89,7 +92,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 @@ -117,7 +120,25 @@ export const updateQuestion = async (
question: updatedQuestion,
});
} catch (error) {
console.log(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.status(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,
createImageLink,
updateQuestion,
} from "../controllers/questionController.ts";
Expand All @@ -13,4 +14,6 @@ router.post("/questions/images", createImageLink);

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.";
Loading

0 comments on commit 9266593

Please sign in to comment.