Skip to content

Commit

Permalink
Added Delete Route
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiru-kumaran-R committed Feb 15, 2024
1 parent c4efb4c commit 5b49eb1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 21 deletions.
43 changes: 37 additions & 6 deletions controllers/controller.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const mongoose = require('mongoose');
const Model = require('../model/question');

exports.getAge = (req, res, next) => {
Expand Down Expand Up @@ -93,7 +94,7 @@ exports.postRandom = (req, res, next) => {
}

exports.getMixtureAndAlligation = (req, res, next) => {
return Model.Mixture
return Model.MixtureAndAlligation
.aggregate([
{ $sample : { size : 1 } },
{$project : { _id : 0, _v : 0} }
Expand All @@ -112,7 +113,7 @@ exports.postMixtureAndAlligation = (req, res, next) => {
const options = req.body.options;
const explanation = req.body.explanation;

const mixture = new Model.Mixture({
const mixture = new Model.MixtureAndAlligation({
question : question,
answer : answer,
options : options,
Expand Down Expand Up @@ -185,7 +186,7 @@ exports.postProfitAndLoss = (req, res, next) => {
}

exports.getPermutationAndCombination = (req, res, next) => {
return Model.Permutation
return Model.PermutationAndCombination
.aggregate([
{ $sample : { size : 1 } },
{$project : { _id : 0, _v : 0} }
Expand All @@ -204,7 +205,7 @@ exports.postPermutationAndCombination = (req, res, next) => {
const options = req.body.options;
const explanation = req.body.explanation;

const permutationAndCombination = new Model.Permutation({
const permutationAndCombination = new Model.PermutationAndCombination({
question : question,
answer : answer,
options : options,
Expand Down Expand Up @@ -277,7 +278,7 @@ exports.postSpeedTimeDistance = (req, res, next) => {
}

exports.getSimpleInterest = (req, res, next) => {
return Model.SimpleAndInterest
return Model.SimpleInterest
.aggregate([
{ $sample : { size : 1 } },
{$project : { _id : 0, _v : 0} }
Expand All @@ -296,7 +297,7 @@ exports.postSimpleInterest = (req, res, next) => {
const options = req.body.options;
const explanation = req.body.explanation;

const simpleInterest = new Model.SimpleAndInterest({
const simpleInterest = new Model.SimpleInterest({
question : question,
answer : answer,
options : options,
Expand Down Expand Up @@ -412,4 +413,34 @@ exports.postPipesAndCisterns = (req, res, next) => {
}
next(err)
})
}

exports.deleteQuestion = (req, res, next) => {
const model = req.params.model;
const question = req.body.question;
if(model){
mongoose.model(model).findOneAndDelete({question : question})
.then(doc => {
if(!doc){
const error = new Error("Question not found");
error.statusCode = 404;
throw error;
}
res.json({
message : 'Question deleted successfully',
deletedQuestion : {
question : doc.question,
answer : doc.answer,
options : doc.options,
explanation : doc.explanation
}
});
})
.catch(err => {
if(!err.statusCode){
err.statusCode = 500
}
next(err)
});
}
}
18 changes: 9 additions & 9 deletions model/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ const questionSchema = new Schema({

const Age = mongoose.model('Age', questionSchema);
const Calendar = mongoose.model('Calendar', questionSchema);
const Mixture = mongoose.model('MixtureAndAlligation', questionSchema);
const Permutation = mongoose.model('PermutationAndCombination', questionSchema);
const MixtureAndAlligation = mongoose.model('MixtureAndAlligation', questionSchema);
const PermutationAndCombination = mongoose.model('PermutationAndCombination', questionSchema);
const PipesAndCistern = mongoose.model('PipesAndCistern', questionSchema);
const ProfitAndLoss = mongoose.model('ProfitAndLoss', questionSchema);
const SimpleAndInterest = mongoose.model('SimpleInterest', questionSchema);
const SimpleInterest = mongoose.model('SimpleInterest', questionSchema);
const SpeedTimeDistance = mongoose.model('SpeedTimeDistance', questionSchema);
const Random = mongoose.model('Random', questionSchema);

module.exports = {
Age,
Calendar,
Mixture,
Permutation,
Random,
Calendar,
PipesAndCistern,
ProfitAndLoss,
SimpleAndInterest,
SpeedTimeDistance,
Random
SimpleInterest,
SpeedTimeDistance,
MixtureAndAlligation,
PermutationAndCombination,
};
18 changes: 14 additions & 4 deletions routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const validation = require('../validation/validation');
const aptitudeController = require("../controllers/controller");

//random routes
router.get("/random", aptitudeController.getRandom);
router.get("/Random", aptitudeController.getRandom);

router.post('/random',validation.validateQuestion ,validation.handleValidationErrors, aptitudeController.postRandom);
router.post('/Random',validation.validateQuestion ,validation.handleValidationErrors, aptitudeController.postRandom);

//mixture and alligations routes
router.get("/MixtureAndAlligation", aptitudeController.getMixtureAndAlligation);
Expand Down Expand Up @@ -41,8 +41,18 @@ router.get("/Calendar", aptitudeController.getCalendar);
router.post("/Calendar",validation.validateQuestion ,validation.handleValidationErrors, aptitudeController.postCalendar);

//Pipes and Cisterns routes
router.get("/PipesAndCisterns", aptitudeController.getPipesAndCistern);
router.get("/PipesAndCistern", aptitudeController.getPipesAndCistern);

router.post("/PipesAndCisterns",validation.validateQuestion ,validation.handleValidationErrors, aptitudeController.postPipesAndCisterns);
router.post("/PipesAndCistern",validation.validateQuestion ,validation.handleValidationErrors, aptitudeController.postPipesAndCisterns);

// Speed Time Distance routes
router.get('/SpeedTimeDistance', aptitudeController.getSpeedTimeDistance);

router.post('/SpeedTimeDistance',validation.validateQuestion ,validation.handleValidationErrors, aptitudeController.postSpeedTimeDistance);

//delete questions routes
router.delete('/:model', aptitudeController.deleteQuestion);

//modify questions routes

module.exports = router;
4 changes: 2 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ app.use((error, req, res, next) => {
const message = error.message;
const statusCode = error.statusCode;
if(error.field){
return res.json({message : message, statusCode : statusCode, field : error.field});
return res.status(statusCode).json({message : message, statusCode : statusCode, field : error.field});
}
return res.json({message : message, statusCode : statusCode});
return res.status(statusCode).json({message : message, statusCode : statusCode});
});

mongoose
Expand Down

1 comment on commit 5b49eb1

@vercel
Copy link

@vercel vercel bot commented on 5b49eb1 Feb 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.