From 27cf7680b3cbf793cde551d3c04ffa19df3d8719 Mon Sep 17 00:00:00 2001 From: Vedant Matanhelia Date: Wed, 6 Sep 2023 02:04:58 +0530 Subject: [PATCH] Fix: fixed a bug where the hidden field was not getting updated and if the question was deleted the test cases were not getting deleted --- api/controllers/questions.js | 11 +++++++++-- api/controllers/testCasesController.js | 16 +++++++++++----- api/routes/questionsRouter.js | 2 +- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/api/controllers/questions.js b/api/controllers/questions.js index e494b0b..d9b4226 100644 --- a/api/controllers/questions.js +++ b/api/controllers/questions.js @@ -1,5 +1,6 @@ const ques = require("../models/ques"); const jwt = require("jsonwebtoken"); +const TestCaseModel = require("../models/testCasesModel"); async function getQuestionByID(req, res) { try { @@ -85,11 +86,17 @@ async function deleteQuestion(req,res){ try{ const deletedItem = await ques.findByIdAndDelete(req.params.id); console.log(req.body.id) + + const testCases = await TestCaseModel.find().where(`_id: ${req.params.id}`).exec(); + + TestCaseModel.deleteMany(testCases); + if (!deletedItem) { return res.status(404).json({ message: 'Item not found' }); } return res.status(201).json({ - message: "Successfully Deleted" + message: "Successfully Deleted", + testCases: testCases }) } catch (error){ @@ -154,4 +161,4 @@ module.exports = { getQuestionByID, updateQuestion, deleteQuestion -}; \ No newline at end of file +}; diff --git a/api/controllers/testCasesController.js b/api/controllers/testCasesController.js index 6d25015..6f1b183 100644 --- a/api/controllers/testCasesController.js +++ b/api/controllers/testCasesController.js @@ -38,7 +38,7 @@ const deleteTestCase = async (req, res) => { return res.status(201).json({ message: "Succesfully deleted test case" }); } catch (error) { return res.status(500).json({ - message: error.message, + message: "Failed to delete test case" }); } }; @@ -46,15 +46,21 @@ const deleteTestCase = async (req, res) => { const updateTestCase = async (req, res) => { try { const testCase = await TestCaseModel.findById(req.params.id); - const { expectedOutput, input, number, hidden, time, memory, explanation, question } = req.body; + + if (!testCase) { + return res.status(500).json({message: "Test case does not exist"}); + } + + const { expectedOutput, input, number, hidden, time, memory, group, question } = req.body; + console.log(hidden, expectedOutput); testCase.expectedOutput = expectedOutput ? expectedOutput : testCase.expectedOutput; testCase.input = input ? input : testCase.input; - testCase.hidden = hidden ? hidden : testCase.hidden; + testCase.hidden = hidden === undefined ? hidden : testCase.hidden; testCase.number = number ? number : testCase.number; testCase.time = time ? time : testCase.time; testCase.memory = memory ? memory : testCase.memory; - testCase.explanation = explanation ? explanation : testCase.explanation; + testCase.group = group ? group : testCase.group; if (question) { const oldQuestion = await QuestionModel.findById(testCase.question); @@ -75,7 +81,7 @@ const updateTestCase = async (req, res) => { return res.status(201).json({ testCase }); } catch (error) { - return res.status(500).json({ message: error.message }); + return res.status(500).json({ message: "Failed to update test cases" }); } } diff --git a/api/routes/questionsRouter.js b/api/routes/questionsRouter.js index 5aa42d7..810ed0d 100644 --- a/api/routes/questionsRouter.js +++ b/api/routes/questionsRouter.js @@ -19,4 +19,4 @@ router.post("/getRound", getByRound); router.put("/updateQuestion/:id", verifyAdminToken, updateQuestion); router.delete("/deleteQuestion/:id",verifyAdminToken, deleteQuestion); -module.exports = router; \ No newline at end of file +module.exports = router;