Ajout initial des fichiers du projet
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
var router = require('express').Router(),
|
||||
mongoose = require('mongoose'),
|
||||
Qcm = mongoose.model('Qcm'),
|
||||
QcmCategory = mongoose.model('QcmCategory'),
|
||||
QcmQuestion = mongoose.model('QcmQuestion'),
|
||||
QcmChoice = mongoose.model('QcmChoice'),
|
||||
User = mongoose.model('User');
|
||||
const auth = require('../auth');
|
||||
|
||||
// Preload qcm objects on routes with ':type'
|
||||
router.param('type', function (req, res, next, type) {
|
||||
Qcm.findOne({ name: type })
|
||||
.populate('categories')
|
||||
.then(qcm => {
|
||||
if (!qcm) {
|
||||
return res.status(404).json({ errors: { "Not Found": "Le QCM est introuvable ou inexistant." } });
|
||||
}
|
||||
req.qcm = qcm;
|
||||
Promise.all(
|
||||
req.qcm.categories.map(category => {
|
||||
return category.populate('questions').then(() => {
|
||||
return Promise.all(
|
||||
category.questions.map(question => question.populate('choices'))
|
||||
).then(() => {
|
||||
return category;
|
||||
}).catch(next);
|
||||
});
|
||||
})
|
||||
).then(() => {
|
||||
return next();
|
||||
}).catch(next);
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
/**
|
||||
* return all qcm
|
||||
*/
|
||||
router.get('/', auth.required, function (req, res, next) {
|
||||
Promise.all([
|
||||
req.payload ? User.findById(req.payload.id) : null
|
||||
]).then(function (results) {
|
||||
let user = results[0];
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
return res.json({ qcm: [] });
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
/**
|
||||
* return a qcm
|
||||
*/
|
||||
router.get('/type/:type', auth.required, function (req, res, next) {
|
||||
Promise.all([
|
||||
req.payload ? User.findById(req.payload.id) : null
|
||||
]).then(function (results) {
|
||||
let user = results[0];
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
return res.json({ qcm: req.qcm.toJSONFor() });
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
/**
|
||||
* save an array of qcm question choice
|
||||
*/
|
||||
router.post('/choices/', auth.required, function (req, res, next) {
|
||||
Promise.all([
|
||||
req.payload ? User.findById(req.payload.id) : null,
|
||||
QcmQuestion
|
||||
.findOne({num: req.body.question.num})
|
||||
.populate('choices')
|
||||
.exec()
|
||||
]).then(async function (results) {
|
||||
const user = results[0];
|
||||
//let numero = (results[1][0].numero + 1);
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
if (user.role !== 'Admin') {
|
||||
return res.status(403).json({ errors: { "Forbidden": "Vous ne disposez pas des autorisations suffisantes" } });
|
||||
}
|
||||
const question = results[1];
|
||||
if (!question) {
|
||||
return res.status(422).json({ errors: { question: "question introuvable" } });
|
||||
}
|
||||
let choices = [];
|
||||
Promise.all(
|
||||
req.body.question.choices.map(data => {
|
||||
const choiceId = new mongoose.Types.ObjectId();
|
||||
const choice = new QcmChoice({ _id: choiceId, index: data.index, libelle: data.libelle, correct: data.correct});
|
||||
choices.push(choiceId);
|
||||
return choice.save();
|
||||
})
|
||||
).then(function () {
|
||||
question.choices = [...choices];
|
||||
question.save().then(function () {
|
||||
QcmQuestion
|
||||
.findOne({num: req.body.question.num})
|
||||
.populate('choices')
|
||||
.exec()
|
||||
.then(function (question) {
|
||||
return res.json({ question: question.toJSONFor() });
|
||||
}).catch(next);
|
||||
}).catch(next);
|
||||
}).catch(next);
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
/**
|
||||
* save an array of qcm category questions
|
||||
*/
|
||||
router.post('/questions/', auth.required, function (req, res, next) {
|
||||
Promise.all([
|
||||
req.payload ? User.findById(req.payload.id) : null,
|
||||
QcmCategory
|
||||
.findOne({num: req.body.category.num})
|
||||
.populate('questions')
|
||||
.exec()
|
||||
]).then(async function (results) {
|
||||
const user = results[0];
|
||||
//let numero = (results[1][0].numero + 1);
|
||||
if (!user) {
|
||||
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
|
||||
}
|
||||
if (user.role !== 'Admin') {
|
||||
return res.status(403).json({ errors: { "Forbidden": "Vous ne disposez pas des autorisations suffisantes" } });
|
||||
}
|
||||
const category = results[1];
|
||||
if (!category) {
|
||||
return res.status(422).json({ errors: { category: "catégorie introuvable" } });
|
||||
}
|
||||
let questions = [];
|
||||
Promise.all(
|
||||
req.body.category.questions.map(data => {
|
||||
const questionId = new mongoose.Types.ObjectId();
|
||||
const question = new QcmQuestion({ _id: questionId, num: data.num, libelle: data.libelle, choices: []});
|
||||
questions.push(questionId);
|
||||
return question.save();
|
||||
})
|
||||
).then(function () {
|
||||
category.questions = [...questions];
|
||||
category.save().then(function () {
|
||||
QcmCategory
|
||||
.findOne({num: req.body.category.num})
|
||||
.populate('questions')
|
||||
.exec()
|
||||
.then(function (category) {
|
||||
return res.json({ category: category.toJSONFor() });
|
||||
}).catch(next);
|
||||
}).catch(next);
|
||||
}).catch(next);
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user