diff --git a/controllers/controller.js b/controllers/controller.js index 6462770..cee7649 100644 --- a/controllers/controller.js +++ b/controllers/controller.js @@ -1,446 +1,539 @@ -const mongoose = require('mongoose'); -const Model = require('../model/question'); - -exports.getAge = (req, res, next) => { - return Model.Age - .aggregate([ - { $sample : { size : 1 } }, - {$project : { _id : 0, _v : 0} } - ]) - .then(doc => { - res.status(200).json(doc[0]) - }) - .catch(err => { - next(err) - }) -} - -exports.postAge = (req, res, next) => { - const question = req.body.question; - const answer = req.body.answer; - const options = req.body.options; - const explanation = req.body.explanation; - - const age = new Model.Age({ - question : question, - answer : answer, - options : options, - explanation : explanation - }) - return age.save() - .then(result => { - res.status(201).json({ - message : 'Question created successfully', - postedQuestion : { - question : question, - answer : answer, - options : options, - explanation : explanation - } - }) - }) - .catch(err => { - if(!err.statusCode){ - err.statusCode = 500; - } - next(err) - }) -} - -exports.getRandom = (req, res, next) => { - return Model.Random - .aggregate([ - { $sample : { size : 1 } }, - {$project : { _id : 0, _v : 0} } - ]) - .then(doc => { - res.status(200).json(doc[0]) - }) - .catch(err => { - next(err) - }) +const mongoose = require("mongoose"); +const Model = require("../model/question"); + +exports.getAge = async (req, res, next) => { + try { + const doc = await Model.Age.aggregate([ + { $sample: { size: 1 } }, + { $project: { _id: 0, _v: 0 } }, + ]); + return res.status(200).json(doc[0]); + } catch (err) { + next(err); + } +}; + +exports.postAge = async (req, res, next) => { + const question = req.body.question; + const answer = req.body.answer; + const options = req.body.options; + const explanation = req.body.explanation; + + try { + const doc = await Model.Age.exists({ question: question }); + console.log(doc); + if (doc) { + const error = new Error("Question Already exists"); + error.statusCode = 409; + throw error; + } + let age = new Model.Age({ + question: question, + answer: answer, + options: options, + explanation: explanation, + }); + // age = JSON.parse(age); + console.log(age); + await age.save(); + res.status(201).json({ + message: "Question created successfully", + postedQuestion: { + question: question, + answer: answer, + options: options, + explanation: explanation, + }, + }); + } catch (err) { + if (!err.statusCode) { + err.statusCode = 500; + } + next(err); + } +}; + +exports.getRandom = async (req, res, next) => { + try { + const doc = await Model.Random.aggregate([ + { $sample: { size: 1 } }, + { $project: { _id: 0, _v: 0 } }, + ]); + return res.status(200).json(doc[0]); + } catch (err) { + next(err); + } }; -exports.postRandom = (req, res, next) => { - const question = req.body.question; - const answer = req.body.answer; - const options = req.body.options; - const explanation = req.body.explanation; +exports.postRandom = async (req, res, next) => { + const question = req.body.question; + const answer = req.body.answer; + const options = req.body.options; + const explanation = req.body.explanation; + try { + const doc = await Model.Random.exists({ question: question }); + console.log(doc); + if (doc) { + const error = new Error("Question Already exists"); + error.statusCode = 409; + throw error; + } const random = new Model.Random({ - question : question, - answer : answer, - options : options, - explanation : explanation - }) - return random.save() - .then(result => { - res.status(201).json({ - message : 'Question created successfully', - postedQuestion : { - question : question, - answer : answer, - options : options, - explanation : explanation - } - }) - }) - .catch(err => { - if(!err.statusCode){ - err.statusCode = 500; - } - next(err) - }) -} - -exports.getMixtureAndAlligation = (req, res, next) => { - return Model.MixtureAndAlligation - .aggregate([ - { $sample : { size : 1 } }, - {$project : { _id : 0, _v : 0} } - ]) - .then(doc => { - res.status(200).json(doc[0]) - }) - .catch(err => { - next(err) - }) -} - -exports.postMixtureAndAlligation = (req, res, next) => { - const question = req.body.question; - const answer = req.body.answer; - const options = req.body.options; - const explanation = req.body.explanation; + question: question, + answer: answer, + options: options, + explanation: explanation, + }); + await random.save(); + res.status(201).json({ + message: "Question created successfully", + postedQuestion: { + question: question, + answer: answer, + options: options, + explanation: explanation, + }, + }); + } catch (err) { + if (!err.statusCode) { + err.statusCode = 500; + } + next(err); + } +}; + +exports.getMixtureAndAlligation = async (req, res, next) => { + try { + const doc = await Model.MixtureAndAlligation.aggregate([ + { $sample: { size: 1 } }, + { $project: { _id: 0, _v: 0 } }, + ]); + return res.status(200).json(doc[0]); + } catch (err) { + next(err); + } +}; + +exports.postMixtureAndAlligation = async (req, res, next) => { + const question = req.body.question; + const answer = req.body.answer; + const options = req.body.options; + const explanation = req.body.explanation; + + try { + const doc = await Model.MixtureAndAlligation.exists({ question: question }); + if (doc) { + const error = new Error("Question already exists"); + error.statusCode = 409; + throw error; + } const mixture = new Model.MixtureAndAlligation({ - question : question, - answer : answer, - options : options, - explanation : explanation - }) - return mixture.save() - .then(result => { - res.status(201).json({ - message : 'Question created successfully', - postedQuestion : { - question : question, - answer : answer, - options : options, - explanation : explanation - } - }) - }) - .catch(err => { - if(!err.statusCode){ - err.statusCode = 500; - } - next(err) - }) -} - -exports.getProfitAndLoss= (req, res, next) => { - return Model.ProfitAndLoss - .aggregate([ - { $sample : { size : 1 } }, - {$project : { _id : 0, _v : 0} } - ]) - .then(doc => { - res.status(200).json(doc[0]) - }) - .catch(err => { - next(err) - }) -} - -exports.postProfitAndLoss = (req, res, next) => { - const question = req.body.question; - const answer = req.body.answer; - const options = req.body.options; - const explanation = req.body.explanation; + question: question, + answer: answer, + options: options, + explanation: explanation, + }); + await mixture.save(); + + res.status(201).json({ + message: "Question created successfully", + postedQuestion: { + question: question, + answer: answer, + options: options, + explanation: explanation, + }, + }); + } catch (err) { + if (!err.statusCode) { + err.statusCode = 500; + } + next(err); + } +}; + +exports.getProfitAndLoss = async (req, res, next) => { + try { + const doc = await Model.ProfitAndLoss.aggregate([ + { $sample: { size: 1 } }, + { $project: { _id: 0, _v: 0 } }, + ]); + return res.status(200).json(doc[0]); + } catch (err) { + next(err); + } +}; + +exports.postProfitAndLoss = async (req, res, next) => { + const question = req.body.question; + const answer = req.body.answer; + const options = req.body.options; + const explanation = req.body.explanation; + + try { + const doc = await Model.ProfitAndLoss.exists({ question: question }); + if (doc) { + const error = new Error("Question already exists"); + error.statusCode = 409; + throw error; + } const profitAndLoss = new Model.ProfitAndLoss({ - question : question, - answer : answer, - options : options, - explanation : explanation - }) - return profitAndLoss.save() - .then(result => { - res.status(201).json({ - message : 'Question created successfully', - postedQuestion : { - question : question, - answer : answer, - options : options, - explanation : explanation - } - }) - }) - .catch(err => { - if(!err.statusCode){ - err.statusCode = 500; - } - next(err) - }) -} - -exports.getPermutationAndCombination = (req, res, next) => { - return Model.PermutationAndCombination - .aggregate([ - { $sample : { size : 1 } }, - {$project : { _id : 0, _v : 0} } - ]) - .then(doc => { - res.status(200).json(doc[0]) - }) - .catch(err => { - next(err) - }) -} - -exports.postPermutationAndCombination = (req, res, next) => { - const question = req.body.question; - const answer = req.body.answer; - const options = req.body.options; - const explanation = req.body.explanation; + question: question, + answer: answer, + options: options, + explanation: explanation, + }); + await profitAndLoss.save(); + res.status(201).json({ + message: "Question created successfully", + postedQuestion: { + question: question, + answer: answer, + options: options, + explanation: explanation, + }, + }); + } catch (err) { + if (!err.statusCode) { + err.statusCode = 500; + } + next(err); + } +}; + +exports.getPermutationAndCombination = async (req, res, next) => { + try { + const doc = await Model.PermutationAndCombination.aggregate([ + { $sample: { size: 1 } }, + { $project: { _id: 0, _v: 0 } }, + ]); + return res.status(200).json(doc[0]); + } catch (err) { + next(err); + } +}; +exports.postPermutationAndCombination = async (req, res, next) => { + const question = req.body.question; + const answer = req.body.answer; + const options = req.body.options; + const explanation = req.body.explanation; + + try { + const doc = await Model.PermutationAndCombination.exists({ + question: question, + }); + if (doc) { + const error = new Error("Question already exists"); + error.statusCode = 409; + throw error; + } const permutationAndCombination = new Model.PermutationAndCombination({ - question : question, - answer : answer, - options : options, - explanation : explanation - }) - return permutationAndCombination.save() - .then(result => { - res.status(201).json({ - message : 'Question created successfully', - postedQuestion : { - question : question, - answer : answer, - options : options, - explanation : explanation - } - }) - }) - .catch(err => { - if(!err.statusCode){ - err.statusCode = 500; - } - next(err) - }) -} - -exports.getSpeedTimeDistance =(req, res, next) => { - return Model.SpeedTimeDistance - .aggregate([ - { $sample : { size : 1 } }, - {$project : { _id : 0, _v : 0} } - ]) - .then(doc => { - res.status(200).json(doc[0]) - }) - .catch(err => { - next(err) - }) -} - -exports.postSpeedTimeDistance = (req, res, next) => { - const question = req.body.question; - const answer = req.body.answer; - const options = req.body.options; - const explanation = req.body.explanation; + question: question, + answer: answer, + options: options, + explanation: explanation, + }); + await permutationAndCombination.save(); + + res.status(201).json({ + message: "Question created successfully", + postedQuestion: { + question: question, + answer: answer, + options: options, + explanation: explanation, + }, + }); + } catch (err) { + if (!err.statusCode) { + err.statusCode = 500; + } + next(err); + } +}; +exports.getSpeedTimeDistance = async (req, res, next) => { + try { + const doc = await Model.SpeedTimeDistance.aggregate([ + { $sample: { size: 1 } }, + { $project: { _id: 0, _v: 0 } }, + ]); + return res.status(200).json(doc[0]); + } catch (err) { + next(err); + } +}; + +exports.postSpeedTimeDistance = async (req, res, next) => { + const question = req.body.question; + const answer = req.body.answer; + const options = req.body.options; + const explanation = req.body.explanation; + + try { + const doc = await Model.SpeedTimeDistance.exists({ question: question }); + if (doc) { + const error = new Error("Question already exists"); + error.statusCode = 409; + throw error; + } const speedTimeDistance = new Model.SpeedTimeDistance({ - question : question, - answer : answer, - options : options, - explanation : explanation - }) - return speedTimeDistance.save() - .then(result => { - res.status(201).json({ - message : 'Question created successfully', - postedQuestion : { - question : question, - answer : answer, - options : options, - explanation : explanation - } - }) - }) - .catch(err => { - if(!err.statusCode){ - err.statusCode = 500; - } - next(err) - }) -} - -exports.getSimpleInterest = (req, res, next) => { - return Model.SimpleInterest - .aggregate([ - { $sample : { size : 1 } }, - {$project : { _id : 0, _v : 0} } - ]) - .then(doc => { - res.status(200).json(doc[0]) - }) - .catch(err => { - next(err) - }) -} - -exports.postSimpleInterest = (req, res, next) => { - const question = req.body.question; - const answer = req.body.answer; - const options = req.body.options; - const explanation = req.body.explanation; + question: question, + answer: answer, + options: options, + explanation: explanation, + }); + await speedTimeDistance.save(); + res.status(201).json({ + message: "Question created successfully", + postedQuestion: { + question: question, + answer: answer, + options: options, + explanation: explanation, + }, + }); + } catch (err) { + if (!err.statusCode) { + err.statusCode = 500; + } + next(err); + } +}; + +exports.getSimpleInterest = async (req, res, next) => { + try { + const doc = await Model.SimpleInterest.aggregate([ + { $sample: { size: 1 } }, + { $project: { _id: 0, _v: 0 } }, + ]); + return res.status(200).json(doc[0]); + } catch (err) { + next(err); + } +}; + +exports.postSimpleInterest = async (req, res, next) => { + const question = req.body.question; + const answer = req.body.answer; + const options = req.body.options; + const explanation = req.body.explanation; + + try { + const doc = await Model.SimpleInterest.exists({ question: question }); + if (doc) { + const error = new Error("Question already exists"); + error.statusCode = 409; + throw error; + } const simpleInterest = new Model.SimpleInterest({ - question : question, - answer : answer, - options : options, - explanation : explanation - }) - return simpleInterest.save() - .then(result => { - res.status(201).json({ - message : 'Question created successfully', - postedQuestion : { - question : question, - answer : answer, - options : options, - explanation : explanation - } - }) - }) - .catch(err => { - if(!err.statusCode){ - err.statusCode = 500; - } - next(err) - }) -} - -exports.getCalendar = (req, res, next) => { - return Model.Calendar - .aggregate([ - { $sample : { size : 1 } }, - {$project : { _id : 0, _v : 0} } - ]) - .then(doc => { - res.status(200).json(doc[0]) - }) - .catch(err => { - next(err) - }) -} - -exports.postCalendar = (req, res, next) => { - const question = req.body.question; - const answer = req.body.answer; - const options = req.body.options; - const explanation = req.body.explanation; + question: question, + answer: answer, + options: options, + explanation: explanation, + }); + await simpleInterest.save(); + + res.status(201).json({ + message: "Question created successfully", + postedQuestion: { + question: question, + answer: answer, + options: options, + explanation: explanation, + }, + }); + } catch (err) { + if (!err.statusCode) { + err.statusCode = 500; + } + next(err); + } +}; +exports.getCalendar = async (req, res, next) => { + try { + const doc = await Model.Calendar.aggregate([ + { $sample: { size: 1 } }, + { $project: { _id: 0, _v: 0 } }, + ]); + return res.status(200).json(doc[0]); + } catch (err) { + next(err); + } +}; + +exports.postCalendar = async (req, res, next) => { + const question = req.body.question; + const answer = req.body.answer; + const options = req.body.options; + const explanation = req.body.explanation; + + try { + const doc = await Model.Calendar.exists({ question: question }); + if (doc) { + const error = new Error("Question already exists"); + error.statusCode = 409; + throw error; + } const calendar = new Model.Calendar({ - question : question, - answer : answer, - options : options, - explanation : explanation - }) - return calendar.save() - .then(result => { - res.status(201).json({ - message : 'Question created successfully', - postedQuestion : { - question : question, - answer : answer, - options : options, - explanation : explanation - } - }) - }) - .catch(err => { - if(!err.statusCode){ - err.statusCode = 500; - } - next(err) - }) -} - -exports.getPipesAndCistern = (req, res, next) => { - return Model.PipesAndCistern - .aggregate([ - { $sample : { size : 1 } }, - {$project : { _id : 0, _v : 0} } - ]) - .then(doc => { - res.status(200).json(doc[0]) - }) - .catch(err => { - next(err) - }) -} - -exports.postPipesAndCisterns = (req, res, next) => { - const question = req.body.question; - const answer = req.body.answer; - const options = req.body.options; - const explanation = req.body.explanation; + question: question, + answer: answer, + options: options, + explanation: explanation, + }); + await calendar.save(); + + res.status(201).json({ + message: "Question created successfully", + postedQuestion: { + question: question, + answer: answer, + options: options, + explanation: explanation, + }, + }); + } catch (err) { + if (!err.statusCode) { + err.statusCode = 500; + } + next(err); + } +}; + +exports.getPipesAndCistern = async (req, res, next) => { + try { + const doc = await Model.PipesAndCistern.aggregate([ + { $sample: { size: 1 } }, + { $project: { _id: 0, _v: 0 } }, + ]); + return res.status(200).json(doc[0]); + } catch (err) { + next(err); + } +}; +exports.postPipesAndCisterns = async (req, res, next) => { + const question = req.body.question; + const answer = req.body.answer; + const options = req.body.options; + const explanation = req.body.explanation; + + try { + const doc = await Model.PipesAndCistern.exists({ question: question }); + if (doc) { + const error = new Error("Question already exists"); + error.statusCode = 409; + throw error; + } const pipesAndCistern = new Model.PipesAndCistern({ - question : question, - answer : answer, - options : options, - explanation : explanation - }) - return pipesAndCistern.save() - .then(result => { - res.status(201).json({ - message : 'Question created successfully', - postedQuestion : { - question : question, - answer : answer, - options : options, - explanation : explanation - } - }) - }) - .catch(err => { - if(!err.statusCode){ - err.statusCode = 500; - } - 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) - }); + question: question, + answer: answer, + options: options, + explanation: explanation, + }); + await pipesAndCistern.save(); + + res.status(201).json({ + message: "Question created successfully", + postedQuestion: { + question: question, + answer: answer, + options: options, + explanation: explanation, + }, + }); + } catch (err) { + if (!err.statusCode) { + err.statusCode = 500; + } + next(err); + } +}; + +exports.deleteQuestion = async (req, res, next) => { + const model = req.params.model; + const question = req.body.question; + + if (!question) { + const error = new Error("Question is required"); + error.statusCode = 422; + throw error; + } + + if (!model) { + const error = new Error("Topic must be defined in the route"); + error.statusCode = 422; + throw error; + } + + try { + const doc = await mongoose + .model(model) + .findOneAndDelete({ question: question }); + if (!doc) { + const error = new Error("Question not found"); + error.statusCode = 404; + throw error; } -} \ No newline at end of file + + 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); + } +}; + +exports.updateQuestion = async (req, res, next) => { + const model = mongoose.model(req.params.model); + + const questionToBeUpdated = req.body.questionToBeUpdated; + const question = req.body.question; + const answer = req.body.answer; + const options = req.body.options; + const explanation = req.body.explanation; + + try { + const doc = await model.exists({ question: questionToBeUpdated }); + if (!doc) { + const error = new Error("Question does not exists."); + error.statusCode = 404; + throw error; + } + const document = await model.findById({ _id: doc._id }); + document.question = question; + document.answer = answer; + document.options = options; + document.explanation = explanation; + await document.save(); + res.json({ + message: "Question updated successfully", + updateQuestionquestion: { + question: document.question, + answer: document.answer, + options: document.options, + explanation: document.explanation, + }, + }); + } catch (err) { + if (!err.statusCode) { + err.statusCode = 500; + } + next(err); + } +}; diff --git a/model/question.js b/model/question.js index c158c53..08ede5d 100644 --- a/model/question.js +++ b/model/question.js @@ -13,7 +13,10 @@ const questionSchema = new Schema({ }, options : { type : [String], - required : true + required : true, + unique : true, + sparse : true, + index : true }, explanation : { type : String, diff --git a/routes/routes.js b/routes/routes.js index d202b20..892a765 100644 --- a/routes/routes.js +++ b/routes/routes.js @@ -54,6 +54,6 @@ router.post('/SpeedTimeDistance',validation.validateQuestion ,validation.handleV router.delete('/:model', aptitudeController.deleteQuestion); //modify questions routes -router.patch('/:model') +router.patch('/:model', aptitudeController.updateQuestion); module.exports = router; diff --git a/server.js b/server.js index 6838bcd..bee864a 100644 --- a/server.js +++ b/server.js @@ -11,7 +11,7 @@ const app = express(); const port = 3000; -app.use(bodyParser.urlencoded({extended : true})) +app.use(bodyParser.urlencoded({ extended: true })); app.use(express.static(__dirname + '/public')); app.get("/", (req, res, next) => { @@ -23,9 +23,9 @@ app.use((error, req, res, next) => { const message = error.message; const statusCode = error.statusCode; if(error.field){ - return res.status(statusCode).json({message : message, statusCode : statusCode, field : error.field}); + return res.json({message : message, statusCode : statusCode, field : error.field}); } - return res.status(statusCode).json({message : message, statusCode : statusCode}); + return res.json({message : message, statusCode : statusCode}); }); mongoose