Skip to content

Commit

Permalink
Merge 400ea29 into aa5fa79
Browse files Browse the repository at this point in the history
  • Loading branch information
itsgratien committed Jun 10, 2019
2 parents aa5fa79 + 400ea29 commit ea3f5dc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
3 changes: 3 additions & 0 deletions controllers/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class CommentController {
model: User,
as: 'userfkey',
attributes: ['username', 'email', 'image', 'id', 'bio']
},
{
model: Votes
}
]
}).then((comment) => {
Expand Down
35 changes: 16 additions & 19 deletions controllers/commentLikes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable object-curly-newline */
import models from '../models/index';

const VoteComment = models.votecomment;
Expand All @@ -16,22 +17,18 @@ class CommentVotesController {
*/
async likeComment(req, res) {
try {
const likeData = {
userId: req.user.id,
commentId:
req.params.commentId,
like: true,
dislike: false
};
// eslint-disable-next-line max-len
const likeData = { userId: req.user.id, commentId: req.params.commentId, like: true, dislike: false };
if (req.likeComment === null) {
await VoteComment.create(likeData);
return res.status(200).json({ message: 'thanks for the support.', userId: req.user.id, comment: req.params.commentId });
return res.status(200).json({ message: 'thanks for the support.', userId: req.user.id, comment: req.params.commentId, hasLiked: true });
}
if (req.likeComment.like === true) {
return res.status(400).json({ error: 'sorry you have already liked this comment.' });
return res.status(400).json({ error: 'sorry you have already liked this comment.', hasLiked: false });
}
await VoteComment.update(likeData, { where: { id: req.likeComment.id } });
return res.status(200).json({ message: 'thanks for the support.', userId: req.user.id, comment: req.likeComment });
return res.status(200).json({ message: 'thanks for the support.', userId: req.user.id, comment: req.likeComment, hasLiked: true
});
} catch (error) {
return res.status(500).json({ error: 'something wrong try again later.' });
}
Expand All @@ -46,23 +43,23 @@ class CommentVotesController {
// eslint-disable-next-line require-jsdoc
async dislikeComment(req, res) {
try {
const dislikeData = {
userId: req.user.id,
commentId: req.params.commentId,
like: false,
dislike: true
};
// eslint-disable-next-line max-len
const dislikeData = { userId: req.user.id, commentId: req.params.commentId, like: false, dislike: true };
if (req.likeComment === null) {
await VoteComment.create(dislikeData);
return res.status(200).json({ message: 'thank for support.' });
return res.status(200).json({ message: 'thanks for the support.', hasDisLiked: true });
}
if (req.likeComment.dislike === true) {
return res.status(400).json({ error: 'sorry you have already disliked this comment.' });
return res.status(400).json({
error: 'sorry you have already disliked this comment.',
hasDisLiked: false
});
}
await VoteComment.update(dislikeData, {
where: { id: req.likeComment.id }
});
return res.status(200).json({ message: 'You have disliked this article.', userId: req.user.id, commentId: req.params.commentId, });
return res.status(200).json({ message: 'thanks for the support.', userId: req.user.id, commentId: req.params.commentId, hasDisLiked: true
});
} catch (error) {
return res.status(500).json({ error: 'something wrong try again later.' });
}
Expand Down
2 changes: 2 additions & 0 deletions migrations/20190429111231-create-article-highlights.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ const articleHighlightsMigration = {
articleId: {
type: Sequelize.INTEGER,
allowNull: false,
onDelete: 'CASCADE',
references: { model: 'articles', key: 'article_id', as: 'articleId' }
},
userId: {
type: Sequelize.INTEGER,
allowNull: false,
onDelete: 'CASCADE',
references: { model: 'users', key: 'id', as: 'userId' }
},
indexStart: {
Expand Down

0 comments on commit ea3f5dc

Please sign in to comment.