-
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
Mireille Niwemuhuza
authored and
Mireille Niwemuhuza
committed
Jun 25, 2019
1 parent
5591ca6
commit efcd119
Showing
11 changed files
with
472 additions
and
78 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
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
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,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(); | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
src/sequelize/migrations/20190624081558-add-commentId-likeDislike.js
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,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') | ||
}; |
Oops, something went wrong.