From 8a78b695b7a9632a016c55b249bdce9cda6485b3 Mon Sep 17 00:00:00 2001 From: HardiReady Date: Fri, 4 May 2018 20:49:45 +0200 Subject: [PATCH] Add generic method for get by id --- api/routes/_generic.js | 16 ++++++++++++++++ api/routes/campaigns.js | 14 ++------------ api/routes/decorations.js | 16 ++++------------ api/routes/ranks.js | 16 ++++------------ api/routes/squads.js | 16 ++++------------ 5 files changed, 30 insertions(+), 48 deletions(-) diff --git a/api/routes/_generic.js b/api/routes/_generic.js index 0d466cd..6843bb1 100644 --- a/api/routes/_generic.js +++ b/api/routes/_generic.js @@ -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; diff --git a/api/routes/campaigns.js b/api/routes/campaigns.js index 136b2ce..573b74b 100644 --- a/api/routes/campaigns.js +++ b/api/routes/campaigns.js @@ -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) => { diff --git a/api/routes/decorations.js b/api/routes/decorations.js index 2302217..6bd782a 100644 --- a/api/routes/decorations.js +++ b/api/routes/decorations.js @@ -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) => { diff --git a/api/routes/ranks.js b/api/routes/ranks.js index 390bb70..2853a34 100644 --- a/api/routes/ranks.js +++ b/api/routes/ranks.js @@ -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) => { diff --git a/api/routes/squads.js b/api/routes/squads.js index 80582ab..8764f4f 100644 --- a/api/routes/squads.js +++ b/api/routes/squads.js @@ -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) => {