Add generic method for get by id
parent
bdb07ecc1e
commit
8a78b695b7
|
@ -3,6 +3,21 @@
|
||||||
// HTTP status codes by name
|
// HTTP status codes by name
|
||||||
const codes = require('./http-codes');
|
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) => {
|
const genericPatch = (req, res, next, modelClass) => {
|
||||||
if (!req.body || (req.body._id && req.body._id !== req.params.id)) {
|
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
|
// 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;
|
exports.genericPatch = genericPatch;
|
||||||
|
|
|
@ -17,6 +17,7 @@ const CampaignModel = require('../models/campaign');
|
||||||
const WarModel = require('../models/war');
|
const WarModel = require('../models/war');
|
||||||
|
|
||||||
// util
|
// util
|
||||||
|
const genericGetById = require('./_generic').genericGetById;
|
||||||
const genericPatch = require('./_generic').genericPatch;
|
const genericPatch = require('./_generic').genericPatch;
|
||||||
|
|
||||||
const campaigns = new express.Router();
|
const campaigns = new express.Router();
|
||||||
|
@ -44,18 +45,7 @@ campaigns.route('/')
|
||||||
|
|
||||||
campaigns.route('/:id')
|
campaigns.route('/:id')
|
||||||
.get(idValidator, (req, res, next) => {
|
.get(idValidator, (req, res, next) => {
|
||||||
CampaignModel.findById(req.params.id, (err, item) => {
|
return genericGetById(req, res, next, CampaignModel);
|
||||||
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();
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
|
|
||||||
.patch(apiAuthenticationMiddleware, checkMT, (req, res, next) => {
|
.patch(apiAuthenticationMiddleware, checkMT, (req, res, next) => {
|
||||||
|
|
|
@ -21,6 +21,9 @@ const resourceLocation = require('../middleware/resource-location').resourceLoca
|
||||||
const DecorationModel = require('../models/decoration');
|
const DecorationModel = require('../models/decoration');
|
||||||
const AwardingsModel = require('../models/awarding');
|
const AwardingsModel = require('../models/awarding');
|
||||||
|
|
||||||
|
// util
|
||||||
|
const genericGetById = require('./_generic').genericGetById;
|
||||||
|
|
||||||
const decoration = new express.Router();
|
const decoration = new express.Router();
|
||||||
|
|
||||||
// routes **********************
|
// routes **********************
|
||||||
|
@ -81,18 +84,7 @@ decoration.route('/')
|
||||||
|
|
||||||
decoration.route('/:id')
|
decoration.route('/:id')
|
||||||
.get(idValidator, (req, res, next) => {
|
.get(idValidator, (req, res, next) => {
|
||||||
DecorationModel.findById(req.params.id, (err, item) => {
|
return genericGetById(req, res, next, DecorationModel);
|
||||||
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();
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
|
|
||||||
.patch(apiAuthenticationMiddleware, checkHl, upload.single('image'), (req, res, next) => {
|
.patch(apiAuthenticationMiddleware, checkHl, upload.single('image'), (req, res, next) => {
|
||||||
|
|
|
@ -20,6 +20,9 @@ const resourceLocation = require('../middleware/resource-location').resourceLoca
|
||||||
// Mongoose Model using mongoDB
|
// Mongoose Model using mongoDB
|
||||||
const RankModel = require('../models/rank');
|
const RankModel = require('../models/rank');
|
||||||
|
|
||||||
|
// util
|
||||||
|
const genericGetById = require('./_generic').genericGetById;
|
||||||
|
|
||||||
const ranks = new express.Router();
|
const ranks = new express.Router();
|
||||||
|
|
||||||
// routes **********************
|
// routes **********************
|
||||||
|
@ -73,18 +76,7 @@ ranks.route('/')
|
||||||
|
|
||||||
ranks.route('/:id')
|
ranks.route('/:id')
|
||||||
.get(idValidator, (req, res, next) => {
|
.get(idValidator, (req, res, next) => {
|
||||||
RankModel.findById(req.params.id, (err, item) => {
|
return genericGetById(req, res, next, RankModel);
|
||||||
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();
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
|
|
||||||
.patch(apiAuthenticationMiddleware, checkHl, upload.single('image'), (req, res, next) => {
|
.patch(apiAuthenticationMiddleware, checkHl, upload.single('image'), (req, res, next) => {
|
||||||
|
|
|
@ -20,6 +20,9 @@ const resourceLocation = require('../middleware/resource-location').resourceLoca
|
||||||
// Mongoose Model using mongoDB
|
// Mongoose Model using mongoDB
|
||||||
const SquadModel = require('../models/squad');
|
const SquadModel = require('../models/squad');
|
||||||
|
|
||||||
|
// util
|
||||||
|
const genericGetById = require('./_generic').genericGetById;
|
||||||
|
|
||||||
const squads = new express.Router();
|
const squads = new express.Router();
|
||||||
|
|
||||||
// routes **********************
|
// routes **********************
|
||||||
|
@ -77,18 +80,7 @@ squads.route('/')
|
||||||
|
|
||||||
squads.route('/:id')
|
squads.route('/:id')
|
||||||
.get(idValidator, (req, res, next) => {
|
.get(idValidator, (req, res, next) => {
|
||||||
SquadModel.findById(req.params.id, (err, item) => {
|
return genericGetById(req, res, next, SquadModel);
|
||||||
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();
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
|
|
||||||
.patch(apiAuthenticationMiddleware, checkHl, upload.single('image'), (req, res, next) => {
|
.patch(apiAuthenticationMiddleware, checkHl, upload.single('image'), (req, res, next) => {
|
||||||
|
|
Loading…
Reference in New Issue