Skip to content

Commit

Permalink
Added update endpoint #22
Browse files Browse the repository at this point in the history
  • Loading branch information
sauramirez committed Sep 21, 2017
1 parent df01cb7 commit 522749c
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions lib/plugins/acopios/index.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
6 changes: 3 additions & 3 deletions lib/plugins/acopios/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 7 additions & 1 deletion lib/plugins/acopios/productos/index.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
66 changes: 66 additions & 0 deletions lib/plugins/acopios/put.js
Original file line number Diff line number Diff line change
@@ -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'
};
16 changes: 16 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -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);
}
})
}
};

0 comments on commit 522749c

Please sign in to comment.