Skip to content

Instantly share code, notes, and snippets.

@m-cakir
Created April 8, 2018 11:48
Show Gist options
  • Select an option

  • Save m-cakir/935e994058c9be96c175fe563b46a458 to your computer and use it in GitHub Desktop.

Select an option

Save m-cakir/935e994058c9be96c175fe563b46a458 to your computer and use it in GitHub Desktop.
Hapi playground routes/movie.js
'use strict';
const Models = require('../models');
const Joi = require('joi');
const Boom = require('boom');
module.exports = function () {
return [{
method: 'GET',
path: '/movies',
config: {
description : 'Get all movies',
notes : 'retrieve movies',
tags : ['api'],
auth : 'jwt',
handler: async (req, h) => {
const movies = await Models.Movie.findAll({
attributes: ['id', 'name', 'director']
});
return h.response(movies);
}
}
}, {
method: 'GET',
path: '/movies/{id}',
config: {
description : 'Get movie by id',
notes : 'retrieve movie via id',
tags : ['api'],
auth : 'jwt',
validate: {
params: {
id : Joi.number().integer().min(1).required()
}
},
handler: async (req, h) => {
const movie = await Models.Movie.findOne({
attributes: ['id', 'name', 'director'],
where : {
id : req.params.id
}
});
if(movie == null){
return Boom.notFound('Movie not found with id: ' + req.params.id);
}
return h.response(movie);
}
}
}];
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment