Refactoring
This commit is contained in:
@@ -20,6 +20,9 @@ router.param('application', function (req, res, next, slug) {
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
/**
|
||||
* return all applications
|
||||
*/
|
||||
router.get('/', auth.required, function (req, res, next) {
|
||||
let query = {};
|
||||
let limit = 25;
|
||||
@@ -68,6 +71,9 @@ router.get('/', auth.required, function (req, res, next) {
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
/**
|
||||
* save an application
|
||||
*/
|
||||
router.post('/', auth.required, function (req, res, next) {
|
||||
Promise.all([
|
||||
User.findById(req.payload.id),
|
||||
@@ -106,7 +112,9 @@ router.post('/', auth.required, function (req, res, next) {
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
// return an application
|
||||
/**
|
||||
* return an application
|
||||
*/
|
||||
router.get('/:application', auth.required, function (req, res, next) {
|
||||
Promise.all([
|
||||
req.payload ? User.findById(req.payload.id) : null,
|
||||
@@ -117,6 +125,9 @@ router.get('/:application', auth.required, function (req, res, next) {
|
||||
}).catch(next);
|
||||
});
|
||||
|
||||
/**
|
||||
* update an application
|
||||
*/
|
||||
router.put('/:application', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
if (req.application.author._id.toString() === req.payload.id.toString()) {
|
||||
@@ -141,7 +152,9 @@ router.put('/:application', auth.required, function (req, res, next) {
|
||||
});
|
||||
});
|
||||
|
||||
// delete application
|
||||
/**
|
||||
* delete an application
|
||||
*/
|
||||
router.delete('/:application', auth.required, function (req, res, next) {
|
||||
User.findById(req.payload.id).then(function (user) {
|
||||
if (!user) {
|
||||
@@ -149,7 +162,7 @@ router.delete('/:application', auth.required, function (req, res, next) {
|
||||
}
|
||||
if (user.role == 'Admin' || req.application.author._id.toString() === req.payload.id.toString()) {
|
||||
return req.application.remove().then(function () {
|
||||
return res.sendStatus(204);
|
||||
return res.json({ deleted: true });
|
||||
});
|
||||
} else {
|
||||
return res.status(403).json({ errors: { "Forbidden": "Vous ne disposez pas des autorisations suffisantes" } });
|
||||
|
||||
Reference in New Issue
Block a user