diff --git a/controllers/controller.js b/controllers/controller.js index fc00a20..6462770 100644 --- a/controllers/controller.js +++ b/controllers/controller.js @@ -1,3 +1,4 @@ +const mongoose = require('mongoose'); const Model = require('../model/question'); exports.getAge = (req, res, next) => { @@ -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} } @@ -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, @@ -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} } @@ -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, @@ -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} } @@ -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, @@ -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) + }); + } } \ No newline at end of file diff --git a/model/question.js b/model/question.js index b82c1f4..c158c53 100644 --- a/model/question.js +++ b/model/question.js @@ -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, }; \ No newline at end of file diff --git a/routes/routes.js b/routes/routes.js index 854021a..887ed59 100644 --- a/routes/routes.js +++ b/routes/routes.js @@ -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); @@ -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; diff --git a/server.js b/server.js index 607f69b..6838bcd 100644 --- a/server.js +++ b/server.js @@ -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