Skip to content

Commit

Permalink
Merge pull request #13 from Black-Sirius69/master
Browse files Browse the repository at this point in the history
feat: created update test case controller and fixed the issues with the other controllers
  • Loading branch information
souvik150 authored Aug 27, 2023
2 parents 4c64b82 + dfa2130 commit dfbb873
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
50 changes: 47 additions & 3 deletions api/controllers/testCasesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ const createTestCase = async (req, res) => {
});

const question = await QuestionModel.findById(req.body.question);
question.testCases.push(testCase);
if (!question.testCases.includes(testCase)) {
question.testCases.push(testCase);
}
await question.save();

return res.status(201).json(testCase);
Expand All @@ -27,16 +29,58 @@ const createTestCase = async (req, res) => {

const deleteTestCase = async (req, res) => {
try {
await TestCaseModel.deleteOne({ _id: req.params.id });
const testCase = await TestCaseModel.findByIdAndDelete(req.params.id);
const question = await QuestionModel.findById(testCase.question);
if (question) {
question.testCases.pull(testCase._id);
await question.save();
}
return res.status(201).json({ message: "Succesfully deleted test case" });
} catch (error) {
return res.status(500).json({
message: "Something went wrong",
message: error.message,
});
}
};

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;

testCase.expectedOutput = expectedOutput ? expectedOutput : testCase.expectedOutput;
testCase.input = input ? input : testCase.input;
testCase.hidden = hidden ? 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;

if (question) {
const oldQuestion = await QuestionModel.findById(testCase.question);
if (oldQuestion) {
oldQuestion.testCases.pull(testCase._id);
await oldQuestion.save();
}

const updated = await QuestionModel.findById(question);
console.log(updated);
updated.testCases.push(testCase._id);
await updated.save();

testCase.question = question;
}

await testCase.save();

return res.status(201).json({ testCase });
} catch (error) {
return res.status(500).json({ message: error.message });
}
}

module.exports = {
createTestCase,
deleteTestCase,
updateTestCase
};
4 changes: 2 additions & 2 deletions api/routes/testCaseRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ const {
createTestCase,
updateTestCase,
deleteTestCase,
fetchQuestions,
} = require("../controllers/testCasesController");
const Router = express.Router;

let router = Router();

router.post("/create", createTestCase);
router.delete("/delete", deleteTestCase);
router.delete("/delete/:id", deleteTestCase);
router.patch("/update/:id", updateTestCase);

module.exports = router;

0 comments on commit dfbb873

Please sign in to comment.