From 0afb943dfc8f04f7b37d2c87fa1b5657305df4e7 Mon Sep 17 00:00:00 2001 From: Dan Ribbens Date: Sat, 2 Feb 2019 16:29:48 -0500 Subject: [PATCH] handles post and update of specific language on page model --- demo/Page/Page.controller.js | 73 +++++++++++++++++++----------------- src/index.js | 7 ++-- src/middleware/locale.js | 14 +++++-- src/tests/middleware.spec.js | 14 ++++++- 4 files changed, 64 insertions(+), 44 deletions(-) diff --git a/demo/Page/Page.controller.js b/demo/Page/Page.controller.js index f67bf8ad22..5c7771337c 100644 --- a/demo/Page/Page.controller.js +++ b/demo/Page/Page.controller.js @@ -3,30 +3,28 @@ import httpStatus from 'http-status'; const pageController = { query(req, res) { - if (req.query.locale) { + if (req.query.locale) Page.setDefaultLanguage(req.query.locale); - } + Page.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 }))); + 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) { Page.findById(req.params.id, (err, doc) => { - if (err) { - return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err }); - } + if (err) + return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({error: err}); - if (!doc) { + 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.toJSON({virtuals: true})); } return res.json(doc); @@ -34,41 +32,48 @@ const pageController = { }, post(req, res) { - Page.create(req.body, err => { - if (err) { - return res.send(httpStatus.INTERNAL_SERVER_ERROR, { error: err }); - } - return res.json({ - message: 'Page created successfully' - }); + Page.setDefaultLanguage(req.locale); + Page.create(req.body, (err, result) => { + if (err) + return res.send(httpStatus.INTERNAL_SERVER_ERROR, {error: err}); + + return res.status(httpStatus.CREATED) + .json({ + message: 'Page created successfully', + result: result.toJSON({virtuals: true}) + }); }); }, update(req, res) { - Page.findOneAndUpdate({ _id: req.params.id }, req.body, { new: true }, (err, doc) => { - if (err) { - return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err }); - } - - if (!doc) { + Page.setDefaultLanguage(req.locale); + Page.findOne({_id: req.params.id}, '', {}, (err, doc) => { + if (!doc) return res.status(httpStatus.NOT_FOUND).send('Not Found'); - } - return res.json({ - message: 'Page updated successfully' + 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: 'Page updated successfully', + result: doc.toJSON({virtuals: true}) + }); }); }); }, delete(req, res) { - Page.findOneAndDelete({ _id: req.params.id }, (err, doc) => { - if (err) { - return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err }); - } + Page.findOneAndDelete({_id: req.params.id}, (err, doc) => { + if (err) + return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({error: err}); - if (!doc) { + if (!doc) return res.status(httpStatus.NOT_FOUND).send('Not Found'); - } return res.send(); }); diff --git a/src/index.js b/src/index.js index 7dfa222442..07f75971d8 100644 --- a/src/index.js +++ b/src/index.js @@ -8,7 +8,7 @@ import User from '../demo/User/User.model'; import fileUpload from 'express-fileupload'; import assetRoutes from './routes/uploads.routes' import config from '../demo/payload.config'; -import language from './middleware/locale'; +import locale from './middleware/locale'; module.exports = { init: options => { @@ -38,19 +38,18 @@ module.exports = { res.header('Access-Control-Allow-Headers', 'Origin X-Requested-With, Content-Type, Accept, Authorization'); res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS'); - res.header('Content-Language', options.config.localization.language); + res.header('Content-Language', options.config.localization.locale); next(); }); options.router.use('/upload', assetRoutes(options.config)); - options.app.use(language(config.localization)); - options.app.use(express.json()); options.app.use(methodOverride('X-HTTP-Method-Override')); options.app.use(express.urlencoded({extended: true})); options.app.use(bodyParser.urlencoded({ extended: true })); + options.app.use(locale(config.localization)); options.app.use(options.router); } }; diff --git a/src/middleware/locale.js b/src/middleware/locale.js index 1f44a50c8f..044028161c 100644 --- a/src/middleware/locale.js +++ b/src/middleware/locale.js @@ -8,15 +8,21 @@ import languageParser from 'accept-language-parser'; */ export default function locale(localization) { return function (req, res, next) { - let query; + let setLocale; if (req.query.locale) { - query = localization.languages.find(locale => locale === req.query.locale); - if (query) { - req.locale = query; + setLocale = localization.languages.find(search => search === req.query.locale); + if (setLocale) { + req.locale = setLocale; } if (req.query.locale === '*' || req.query.locale === 'all') return next(); } + if (req.body.locale){ + setLocale = localization.languages.find(search => search === req.body.locale); + if (setLocale) { + req.locale = setLocale; + } + } if (!req.locale && req.headers['accept-language']) req.locale = languageParser.pick(localization.languages, req.headers['accept-language']); if (!req.locale) diff --git a/src/tests/middleware.spec.js b/src/tests/middleware.spec.js index 20be1da775..e6cbf2398b 100644 --- a/src/tests/middleware.spec.js +++ b/src/tests/middleware.spec.js @@ -48,7 +48,8 @@ describe('Payload Middleware', () => { beforeEach(() => { req = { query: {}, - headers: {} + headers: {}, + body: {} }; localization = { languages: ['en', 'es'], @@ -67,7 +68,7 @@ describe('Payload Middleware', () => { }); it('Supports query param fallback to default', () => { - req.query.lang = 'pt'; + req.query.locale = 'pt'; locale(localization)(req, res, next); @@ -118,5 +119,14 @@ describe('Payload Middleware', () => { expect(next).toHaveBeenCalledTimes(1); expect(req.locale).toBeUndefined(); }); + + it('Supports locale in body on post', () => { + req.body = {locale: 'es'}; + req.method = 'post'; + locale(localization)(req, res, next); + + expect(next).toHaveBeenCalledTimes(1); + expect(req.locale).toEqual('es'); + }); }); });