diff --git a/demo/Post/Post.requestHandler.js b/demo/Post/Post.requestHandler.js new file mode 100644 index 0000000000..59b0a0b06d --- /dev/null +++ b/demo/Post/Post.requestHandler.js @@ -0,0 +1,17 @@ +import Post from './Post.model'; +import httpStatus from 'http-status'; + +const destroy = (req, res) => { + // Do something unique to deleting this model + Post.findOneAndDelete({_id: req.params.id}, (err, doc) => { + if (err) + return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({error: err}); + + if (!doc) + return res.status(httpStatus.NOT_FOUND).send('Not Found'); + + return res.send(); + }); +}; + +export {destroy}; diff --git a/demo/Post/Post.routes.js b/demo/Post/Post.routes.js index 30c47aebb3..6081dac9c9 100644 --- a/demo/Post/Post.routes.js +++ b/demo/Post/Post.routes.js @@ -1,7 +1,10 @@ import express from 'express'; -import postPolicy from './Post.policy'; +import {query, create, find} from '../../src/requestHandlers'; // payload implementations + +import update from '../shared/requestHandlers/update'; // application implementations +import {destroy} from './Post.requestHandler'; // model specific implementations import Post from './Post.model'; -import requestHandler from '../../src/requestHandlers'; +import postPolicy from './Post.policy'; const router = express.Router(); // eslint-disable-line new-cap @@ -14,13 +17,13 @@ router.all('', (req, res, next) => { router .route('') - .get(postPolicy.query, requestHandler.query) - .post(postPolicy.post, requestHandler.post); + .get(postPolicy.query, query) + .post(postPolicy.post, create); router .route('/:id') - .get(postPolicy.find, requestHandler.find) - .put(postPolicy.update, requestHandler.update) - .delete(postPolicy.delete, requestHandler.delete); + .get(postPolicy.find, find) + .put(postPolicy.update, update) + .delete(postPolicy.delete, destroy); export default router; diff --git a/demo/shared/requestHandlers/update.js b/demo/shared/requestHandlers/update.js new file mode 100644 index 0000000000..4ccdd18d48 --- /dev/null +++ b/demo/shared/requestHandlers/update.js @@ -0,0 +1,24 @@ +import httpStatus from '../../../src/requestHandlers'; + +export default (req, res) => { + // do something custom specific to this app + req.model.setDefaultLanguage(req.locale); + req.model.findOne({_id: req.params.id}, '', {}, (err, doc) => { + if (!doc) + return res.status(httpStatus.NOT_FOUND).send('Not Found'); + + Object.keys(req.body).forEach(e => { + doc[e] = req.body[e]; + }); + + doc.save((err) => { + if (err) + return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({error: err}); + + return res.json({ + message: 'success', + result: doc.toJSON({virtuals: true}) + }); + }); + }); +}; diff --git a/src/requestHandlers/index.js b/src/requestHandlers/index.js index 5f0d12a4ad..32d9ee3c33 100644 --- a/src/requestHandlers/index.js +++ b/src/requestHandlers/index.js @@ -1,82 +1,80 @@ import httpStatus from 'http-status'; -const requestHandler = { - query(req, res) { - if (req.query.locale) - req.model.setDefaultLanguage(req.query.locale); +const query = (req, res) => { + if (req.query.locale) + req.model.setDefaultLanguage(req.query.locale); - req.model.apiQuery(req.query, (err, pages) => { - if (err) - return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({error: err}); + req.model.apiQuery(req.query, (err, pages) => { + if (err) + return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({error: err}); - return res.json(pages.map(page => page.toJSON({virtuals: !!req.locale}))); - }); - }, - - find(req, res) { - req.model.findById(req.params.id, (err, doc) => { - if (err) - return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({error: err}); - - if (!doc) - return res.status(httpStatus.NOT_FOUND).send('Not Found'); - - if (req.locale) { - doc.setLanguage(req.locale); - return res.json(doc.toJSON({virtuals: true})); - } - - return res.json(doc); - }); - }, - - post(req, res) { - req.model.setDefaultLanguage(req.locale); - req.model.create(req.body, (err, result) => { - if (err) - return res.send(httpStatus.INTERNAL_SERVER_ERROR, {error: err}); - - return res.status(httpStatus.CREATED) - .json({ - message: 'success', - result: result.toJSON({virtuals: true}) - }); - }); - }, - - update(req, res) { - req.model.setDefaultLanguage(req.locale); - req.model.findOne({_id: req.params.id}, '', {}, (err, doc) => { - if (!doc) - return res.status(httpStatus.NOT_FOUND).send('Not Found'); - - Object.keys(req.body).forEach(e => { - doc[e] = req.body[e]; - }); - - doc.save((err) => { - if (err) - return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({error: err}); - - return res.json({ - message: 'success', - result: doc.toJSON({virtuals: true}) - }); - }); - }); - }, - - delete(req, res) { - req.model.findOneAndDelete({_id: req.params.id}, (err, doc) => { - if (err) - return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({error: err}); - - if (!doc) - return res.status(httpStatus.NOT_FOUND).send('Not Found'); - - return res.send(); - }); - }, + return res.json(pages.map(page => page.toJSON({virtuals: !!req.locale}))); + }); }; -export default requestHandler; +const find = (req, res) => { + req.model.findById(req.params.id, (err, doc) => { + if (err) + return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({error: err}); + + if (!doc) + return res.status(httpStatus.NOT_FOUND).send('Not Found'); + + if (req.locale) { + doc.setLanguage(req.locale); + return res.json(doc.toJSON({virtuals: true})); + } + + return res.json(doc); + }); +}; + +const create = (req, res) => { + req.model.setDefaultLanguage(req.locale); + req.model.create(req.body, (err, result) => { + if (err) + return res.send(httpStatus.INTERNAL_SERVER_ERROR, {error: err}); + + return res.status(httpStatus.CREATED) + .json({ + message: 'success', + result: result.toJSON({virtuals: true}) + }); + }); +}; + +const update = (req, res) => { + req.model.setDefaultLanguage(req.locale); + req.model.findOne({_id: req.params.id}, '', {}, (err, doc) => { + if (!doc) + return res.status(httpStatus.NOT_FOUND).send('Not Found'); + + Object.keys(req.body).forEach(e => { + doc[e] = req.body[e]; + }); + + doc.save((err) => { + if (err) + return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({error: err}); + + return res.json({ + message: 'success', + result: doc.toJSON({virtuals: true}) + }); + }); + }); +}; + +const destroy = (req, res) => { + req.model.findOneAndDelete({_id: req.params.id}, (err, doc) => { + if (err) + return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({error: err}); + + if (!doc) + return res.status(httpStatus.NOT_FOUND).send('Not Found'); + + return res.send(); + }); +}; + +export {query, find, create, update, destroy};