Skip to content

Commit

Permalink
Merge pull request #9 from Black-Sirius69/master
Browse files Browse the repository at this point in the history
Feat: Created delete controller and cleanup
  • Loading branch information
Mr-Emerald-Wolf authored Aug 26, 2023
2 parents 99a5d47 + 1ccea93 commit 9a13587
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
31 changes: 25 additions & 6 deletions api/controllers/testCasesController.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
};
8 changes: 3 additions & 5 deletions api/models/testCasesModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
13 changes: 9 additions & 4 deletions api/routes/testCaseRouter.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 9a13587

Please sign in to comment.