diff --git a/api/controllers/testCasesController.js b/api/controllers/testCasesController.js index 86f95e2..12556f7 100644 --- a/api/controllers/testCasesController.js +++ b/api/controllers/testCasesController.js @@ -1,6 +1,7 @@ const TestCaseModel = require("../models/testCasesModel.js"); +const QuestionModel = require("../models/questionModel.js"); -async function createTestCases(req, res) { +const createTestCase = async (req, res) => { try { const testCase = await TestCaseModel.create({ expectedOutput: req.body.expectedOutput, @@ -11,13 +12,31 @@ async function createTestCases(req, res) { memory: req.body.memory, question: req.body.question, }); - result = await testCase.save(); - return res.status(201).json(result); + + const question = await QuestionModel.findById(req.body.question); + question.testCases.push(testCase); + await question.save(); + + return res.status(201).json(testCase); + } catch (error) { + return res.status(500).json({ + message: "Something went wrong", + }); + } +}; + +const deleteTestCase = async (req, res) => { + try { + await TestCaseModel.deleteOne({ _id: req.params.id }); + return res.status(201).json({ message: "Succesfully deleted test case" }); } catch (error) { return res.status(500).json({ - message: "Error creating testcase", + message: "Something went wrong", }); } -} +}; -module.exports = createTestCases; +module.exports = { + createTestCase, + deleteTestCase, +}; diff --git a/api/models/testCasesModel.js b/api/models/testCasesModel.js index 20d90fe..91344a5 100644 --- a/api/models/testCasesModel.js +++ b/api/models/testCasesModel.js @@ -8,11 +8,9 @@ const TestCaseSchema = new mongoose.Schema({ time: { type: Number, default: 0 }, memory: { type: Number, default: 0 }, explanation: { type: String }, - question: { type: mongoose.Schema.Types.ObjectId, ref: "Question" } + question: { type: mongoose.Schema.Types.ObjectId, ref: "Question" }, }); -const TestCaseModel = mongoose.model('Testcase', TestCaseSchema); - - -module.exports = TestCaseModel +const TestCaseModel = mongoose.model("Testcase", TestCaseSchema); +module.exports = TestCaseModel; diff --git a/api/routes/testCaseRouter.js b/api/routes/testCaseRouter.js index 08b4202..604d6be 100644 --- a/api/routes/testCaseRouter.js +++ b/api/routes/testCaseRouter.js @@ -1,10 +1,15 @@ const express = require("express"); -const createTestCases = require("../controllers/testCasesController"); +const { + createTestCase, + updateTestCase, + deleteTestCase, + fetchQuestions, +} = require("../controllers/testCasesController"); const Router = express.Router; +let router = Router(); -let router = Router() - -router.post("/create", createTestCases); +router.post("/create", createTestCase); +router.delete("/delete", deleteTestCase); module.exports = router;