Skip to content

Commit

Permalink
Merge 8984b8f into a7df387
Browse files Browse the repository at this point in the history
  • Loading branch information
nshutijonathan committed Oct 21, 2019
2 parents a7df387 + 8984b8f commit 1a36a85
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 99 deletions.
196 changes: 122 additions & 74 deletions src/controllers/articles.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ 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);
Expand Down Expand Up @@ -69,24 +69,31 @@ 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) {
req.offset = 0;
}
const { searchQueries, offset, limit } = req;
const articles = await articleService.getAllArticles(offset, limit, searchQueries);
const articles = await articleService.getAllArticles(
offset,
limit,
searchQueries
);
if (!articles) {
return res.status(200).json({ status: 200, message: 'There is no article.' });
return res
.status(200)
.json({ status: 200, message: 'There is no article.' });
}

const allArticles = _.map(
articles,
_.partialRight(_.pick, [
Expand All @@ -101,31 +108,37 @@ class Articles {
'flagged',
'images',
'views',
'createdAt',
'createdAt'
])
);

await Promise.all(allArticles.map(async (article) => {
try {
const userDetails = await Userservice.getOneUser(article.authorId);
const { username, image } = userDetails;
const readTime = Helper.calculateReadTime(article.body);
const timeAgo = moment(article.createdAt).fromNow();
article.readtime = readTime;
article.username = username;
article.userImage = image;
article.timeCreated = timeAgo;
return true;
} catch (error) {
console.log(error);
}
}));
await Promise.all(
allArticles.map(async (article) => {
try {
const userDetails = await Userservice.getOneUser(article.authorId);
const { username, image } = userDetails;
const readTime = Helper.calculateReadTime(article.body);
const timeAgo = moment(article.createdAt).fromNow();
article.readtime = readTime;
article.username = username;
article.userImage = image;
article.timeCreated = timeAgo;
return true;
} catch (error) {
console.log(error);
}
})
);
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);
util.setSuccess(
200,
'The most popular articles on authors haven',
mostPopular
);
return util.send(res);
}
return res.status(200).json({
Expand All @@ -137,14 +150,39 @@ class Articles {
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} article
* @memberof Articles
*/
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} articles
* @memberof Articles
*/
static async SpecificUserArticles(req, res) {
const findArticles = await db.findAll({
where: { authorId: req.auth.id }
});
if (!findArticles) {
return res.status(200).send({
message: 'no articles'
});
}
if (findArticles) {
return res.status(200).send({
data: findArticles
});
}
}

/**
*
*
* @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 @@ -166,7 +204,7 @@ class Articles {
'favoritedcount',
'flagged',
'images',
'views',
'views'
]);
const readTime = Helper.calculateReadTime(article.body);
article.readtime = readTime;
Expand Down Expand Up @@ -206,14 +244,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 @@ -227,7 +265,8 @@ class Articles {
if (req.auth.id !== findArticle.authorId) {
return res.status(403).json({
status: 403,
message: 'Sorry you can not DELETE an article that does not belong to you.'
message:
'Sorry you can not DELETE an article that does not belong to you.'
});
}
await db.destroy({
Expand All @@ -240,25 +279,28 @@ 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 }
});
if (!findArticle) {
return res.status(200).json({ status: 200, message: 'That article does not exist' });
return res
.status(200)
.json({ status: 200, message: 'That article does not exist' });
}
if (req.auth.id !== findArticle.authorId) {
return res.status(403).json({
status: 403,
message: 'Sorry you can not UPDATE an article that does not belong to you.'
message:
'Sorry you can not UPDATE an article that does not belong to you.'
});
}
const { title, body, description } = req.body;
Expand All @@ -276,14 +318,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 All @@ -297,15 +339,21 @@ class Articles {
const url = `${location}/articles/${req.params.slug}`;
switch (req.params.channel) {
case 'facebook':
await OpenUrlHelper.openUrl(`https:www.facebook.com/sharer/sharer.php?u=${url}`);
await OpenUrlHelper.openUrl(
`https:www.facebook.com/sharer/sharer.php?u=${url}`
);
util.setSuccess(200, `Article shared to ${req.params.channel}`, url);
return util.send(res);
case 'twitter':
await OpenUrlHelper.openUrl(`https://twitter.com/intent/tweet?url=${url}`);
await OpenUrlHelper.openUrl(
`https://twitter.com/intent/tweet?url=${url}`
);
util.setSuccess(200, `Article shared to ${req.params.channel}`, url);
return util.send(res);
case 'mail':
await OpenUrlHelper.openUrl(`mailto:?subject=${article.title}&body=${url}`);
await OpenUrlHelper.openUrl(
`mailto:?subject=${article.title}&body=${url}`
);
util.setSuccess(200, `Article shared to ${req.params.channel}`, url);
return util.send(res);
default:
Expand Down
Loading

0 comments on commit 1a36a85

Please sign in to comment.