Skip to content

Commit

Permalink
Merge pull request #39 from andela/ft/report-article-167190456
Browse files Browse the repository at this point in the history
#167190456 An authorized user should be able to report an …
  • Loading branch information
topseySuave committed Aug 8, 2019
2 parents 0b55ec3 + 2c88f5c commit 56d3935
Show file tree
Hide file tree
Showing 12 changed files with 730 additions and 69 deletions.
132 changes: 66 additions & 66 deletions package-lock.json

Large diffs are not rendered by default.

137 changes: 137 additions & 0 deletions server/controllers/reportController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import sequelize from 'sequelize';
import models from '../db/models';
import utils from '../helpers/Utilities';


const { Op } = sequelize;
/**
* @Module ArticleController
* @description Controlls all activities related to Articles
*/
class reportController {
/**
* @description Creates an Article
* @param {Object} req - request object
* @param {Object} res - response object
* @returns {Object} Article data
* @memberof ArticleController
*/
static async reportArticle(req, res) {
const article = await models.Article.findOne({
where: {
slug: req.params.slug
},
include: [{
model: models.User,
as: 'author',
attributes: ['firstname', 'lastname', 'username', 'image', 'email']
}]
});
if (!article) {
return utils.errorStat(res, 404, 'Article not found');
}

if (article) {
const authorId = req.user.id;
const articleId = article.id;
const { reportDetails, reportType } = req.body;
const findReport = await models.Report.findOne({
where: { authorId, articleId }
});

if (!findReport) {
const report = await models.Report.create({
reportDetails,
reportType,
authorId,
articleId
});
if (report) {
return utils.successStat(res, 201, 'report', report);
}
}
return utils.errorStat(res, 409, 'article already reported by you');
}
}

/**
* @description Creates an Article
* @param {Object} req - request object
* @param {Object} res - response object
* @returns {Object} Article data
* @memberof ReportController
*/
static async getAllArticleReports(req, res) {
const allReports = await models.Report.findAll({
include: [
{
model: models.User,
as: 'author',
attributes: [
'firstname',
'lastname',
'username',
'image',
'email'
]
},
{
model: models.Article,
attributes: [
'title',
'description',
'articleBody',
'image',
'tagList',
'slug'
]
}
]
});
return utils.successStat(res, 200, 'reports', allReports);
}

/**
* @description Creates an Article
* @param {Object} req - request object
* @param {Object} res - response object
* @returns {Object} Article data
* @memberof ReportController
*/
static async getOneReport(req, res) {
const report = await models.Report.findOne({
where: {
[Op.and]: [{ id: req.params.reportId }, { authorId: req.user.id }]
},
include: [
{
model: models.User,
as: 'author',
attributes: [
'firstname',
'lastname',
'username',
'image',
'email'
]
},
{
model: models.Article,
attributes: [
'title',
'description',
'articleBody',
'image',
'tagList',
'slug'
]
}
]
});
if (!report) return utils.errorStat(res, 404, 'Report not found');
return utils.successStat(res, 200, 'report', report);
}
}


export default reportController;
31 changes: 31 additions & 0 deletions server/db/migrations/20190808133814-create-report.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('Reports', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
reportDetails: {
type: Sequelize.STRING
},
reportType: {
type: Sequelize.STRING
},
authorId: {
type: Sequelize.INTEGER
},
articleId: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: queryInterface => queryInterface.dropTable('Reports')
};
13 changes: 13 additions & 0 deletions server/db/models/report.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = (sequelize, DataTypes) => {
const Report = sequelize.define('Report', {
reportDetails: DataTypes.STRING,
reportType: DataTypes.STRING,
authorId: DataTypes.INTEGER,
articleId: DataTypes.INTEGER
}, {});
Report.associate = (models) => {
Report.belongsTo(models.Article, { foreignKey: 'articleId' });
Report.belongsTo(models.User, { as: 'author', foreignKey: 'authorId' });
};
return Report;
};
Loading

0 comments on commit 56d3935

Please sign in to comment.