Skip to content

Commit

Permalink
Merge ee66dd1 into 79025f2
Browse files Browse the repository at this point in the history
  • Loading branch information
habinezadalvan committed Oct 14, 2019
2 parents 79025f2 + ee66dd1 commit daec291
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
29 changes: 29 additions & 0 deletions src/controllers/articles.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,14 @@ class Articles {
article.readtime = readTime;
return true;
});
const popularArticles = allArticles.slice(0);
popularArticles.sort((a, b) => b.views - a.views);
const mostPopular = popularArticles.slice(0, 9);

if (req.query.popular) {
util.setSuccess(200, 'The most popular articles on authors haven', mostPopular);
return util.send(res);
}
return res.status(200).json({
status: 200,
message: 'List of all articles',
Expand Down Expand Up @@ -129,6 +136,28 @@ class Articles {
const item = 'article';
await statsService.createStat({ description, item, readerId }, 'Stats');
}
let viewObject = {
slug: findArticle.slug,
title: findArticle.title,
description: findArticle.description,
body: findArticle.body,
flagged: findArticle.flagged,
favorited: findArticle.favorited,
favoritedcount: findArticle.favoritedcount,
images: findArticle.images,
views: 1
};
if (findArticle) {
viewObject = { ...viewObject, views: findArticle.views + 1 };
await db.update(viewObject, {
where: {
id: findArticle.id
},
returing: true
});
} else {
await db.create(viewObject);
}
return res.status(200).json({
status: 200,
message: 'Article successfully retrieved',
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/query.check.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const searchArticleQuerybuilder = (searchQueries) => {

export const checkQuery = (req, res, next) => {
let {
limit, page, ...searchQueries
limit, page, popular, ...searchQueries
} = req.query;
const validQueries = ['author', 'keyword', 'tag', 'title'];
let isValidRequest = true;
Expand Down
4 changes: 3 additions & 1 deletion src/services/article.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ class articleService {
* @static
* @param {*} offset
* @param {*} limit
* @param {*} popular
* @param {*} searchQueries
* @returns {object} data
* @memberof articleService
*/
static async getAllArticles(offset, limit, searchQueries) {
static async getAllArticles(offset, limit, popular, searchQueries) {
try {
return await db.findAll({
where: searchQueries,
order: [
['createdAt', 'DESC']
],
popular,
offset,
limit,
});
Expand Down

0 comments on commit daec291

Please sign in to comment.