commit
3d78b467ac
@ -29,6 +29,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"babel": "5.8.3",
|
"babel": "5.8.3",
|
||||||
|
"boom": "^3.2.2",
|
||||||
"joi": "7.2.1",
|
"joi": "7.2.1",
|
||||||
"lodash": "4.0.0"
|
"lodash": "4.0.0"
|
||||||
}
|
}
|
||||||
|
15
src/crud.js
15
src/crud.js
@ -2,6 +2,7 @@ import joi from 'joi';
|
|||||||
import error from './error';
|
import error from './error';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import { parseInclude, parseWhere } from './utils';
|
import { parseInclude, parseWhere } from './utils';
|
||||||
|
import { notFound } from 'boom';
|
||||||
|
|
||||||
let prefix;
|
let prefix;
|
||||||
let defaultConfig;
|
let defaultConfig;
|
||||||
@ -50,16 +51,19 @@ export const get = (server, model) => {
|
|||||||
async handler(request, reply) {
|
async handler(request, reply) {
|
||||||
const include = parseInclude(request);
|
const include = parseInclude(request);
|
||||||
const where = parseWhere(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 });
|
const instance = await model.findOne({ where, include });
|
||||||
|
|
||||||
|
if (!instance) return void reply(notFound(`${id} not found.`));
|
||||||
|
|
||||||
reply(instance);
|
reply(instance);
|
||||||
},
|
},
|
||||||
config: _.defaultsDeep({
|
config: _.defaultsDeep({
|
||||||
validate: {
|
validate: {
|
||||||
params: joi.object().keys({
|
params: joi.object().keys({
|
||||||
id: joi.number().integer()
|
id: joi.any()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}, defaultConfig)
|
}, defaultConfig)
|
||||||
@ -186,12 +190,15 @@ export const update = (server, model) => {
|
|||||||
|
|
||||||
@error
|
@error
|
||||||
async handler(request, reply) {
|
async handler(request, reply) {
|
||||||
let instance = await model.findOne({
|
const {id} = request.params;
|
||||||
|
const instance = await model.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: request.params.id
|
id
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!instance) return void reply(notFound(`${id} not found.`));
|
||||||
|
|
||||||
await instance.update(request.payload);
|
await instance.update(request.payload);
|
||||||
|
|
||||||
reply(instance);
|
reply(instance);
|
||||||
|
Loading…
Reference in New Issue
Block a user