Skip to content

Instantly share code, notes, and snippets.

@msfidelis
Created January 2, 2018 15:21
Show Gist options
  • Save msfidelis/f9d977cc863fd19601edec893f8b05a0 to your computer and use it in GitHub Desktop.
Save msfidelis/f9d977cc863fd19601edec893f8b05a0 to your computer and use it in GitHub Desktop.
'use strict';
const ProductSchema = require('../models/Products');
const Joi = require('joi');
const Boom = require('boom');
const hash = require('take-my-hash');
const cache = require('../configs/cache');
/**
* Atualiza um produto informado pela ID
* PUT /products/{id}
*/
module.exports = [
{
method: "PUT",
path: "/products/{id}",
handler: (req, res) => {
//Encontra o Item e atualiza o mesmo
ProductSchema.findByIdAndUpdate(req.params.id, {$set: req.payload}, {new: true})
.then(result => {
//Caso o mesmo não exista, retorna um status de 404
if (!result) {
let message = `Product ${req.params.id} not found`;
res(Boom.notFound(message));
} else {
const productHash = hash.sha1('products' + req.params.id);
// Seta o item atualizado no cache
cache.setAsync(productHash, JSON.stringify(result), 'EX', 100)
.then(success => {
console.log("Atualizou o cache");
res(result);
}).catch(err => {
console.log(err);
res(Boom.internal(err));
});
}
})
.catch(err => {
res(Boom.internal(err));
});
},
config: {
validate: {
payload: {
name: Joi.string(),
description: Joi.string(),
price: Joi.number(),
tags: Joi.array()
},
params: {
id: Joi.string().required().min(20)
}
}
}
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment