-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
166816223-feature(report, article): user can report article
- add report migration - add flagged column in article table - add Report Model - add report route - add report Controller - add tests [Delivers #166816223]
- Loading branch information
Obi chinedu Frank
committed
Jul 18, 2019
1 parent
8d026bd
commit 35f1901
Showing
13 changed files
with
501 additions
and
0 deletions.
There are no files selected for viewing
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
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,42 @@ | ||
import db from '../../db/models'; | ||
|
||
export default { | ||
reportArticle: async (req, res) => { | ||
const { | ||
user, body: { | ||
articleId, message | ||
} | ||
} = req; | ||
|
||
await db.Report.create({ | ||
articleId, | ||
userId: user.id, | ||
message, | ||
}); | ||
|
||
return res.status(200).send({ | ||
message: 'thank you for the report' | ||
}); | ||
}, | ||
|
||
getReportedArticles: async (req, res) => { | ||
const articles = await db.Report.findAll({ | ||
include: [ | ||
{ | ||
model: db.Article, | ||
as: 'articles' | ||
}, | ||
{ | ||
model: db.User, | ||
as: 'reporter', | ||
attributes: ['firstName', 'lastName', 'email', 'image', 'id', 'username'] | ||
}, | ||
], | ||
|
||
}); | ||
|
||
return res.status(200).send({ | ||
articles | ||
}); | ||
} | ||
}; |
9 changes: 9 additions & 0 deletions
9
db/migrations/20190716135159-add_flag_column_to_articles_table.js
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,9 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface | ||
.addColumn('Articles', 'flagged', { | ||
type: Sequelize.BOOLEAN, | ||
allowNull: false, | ||
defaultValue: false | ||
}), | ||
down: queryInterface => queryInterface.removeColumn('Articles', 'flagged'), | ||
}; |
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,43 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Reports', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
message: { | ||
type: Sequelize.TEXT | ||
}, | ||
userId: { | ||
allowNull: false, | ||
type: Sequelize.INTEGER, | ||
required: true, | ||
onDelete: 'CASCADE', | ||
references: { | ||
model: 'Users', | ||
key: 'id', | ||
as: 'user', | ||
} | ||
}, | ||
articleId: { | ||
allowNull: false, | ||
type: Sequelize.INTEGER, | ||
onDelete: 'CASCADE', | ||
references: { | ||
model: 'Articles', | ||
key: 'id', | ||
as: 'article', | ||
} | ||
}, | ||
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
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,19 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Report = sequelize.define('Report', { | ||
message: DataTypes.TEXT, | ||
userId: DataTypes.INTEGER, | ||
articleId: DataTypes.INTEGER | ||
}, {}); | ||
Report.associate = (models) => { | ||
Report.belongsTo(models.Article, { | ||
foreignKey: 'articleId', | ||
as: 'articles' | ||
}); | ||
|
||
Report.belongsTo(models.User, { | ||
foreignKey: 'userId', | ||
as: 'reporter' | ||
}); | ||
}; | ||
return Report; | ||
}; |
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
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
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
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,25 @@ | ||
import express from 'express'; | ||
import Middleware from '../../middlewares'; | ||
import Report from '../../controllers/reports'; | ||
import Validation from '../../validators/articles'; | ||
|
||
|
||
const router = express.Router(); | ||
|
||
router.post( | ||
'/', | ||
Middleware.authenticate, | ||
Middleware.isblackListedToken, | ||
Validation.reportArticle, | ||
Report.reportArticle | ||
); | ||
|
||
router.get( | ||
'/articles', | ||
Middleware.authenticate, | ||
Middleware.isblackListedToken, | ||
Middleware.isAdmin, | ||
Report.getReportedArticles | ||
); | ||
|
||
export default router; |
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
Oops, something went wrong.