From 7facdf08402658e83cf5ad585cbb5de661521baf Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Thu, 3 Nov 2016 16:34:01 -0600 Subject: [PATCH 1/6] make the settings routes actually work --- app.js | 4 ++++ models/setting.js | 12 +++++++++++- package.json | 1 + routes/api/settings/index.js | 4 ++-- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app.js b/app.js index e7ded2f9c..aa75e5c31 100644 --- a/app.js +++ b/app.js @@ -2,9 +2,13 @@ // Initialize application framework. const express = require('express'); +const bodyParser = require('body-parser'); const app = express(); +app.use(bodyParser.urlencoded({extended: true})); +app.use(bodyParser.json()); + app.use('/api/v1', require('./routes/api')); module.exports = app; diff --git a/models/setting.js b/models/setting.js index dd82b3e42..59de7374e 100644 --- a/models/setting.js +++ b/models/setting.js @@ -12,12 +12,22 @@ const SettingSchema = new Schema({ } }); +/** + * gets the entire settings record and sends it back + * @return {Promise} settings the whole settings record + */ SettingSchema.statics.getSettings = function () { - return this.findOne(); + return this.findOne({id: '1'}); }; +/** + * this will update the settings object with whatever you pass in + * @param {object} setting a hash of whatever settings you want to update + * @return {Promise} settings Promise that resolves to the entire (updated) settings object. + */ SettingSchema.statics.updateSettings = function (setting) { // there should only ever be one record unless something has gone wrong. + console.log('new setting', setting); return this.findOneAndUpdate({id: '1'}, {$set: setting}, {new: true}); }; diff --git a/package.json b/package.json index b7fb48d34..7d561d926 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ }, "homepage": "https://github.com/coralproject/talk#readme", "dependencies": { + "body-parser": "^1.15.2", "debug": "^2.2.0", "express": "^4.14.0", "mongoose": "^4.6.5" diff --git a/routes/api/settings/index.js b/routes/api/settings/index.js index fa5d3a620..cda9a3806 100644 --- a/routes/api/settings/index.js +++ b/routes/api/settings/index.js @@ -3,11 +3,11 @@ const router = express.Router(); const Setting = require('../../../models/setting'); router.get('/', (req, res, next) => { - Setting.getSettings().then(res.json).catch(next); + Setting.getSettings().then(res.json.bind(res)).catch(next); }); router.put('/', (req, res, next) => { - Setting.updateSettings(req.body).then(res.json).catch(next); + Setting.updateSettings(req.body).then(res.json.bind(res)).catch(next); }); module.exports = router; From de405492b9dffaee5f88ae4f1e68cb4a30bbc2f6 Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Thu, 3 Nov 2016 16:36:33 -0600 Subject: [PATCH 2/6] remove log --- models/setting.js | 1 - 1 file changed, 1 deletion(-) diff --git a/models/setting.js b/models/setting.js index 59de7374e..b47b6e9e2 100644 --- a/models/setting.js +++ b/models/setting.js @@ -27,7 +27,6 @@ SettingSchema.statics.getSettings = function () { */ SettingSchema.statics.updateSettings = function (setting) { // there should only ever be one record unless something has gone wrong. - console.log('new setting', setting); return this.findOneAndUpdate({id: '1'}, {$set: setting}, {new: true}); }; From 81e16f8a8d3f4b393bc9c820a17c2e9f7cba695d Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Thu, 3 Nov 2016 16:47:18 -0600 Subject: [PATCH 3/6] don't bind res.json --- routes/api/settings/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routes/api/settings/index.js b/routes/api/settings/index.js index cda9a3806..5cb7bcc01 100644 --- a/routes/api/settings/index.js +++ b/routes/api/settings/index.js @@ -3,11 +3,11 @@ const router = express.Router(); const Setting = require('../../../models/setting'); router.get('/', (req, res, next) => { - Setting.getSettings().then(res.json.bind(res)).catch(next); + Setting.getSettings().then(settings => res.json(settings)).catch(next); }); router.put('/', (req, res, next) => { - Setting.updateSettings(req.body).then(res.json.bind(res)).catch(next); + Setting.updateSettings(req.body).then(settings => res.json(settings)).catch(next); }); module.exports = router; From d28f46f9caf6227c5f75c0cae49fa03a0e1aa179 Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Thu, 3 Nov 2016 16:49:03 -0600 Subject: [PATCH 4/6] disallow url-encoded payloads. --- app.js | 1 - 1 file changed, 1 deletion(-) diff --git a/app.js b/app.js index aa75e5c31..8d32c7ec2 100644 --- a/app.js +++ b/app.js @@ -6,7 +6,6 @@ const bodyParser = require('body-parser'); const app = express(); -app.use(bodyParser.urlencoded({extended: true})); app.use(bodyParser.json()); app.use('/api/v1', require('./routes/api')); From 877200cf1908da2edbc0178f4686b060d05fc776 Mon Sep 17 00:00:00 2001 From: Belen Curcio Date: Fri, 4 Nov 2016 10:51:08 -0300 Subject: [PATCH 5/6] settings schema swagger --- swagger.yaml | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/swagger.yaml b/swagger.yaml index a7fdc973d..50df1ff4b 100644 --- a/swagger.yaml +++ b/swagger.yaml @@ -150,14 +150,22 @@ paths: description: Settings responses: 200: - description: OK + description: Get Setting + schema: + type: array + items: + $ref: '#/definitions/Setting' put: tags: - Settings description: Settings responses: 204: - description: OK + description: Updated Setting + schema: + type: array + items: + $ref: '#/definitions/Setting' @@ -212,3 +220,21 @@ definitions: type: string user_id: type: string + Setting: + type: object + properties: + id: + type: string + moderation: + type: string + enum: + - pre + - post + created_at: + type: string + format: date-time + description: Creation Date-Time + updated_at: + type: string + format: date-time + description: Updated Date-Time From dba1b04558376b74927b9ae1e165cd3f531bce88 Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Fri, 4 Nov 2016 14:06:02 -0600 Subject: [PATCH 6/6] 204 no content --- routes/api/settings/index.js | 2 +- swagger.yaml | 6 ------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/routes/api/settings/index.js b/routes/api/settings/index.js index 5cb7bcc01..53bf53f3c 100644 --- a/routes/api/settings/index.js +++ b/routes/api/settings/index.js @@ -7,7 +7,7 @@ router.get('/', (req, res, next) => { }); router.put('/', (req, res, next) => { - Setting.updateSettings(req.body).then(settings => res.json(settings)).catch(next); + Setting.updateSettings(req.body).then(() => res.status(204).end()).catch(next); }); module.exports = router; diff --git a/swagger.yaml b/swagger.yaml index 50df1ff4b..3ab5a21df 100644 --- a/swagger.yaml +++ b/swagger.yaml @@ -162,12 +162,6 @@ paths: responses: 204: description: Updated Setting - schema: - type: array - items: - $ref: '#/definitions/Setting' - - definitions: Item: