Skip to content

Commit

Permalink
ft(articles):get articles for specific user[Finishes#169254678]
Browse files Browse the repository at this point in the history
  • Loading branch information
nshutijonathan committed Oct 21, 2019
1 parent 8b49ab5 commit 56f37eb
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 84 deletions.
164 changes: 105 additions & 59 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,7 +108,7 @@ class Articles {
'flagged',
'images',
'views',
'createdAt',
'createdAt'
])
);

Expand All @@ -125,7 +132,11 @@ class Articles {
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 +148,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 +202,7 @@ class Articles {
'favoritedcount',
'flagged',
'images',
'views',
'views'
]);
const timeAgo = moment(article.createdAt).fromNow();
const readTime = Helper.calculateReadTime(article.body);
Expand Down Expand Up @@ -208,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 @@ -229,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 @@ -242,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 @@ -278,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 @@ -299,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 56f37eb

Please sign in to comment.