Skip to content

Commit

Permalink
chore(comment): add field depicting if user has liked comment
Browse files Browse the repository at this point in the history
[Finishes #165680926]
  • Loading branch information
Dsalz committed Apr 29, 2019
1 parent 883b574 commit 5bf20bb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
23 changes: 20 additions & 3 deletions server/controllers/articleComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const {
User,
Notification,
UserNotification,
CommentEditHistory
CommentEditHistory,
ArticleCommentLike
} = models;

/**
Expand All @@ -25,8 +26,9 @@ class Comment {
* @returns {object} - The comment object
*/
static async getComments(req, res) {
const articleId = req.article.id;
const comments = await ArticleComment.findAll({
const { user, article } = req;
const articleId = article.id;
let comments = await ArticleComment.findAll({
where: {
articleId
},
Expand All @@ -38,6 +40,21 @@ class Comment {
}
]
});
if (user.id) {
comments = await Promise.all(comments.map(async (comment) => {
const userLike = await ArticleCommentLike.findOne({
where: {
userId: user.id,
commentId: comment.dataValues.id,
articleId: comment.dataValues.articleId
}
});
return {
...comment.dataValues,
hasLiked: Boolean(userLike)
};
}));
}
return response(res).success({
message: 'Comments successfully retrieved',
comments
Expand Down
1 change: 1 addition & 0 deletions server/routes/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ router.post('/articles/:slug/highlight',

router.get('/articles/:slug/comments',
AuthenticateArticle.verifyArticle,
AuthenticateUser.identifyUser,
Comment.getComments);

router.get('/articles/tags',
Expand Down
18 changes: 18 additions & 0 deletions server/test/comment.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,27 @@ describe('PATCH update comment', () => {
});

describe('GET comments of a single article', () => {
before((done) => {
chai
.request(app)
.post('/api/users')
.send({
firstname: 'Chris',
lastname: 'James',
email: 'chr2@gmail.com',
username: 'Chr2',
password: '12345678'
})
.end((err, res) => {
const { token } = res.body.user;
userToken = token;
done(err);
});
});
it('should return 200 and get comments for an article', (done) => {
chai.request(app)
.get('/api/articles/this-is-an-article-1/comments')
.set('authorization', `Bearer ${userToken}`)
.end((err, res) => {
expect(res.status).to.be.equal(200);
expect(res.body.message).to.be.equal('Comments successfully retrieved');
Expand Down

0 comments on commit 5bf20bb

Please sign in to comment.