-
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.
[Feature #166240828] Enable the users to fetch articles ratings
- Loading branch information
1 parent
86a977d
commit ab2904b
Showing
24 changed files
with
763 additions
and
237 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 |
---|---|---|
|
@@ -19,5 +19,6 @@ src/api/migrations | |
|
||
# Seeders | ||
src/api/seeders | ||
# Models | ||
|
||
# Models | ||
src/api/models |
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,99 @@ | ||
import _ from 'lodash'; | ||
import models, { sequelize } from '../models'; | ||
import status from '../../helpers/constants/status.codes'; | ||
import sendError from '../../helpers/error.sender'; | ||
import calculateRatingsPercentage from '../../helpers/calculate.ratings.percentages'; | ||
import errorMessages from '../../helpers/constants/error.messages'; | ||
import generatePaginationDetails from '../../helpers/generate.pagination.details'; | ||
|
||
const { Rating, User } = models; | ||
|
||
/** | ||
* containing all controllers of the ratings process | ||
* | ||
* @export | ||
* @class ratings | ||
*/ | ||
export default class articleRatings { | ||
/** | ||
* A controller to calculate the ratings percentages, | ||
* | ||
* @param {Object} req - the request object | ||
* @param {Object} res - the result object | ||
* @returns {Object} res | ||
*/ | ||
static async getArticleRatingStatistics(req, res) { | ||
// Initialising variables | ||
const result = {}; | ||
const { slug } = req.params; | ||
|
||
// Fetching the articles count per rating | ||
const ratingsCount = await Rating.findAll({ | ||
where: { | ||
articleSlug: slug | ||
}, | ||
group: ['rating'], | ||
attributes: ['rating', [sequelize.fn('COUNT', 'TagName'), 'count']] | ||
}); | ||
|
||
// Sending the result | ||
if (_.isEmpty(ratingsCount)) { | ||
// Error if there are no ratings for the article | ||
sendError(status.NOT_FOUND, res, 'ratings', errorMessages.ratingsNotFound); | ||
result.status = status.NOT_FOUND; | ||
} else { | ||
// Calculating the percentages | ||
const ratingsWithPercentages = calculateRatingsPercentage(ratingsCount); | ||
|
||
// sending the result back | ||
result.status = status.OK; | ||
result.data = ratingsWithPercentages; | ||
res.status(status.OK).json(result); | ||
} | ||
} | ||
|
||
/** | ||
* A controller to get users for a given rating | ||
* with pagination support | ||
* | ||
* @param {Object} req - the request object | ||
* @param {Object} res - the result object | ||
* @returns {Object} res | ||
*/ | ||
static async getRatingUsers(req, res) { | ||
// Initialising variables | ||
const result = {}; | ||
const { offset, limit } = req.query; | ||
const { slug, rating } = req.params; | ||
|
||
// Fetching data from the database | ||
const ratingUsers = await User.findAndCountAll({ | ||
attributes: ['id', 'username', 'email', 'firstName', 'lastName'], | ||
include: [ | ||
{ | ||
model: Rating, | ||
where: { | ||
articleSlug: slug, | ||
rating | ||
}, | ||
attributes: [] | ||
} | ||
], | ||
offset, | ||
limit | ||
}); | ||
|
||
if (_.isEmpty(ratingUsers.rows)) { | ||
sendError(status.NOT_FOUND, res, 'Data', 'No users fond for the provided entries'); | ||
} else { | ||
// sending the result back | ||
result.status = status.OK; | ||
result.paginationDetails = generatePaginationDetails(ratingUsers.count, | ||
ratingUsers.rows, | ||
offset, | ||
limit); | ||
result.data = ratingUsers.rows; | ||
res.status(status.OK).json(result); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.