Skip to content

Commit 4fb3fcd

Browse files
Fix deleting of questions should the test case folder not exist (#77)
1 parent 70832f9 commit 4fb3fcd

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

question_service/src/controller/questionController.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,20 @@ export const updateQuestion = async (req : any, res : any) => {
214214
// @access admin only
215215
export const deleteQuestion = async (req : any, res : any) => {
216216
try {
217-
// function provided by mongoose to find an Question document with a given ID
218-
// req.params.id is retrieved from /:id in route
219-
const question = await Question.findById(req.params.id);
220-
if(question === null) {
221-
throw Error('Invalid ID. Question not found in database.');
222-
}
217+
// function provided by mongoose to find an Question document with a given ID
218+
// req.params.id is retrieved from /:id in route
219+
const question = await Question.findById(req.params.id);
220+
if (question === null) {
221+
throw Error("Invalid ID. Question not found in database.");
222+
}
223223

224-
await fs.rm(`/app/question_test_cases/${question._id}`);
225-
await question.deleteOne();
226-
res.status(200).json({ message: 'Question removed' });
224+
const testcases = `/app/question_test_cases/${question._id}/`;
225+
if (fs.existsSync(testcases)) {
226+
await fs.rm(testcases, { recursive: true });
227+
}
228+
await question.deleteOne();
229+
res.status(200).json({ message: "Question removed" });
227230
} catch (error: any) {
228-
res.status(404).json({ message: error.message })
231+
res.status(404).json({ message: error.message });
229232
}
230233
}

0 commit comments

Comments
 (0)