Last active
August 29, 2015 14:23
-
-
Save myndzi/20dc013f0059b637508a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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