diff --git a/api/controllers/game.js b/api/controllers/game.js index 6ca8ec3..2945379 100644 --- a/api/controllers/game.js +++ b/api/controllers/game.js @@ -26,33 +26,9 @@ module.exports = { remove: function () { }, get: function (request) { - return new Promise(function (resolve, reject) { - var game = new Game(request.params); - - var query = (utils.isNumeric(request.params.idOrSlug) ? - 'SELECT * FROM games WHERE id = $1' : - 'SELECT * FROM games WHERE slug = $1' - ); - - request.pg.client.query(query, [request.params.idOrSlug], - function (err, result) { - if (err) { - return reject(err); - } - - if (!result.rows.length) { - // TODO: Return proper JSON when row couldn't be seleced. - return reject({ - statusCode: 404, - error: 'Not Found', - message: 'No game found' - }); - } - - // TODO: Throw error for `err` when row couldn't get be selected. - resolve(err || result.rows[0]); - }); - }); + return Game.objects.get(request.pg.client, { + idOrSlug: request.params.idOrSlug + }).catch(utils.returnError); }, update: function () { } diff --git a/api/models/game.js b/api/models/game.js index d0844b7..0fe03f7 100644 --- a/api/models/game.js +++ b/api/models/game.js @@ -1,5 +1,7 @@ var Promise = require('es6-promise').Promise; +var utils = require('../../lib/utils'); + function Game(data) { // Game Description is optional. @@ -17,11 +19,27 @@ function Game(data) { this.slug = data.slug; } -Game.get = function get(data) { +Game.objects = {}; + +Game.objects.get = function (db, data) { return new Promise(function (resolve, reject) { + var query = (utils.isNumeric(data.idOrSlug) ? + 'SELECT * FROM games WHERE id = $1' : + 'SELECT * FROM games WHERE slug = $1' + ); + db.query(query, [data.idOrSlug], function (err, result) { + if (err) { + return reject(err); + } + if (!result.rows.length) { + return reject(utils.errors.DoesNotExist()); + } + // TODO: Throw error when row couldn't get be selected (#259). + resolve(err || result.rows[0]); + }); }); }; diff --git a/api/routes.js b/api/routes.js index 9e3bb48..8a799f8 100644 --- a/api/routes.js +++ b/api/routes.js @@ -59,13 +59,7 @@ module.exports = function (server) { method: 'GET', path: '/games/{idOrSlug}', handler: function (request, reply) { - console.log('games/id handler'); - gameController.get(request).then(reply, function (response) { - reply(response).code(response.statusCode); - }).catch(function (err) { - console.error(err); - reply(err); - }); + gameController.get(request).then(reply).catch(reply); }, // config: { // validate: { diff --git a/lib/utils/index.js b/lib/utils/index.js index a987058..293ac71 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -1,12 +1,21 @@ +var Boom = require('boom'); var Promise = require('es6-promise').Promise; +module.exports.errors = require('./errors'); + + module.exports.isNumeric = function isNumeric(obj) { return !isNaN(parseFloat(obj)) && isFinite(obj); }; -module.exports.promisify = function promisify(func) { +module.exports.stringifyObject = stringifyObject = function (obj) { + return obj instanceof Object ? JSON.stringify(obj) : obj; +}; + + +module.exports.promisify = function (func) { return function () { var args = Array.prototype.slice.apply(arguments); @@ -21,3 +30,18 @@ module.exports.promisify = function promisify(func) { }); }; }; + + +module.exports.returnError = function (err) { + console.error('Caught error: %s', stringifyObject(err)); + switch (err.name) { + case 'DatabaseError': + return Boom.badImplementation(err.message || 'database_error'); + case 'DoesNotExist': + return Boom.notFound(err.message || 'does_not_exist'); + case 'ValidationError': + return Boom.badRequest(err.message || 'validation_error'); + default: + return Boom.badImplementation(); + } +}; diff --git a/package.json b/package.json index eb5a4b3..1c8cd3f 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "joi": "^5.0.2", "pg": "^4.1.1", "hapi-node-postgres": "cvan/hapi-node-postgres#no-pg-native", - "node-pg-migrate": "0.0.7" + "node-pg-migrate": "0.0.7", + "boom": "^2.6.1" }, "devDependencies": { "gulp": "^3.8.7",