Skip to content

Commit

Permalink
Ft 164489801 Implemented listing all reported articles
Browse files Browse the repository at this point in the history
  • Loading branch information
jnkindi committed Apr 30, 2019
1 parent 37884c0 commit 178a23c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
29 changes: 28 additions & 1 deletion controllers/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class Article {
*@author: Jacques Nyilinkindi
* @param {Object} req
* @param {Object} res
* @returns {Object} Add reporting category
* @returns {Object} Reporting an article
*/
static async reportingArticle(req, res) {
const articleDetails = await ArticleModel.findOne({ where: { slug: req.params.slug } });
Expand All @@ -195,6 +195,33 @@ class Article {
},
};
return res.status(201).json({ report: response });
} catch (error) { return res.status(500).json({ error }); }
}


/**
*@author: Jacques Nyilinkindi
* @param {Object} req
* @param {Object} res
* @returns {Object} Get reported articles
*/
static async getReportedArticle(req, res) {
try {
const reported = await articleReporting.reportedArticles();
if (!reported) { return res.status(404).json({ message: 'No reported article found!' }); }
const response = reported.map(({
id, description, name, articleid, title, slug
}) => ({
id,
category: name,
description,
article: {
id: articleid,
slug,
title
}
}));
return res.status(201).json({ report: response });
} catch (error) {
return res.status(500).json({ error });
}
Expand Down
9 changes: 9 additions & 0 deletions models/articlereporting.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ const ArticleReportingModel = (Sequelize, DataTypes) => {
}, {
freezeTableName: true // Model tableName will be the same as the model name
});
ArticleReporting.reportedArticles = async () => {
const result = await Sequelize.query(
`SELECT articlereporting.id, articlereporting.description, reportingcategory.name,
articles.id as articleid, articles.title, articles.slug FROM articlereporting, reportingcategory,
articles WHERE articlereporting.articleid = articles.id AND articlereporting.categoryid = reportingcategory.id`,
{ type: Sequelize.QueryTypes.SELECT }
);
return result;
};
return ArticleReporting;
};
export default ArticleReportingModel;
1 change: 1 addition & 0 deletions routes/api/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ router.get('/report/category', AuthToken, articleController.reportingCategories)
router.put('/report/category/:id', AuthToken, articleController.editReportingCategory);
router.delete('/report/category/:id', AuthToken, articleController.deleteReportingCategory);
router.post('/:slug/report/', AuthToken, articleController.reportingArticle);
router.get('/reports', AuthToken, articleController.getReportedArticle);
router.get('/:slug', errorHandler(articleController.getArticle));

router.delete('/:slug');
Expand Down

0 comments on commit 178a23c

Please sign in to comment.