Skip to content

Instantly share code, notes, and snippets.

@michaellujan
Created August 19, 2015 20:04
Show Gist options
  • Save michaellujan/47ea8ef56aaf2c9c9cde to your computer and use it in GitHub Desktop.
Save michaellujan/47ea8ef56aaf2c9c9cde to your computer and use it in GitHub Desktop.
var Games = require('../models/game');
var controller = function() {};
controller.prototype = {
delete: function (id, callback) {
Games.findById(id).exec(function (err, game) {
if (err) return callback({status: 500, body: err});
game.remove();
return callback({status: 204, body: ''});
});
},
get: function (limit, skip, callback) {
Games.find().lean().limit(limit).skip(skip).populate('team_home').populate('team_away').populate('venue').exec(function (err, games) {
if (err) return callback({status: 500, body: err});
return callback({status: 200, body: games});
});
},
getById: function (id, callback) {
Games.findById(id).populate('team_home').populate('team_away').populate('venue').exec(function (err, game) {
if (err) return callback({status: 500, body: err});
if (game === null) return callback({status: 404, body: {message: 'No Games Found'}});
return callback({status: 200, body: game});
});
},
save: function (request, callback) {
var game = new Games(request);
game.save(function (err, result) {
if (err) return callback({status: 500, body: err});
Games.findById(result._id).populate('team_home').populate('team_away').populate('venue').exec(function (err, data) {
if (err) return callback({status: 500, body: err});
return callback({status: 201, body: data});
});
});
},
update: function (id, update, callback) {
update.updated_at = Date.now();
Games.update({_id: id}, update).exec(function (err) {
if (err) return callback({status: 500, body: err});
return callback({status: 204, body: ''});
});
}
};
module.exports = new controller();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment