diff --git a/app.js b/app.js index 0c8b57b..f15e50c 100644 --- a/app.js +++ b/app.js @@ -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'); diff --git a/models/mongo/QCM.js b/models/mongo/QCM.js new file mode 100644 index 0000000..084a266 --- /dev/null +++ b/models/mongo/QCM.js @@ -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); diff --git a/models/mongo/QCMCategories.js b/models/mongo/QCMCategories.js new file mode 100644 index 0000000..e42d836 --- /dev/null +++ b/models/mongo/QCMCategories.js @@ -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); \ No newline at end of file diff --git a/routes/api/index.js b/routes/api/index.js index 524529e..70118fe 100644 --- a/routes/api/index.js +++ b/routes/api/index.js @@ -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')); diff --git a/routes/api/qcm.js b/routes/api/qcm.js new file mode 100644 index 0000000..64b911d --- /dev/null +++ b/routes/api/qcm.js @@ -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; \ No newline at end of file