-
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.
Merge branch 'development' of https://github.com/andela/coding-geeks-…
…ah-backend into ft-user-update-and-delete-articles-167966138
- Loading branch information
Showing
27 changed files
with
1,423 additions
and
9,356 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,44 @@ | ||
import model from '../sequelize/models'; | ||
|
||
const { User, Rating } = model; | ||
|
||
/** | ||
* @description Rate the article | ||
*/ | ||
class ArticleRate { | ||
/** | ||
* @description rate article | ||
* @param {object} req | ||
* @param {object} res | ||
* @returns {object} returns object containing article rates | ||
*/ | ||
static async rateArticle(req, res) { | ||
const { rate } = req.body; | ||
const rateAuthor = await User.findOne({ where: { id: req.userData.id } }); | ||
const ratings = await Rating.findOne( | ||
{ where: { reviewerId: rateAuthor.id, articleId: req.params.id } } | ||
); | ||
|
||
if (ratings) { | ||
const [, updatedRating] = await Rating.update( | ||
{ rate }, { where: { id: ratings.id }, returning: true } | ||
); | ||
return res.status(200).send({ | ||
message: 'Successfully updated the rate of this article', | ||
data: { rating: updatedRating } | ||
}); | ||
} | ||
const rating = await Rating.create({ | ||
rate, | ||
articleId: req.params.id, | ||
reviewerId: rateAuthor.id | ||
}); | ||
|
||
return res.status(201).send({ | ||
message: 'Successfully rated this article', | ||
data: { rating } | ||
}); | ||
} | ||
} | ||
|
||
export default ArticleRate; |
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,49 @@ | ||
import db from '../../sequelize/models'; | ||
|
||
const { Article } = db; | ||
|
||
/** | ||
* @exports ArticRatelehelper | ||
* @class Articlehelper | ||
* @description Helps to generate token and passwor hashing | ||
* */ | ||
class ArticleRatelehelper { | ||
/** | ||
* Check the environment | ||
* @function validateArticleRated | ||
* @param {integer} id - Check the rate | ||
* @param {integer} user - Check the user | ||
* @return {string} Validate the rate | ||
*/ | ||
static async validateArticleRated(id, user) { | ||
const findArticle = await Article.findOne({ where: { id } }); | ||
if (findArticle) { | ||
const articleOwner = await Article.findAll({ | ||
where: { id, authorId: user } | ||
}); | ||
if (articleOwner.length) { | ||
return 'Sorry! You cannot rate your article'; | ||
} | ||
} | ||
if (findArticle == null) { | ||
return 'This Article does not exist'; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Check the environment | ||
* @function validateRating | ||
* @param {integer} rate - Check the rate | ||
* @return {string} Validate the rate | ||
*/ | ||
static validateRating(rate) { | ||
const regRaterange = /^[1-5]{1}?$/; | ||
if (!regRaterange.test(rate)) { | ||
return 'Rate must be between 1 and 5'; | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
export default ArticleRatelehelper; |
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.