-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ft(like-and-dislike-a-comment): Allow user to like and dislike a comment
- Add comment id column inside LikeAndDislike table - Create 2 routes for liking and disliking the comment - Implement functions to allow user like and dislike comments - Write unit tests for implemented functions - Document my feature using Swagger [(Finishes #166790108)]
- Loading branch information
Joseph Nkurunziza
committed
Jul 22, 2019
1 parent
7f56beb
commit baa26df
Showing
12 changed files
with
333 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
service_name: travis-pro | ||
repo_token: 5wHJgqAVMocmuMfz7Nr26hEBqmO7gtzJJ | ||
repo_token: G3KUaGgIQZdYITPWuU0uIfjZckwOlOq8C |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* eslint-disable require-jsdoc */ | ||
import Sequelize from 'sequelize'; | ||
import model from '../../models'; | ||
|
||
const { Op } = Sequelize; | ||
|
||
const { LikeAndDislike, Comments } = model; | ||
|
||
class LikeCommentHelpers { | ||
static async findOneComment(commentId) { | ||
const foundComment = await Comments.findOne({ where: { id: commentId } }); | ||
|
||
return foundComment; | ||
} | ||
|
||
static async userLikedOrDiskedComment(id, commentId) { | ||
const userReacted = await LikeAndDislike.findAll({ where: { userId: id, | ||
commentId } }); | ||
|
||
return userReacted; | ||
} | ||
|
||
static async findDislikedComment(id, commentId) { | ||
const unlikedArticle = await LikeAndDislike.findAll({ where: { userId: id, | ||
commentId, | ||
[Op.or]: [{ likes: false }, { dislikes: true }] } }); | ||
|
||
return unlikedArticle; | ||
} | ||
|
||
static async findLikedComment(id, commentId) { | ||
const unlikedArticle = await LikeAndDislike.findAll({ where: { userId: id, | ||
commentId, | ||
[Op.or]: [{ dislikes: false }, { likes: true }] } }); | ||
|
||
return unlikedArticle; | ||
} | ||
|
||
static async findDisliked(id, commentId) { | ||
const disliked = await LikeAndDislike.findAll({ where: { userId: id, | ||
commentId, | ||
dislikes: true } }); | ||
|
||
return disliked; | ||
} | ||
|
||
static async ChangeFromDislikeToLike(dislikeId) { | ||
const updateFromDislikeToLike = await LikeAndDislike.update({ dislikes: false, likes: true }, | ||
{ where: { id: dislikeId } }); | ||
|
||
return updateFromDislikeToLike; | ||
} | ||
|
||
static async ChangeFromLikeToDisLike(dislikeId) { | ||
const updateFromDislikeToLike = await LikeAndDislike.update({ dislikes: true, likes: false }, | ||
{ where: { id: dislikeId } }); | ||
|
||
return updateFromDislikeToLike; | ||
} | ||
|
||
static async findCommentLikes(id, commentId) { | ||
const liked = await LikeAndDislike.findAll({ where: { userId: id, | ||
commentId, | ||
likes: true } }); | ||
|
||
return liked; | ||
} | ||
|
||
static async undoLikeOrDislikeComments(commentLike) { | ||
const unlikeComment = await LikeAndDislike.update({ likes: false, dislikes: false }, | ||
{ where: { id: commentLike } }); | ||
|
||
return unlikeComment; | ||
} | ||
|
||
static async countCommentLikes(commentId) { | ||
const likes = await LikeAndDislike.count({ where: { commentId, | ||
likes: true } }); | ||
|
||
return likes; | ||
} | ||
|
||
static async countCommentDislikes(commentId) { | ||
const dislikes = await LikeAndDislike.count({ where: { commentId, | ||
dislikes: true } }); | ||
|
||
return dislikes; | ||
} | ||
} | ||
|
||
export default LikeCommentHelpers; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import model from '../models'; | ||
import LikeCommentHelpers from './helpers/likeCommentHelper'; | ||
|
||
const { LikeAndDislike } = model; | ||
/** | ||
* This LikeComment class | ||
*/ | ||
class LikeComment { | ||
/** | ||
* | ||
* @param {Object} req | ||
* @param {Object} res | ||
* @returns {Object} Number of likes | ||
*/ | ||
static async likeComment(req, res) { | ||
const commentId = req.params.commentParamsId; | ||
const { id } = req.user; | ||
const comment = await LikeCommentHelpers.findOneComment(commentId); | ||
|
||
if (!comment) return res.status(404).json({ message: 'Comment not found' }); | ||
|
||
const userReacted = await LikeCommentHelpers.userLikedOrDiskedComment(id, commentId); | ||
const dislikedComment = await LikeCommentHelpers.findDislikedComment(id, commentId); | ||
const liked = await LikeCommentHelpers.findCommentLikes(id, commentId); | ||
|
||
if (!userReacted[0]) { | ||
await LikeAndDislike.create({ userId: id, | ||
commentId: comment.id, | ||
likes: true, | ||
dislikes: false }); | ||
const likes = await LikeCommentHelpers.countCommentLikes(comment.id); | ||
|
||
return res.status(200).json({ likes }); | ||
} | ||
|
||
if (liked[0]) { | ||
await LikeCommentHelpers.undoLikeOrDislikeComments(liked[0].id); | ||
const likes = await LikeCommentHelpers.countCommentLikes(comment.id); | ||
|
||
return res.status(200).json({ likes }); | ||
} | ||
|
||
if (dislikedComment[0]) { | ||
await LikeCommentHelpers.ChangeFromDislikeToLike(dislikedComment[0].id); | ||
const likes = await LikeCommentHelpers.countCommentLikes(comment.id); | ||
|
||
return res.status(200).json({ likes }); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {Object} req | ||
* @param {Object} res | ||
* @returns {Object} Number of Dislikes | ||
*/ | ||
static async DislikeComment(req, res) { | ||
const commentId = req.params.commentParamsId; | ||
const { id } = req.user; | ||
const comment = await LikeCommentHelpers.findOneComment(commentId); | ||
|
||
if (!comment) return res.status(404).json({ message: 'Comment not found' }); | ||
|
||
const userReacted = await LikeCommentHelpers.userLikedOrDiskedComment(id, commentId); | ||
const likedComment = await LikeCommentHelpers.findLikedComment(id, commentId); | ||
const dislikedComment = await LikeCommentHelpers.findDisliked(id, commentId); | ||
|
||
if (!userReacted[0]) { | ||
await LikeAndDislike.create({ userId: id, | ||
commentId: comment.id, | ||
likes: false, | ||
dislikes: true }); | ||
const dislikes = await LikeCommentHelpers.countCommentDislikes(comment.id); | ||
|
||
return res.status(200).json({ dislikes }); | ||
} | ||
|
||
if (likedComment[0]) { | ||
await LikeCommentHelpers.ChangeFromLikeToDisLike(likedComment[0].id); | ||
const dislikes = await LikeCommentHelpers.countCommentDislikes(comment.id); | ||
|
||
return res.status(200).json({ dislikes }); | ||
} | ||
|
||
if (dislikedComment[0]) { | ||
await LikeCommentHelpers.undoLikeOrDislikeComments(dislikedComment[0].id); | ||
const dislikes = await LikeCommentHelpers.countCommentDislikes(comment.id); | ||
|
||
return res.status(200).json({ dislikes }); | ||
} | ||
} | ||
} | ||
|
||
export default LikeComment; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
/* eslint-disable require-jsdoc */ | ||
|
||
import Sequelize from 'sequelize'; | ||
import model from '../models'; | ||
|
||
|
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.