-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#159987732 Users can get their reading stats (#52)
* fix uncaught error in category controller * ft(user-reading-stats): users can get their reading stats - user's view should be logged everytime they get an article - users should be able to see the number of articles read - users should be able to get the articles they have read [Delivers 159987732] * ft(users'-reading-stats): users can get their reading stats - log user's view to the ArticleView model when they view an article - users can get all the articles they have viewed - users can get articles that users have liked - users can get their most read category - add more tests to Article model - add tests to the ArticleView model - sanitize category name before processing the request [Delivers 159987732] * ft(user-reading-stats): - refactor mode function to use more descriptive variables - add jsdoc comment to mode util function - change getSpecific method name to getArticle in articles controller - change getAll method name to getAllArticles in articles controller [Delivers 159987732] * ft(user-reading-stats): fix failing travis build [Delivers 159987732] * ft(user-reading-stats): fix failing travis build [Delivers 159987732] * ft(user-reading-stats): update swagger documentation - update swagger documentation - change the route for adding articles to a category [Delivers 159987732] * ft(user-reading-stats): users should be able to get their reading stats - change the name of saveArticleView method to postArticleHistory - change the name of ArticleView model to ArticleViewHistory [Delivers 159987732] * Fix merge conflicts in article.js file * ft(swagger-documentation): fix swagger docummentation error [Fixes 159987732] * fix hound errors
- Loading branch information
1 parent
7d8a850
commit 71b32ab
Showing
24 changed files
with
986 additions
and
113 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,109 @@ | ||
import { | ||
Article, | ||
ArticleViewHistory, | ||
Category, | ||
LikesDislikes, | ||
ArticleCategory, | ||
} from '../models'; | ||
import mode from '../utils/modeValue'; | ||
|
||
/** | ||
* | ||
* @description controller class with methods for getting user's reading stats | ||
* @class RedingStatsController | ||
*/ | ||
class ReadingStatsController { | ||
/** | ||
* @description Get all categories | ||
* @param {object} req body of the user's request | ||
* @param {function} res response from the server | ||
* @param {function} next response from the server | ||
* @returns {object} The body of the response message | ||
* @memberof ReadingStatsController | ||
*/ | ||
static getAllReadingStats(req, res, next) { | ||
const { userId } = req; | ||
ArticleViewHistory.findAll({ | ||
where: { userId }, | ||
attributes: ['articleId'], | ||
include: [ | ||
{ | ||
model: Article, | ||
as: 'article', | ||
attributes: ['title', 'body', 'description', 'authorId'] | ||
} | ||
] | ||
}) | ||
.then((articles) => { | ||
const articleCount = articles.map(article => article.articleId); | ||
return LikesDislikes.findAll({ | ||
where: { userId }, | ||
attributes: ['articleId', 'reaction'], | ||
include: [ | ||
{ | ||
model: Article, | ||
as: 'article', | ||
attributes: ['title', 'body', 'description'] | ||
} | ||
] | ||
}) | ||
.then((reaction) => { | ||
ArticleCategory.findAll({ | ||
where: { articleId: articleCount } | ||
}) | ||
.then((result) => { | ||
const categoryIds = result.map(article => article.categoryId); | ||
return Category.findAll({ | ||
where: { id: categoryIds }, | ||
attributes: ['name'] | ||
}) | ||
.then((categoryNames) => { | ||
let mostReadCategory; | ||
if (categoryNames.length === 0) { | ||
mostReadCategory = 'Articles have not been added to any category'; | ||
} | ||
const names = categoryNames.map(category => category.name); | ||
mostReadCategory = mode(names); | ||
return res.status(200).json({ | ||
status: 'success', | ||
articlesRead: articles.length === 0 ? 'No articles found' : articles, | ||
numberOfArticlesRead: articleCount.length, | ||
articleReactions: reaction.length === 0 ? 'No reactions found' : reaction, | ||
mostReadCategory | ||
}); | ||
}) | ||
.catch(error => next(error)); | ||
}) | ||
.catch(error => next(error)); | ||
}) | ||
.catch(error => next(error)); | ||
}) | ||
.catch(error => next(error)); | ||
} | ||
|
||
/** | ||
* @description Get all categories | ||
* @param {object} req body of the user's request | ||
* @param {function} res response from the server | ||
* @param {function} next response from the server | ||
* @returns {object} The body of the response message | ||
* @param {string} articleId body of the user's request | ||
* @memberof ReadingStatsController | ||
*/ | ||
static postArticleViewHistory(req, res, next) { | ||
const { userId, articleId } = res.locals; | ||
ArticleViewHistory.findOne({ | ||
where: { userId, articleId }, | ||
}) | ||
.then((userView) => { | ||
if (!userView) { | ||
ArticleViewHistory.create({ articleId, userId }) | ||
.then(() => res.status(201).send()) | ||
.catch(next); | ||
} | ||
}) | ||
.catch(next); | ||
} | ||
} | ||
|
||
export default ReadingStatsController; |
Oops, something went wrong.