Files
headup_api/routes/api/qcm.js
T
2024-01-11 16:54:27 +01:00

50 lines
1.3 KiB
JavaScript

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;