From 522749c5dbb106e69f8993e30a9fe2ae178f2d20 Mon Sep 17 00:00:00 2001 From: Essau Date: Thu, 21 Sep 2017 11:53:45 -0500 Subject: [PATCH] Added update endpoint #22 --- CHANGELOG.md | 1 + lib/plugins/acopios/index.js | 18 +++---- lib/plugins/acopios/post.js | 6 +-- lib/plugins/acopios/productos/index.js | 8 +++- lib/plugins/acopios/put.js | 66 ++++++++++++++++++++++++++ lib/utils.js | 16 +++++++ 6 files changed, 102 insertions(+), 13 deletions(-) create mode 100644 lib/plugins/acopios/put.js create mode 100644 lib/utils.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 14fa3b6..282ff30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] ### Added +- Added PUT enpoint for `/acopios/{id}` using new schema - Add travis config - Fixed POST enpoint for `/acopios` using new schema - Update schema to reflect new one diff --git a/lib/plugins/acopios/index.js b/lib/plugins/acopios/index.js index cbbd695..aec7d6d 100644 --- a/lib/plugins/acopios/index.js +++ b/lib/plugins/acopios/index.js @@ -1,16 +1,16 @@ 'use strict'; -exports.register = function (server, options, next) { - - require('./post').register(server, options, (err) => { +const AcopioUtils = require('../../utils'); +const Path = require('path'); - if (err) { - next(err); - return; - } +exports.register = function (server, options, next) { - require('./get').register(server, options, next); - }); + AcopioUtils.addRoutes([ + Path.join(__dirname, './post'), + Path.join(__dirname, './get'), + Path.join(__dirname, './put') + ], server, options, next); + next(); }; exports.register.attributes = { diff --git a/lib/plugins/acopios/post.js b/lib/plugins/acopios/post.js index bd05ef7..d904214 100644 --- a/lib/plugins/acopios/post.js +++ b/lib/plugins/acopios/post.js @@ -25,13 +25,13 @@ exports.register = function (server, options, next) { } }, validate: { - payload: Joi.object({ + payload: { nombre: Joi.string().required(), direccion: Joi.string().required(), latitud: Joi.number().min(0).max(90).required(), longitud: Joi.number().min(-180).max(180).required(), - status: Joi.string().required() - }).label('Acopio Request') + status: Joi.string().required(), + } } }, handler: function (request, reply) { diff --git a/lib/plugins/acopios/productos/index.js b/lib/plugins/acopios/productos/index.js index c889db5..b90a9be 100644 --- a/lib/plugins/acopios/productos/index.js +++ b/lib/plugins/acopios/productos/index.js @@ -1,8 +1,14 @@ 'use strict'; +const AcopioUtils = require('../../../utils'); +const Path = require('path'); + exports.register = function (server, options, next) { - require('./get').register(server, options, next); + AcopioUtils.addRoutes([ + Path.join(__dirname, './get') + ], server, options, next); + next(); }; exports.register.attributes = { diff --git a/lib/plugins/acopios/put.js b/lib/plugins/acopios/put.js new file mode 100644 index 0000000..7b1a012 --- /dev/null +++ b/lib/plugins/acopios/put.js @@ -0,0 +1,66 @@ +'use strict'; + +const AcopioSchema = require('../../schemas/centro_de_acopio'); +const Joi = require('joi'); + +exports.register = function (server, options, next) { + + server.route({ + method: 'PUT', + path: '/v1/acopios/{id}', + config: { + auth: 'jwt', + description: 'Crea un nuevo centro de acopio', + notes: 'Agrega un centro de acopio nuevo', + tags: ['api'], + plugins: { + 'hapi-swagger': { + security: [{ jwt: [] }], + responses: { + 200: { + description: 'Success', + schema: AcopioSchema.CentroDeAcopio + } + } + } + }, + validate: { + payload: { + nombre: Joi.string().required(), + direccion: Joi.string().required(), + latitud: Joi.number().min(0).max(90).required(), + longitud: Joi.number().min(-180).max(180).required(), + status: Joi.string().required(), + } + } + }, + handler: async function (request, reply) { + + console.log('Auth', request.auth); + if (request.auth.credentials.scope.indexOf('update:acopios') === -1) { + return reply().code(401); + } + const database = request.getDb('acopiodb'); + const CollectionCenter = database.getModel('CentroDeAcopio'); + try { + const centroDeAcopio = await CollectionCenter.findById(request.params.id); + centroDeAcopio.nombre = request.payload.nombre; + centroDeAcopio.direccion = request.payload.direccion; + centroDeAcopio.latitud = request.payload.latitud; + centroDeAcopio.longitud = request.payload.longitud; + centroDeAcopio.status = request.payload.status; + await centroDeAcopio.save(); + return reply(centroDeAcopio); + } + catch(err) { + console.error(err); + reply().code(500); + }; + } + }); + next(); +}; + +exports.register.attributes = { + name: 'acopios-post' +}; diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 0000000..31bc9ef --- /dev/null +++ b/lib/utils.js @@ -0,0 +1,16 @@ +'use strict'; + +/** + * @param {array} routes - Array of route paths to be added + */ +exports.addRoutes = function (routes, server, options, next) { + + for (const route of routes) { + require(route).register(server, options, (err) => { + + if (err) { + next(err); + } + }) + } +};