This commit is contained in:
Christopher Van 2015-01-14 21:35:40 -08:00
Родитель bc8327c0da
Коммит 4db2576d33
3 изменённых файлов: 158 добавлений и 173 удалений

Просмотреть файл

@ -1,38 +1,119 @@
'use strict'; 'use strict';
var Joi = require('joi');
var Game = require('../models/game'); var Game = require('../models/game');
var utils = require('../../lib/utils');
module.exports = { var gameSchema = {
all: function (request) { // Game Description is optional.
return Game.objects.all(request.params) 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')
};
exports.all = {
handler: function (request, reply) {
Game.objects.all(request.params)
.then(function (games) { .then(function (games) {
return games.map(Game.getPublicObj);
}); reply(games.map(Game.getPublicObj));
}, })
create: function (request) { .catch(function (err) {
return Game.objects.create(request.payload)
.then(function (res) { reply(utils.returnError(err));
return { });
body: Game.getPublicObj(res.body), }
uri: res.uri };
};
});
}, exports.create = {
get: function (request) { validate: {
return Game.objects.get(request.params) payload: gameSchema
.then(Game.getPublicObj); },
}, handler: function (request, reply) {
remove: function (request) {
return Game.objects.remove(request.params); Game.objects.create(request.payload)
}, .then(function (res) {
update: function (request) {
return Game.objects.update(request.params, request.payload) var body = Game.getPublicObj(res.body);
.then(function (res) { var uri = res.uri;
return {
body: Game.getPublicObj(res.body), reply(body).created(uri);
uri: res.uri })
}; .catch(function (err) {
reply(utils.returnError(err));
});
}
};
exports.get = {
handler: function (request, reply) {
Game.objects.get(request.params)
.then(function (game) {
reply(Game.getPublicObj(game));
})
.catch(function (err) {
reply(utils.returnError(err));
});
}
};
exports.remove = {
handler: function (request, reply) {
Game.objects.remove(request.params)
.then(reply)
.catch(function (err) {
reply(utils.returnError(err));
});
}
};
exports.update = {
validate: {
payload: gameSchema
},
handler: function (request, reply) {
Game.objects.update(request.params, request.payload)
.then(function (res) {
var body = Game.getPublicObj(res.body);
var uri = res.uri;
if (uri) {
return reply(body).redirect(uri);
}
reply(body);
})
.catch(function (err) {
reply(utils.returnError(err));
}); });
} }
}; };

Просмотреть файл

@ -1,38 +1 @@
'use strict'; 'use strict';
var User = require('../models/user');
module.exports = {
all: function (request) {
return User.objects.all(request.params)
.then(function (users) {
return users.map(User.getPublicObj);
});
},
create: function (request) {
return User.objects.create(request.payload)
.then(function (res) {
return {
body: User.getPublicObj(res.body),
uri: res.uri
};
});
},
get: function (request) {
return User.objects.get(request.params)
.then(User.getPublicObj);
},
remove: function (request) {
return User.objects.remove(request.params);
},
update: function (request) {
return User.objects.update(request.params, request.payload)
.then(function (res) {
return {
body: User.getPublicObj(res.body),
uri: res.uri
};
});
}
};

Просмотреть файл

