Skip to content

Commit

Permalink
feat: like and dislike comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Mireille Niwemuhuza authored and Mireille Niwemuhuza committed Jun 25, 2019
1 parent 5591ca6 commit efcd119
Show file tree
Hide file tree
Showing 11 changed files with 472 additions and 78 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"test:reset": "NODE_ENV=test npm run migrate:reset && NODE_ENV=test nyc --reporter=html --reporter=text mocha ./test/*.js --timeout 80000 --exit --require @babel/register --require regenerator-runtime",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"test:local": "yarn undo && yarn migrate && yarn seed && yarn test",
"migrate:reset": "sequelize db:drop && sequelize db:create && sequelize db:migrate",
"migrate:reset": "sequelize db:drop && sequelize db:create && sequelize db:migrate && sequelize db:seed:all",
"migrate": "sequelize db:migrate",
"undo": "sequelize db:migrate:undo",
"undo:all": "sequelize db:migrate:undo",
Expand Down
10 changes: 5 additions & 5 deletions src/api/controllers/bookmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Bookmark {
* @param {Object} res - Response object
* @returns {Object} - Response object
*/
static async Bookmark(req, res) {
static async bookmark(req, res) {
const { id } = req.user;
const { slug } = req.params;
const data = {
Expand All @@ -30,12 +30,12 @@ class Bookmark {

});
if (!response[0]) {
const NewBookmark = await Bookmarks.create({
const newBookmark = await Bookmarks.create({
slug: data.slug,
userId: data.userId
});
return res.status(201).json({
data: NewBookmark,
data: newBookmark,
message: 'Bookmark created'
});
}
Expand All @@ -53,13 +53,13 @@ class Bookmark {
*/
static async getOwnerBookmarks(req, res) {
const { id } = req.user;
const YourBookmarks = await Bookmarks.findAll({
const yourBookmarks = await Bookmarks.findAll({
where: {
userId: id
}
});
res.status(200).json({
data: YourBookmarks
data: yourBookmarks
});
}
}
Expand Down
190 changes: 157 additions & 33 deletions src/api/controllers/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,13 @@ export default class comments {
if (nestedComments[0]) {
await models.Comment.update(
{
comment: 'This comment has been deleted!'
comment:
'This comment has been deleted!'
},
{ where: { id: commentId } }
).then(() => {
return res.status(200).json({
message: 'Comment deleted',
message: 'Comment deleted'
});
});
} else if (userId === id || isAdmin === true) {
Expand Down Expand Up @@ -181,38 +182,161 @@ export default class comments {
message: 'Not found!'
});
}
await models.Article
.findAll({
attributes: [
'title',
'description',
'body'
],
where: {
slug
},
include: [
{
model: models.Comment,
attributes: ['comment'],
where: {
articleId: findSlug[0].dataValues.id
},
include: [
{
model: models.Comment,
attributes: ['comment']
}
]
}
]
})
.then((data) => {
if (data) {
return res.status(200).json({
data
});
await models.Article.findAll({
attributes: [
'title',
'description',
'body'
],
where: {
slug
},
include: [
{
model: models.Comment,
attributes: ['comment'],
where: {
articleId: findSlug[0].dataValues.id
},
include: [
{
model: models.Comment,
attributes: ['comment']
}
]
}
]
}).then((data) => {
if (data) {
return res.status(200).json({
data
});
}
});
}

/**
* @description - Users should be able to like a comment
* @param {Object} req - Request Object
* @param {Object} res - Response Object
* @returns {Object} - Response object
*/
static async likeComment(req, res) {
const { commentId } = req.params;
const { id, firstName } = req.user;
const hasDisliked = await models.LikeDislike.findAll({
where: {
commentId,
userId: id,
dislikes: 1
}
});
if (hasDisliked[0]) {
await models.LikeDislike.update(
{ dislikes: 0, likes: 1 },
{ where: { id: hasDisliked[0].id } }
);
return res.status(200).json({
message: `Dear ${firstName}, Thank you for liking this comment!`
});
}
await models.LikeDislike.create({
userId: id,
commentId,
dislikes: 0,
likes: 1
});

return res.status(201).json({
message: `Dear ${firstName}, Thank you for liking this comment!`
});
}

/**
* @description - Users should be able to dislike a comment
* @param {Object} req - Request Object
* @param {Object} res - Response Object
* @returns {Object} - Response object
*/
static async dislikeComment(req, res) {
const { commentId } = req.params;
const { id, firstName } = req.user;
const hasLiked = await models.LikeDislike.findAll({
where: {
commentId,
userId: id,
likes: 1
}
});
if (hasLiked[0]) {
await models.LikeDislike.update(
{ dislikes: 1, likes: 0 },
{ where: { id: hasLiked[0].id } }
);
return res.status(200).json({
message: `Dear ${firstName}, Thank you for disliking this comment!`
});
}
await models.LikeDislike.create({
userId: id,
commentId,
dislikes: 1,
likes: 0
});

return res.status(201).json({
message: `Dear ${firstName}, Thank you for disliking this comment!`
});
}

/**
* @description - Users should be able to like a comment
* @param {Object} req - Request Object
* @param {Object} res - Response Object
* @returns {Object} - Response object
*/
static async countLikes(req, res) {
const { commentId } = req.params;

// Get comment likes
const likeCount = await models.LikeDislike.count({
where: {
commentId,
likes: 1
}
});
return res.status(200).json({
status: 200,
data: {
commentId,
likes: likeCount
}
});
}

/**
* @description - Users should be able to dislike a comment
* @param {Object} req - Request Object
* @param {Object} res - Response Object
* @returns {Object} - Response object
*/
static async countDislikes(req, res) {
const { commentId } = req.params;

// Get comment dislikes
const dislikeCount = await models.LikeDislike.count({
where: {
commentId,
dislikes: 1
}
});

return res.status(200).json({
status: 200,
data: {
commentId,
dislikes: dislikeCount
}
});
}
}
18 changes: 15 additions & 3 deletions src/api/routes/articlesRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import comment from '../../middleware/validComment';
import RatingController from '../controllers/ratingController';
import slugExist from '../../middleware/slugExist';
import bookmarkController from '../controllers/bookmark';
import checkLikesandDislikes from '../../middleware/checkLikesDislikes';


const articlesRouter = Router();
Expand All @@ -24,13 +25,15 @@ const {
} = articlesController;
const { verifyToken } = Auth;
const { createRatings, UpdateRatings } = RatingController;
const { Bookmark } = bookmarkController;
const { bookmark } = bookmarkController;


const {
createComment, editComment, deleteComment, getComment, commentAcomment
createComment, editComment, deleteComment, getComment, commentAcomment,
likeComment, dislikeComment, countLikes, countDislikes
} = commentsController;
const { checkComment, checkParameter, articleExists } = comment;
const { liked, disliked } = checkLikesandDislikes;

articlesRouter
.post('/', verifyToken, validateBody('createArticle'), createArticle)
Expand Down Expand Up @@ -61,6 +64,15 @@ articlesRouter.put('/:slug/rating', verifyToken, validateBody('validateRating'),

// Bookmarks routes

articlesRouter.post('/:slug/bookmark', verifyToken, slugExist, Bookmark);
articlesRouter.post('/:slug/bookmark', verifyToken, slugExist, bookmark);
// like and dislike comments
articlesRouter.post('/comments/:commentId/like', verifyToken, checkParameter, liked, likeComment);
articlesRouter.post('/comments/:commentId/dislike', verifyToken, checkParameter, disliked, dislikeComment);

// get likes and dislikes of comments

articlesRouter.get('/comments/:commentId/dislikes', checkParameter, countDislikes);
articlesRouter.get('/comments/:commentId/likes', checkParameter, countLikes);


export default articlesRouter;
59 changes: 59 additions & 0 deletions src/middleware/checkLikesDislikes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import models from '../sequelize/models';

/**
* @class checkLikesDislikes
* @description a class to check if a user has already like or disliked a comment
*/
export default class checkLikesDislikes {
/**
* Verify if the user has already liked the comment
* @param {Object} req - Request
* @param {Object} res - Response
* @param {Function} next -Next
* @returns {Object} The response object
*/
static async liked(req, res, next) {
const { commentId } = req.params;
const { id, firstName } = req.user;
const hasLiked = await models.LikeDislike.findAll({
where: {
commentId,
userId: id,
likes: 1
}
});
// If the user has already liked that comment
if (hasLiked[0]) {
return res.status(400).json({
message: `Dear ${firstName}, You have already liked this comment!`
});
}
next();
}

/**
* Verify if the user has already disliked the comment
* @param {Object} req - Request
* @param {Object} res - Response
* @param {Function} next -Next
* @returns {Object} The response object
*/
static async disliked(req, res, next) {
const { commentId } = req.params;
const { id, firstName } = req.user;
const hasDisliked = await models.LikeDislike.findAll({
where: {
commentId,
userId: id,
dislikes: 1
}
});
// If the user has already disliked that comment
if (hasDisliked[0]) {
return res.status(400).json({
message: `Dear ${firstName}, You have already disliked this comment!`
});
}
next();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default {
type: Sequelize.INTEGER
},
articleId: {
allowNull: false,
allowNull: true,
type: Sequelize.INTEGER,
references: {
model: 'Articles',
Expand All @@ -30,12 +30,10 @@ export default {
likes: {
allowNull: false,
type: Sequelize.INTEGER,
default: 1,
},
dislikes: {
allowNull: false,
type: Sequelize.INTEGER,
default: 1,
},
createdAt: {
allowNull: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.addColumn('LikeDislikes', 'commentId', {
type: Sequelize.INTEGER,
references: {
model: 'Comments',
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE'
}),

down: queryInterface => queryInterface.removeColumn('LikeDislikes', 'commentId')
};
Loading

0 comments on commit efcd119

Please sign in to comment.