-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(Likes & Dislikes): Allowing a user to like or dislike an article. - ensuring that the user can like or dislike an article. [Delivers #164489935] changes changes
- Loading branch information
Copain Fabrice BIENAIME
authored and
Copain Fabrice BIENAIME
committed
Apr 23, 2019
1 parent
4176913
commit 1d79206
Showing
11 changed files
with
392 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import models from '../models/index'; | ||
|
||
const Votes = models.vote; | ||
|
||
/** * | ||
* @param {request } req request | ||
* @param {response} res response | ||
*/ | ||
|
||
// eslint-disable-next-line require-jsdoc | ||
class VotesController { | ||
/** * | ||
* @param {request } req request | ||
* @param {response} res response | ||
* @returns {message} message | ||
*/ | ||
static async likes(req, res) { | ||
try { | ||
const likeData = { | ||
user: req.user.id, | ||
article: req.params.articleId, | ||
like: true, | ||
dislike: false | ||
}; | ||
if (req.vote === null) { | ||
await Votes.create(likeData); | ||
return res.status(200).json({ message: 'thanks for the support.' }); | ||
} if (req.vote.like === true) { | ||
return res.status(400).json({ error: 'sorry you have already liked this article.' }); | ||
} | ||
await Votes.update(likeData, { where: { vote_id: req.vote.vote_id } }); | ||
return res.status(200).json({ | ||
message: 'thanks for the support.', | ||
userId: req.user.id, | ||
article: req.params.articleId, | ||
}); | ||
} catch (error) { | ||
return res.status(500).json({ error: 'something wrong try again later.' }); | ||
} | ||
} | ||
|
||
/** * | ||
* @param {request } req request | ||
* @param {response} res response | ||
* @returns {message} message | ||
*/ | ||
|
||
// eslint-disable-next-line require-jsdoc | ||
static async dislikes(req, res) { | ||
try { | ||
const dislikeData = { | ||
user: req.user.id, | ||
article: req.params.articleId, | ||
like: false, | ||
dislike: true | ||
}; | ||
if (req.vote === null) { | ||
await Votes.create(dislikeData); | ||
return res.status(200).json({ message: 'thank for support.' }); | ||
} if (req.vote.dislike === true) { | ||
return res.status(400).json({ error: 'sorry you have already disliked this article.' }); | ||
} | ||
await Votes.update(dislikeData, { where: { vote_id: req.vote.vote_id } }); | ||
return res.status(200).json({ | ||
message: 'You have disliked this article.', | ||
userId: req.user.id, | ||
article: req.params.articleId, | ||
}); | ||
} catch (error) { | ||
return res.status(500).json({ error: 'something wrong try again later.' }); | ||
} | ||
} | ||
} | ||
|
||
export default VotesController; |
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 @@ | ||
import passport from 'passport'; | ||
|
||
const isAuth = (req, res, next) => { | ||
passport.authenticate('jwt', { session: false }, (error, user) => { | ||
if (error) { | ||
next(error); | ||
} | ||
req.user = user; | ||
next(); | ||
})(req, res, next); | ||
}; | ||
|
||
export default isAuth; |
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 @@ | ||
import models from '../models/index'; | ||
|
||
const Vote = models.vote; | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
const checkVote = async (req, res) => { | ||
try { | ||
const search = await Vote.findOne({ where: { user: req.user.id } }); | ||
return search; | ||
} catch (error) { | ||
return res.status(500).json({ status: 500, message: `something wrong please try again.${error}` }); | ||
} | ||
}; | ||
const checkLikes = async (req, res, next) => { | ||
const vote = await checkVote(req, res); | ||
if (!vote) { | ||
req.vote = null; | ||
next(); | ||
} else { | ||
req.vote = vote; | ||
next(); | ||
} | ||
}; | ||
export default checkLikes; |
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,52 @@ | ||
|
||
|
||
const voteMigration = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('votes', | ||
{ | ||
vote_id: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
primaryKey: true, | ||
autoIncrement: true | ||
}, | ||
user: { | ||
type: Sequelize.INTEGER, | ||
onDelete: 'CASCADE', | ||
allowNull: false, | ||
references: { | ||
model: 'users', | ||
key: 'id' | ||
} | ||
|
||
}, | ||
article: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
onDelete: 'CASCADE', | ||
references: { | ||
model: 'articles', | ||
key: 'article_id' | ||
} | ||
}, | ||
like: { | ||
type: Sequelize.BOOLEAN, | ||
defaultValue: false | ||
}, | ||
dislike: { | ||
type: Sequelize.BOOLEAN, | ||
defaultValue: false | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
default: true | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: queryInterface => queryInterface.dropTable('votes') | ||
}; | ||
|
||
export default voteMigration; |
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,18 @@ | ||
const voteModels = (Sequelize, DataTypes) => { | ||
const Votes = Sequelize.define('vote', { | ||
vote_id: { | ||
type: DataTypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true | ||
}, | ||
user: { type: DataTypes.INTEGER, references: { model: 'user', key: 'id' } }, | ||
article: { type: DataTypes.INTEGER, references: { model: 'article', key: 'article_id' } }, | ||
like: { type: DataTypes.BOOLEAN }, | ||
dislike: { type: DataTypes.BOOLEAN }, | ||
}, {}); | ||
Votes.associate = (models) => { | ||
Votes.belongsTo(models.article, { as: 'articlefkey', foreignKey: 'article' }); | ||
Votes.belongsTo(models.user, { as: 'userfkey', foreignKey: 'user' }); | ||
}; | ||
return Votes; | ||
}; | ||
|
||
export default voteModels; |
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.