Created
April 8, 2018 11:48
-
-
Save m-cakir/935e994058c9be96c175fe563b46a458 to your computer and use it in GitHub Desktop.
Hapi playground routes/movie.js
This file contains hidden or 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
| '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