From 6958c496228b49d85d62376f7aa7cccc105d935e Mon Sep 17 00:00:00 2001 From: Joey Baker Date: Wed, 29 Jun 2016 21:27:44 -0700 Subject: [PATCH 1/3] Build: install boom --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 073fa31..2639f8b 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ }, "dependencies": { "babel": "5.8.3", + "boom": "^3.2.2", "joi": "7.2.1", "lodash": "4.0.0" } From 3f419284be16a10960f78e00508c54fe47f260fe Mon Sep 17 00:00:00 2001 From: Joey Baker Date: Wed, 29 Jun 2016 21:28:56 -0700 Subject: [PATCH 2/3] Fix: Reply with 404 when required --- src/crud.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/crud.js b/src/crud.js index 5af5d2a..4558df7 100644 --- a/src/crud.js +++ b/src/crud.js @@ -2,6 +2,7 @@ import joi from 'joi'; import error from './error'; import _ from 'lodash'; import { parseInclude, parseWhere } from './utils'; +import { notFound } from 'boom'; let prefix; let defaultConfig; @@ -50,10 +51,13 @@ export const get = (server, model) => { async handler(request, reply) { const include = parseInclude(request); const where = parseWhere(request); - if (request.params.id) where.id = request.params.id; + const {id} = request.params; + if (id) where.id = id; const instance = await model.findOne({ where, include }); + if (!instance) return void reply(notFound(`${id} not found.`)); + reply(instance); }, config: _.defaultsDeep({ @@ -186,12 +190,15 @@ export const update = (server, model) => { @error async handler(request, reply) { - let instance = await model.findOne({ + const {id} = request.params; + const instance = await model.findOne({ where: { - id: request.params.id + id } }); + if (!instance) return void reply(notFound(`${id} not found.`)); + await instance.update(request.payload); reply(instance); From 0698a8a3ad559a213c50d82c17ca5486230b1edb Mon Sep 17 00:00:00 2001 From: Joey Baker Date: Wed, 29 Jun 2016 21:30:20 -0700 Subject: [PATCH 3/3] Fix: crud: GET id's can be strings or numbers --- src/crud.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crud.js b/src/crud.js index 4558df7..7f62721 100644 --- a/src/crud.js +++ b/src/crud.js @@ -63,7 +63,7 @@ export const get = (server, model) => { config: _.defaultsDeep({ validate: { params: joi.object().keys({ - id: joi.number().integer() + id: joi.any() }) } }, defaultConfig)