Skip to content

Commit

Permalink
Fix deleting of questions should the test case folder not exist (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eclipse-Dominator authored Nov 15, 2023
1 parent 70832f9 commit 4fb3fcd
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions question_service/src/controller/questionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,20 @@ export const updateQuestion = async (req : any, res : any) => {
// @access admin only
export const deleteQuestion = async (req : any, res : any) => {
try {
// function provided by mongoose to find an Question document with a given ID
// req.params.id is retrieved from /:id in route
const question = await Question.findById(req.params.id);
if(question === null) {
throw Error('Invalid ID. Question not found in database.');
}
// function provided by mongoose to find an Question document with a given ID
// req.params.id is retrieved from /:id in route
const question = await Question.findById(req.params.id);
if (question === null) {
throw Error("Invalid ID. Question not found in database.");
}

await fs.rm(`/app/question_test_cases/${question._id}`);
await question.deleteOne();
res.status(200).json({ message: 'Question removed' });
const testcases = `/app/question_test_cases/${question._id}/`;
if (fs.existsSync(testcases)) {
await fs.rm(testcases, { recursive: true });
}
await question.deleteOne();
res.status(200).json({ message: "Question removed" });
} catch (error: any) {
res.status(404).json({ message: error.message })
res.status(404).json({ message: error.message });
}
}

0 comments on commit 4fb3fcd

Please sign in to comment.