-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from andela/ft/report-article-167190456
#167190456 An authorized user should be able to report an …
- Loading branch information
Showing
12 changed files
with
730 additions
and
69 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Oops, something went wrong.