2014-12-17 09:28:59 +03:00
|
|
|
var joi = require('joi');
|
|
|
|
|
2014-12-11 02:16:36 +03:00
|
|
|
var gameController = require('./controllers/game.js');
|
2014-12-18 03:33:38 +03:00
|
|
|
var utils = require('../lib/utils');
|
2014-12-11 02:16:36 +03:00
|
|
|
|
|
|
|
|
2014-12-19 04:33:10 +03:00
|
|
|
var gameSchema = {
|
|
|
|
// Game Description is optional.
|
|
|
|
description: joi.string().example('Mario Bros. is a sweet adventure game.'),
|
|
|
|
|
|
|
|
// Game URL must start with `https://` or `http://`.
|
|
|
|
game_url: joi.string().regex(/^https?:\/\//).required()
|
|
|
|
.example('http://nintendo.com/mario-bros/'),
|
|
|
|
|
|
|
|
// Game Name cannot be longer than 150 characters long.
|
|
|
|
name: joi.string().max(150).required()
|
|
|
|
.example('Mario Bros.'),
|
|
|
|
|
|
|
|
// Game Slug cannot be all digits, all underscores, or all hyphens
|
|
|
|
// and must contain only letters, numbers, underscores, and hyphens.
|
|
|
|
// TODO: Throw an error if `slug` is already taken.
|
|
|
|
slug: joi.string().regex(/^(?!\d*$)(?!_*$)(?!-*$)[\w-]+$/).required()
|
|
|
|
.example('mario-bros')
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-12-11 02:16:36 +03:00
|
|
|
module.exports = function (server) {
|
2014-12-18 06:21:17 +03:00
|
|
|
/*
|
|
|
|
Sample usage:
|
|
|
|
|
2014-12-20 00:44:48 +03:00
|
|
|
curl 'http://localhost:4000/games' -i
|
2014-12-18 06:21:17 +03:00
|
|
|
|
|
|
|
*/
|
2014-12-11 02:16:36 +03:00
|
|
|
server.route({
|
|
|
|
method: 'GET',
|
|
|
|
path: '/games',
|
|
|
|
handler: function (request, reply) {
|
2014-12-18 07:10:06 +03:00
|
|
|
gameController.all(request)
|
2014-12-18 03:33:38 +03:00
|
|
|
.then(reply)
|
|
|
|
.catch(function (err) {
|
|
|
|
reply(utils.returnError(err));
|
2014-12-18 01:06:46 +03:00
|
|
|
});
|
2014-12-17 09:28:59 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-12-18 06:21:17 +03:00
|
|
|
/*
|
|
|
|
Sample usage:
|
|
|
|
|
|
|
|
curl -X POST 'http://localhost:4000/games' \
|
|
|
|
-d '{"name": "mario bros", "game_url": "http://nintendo.com", "slug": "mario"}' \
|
2014-12-20 00:44:48 +03:00
|
|
|
-H 'Content-Type: application/json' -i
|
2014-12-18 06:21:17 +03:00
|
|
|
|
|
|
|
*/
|
2014-12-17 09:28:59 +03:00
|
|
|
server.route({
|
|
|
|
method: 'POST',
|
|
|
|
path: '/games',
|
|
|
|
handler: function (request, reply) {
|
2014-12-18 03:33:38 +03:00
|
|
|
gameController.create(request)
|
2014-12-20 00:44:48 +03:00
|
|
|
.then(function (res) {
|
|
|
|
reply(res.body).created(res.uri);
|
|
|
|
})
|
2014-12-18 03:33:38 +03:00
|
|
|
.catch(function (err) {
|
|
|
|
reply(utils.returnError(err));
|
2014-12-18 01:06:46 +03:00
|
|
|
});
|
2014-12-17 09:28:59 +03:00
|
|
|
},
|
|
|
|
config: {
|
|
|
|
validate: {
|
2014-12-19 04:33:10 +03:00
|
|
|
payload: gameSchema
|
2014-12-17 09:28:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-12-18 06:21:17 +03:00
|
|
|
/*
|
|
|
|
Sample usage:
|
|
|
|
|
2014-12-20 00:44:48 +03:00
|
|
|
curl 'http://localhost:4000/games/1' -i
|
|
|
|
curl 'http://localhost:4000/games/mario' -i
|
2014-12-18 06:21:17 +03:00
|
|
|
|
|
|
|
*/
|
2014-12-17 09:28:59 +03:00
|
|
|
// TODO: Throw error if neither `/games/{id}` nor `/games/{slug}` resolves.
|
|
|
|
server.route({
|
|
|
|
method: 'GET',
|
|
|
|
path: '/games/{idOrSlug}',
|
|
|
|
handler: function (request, reply) {
|
2014-12-18 03:33:38 +03:00
|
|
|
gameController.get(request)
|
|
|
|
.then(reply)
|
|
|
|
.catch(function (err) {
|
|
|
|
reply(utils.returnError(err));
|
|
|
|
});
|
2014-12-17 09:28:59 +03:00
|
|
|
},
|
2014-12-17 11:30:13 +03:00
|
|
|
// config: {
|
|
|
|
// validate: {
|
|
|
|
// params: {
|
|
|
|
// // Game Slug cannot be all digits, all underscores, or all hyphens
|
|
|
|
// // and must contain only letters, numbers, underscores, and hyphens.
|
|
|
|
// // TODO: ID *or* slug is required; enforce.
|
|
|
|
// idOrSlug: joi.number().integer().example('9'),
|
2014-12-17 09:28:59 +03:00
|
|
|
|
2014-12-17 11:30:13 +03:00
|
|
|
// // Game Slug cannot be all digits, all underscores, or all hyphens
|
|
|
|
// // and must contain only letters, numbers, underscores, and hyphens.
|
|
|
|
// // TODO: ID *or* slug is required; enforce.
|
|
|
|
// idOrSlug: joi.string().regex(/^(?!\d*$)(?!_*$)(?!-*$)[\w-]+$/)
|
|
|
|
// .example('mario-bros')
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2014-12-11 02:16:36 +03:00
|
|
|
});
|
2014-12-19 03:03:51 +03:00
|
|
|
|
2014-12-19 06:51:12 +03:00
|
|
|
/*
|
|
|
|
Sample usage:
|
|
|
|
|
2014-12-20 00:44:48 +03:00
|
|
|
curl -X DELETE 'http://localhost:4000/games/1' -i
|
|
|
|
curl -X DELETE 'http://localhost:4000/games/mario' -i
|
2014-12-19 06:51:12 +03:00
|
|
|
|
|
|
|
*/
|
2014-12-19 03:03:51 +03:00
|
|
|
server.route({
|
|
|
|
method: 'DELETE',
|
|
|
|
path: '/games/{idOrSlug}',
|
|
|
|
handler: function (request, reply) {
|
|
|
|
gameController.remove(request)
|
|
|
|
.then(reply)
|
|
|
|
.catch(function (err) {
|
|
|
|
reply(utils.returnError(err));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2014-12-19 04:33:10 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
Sample usage:
|
|
|
|
|
|
|
|
curl -X PUT 'http://localhost:4000/games/1' \
|
|
|
|
-d '{"name": "mario bros", "game_url": "http://nintendo.com", "slug": "mario"}' \
|
2014-12-20 00:20:07 +03:00
|
|
|
-H 'Content-Type: application/json' -i
|
2014-12-19 04:33:10 +03:00
|
|
|
curl -X PUT 'http://localhost:4000/games/mario' \
|
|
|
|
-d '{"name": "mario bros", "game_url": "http://nintendo.com", "slug": "mario"}' \
|
2014-12-20 00:20:07 +03:00
|
|
|
-H 'Content-Type: application/json' -i
|
|
|
|
curl -X PUT 'http://localhost:4000/games/wario' \
|
|
|
|
-d '{"name": "wario bros", "game_url": "http://wintendo.com", "slug": "wario"}' \
|
|
|
|
-H 'Content-Type: application/json' -i
|
2014-12-19 04:33:10 +03:00
|
|
|
|
|
|
|
*/
|
|
|
|
server.route({
|
|
|
|
method: 'PUT',
|
|
|
|
path: '/games/{idOrSlug}',
|
|
|
|
handler: function (request, reply) {
|
|
|
|
gameController.update(request)
|
2014-12-20 00:20:07 +03:00
|
|
|
.then(function (res) {
|
|
|
|
if (res.uri) {
|
|
|
|
return reply(res.body).redirect(res.uri);
|
|
|
|
}
|
|
|
|
return reply(res.body);
|
|
|
|
})
|
2014-12-19 04:33:10 +03:00
|
|
|
.catch(function (err) {
|
|
|
|
reply(utils.returnError(err));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
config: {
|
|
|
|
validate: {
|
|
|
|
payload: gameSchema
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-12-11 02:16:36 +03:00
|
|
|
};
|