-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
440 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import models from '../models'; | ||
import statusCode from '../../helpers/constants/status.codes'; | ||
|
||
/** | ||
* Contains all like dislike comment controllers | ||
* @export | ||
* @class LikeDislikeController | ||
*/ | ||
export default class commentReactionsController { | ||
/** | ||
* add a new like or dislike | ||
* @static | ||
* @param {Object} req the request | ||
* @param {Object} res the response to be sent | ||
* @memberof LikeDislikeCommentController | ||
* @returns {Object} res | ||
*/ | ||
static async likeComment(req, res) { | ||
const { commentId } = req.params; | ||
const { id } = req.user.user; | ||
const hasDisliked = await models.LikeDislike.findOne({ | ||
where: { | ||
commentId, | ||
userId: id, | ||
dislikes: 1, | ||
likes: 0 | ||
} | ||
}); | ||
if (hasDisliked) { | ||
await models.LikeDislike.update({ dislikes: 0, likes: 1 }, | ||
{ where: { id: hasDisliked.dataValues.id } }); | ||
return res.status(statusCode.OK).json({ | ||
status: statusCode.OK, | ||
message: ' You have successfully liked this comment ' | ||
}); | ||
} | ||
await models.LikeDislike.create({ | ||
userId: id, | ||
commentId, | ||
dislikes: 0, | ||
likes: 1 | ||
}); | ||
|
||
return res.status(statusCode.CREATED).json({ | ||
status: statusCode.CREATED, | ||
message: ' You have successfully liked 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 } = req.user.user; | ||
const hasLiked = await models.LikeDislike.findOne({ | ||
where: { | ||
commentId, | ||
userId: id, | ||
likes: 1 | ||
} | ||
}); | ||
if (hasLiked) { | ||
await models.LikeDislike.update({ dislikes: 1, likes: 0 }, | ||
{ where: { id: hasLiked.dataValues.id } }); | ||
return res.status(statusCode.OK).json({ | ||
status: statusCode.OK, | ||
message: ' You have successfully disliked this comment ' | ||
}); | ||
} | ||
|
||
models.LikeDislike.create({ | ||
userId: id, | ||
commentId, | ||
dislikes: 1, | ||
likes: 0 | ||
}); | ||
|
||
return res.status(statusCode.CREATED).json({ | ||
status: statusCode.CREATED, | ||
message: ' You have successfully disliked this comment ' | ||
}); | ||
} | ||
|
||
/** | ||
* @description - Users should be able to count numbers of likes | ||
* @param {Object} req - Request Object | ||
* @param {Object} res - Response Object | ||
* @returns {Object} - Response object | ||
*/ | ||
static async getLikeCount(req, res) { | ||
const { commentId } = req.params; | ||
const likeCount = await models.LikeDislike.count({ | ||
where: { | ||
commentId, | ||
likes: 1 | ||
} | ||
}); | ||
return res.status(statusCode.OK).json({ | ||
status: statusCode.OK, | ||
likeCount | ||
}); | ||
} | ||
|
||
/** | ||
* @description - Users should be able to count numbers of dislikes | ||
* @param {Object} req - Request Object | ||
* @param {Object} res - Response Object | ||
* @returns {Object} - Response object | ||
*/ | ||
static async getDislikeCount(req, res) { | ||
const { commentId } = req.params; | ||
const disLikeCount = await models.LikeDislike.count({ | ||
where: { | ||
commentId, | ||
dislikes: 1 | ||
} | ||
}); | ||
return res.status(statusCode.OK).json({ | ||
status: statusCode.OK, | ||
disLikeCount | ||
}); | ||
} | ||
} |
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,35 @@ | ||
export default { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('LikeDislikes', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
commentId: { | ||
allowNull: false, | ||
type: Sequelize.INTEGER, | ||
references: { model: 'Comments', key: 'id' } | ||
}, | ||
userId: { | ||
allowNull: false, | ||
type: Sequelize.INTEGER, | ||
references: { model: 'Users', key: 'id' } | ||
}, | ||
likes: { | ||
type: Sequelize.INTEGER | ||
}, | ||
dislikes: { | ||
type: Sequelize.INTEGER | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: (queryInterface, Sequelize) => queryInterface.dropTable('LikeDislikes') | ||
}; |
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,43 @@ | ||
export default (sequelize, DataTypes) => { | ||
const LikeDislike = sequelize.define('LikeDislikes', | ||
{ | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: DataTypes.INTEGER | ||
}, | ||
commentId: { | ||
allowNull: false, | ||
type: DataTypes.INTEGER, | ||
references: { model: 'Comments', key: 'id' } | ||
}, | ||
userId: { | ||
allowNull: false, | ||
type: DataTypes.INTEGER, | ||
references: { model: 'Comments', key: 'id' } | ||
}, | ||
likes: { type: DataTypes.INTEGER, allowNull: false }, | ||
dislikes: { type: DataTypes.INTEGER, allowNull: false } | ||
}, | ||
{ | ||
tableName: 'LikeDislikes' | ||
}); | ||
|
||
LikeDislike.associate = (models) => { | ||
LikeDislike.belongsTo(models.Comment, { | ||
foreignKey: 'commentId', | ||
onUpdate: 'CASCADE', | ||
onDelete: 'CASCADE', | ||
hooks: true | ||
}); | ||
LikeDislike.belongsTo(models.User, { | ||
foreignKey: 'userId', | ||
onUpdate: 'CASCADE', | ||
onDelete: 'CASCADE', | ||
hooks: true | ||
}); | ||
}; | ||
|
||
return LikeDislike; | ||
}; |
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,22 @@ | ||
import { Router } from 'express'; | ||
import commentReaction from '../controllers/commentReactionsController'; | ||
import checkValidToken from '../../middlewares/checkValidToken'; | ||
import errorHandler from '../../middlewares/errorHandler'; | ||
import likeDislikeCheck from '../../middlewares/commentReaction'; | ||
|
||
const likeDislikeRouter = new Router(); | ||
likeDislikeRouter.post('/like/:commentId', | ||
checkValidToken, | ||
likeDislikeCheck.liked, | ||
errorHandler(commentReaction.likeComment)); | ||
likeDislikeRouter.post('/dislike/:commentId', | ||
checkValidToken, | ||
likeDislikeCheck.disliked, | ||
errorHandler(commentReaction.dislikeComment)); | ||
likeDislikeRouter.get('/dislike/:commentId', | ||
checkValidToken, | ||
errorHandler(commentReaction.getDislikeCount)); | ||
likeDislikeRouter.get('/like/:commentId', | ||
checkValidToken, | ||
errorHandler(commentReaction.getLikeCount)); | ||
export default likeDislikeRouter; |
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,24 @@ | ||
export default { | ||
up: (queryInterface, Sequelize) => queryInterface.bulkInsert('Comments', | ||
[ | ||
{ | ||
id: 5, | ||
body: 'How to create sequalize seeds', | ||
userId: 2, | ||
articleSlug: 'What-is-a-Version-1-UUID', | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}, | ||
{ | ||
id: 6, | ||
body: 'How to create sequalized great seeds', | ||
userId: 2, | ||
articleSlug: 'What-is-a-Version-1-UUID', | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
} | ||
], | ||
{}), | ||
|
||
down: (queryInterface, Sequelize) => queryInterface.bulkDelete('Comments', null, {}) | ||
}; |
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,62 @@ | ||
import models from '../api/models'; | ||
import statusCodes from '../helpers/constants/status.codes'; | ||
|
||
/** | ||
* @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 } = req.user.user; | ||
const hasLiked = await models.LikeDislike.findOne({ | ||
where: { | ||
commentId, | ||
userId: id, | ||
likes: 1 | ||
} | ||
}); | ||
// If the user has already liked that comment | ||
if (hasLiked) { | ||
return res.status(statusCodes.BAD_REQUEST).json({ | ||
status: statusCodes.BAD_REQUEST, | ||
message: '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 } = req.user.user; | ||
const hasDisliked = await models.LikeDislike.findOne({ | ||
where: { | ||
commentId, | ||
userId: id, | ||
dislikes: 1 | ||
} | ||
}); | ||
// If the user has already disliked that comment | ||
if (hasDisliked) { | ||
return res.status(statusCodes.BAD_REQUEST).json({ | ||
status: statusCodes.BAD_REQUEST, | ||
message: 'You have already disliked this comment!' | ||
}); | ||
} | ||
next(); | ||
} | ||
} |
Oops, something went wrong.