Skip to content

Instantly share code, notes, and snippets.

@msfidelis
Created January 3, 2018 15:39
Show Gist options
  • Save msfidelis/0a5553c2748e7c34ad193f109fb71a7e to your computer and use it in GitHub Desktop.
Save msfidelis/0a5553c2748e7c34ad193f109fb71a7e to your computer and use it in GitHub Desktop.
//...
/**
* Detail do produto
* Exibe mais detalhes que na listagem geral
* Todo produto deve ser identificado pela hash do _id
*/
{
method: "GET",
path: "/products/{id}",
handler: (req, res) => {
const productHash = Hash.sha1('products' + req.params.id);
//Procura o id do produto no cache
cache.getAsync(productHash)
.then(productcache => {
//Verifica se o item existe no cache
if (productcache) {
productcache = JSON.parse(productcache);
res(productcache);
} else {
//Caso não exista no cache, o MongoDB é consultado
ProductService
.findProductById(req.params.id )
.then(product => {
if (!product) {
res(Boom.notFound());
} else {
cache.setAsync(productHash, JSON.stringify(product), 'EX', 100)
.then(success => res(product))
.catch(err => err(Boom.internal(err)));
}
}).catch(err => res(Boom.notFound(err)));
}
}).catch(err => res(Boom.internal(err)));
},
config: {
validate: {
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