Ajout de models et routes pour les QCM
This commit is contained in:
@@ -65,6 +65,7 @@ require('./models/mongo/Aeronef');
|
||||
require('./models/mongo/Canopy');
|
||||
require('./models/mongo/Dropzone');
|
||||
require('./models/mongo/Jump');
|
||||
require('./models/mongo/QCM');
|
||||
/*
|
||||
require('./models/mongo/Metal');
|
||||
require('./models/mongo/Price');
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
var mongoose = require('mongoose');
|
||||
var uniqueValidator = require('mongoose-unique-validator');
|
||||
var slug = require('slug');
|
||||
|
||||
var QCMSchema = new mongoose.Schema({
|
||||
name: { type: String, lowercase: true, unique: true },
|
||||
categories: [
|
||||
{ type: mongoose.Schema.Types.ObjectId, ref: 'QCMCategory' }
|
||||
]
|
||||
}, {timestamps: true, toJSON: {virtuals: true}});
|
||||
|
||||
QCMSchema.plugin(uniqueValidator, { message: 'is already taken' });
|
||||
|
||||
QCMSchema.methods.toJSONFor = function(){
|
||||
return {
|
||||
name: this.name,
|
||||
categories: this.categories,
|
||||
createdAt: this.createdAt,
|
||||
updatedAt: this.updatedAt
|
||||
};
|
||||
};
|
||||
|
||||
mongoose.model('QCM', QCMSchema);
|
||||
@@ -0,0 +1,23 @@
|
||||
var mongoose = require('mongoose');
|
||||
var uniqueValidator = require('mongoose-unique-validator');
|
||||
var slug = require('slug');
|
||||
|
||||
var QCMCategorySchema = new mongoose.Schema({
|
||||
num: number,
|
||||
name: { type: String, lowercase: true, unique: true },
|
||||
questions: []
|
||||
}, {timestamps: true, toJSON: {virtuals: true}});
|
||||
|
||||
QCMCategorySchema.plugin(uniqueValidator, { message: 'is already taken' });
|
||||
|
||||
QCMCategorySchema.methods.toJSONFor = function(){
|
||||
return {
|
||||
num: this.num,
|
||||
name: this.name,
|
||||
questions: this.questions,
|
||||
createdAt: this.createdAt,
|
||||
updatedAt: this.updatedAt
|
||||
};
|
||||
};
|
||||
|
||||
mongoose.model('QCMCategory', QCMCategorySchema);
|
||||
@@ -7,6 +7,7 @@ router.use('/aeronefs', require('./aeronefs'));
|
||||
router.use('/canopies', require('./canopies'));
|
||||
router.use('/dropzones', require('./dropzones'));
|
||||
router.use('/jumps', require('./jumps'));
|
||||
router.use('/qcm', require('./qcm'));
|
||||
/*
|
||||
router.use('/products', require('./products'));
|
||||
router.use('/metals', require('./metals'));
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
var router = require('express').Router(),
|
||||
mongoose = require('mongoose'),
|
||||
QCM = mongoose.model('QCM'),
|
||||
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(function (qcm) {
|
||||
if (!qcm) {
|
||||
return res.sendStatus(404);
|
||||
}
|
||||
req.qcm = qcm;
|
||||
return 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) {
|
||||
var user = results[0];
|
||||
if (!user) {
|
||||
return res.sendStatus(401);
|
||||
}
|
||||
return res.json({ qcm: [] });
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
/**
|
||||
* return a qcm
|
||||
*/
|
||||
router.get('/:type', auth.required, function (req, res, next) {
|
||||
Promise.all([
|
||||
req.payload ? User.findById(req.payload.id) : null
|
||||
]).then(function (results) {
|
||||
var user = results[0];
|
||||
if (!user) {
|
||||
return res.sendStatus(401);
|
||||
}
|
||||
return res.json({ qcm: req.qcm.toJSONFor() });
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user