Skip to content

Instantly share code, notes, and snippets.

@myndzi
Last active August 29, 2015 14:23
Show Gist options
  • Save myndzi/20dc013f0059b637508a to your computer and use it in GitHub Desktop.
Save myndzi/20dc013f0059b637508a to your computer and use it in GitHub Desktop.
// controller
exports.likePost = function (req, res) {
// validate the input
req.checkBody('post_id', 'Post id is required').notEmpty();
req.checkBody('post_id', 'Post id is needs to be an integer').isInt();
req.checkBody('post_username', 'Post username is required').notEmpty();
req.checkBody('like_username', 'Like username is required').notEmpty();
var errors = req.validationErrors();
console.log(errors);
if (errors) {
res.status(400).send(errors);
return;
}
return postModel.likePost(req.body)
.then(function () {
res.sendStatus(200);
})
.catch(function (err) {
console.log('MySQL Connection error: ' + err);
res.sendStatus(500);
});
}
// model
exports.likePost = function (post_data) {
return knex.transaction(function (trx) {
return trx.insert(post_data)
.into('wall_post_likes')
.then(function () {
return trx('wall_post_likes')
.increment('total_likes')
.where('post_id', post_data.post_id);
})
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment