diff --git a/LICENSE b/LICENSE deleted file mode 100644 index e703f28..0000000 --- a/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2023 Siddhant Pawar - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/controllers/questionController.js b/controllers/questionController.js index 8d3591c..3ceac25 100644 --- a/controllers/questionController.js +++ b/controllers/questionController.js @@ -1,32 +1,39 @@ const questionModel = require('../models/question'); const mongoose = require('mongoose'); - const createQuestion = async (req, res) => { + const { + paperName, + questionArray, + topicsArray, + difficulty, + totalMarks, + userId + } = req.body; + const newQuestion = new questionModel({ _id: new mongoose.Types.ObjectId(), - paperName: req.body.paperName, - question: req.body.questionArray, - subject: req.body.questionArray[0].subject, - topic: req.body.topicsArray, + paperName, + question: questionArray, + subject: questionArray.length > 0 ? questionArray[0].subject : null, + topic: topicsArray, difficulty: { - easy: req.body.difficulty.easy, - medium: req.body.difficulty.medium, - hard: req.body.difficulty.hard, + easy: difficulty.easy, + medium: difficulty.medium, + hard: difficulty.hard, }, - totalMarks: req.body.totalMarks, - createdBy: req.body.userId, - }, { timestamps: true }); - + totalMarks, + createdBy: userId, + }); try { await newQuestion.save(); - return res.redirect('/'); + return res.status(201).json({ message: 'Question created successfully' }); } catch (err) { - console.log(err); - res.status(500).json({ message: "Something went wrong" }); + console.error(err); + return res.status(500).json({ message: 'Internal Server Error' }); } -} +}; module.exports = { createQuestion, diff --git a/models/question.js b/models/question.js index 08e3501..88d1177 100644 --- a/models/question.js +++ b/models/question.js @@ -1,18 +1,53 @@ const mongoose = require('mongoose'); const questionSchema = new mongoose.Schema({ - _id: mongoose.Schema.Types.ObjectId, - paperName: String, - question: [Object], - subject: String, - topic: [String], + _id: mongoose.Schema.Types.ObjectId, + paperName: { + type: String, + required: true + }, + question: { + type: [Object], // You might want to specify a more specific schema for questions + required: true + }, + subject: { + type: String, + required: true + }, + topic: { + type: [String], + required: true + }, difficulty: { - easy: String, - medium: String, - hard: String, + easy: { + type: String, + required: true + }, + medium: { + type: String, + required: true + }, + hard: { + type: String, + required: true + } + }, + totalMarks: { + type: Number, + required: true, + validate: { + validator: function (value) { + return value >= 0; + }, + message: 'Total marks must be a non-negative number.' + } }, - totalMarks: Number, - createdBy: mongoose.Schema.Types.ObjectId, + createdBy: { + type: mongoose.Schema.Types.ObjectId, + required: true + } }, { timestamps: true }); -module.exports = mongoose.model("Question", questionSchema); +const QuestionModel = mongoose.model('Question', questionSchema); + +module.exports = QuestionModel;