Skip to content

Commit

Permalink
Merge 454b98e into 6e3b9c2
Browse files Browse the repository at this point in the history
  • Loading branch information
manzif committed Jul 24, 2019
2 parents 6e3b9c2 + 454b98e commit 296d515
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/controllers/articleRatingControllers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import model from '../db/models/index';
import paginate from '../helpers/paginate';

const { Users, Articles, Rating } = model;

Expand Down Expand Up @@ -71,28 +72,30 @@ class ArticleRatingManager {
* @returns {message} That article is not available
*/
static async ratingAverage(req, res) {
const averageRating = await Rating.findAll({
where: { articleId: req.params.articleId },
attributes: ['rating'],
raw: true,
});
const pageNumber = paginate(req.query.page, req.query.pageSize);
const averageRating = await Rating.findAll({ where: { articleId: req.params.articleId }, attributes: ['rating'], raw: true, });
if (!averageRating.length) {
return res.status(400).send({
message: 'The Article requested has not been rated yet'
});
return res.status(400).send({ message: 'The Article requested has not been rated yet' });
}
let totalrating = 0;
averageRating.map((rating) => {
totalrating += rating.rating;
return averageRating;
});
const average = (totalrating / averageRating.length).toFixed(1);
return res.status(200).send({
rating: {
average,
raters: averageRating.length
}
const userNames = await Rating.findAll({
offset: pageNumber.offset,
limit: pageNumber.limit,
include: [{ model: Users, attributes: ['username'], raw: true }]
});
if (!userNames.length) {
return res.status(404).json({ error: 'The raters requested can not be found' });
}
const ratersNames = [];
for (let i = 0; i < userNames.length; i += 1) {
ratersNames.push(userNames[i].dataValues.User.dataValues.username);
}
const average = (totalrating / averageRating.length).toFixed(1);
return res.status(200).send({ rating: { average, raters: averageRating.length, ratersNames } });
}
}
export default ArticleRatingManager;
14 changes: 14 additions & 0 deletions swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,20 @@
"schema": {
"$ref": "#/definitions/fecthratings"
}
},
{
"name": "page",
"in": "query",
"type": "string",
"description": "specify page",
"require": true
},
{
"name": "pageSize",
"in": "query",
"type": "string",
"description": "specify pageSize",
"require": true
}
],
"produces": [
Expand Down

0 comments on commit 296d515

Please sign in to comment.