diff --git a/api/controllers/questions.js b/api/controllers/questions.js index da852e8..e6f3826 100644 --- a/api/controllers/questions.js +++ b/api/controllers/questions.js @@ -1,4 +1,6 @@ const ques = require("../models/ques"); +const jwt = require("jsonwebtoken"); +const TestCaseModel = require("../models/testCasesModel"); async function getQuestionByID(req, res) { try { @@ -83,11 +85,21 @@ async function getByRound(req, res) { async function deleteQuestion(req,res){ try{ const deletedItem = await ques.findByIdAndDelete(req.params.id); +<<<<<<< HEAD + console.log(req.body.id) + + const testCases = await TestCaseModel.find().where(`_id: ${req.params.id}`).exec(); + + TestCaseModel.deleteMany(testCases); + +======= +>>>>>>> ef7378f59c6d585adc405c4be1bc8a1b2cf8ca77 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){ @@ -148,4 +160,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 86b8809..53a6aa4 100644 --- a/api/controllers/testCasesController.js +++ b/api/controllers/testCasesController.js @@ -40,7 +40,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" }); } }; @@ -48,15 +48,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); @@ -77,7 +83,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;