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