Skip to content

Commit

Permalink
Merge pull request #1 from SiddhantPawar03/master
Browse files Browse the repository at this point in the history
refactor(question): imporved code stylings & validation
  • Loading branch information
SiddhantPawar03 authored Nov 20, 2023
2 parents 577f67a + a7b57ff commit f60dd9b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 48 deletions.
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

39 changes: 23 additions & 16 deletions controllers/questionController.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
57 changes: 46 additions & 11 deletions models/question.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit f60dd9b

Please sign in to comment.