Skip to content

Commit

Permalink
feature(views and popular articles): get article views and popular ar…
Browse files Browse the repository at this point in the history
…ticles [Finishes #169124758] (#63)
  • Loading branch information
habinezadalvan authored and salviosage committed Oct 16, 2019
1 parent 79025f2 commit 8863da4
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 53 deletions.
157 changes: 106 additions & 51 deletions src/controllers/articles.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@ const db = models.Article;
*/
class Articles {
/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} data
* @memberof Articles
*/
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} data
* @memberof Articles
*/
static async createArticles(req, res) {
const userId = req.auth.id;
const findUser = await Userservice.getOneUser(userId);
const images = await cloudinaryHelper.generateCloudinaryUrl(req.files);
// const images = await cloudinaryHelper.generateCloudinaryUrl(req.files);
const { images } = req.body;

if (findUser) {
const { title } = req.body;
Expand Down Expand Up @@ -67,14 +68,14 @@ class Articles {
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} articles
* @memberof Articles
*/
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} articles
* @memberof Articles
*/
static async getAllArticles(req, res) {
const counter = await db.count();
if (req.offset >= counter) {
Expand All @@ -85,14 +86,35 @@ class Articles {
if (!articles) {
return res.status(200).json({ status: 200, message: 'There is no article.' });
}
const allArticles = _.map(articles, _.partialRight(_.pick, ['slug', 'title', 'description', 'body', 'taglist', 'favorited', 'favoritedcount', 'flagged', 'images', 'views']));
const allArticles = _.map(
articles,
_.partialRight(_.pick, [
'slug',
'title',
'description',
'body',
'taglist',
'favorited',
'favoritedcount',
'flagged',
'images',
'views'
])
);

allArticles.map((article) => {
const readTime = Helper.calculateReadTime(article.body);
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 All @@ -102,14 +124,14 @@ class Articles {
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} article
* @memberof Articles
*/
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} article
* @memberof Articles
*/
static async getOneArticle(req, res) {
const findArticle = await db.findOne({
where: { slug: req.params.slug }
Expand All @@ -120,7 +142,18 @@ class Articles {
message: 'That article does not exist!'
});
}
const article = _.pick(findArticle, ['slug', 'title', 'description', 'body', 'taglist', 'favorited', 'favoritedcount', 'flagged', 'images', 'views']);
const article = _.pick(findArticle, [
'slug',
'title',
'description',
'body',
'taglist',
'favorited',
'favoritedcount',
'flagged',
'images',
'views'
]);
const readTime = Helper.calculateReadTime(article.body);
article.readtime = readTime;
if (req.auth) {
Expand All @@ -129,6 +162,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 All @@ -137,14 +192,14 @@ class Articles {
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} message
* @memberof Articles
*/
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} message
* @memberof Articles
*/
static async deleteArticle(req, res) {
const findArticle = await db.findOne({
where: { slug: req.params.slug }
Expand All @@ -171,14 +226,14 @@ class Articles {
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {Object} updated article details
* @memberof Articles
*/
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {Object} updated article details
* @memberof Articles
*/
static async UpdateArticle(req, res) {
const findArticle = await db.findOne({
where: { slug: req.params.slug }
Expand Down Expand Up @@ -207,14 +262,14 @@ class Articles {
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {Object} share article over email and social media channelds
* @memberof Articles
*/
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {Object} share article over email and social media channelds
* @memberof Articles
*/
static async shareArticle(req, res) {
const article = await db.findOne({
where: { slug: req.params.slug }
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 8863da4

Please sign in to comment.