implement `DELETE /games/{idOrSlug}` in hapi; exclude soft-deleted games (fixes #262; fixes #293)

This commit is contained in:
Christopher Van 2014-12-18 16:03:51 -08:00
Родитель 6964ba2a8f
Коммит 492e25a99c
5 изменённых файлов: 80 добавлений и 5 удалений

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

@ -99,6 +99,10 @@ To run migrations, run this from the shell:
node node_modules/.bin/pg-migrate up
To create a new migration:
node node_modules/.bin/pg-migrate create <migrationName>
To get a list the tables:
\dt+
@ -110,3 +114,7 @@ To get a table's schema:
To delete a table:
drop table <table_name>;
To view a table in "extended display":
\x on

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

@ -19,7 +19,10 @@ module.exports = {
idOrSlug: request.params.idOrSlug
});
},
remove: function () {
remove: function (request) {
return Game.objects.remove(request.pg.client, {
idOrSlug: request.params.idOrSlug
});
},
update: function () {
}

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

@ -25,7 +25,9 @@ Game.objects = {};
Game.objects.all = function (db, data) {
return new Promise(function (resolve, reject) {
db.query('SELECT * FROM games', function (err, result) {
db.query('SELECT * FROM games WHERE deleted = false',
function (err, result) {
if (err) {
return reject(utils.errors.DatabaseError(err));
}
@ -56,11 +58,11 @@ Game.objects.create = function (db, data) {
};
Game.objects.get = function (db, data) {
Game.objects._select = function (db, data, columns) {
return new Promise(function (resolve, reject) {
var query = (utils.isNumeric(data.idOrSlug) ?
'SELECT * FROM games WHERE id = $1' :
'SELECT * FROM games WHERE slug = $1'
'SELECT ' + columns + ' FROM games WHERE id = $1 AND deleted = false' :
'SELECT ' + columns + ' FROM games WHERE slug = $1 AND deleted = false'
);
db.query(query, [data.idOrSlug], function (err, result) {
@ -78,4 +80,41 @@ Game.objects.get = function (db, data) {
};
Game.objects.get = function (db, data) {
return Game.objects._select(db, data, '*');
};
Game.objects.exists = function (db, data) {
return Game.objects._select(db, data, '1');
};
Game.objects.remove = function (db, data) {
return Game.objects.exists(db, data).then(function () {
return new Promise(function (resolve, reject) {
var query = (utils.isNumeric(data.idOrSlug) ?
'UPDATE games SET deleted = true WHERE id = $1' :
'UPDATE games SET deleted = true WHERE slug = $1'
);
db.query(query, [data.idOrSlug], function (err, result) {
if (err) {
return reject(utils.errors.DatabaseError(err));
}
// This should never be possible.
if (!result.rowCount) {
return reject(utils.errors.ValidationError('already_deleted'));
}
resolve({success: true});
});
});
});
};
module.exports = Game;

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

@ -100,4 +100,16 @@ 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));
});
}
});
};

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

@ -0,0 +1,13 @@
exports.up = function (pgm, run) {
pgm.addColumns('games', {
deleted: {type: 'bool', default: 'false'}
});
run();
};
exports.down = function (pgm, run) {
pgm.dropColumns('games', {
deleted: {}
});
run();
};