Add generic method for get by id
parent
bdb07ecc1e
commit
8a78b695b7
|
@ -3,6 +3,21 @@
|
|||
// HTTP status codes by name
|
||||
const codes = require('./http-codes');
|
||||
|
||||
const genericGetById = (req, res, next, modelClass) => {
|
||||
modelClass.findById(req.params.id, (err, item) => {
|
||||
if (err) {
|
||||
err.status = codes.servererror;
|
||||
return next(err);
|
||||
} else if (!item) {
|
||||
err = new Error('item not found');
|
||||
err.status = codes.notfound;
|
||||
return next(err);
|
||||
}
|
||||
res.locals.items = item;
|
||||
return next();
|
||||
});
|
||||
};
|
||||
|
||||
const genericPatch = (req, res, next, modelClass) => {
|
||||
if (!req.body || (req.body._id && req.body._id !== req.params.id)) {
|
||||
// little bit different as in PUT. :id does not need to be in data, but if the _id and url id must match
|
||||
|
@ -34,4 +49,5 @@ const genericPatch = (req, res, next, modelClass) => {
|
|||
});
|
||||
};
|
||||
|
||||
exports.genericGetById = genericGetById;
|
||||
exports.genericPatch = genericPatch;
|
||||
|
|
|
@ -17,6 +17,7 @@ const CampaignModel = require('../models/campaign');
|
|||
const WarModel = require('../models/war');
|
||||
|
||||
// util
|
||||
const genericGetById = require('./_generic').genericGetById;
|
||||
const genericPatch = require('./_generic').genericPatch;
|
||||
|
||||
const campaigns = new express.Router();
|
||||
|
@ -44,18 +45,7 @@ campaigns.route('/')
|
|||
|
||||
campaigns.route('/:id')
|
||||
.get(idValidator, (req, res, next) => {
|
||||
CampaignModel.findById(req.params.id, (err, item) => {
|
||||
if (err) {
|
||||
err.status = codes.servererror;
|
||||
return next(err);
|
||||
} else if (!item) {
|
||||
err = new Error('item not found');
|
||||
err.status = codes.notfound;
|
||||
return next(err);
|
||||
}
|
||||
res.locals.items = item;
|
||||
return next();
|
||||
});
|
||||
return genericGetById(req, res, next, CampaignModel);
|
||||
})
|
||||
|
||||
.patch(apiAuthenticationMiddleware, checkMT, (req, res, next) => {
|
||||
|
|
|
@ -21,6 +21,9 @@ const resourceLocation = require('../middleware/resource-location').resourceLoca
|
|||
const DecorationModel = require('../models/decoration');
|
||||
const AwardingsModel = require('../models/awarding');
|
||||
|
||||
// util
|
||||
const genericGetById = require('./_generic').genericGetById;
|
||||
|
||||
const decoration = new express.Router();
|
||||
|
||||
// routes **********************
|
||||
|
@ -81,18 +84,7 @@ decoration.route('/')
|
|||
|
||||
decoration.route('/:id')
|
||||
.get(idValidator, (req, res, next) => {
|
||||
DecorationModel.findById(req.params.id, (err, item) => {
|
||||
if (err) {
|
||||
err.status = codes.servererror;
|
||||
return next(err);
|
||||
} else if (!item) {
|
||||
err = new Error('item not found');
|
||||
err.status = codes.notfound;
|
||||
return next(err);
|
||||
}
|
||||
res.locals.items = item;
|
||||
next();
|
||||
});
|
||||
return genericGetById(req, res, next, DecorationModel);
|
||||
})
|
||||
|
||||
.patch(apiAuthenticationMiddleware, checkHl, upload.single('image'), (req, res, next) => {
|
||||
|
|
|
@ -20,6 +20,9 @@ const resourceLocation = require('../middleware/resource-location').resourceLoca
|
|||
// Mongoose Model using mongoDB
|
||||
const RankModel = require('../models/rank');
|
||||
|
||||
// util
|
||||
const genericGetById = require('./_generic').genericGetById;
|
||||
|
||||
const ranks = new express.Router();
|
||||
|
||||
// routes **********************
|
||||
|
@ -73,18 +76,7 @@ ranks.route('/')
|
|||
|
||||
ranks.route('/:id')
|
||||
.get(idValidator, (req, res, next) => {
|
||||
RankModel.findById(req.params.id, (err, item) => {
|
||||
if (err) {
|
||||
err.status = codes.servererror;
|
||||
return next(err);
|
||||
} else if (!item) {
|
||||
err = new Error('item not found');
|
||||
err.status = codes.notfound;
|
||||
return next(err);
|
||||
}
|
||||
res.locals.items = item;
|
||||
next();
|
||||
});
|
||||
return genericGetById(req, res, next, RankModel);
|
||||
})
|
||||
|
||||
.patch(apiAuthenticationMiddleware, checkHl, upload.single('image'), (req, res, next) => {
|
||||
|
|
|
@ -20,6 +20,9 @@ const resourceLocation = require('../middleware/resource-location').resourceLoca
|
|||
// Mongoose Model using mongoDB
|
||||
const SquadModel = require('../models/squad');
|
||||
|
||||
// util
|
||||
const genericGetById = require('./_generic').genericGetById;
|
||||
|
||||
const squads = new express.Router();
|
||||
|
||||
// routes **********************
|
||||
|
@ -77,18 +80,7 @@ squads.route('/')
|
|||
|
||||
squads.route('/:id')
|
||||
.get(idValidator, (req, res, next) => {
|
||||
SquadModel.findById(req.params.id, (err, item) => {
|
||||
if (err) {
|
||||
err.status = codes.servererror;
|
||||
return next(err);
|
||||
} else if (!item) {
|
||||
err = new Error('item not found');
|
||||
err.status = codes.notfound;
|
||||
return next(err);
|
||||
}
|
||||
res.locals.items = item;
|
||||
next();
|
||||
});
|
||||
return genericGetById(req, res, next, SquadModel);
|
||||
})
|
||||
|
||||
.patch(apiAuthenticationMiddleware, checkHl, upload.single('image'), (req, res, next) => {
|
||||
|
|
Loading…
Reference in New Issue