@ -1,33 +1,13 @@
'use strict'; 'use strict';
var joi = require('joi'); var Game = require('./controllers/game');
var gameController = require('./controllers/game.js');
var utils = require('../lib/utils');
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')
};
module.exports = function (server) { module.exports = function (server) {
/* /*
Sample usage: Sample usage:
curl 'http://localhost:4000/games' -i curl 'http://localhost:4000/games' -i
@ -36,82 +16,49 @@ module.exports = function (server) {
server.route({ server.route({
method: 'GET', method: 'GET',
path: '/games', path: '/games',
handler: function (request, reply) { config: Game.all
gameController.all(request)
.then(reply)
.catch(function (err) {
reply(utils.returnError(err));
});
}
}); });
/* /*
Sample usage: Sample usage:
curl -X POST 'http://localhost:4000/games' \ curl -X POST 'http://localhost:4000/games' \
-d '{"name": "mario bros", "game_url": "http://nintendo.com", ' \ -H 'Content-Type: application/json' -i -d@- <<EOF
'"slug": "mario"}' \ {
-H 'Content-Type: application/json' -i "name": "mario bros",
"game_url": "http://nintendo.com",
"slug": "mario"
}
EOF
*/ */
server.route({ server.route({
method: 'POST', method: 'POST',
path: '/games', path: '/games',
handler: function (request, reply) { config: Game.create
gameController.create(request)
.then(function (res) {
reply(res.body).created(res.uri);
})
.catch(function (err) {
reply(utils.returnError(err));
});
},
config: {
validate: {
payload: gameSchema
}
}
}); });
/* /*
Sample usage: Sample usage:
curl 'http://localhost:4000/games/1' -i curl 'http://localhost:4000/games/1' -i
curl 'http://localhost:4000/games/mario' -i curl 'http://localhost:4000/games/mario' -i
*/ */
// TODO: Throw error if neither `/games/{id}` nor `/games/{slug}` resolves.
server.route({ server.route({
method: 'GET', method: 'GET',
path: '/games/{idOrSlug}', path: '/games/{idOrSlug}',
handler: function (request, reply) { config: Game.get
gameController.get(request)
.then(reply)
.catch(function (err) {
reply(utils.returnError(err));
});
},
// 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'),
// // 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')
// }
// }
// }
}); });
/* /*
Sample usage: Sample usage:
curl -X DELETE 'http://localhost:4000/games/1' -i curl -X DELETE 'http://localhost:4000/games/1' -i
@ -121,51 +68,45 @@ module.exports = function (server) {
server.route({ server.route({
method: 'DELETE', method: 'DELETE',
path: '/games/{idOrSlug}', path: '/games/{idOrSlug}',
handler: function (request, reply) { config: Game.remove
gameController.remove(request)
.then(reply)
.catch(function (err) {
reply(utils.returnError(err));
});
}
}); });
/* /*
Sample usage: Sample usage:
curl -X PUT 'http://localhost:4000/games/1' \ curl -X PUT 'http://localhost:4000/games/1' \
-d '{"name": "mario bros", "game_url": "http://nintendo.com", ' \ -H 'Content-Type: application/json' -i -d@- <<EOF
'"slug": "mario"}' \ {
-H 'Content-Type: application/json' -i "name": "mario bros",
"game_url": "http://nintendo.com",
"slug": "mario"
}
EOF
curl -X PUT 'http://localhost:4000/games/mario' \ curl -X PUT 'http://localhost:4000/games/mario' \
-d '{"name": "mario bros", "game_url": "http://nintendo.com", ' \ -H 'Content-Type: application/json' -i -d@- <<EOF
'"slug": "mario"}' \ {
-H 'Content-Type: application/json' -i "name": "mario bros",
"game_url": "http://nintendo.com",
"slug": "mario"
}
EOF
curl -X PUT 'http://localhost:4000/games/wario' \ curl -X PUT 'http://localhost:4000/games/wario' \
-d '{"name": "wario bros", "game_url": "http://wintendo.com", ' \ -H 'Content-Type: application/json' -i -d@- <<EOF
'"slug": "wario"}' \ {
-H 'Content-Type: application/json' -i "name": "wario bros",
"game_url": "http://wintendo.com",
"slug": "wario"
}
EOF
*/ */
server.route({ server.route({
method: 'PUT', method: 'PUT',
path: '/games/{idOrSlug}', path: '/games/{idOrSlug}',
handler: function (request, reply) { config: Game.update
gameController.update(request)
.then(function (res) {
if (res.uri) {
return reply(res.body).redirect(res.uri);
}
return reply(res.body);
})
.catch(function (err) {
reply(utils.returnError(err));
});
},
config: {
validate: {
payload: gameSchema
}
}
}); });
}; };