Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //...
- // favorite article
- router.post('/:article/favorite', auth.required, function(req, res, next) {
- // extract id from posted article
- var articleId = req.article._id;
- // find posted user, favorite the article, update favorite count, and return article
- User.findById(req.payload.id).then(function(user){
- if (!user) { return res.sendStatus(401); }
- return user.favorite(articleId).then(function(){
- return req.article.updateFavoriteCount().then(function(article){
- return res.json({article: article.toJSONFor(user)});
- });
- });
- }).catch(next);
- });
- // unfavorite. Ditto favorite method, but using user.unfavorite
- router.delete('/:article/favorite', auth.required, function(req, res, next) {
- var articleId = req.article._id;
- User.findById(req.payload.id).then(function (user){
- if (!user) { return res.sendStatus(401); }
- return user.unfavorite(articleId).then(function(){
- return req.article.updateFavoriteCount().then(function(article){
- return res.json({article: article.toJSONFor(user)});
- });
- });
- }).catch(next);
- });
- module.exports = router;
Advertisement
Add Comment
Please, Sign In to add comment