-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df01cb7
commit 522749c
Showing
6 changed files
with
102 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}) | ||
} | ||
}; |