-
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.
- Loading branch information
Showing
20 changed files
with
1,009 additions
and
9 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,69 @@ | ||
import Sequelize from 'sequelize'; | ||
import services from '../services'; | ||
import models from '../database/models'; | ||
import helpers from '../helpers'; | ||
|
||
const { Op } = Sequelize; | ||
|
||
const { reportHelper: { checkForBadWords }, responseMessage } = helpers; | ||
|
||
const { Report } = models; | ||
|
||
const { novelServices: { findNovel } } = services; | ||
|
||
const createReport = async (req, res) => { | ||
const { slug } = req.params; | ||
const { type, body } = req.body; | ||
const { user: { id: userId } } = req; | ||
let isConfirmed = false; | ||
|
||
try { | ||
const novel = await findNovel(slug); | ||
if (!novel) { | ||
return responseMessage(res, 404, { error: 'novel not found' }); | ||
} | ||
const { body: novelBody, id: novelId } = novel; | ||
if (type === 'badWords') { | ||
isConfirmed = checkForBadWords(novelBody); | ||
} | ||
Report.create({ | ||
type, body, userId, novelId, isConfirmed | ||
}); | ||
return responseMessage(res, 201, { message: 'report was created successfully' }); | ||
} catch (error) { | ||
responseMessage(res, 500, { error: error.message }); | ||
} | ||
}; | ||
|
||
const getReports = async (req, res) => { | ||
const { isHandled } = req.query; | ||
const reportCondition = isHandled ? { isHandled } : { id: { [Op.ne]: null } }; | ||
try { | ||
const reports = await Report.findAll({ where: reportCondition }); | ||
responseMessage(res, 200, { data: reports }); | ||
} catch (error) { | ||
responseMessage(res, 500, { error: error.message }); | ||
} | ||
}; | ||
|
||
const markAsHandled = async (req, res) => { | ||
const { id } = req.params; | ||
try { | ||
const report = await Report.findByPk(id); | ||
if (!report) { | ||
return responseMessage(res, 400, { error: 'report don\'t exist' }); | ||
} | ||
Report.update({ isHandled: true }, { | ||
where: { id } | ||
}); | ||
return responseMessage(res, 200, { message: 'report was mark as Handled successfully' }); | ||
} catch (error) { | ||
responseMessage(res, 500, { error: error.message }); | ||
} | ||
}; | ||
|
||
export default { | ||
createReport, | ||
getReports, | ||
markAsHandled | ||
}; |
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,50 @@ | ||
|
||
|
||
export const up = (queryInterface, Sequelize) => queryInterface.createTable('Reports', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: Sequelize.UUID, | ||
}, | ||
type: { | ||
type: Sequelize.STRING | ||
}, | ||
body: { | ||
type: Sequelize.STRING | ||
}, | ||
userId: { | ||
type: Sequelize.UUID, | ||
onDelete: 'CASCADE', | ||
allowNull: false, | ||
references: { | ||
model: 'Users', | ||
key: 'id' | ||
}, | ||
}, | ||
novelId: { | ||
type: Sequelize.UUID, | ||
onDelete: 'CASCADE', | ||
allowNull: false, | ||
references: { | ||
model: 'Novels', | ||
key: 'id' | ||
}, | ||
}, | ||
isConfirmed: { | ||
type: Sequelize.BOOLEAN, | ||
defaultValue: false | ||
}, | ||
isHandled: { | ||
type: Sequelize.BOOLEAN, | ||
defaultValue: false | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
export const down = queryInterface => queryInterface.dropTable('Reports'); |
13 changes: 13 additions & 0 deletions
13
src/database/migrations/20190814224806-add-column-to-novel.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,13 @@ | ||
export const up = (queryInterface, Sequelize) => queryInterface.addColumn( | ||
'Novels', | ||
'isBanned', | ||
{ | ||
type: Sequelize.BOOLEAN, | ||
defaultValue: false | ||
} | ||
); | ||
|
||
export const down = queryInterface => queryInterface.removeColumn( | ||
'Novels', | ||
'isBanned', | ||
); |
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,35 @@ | ||
export default (sequelize, DataTypes) => { | ||
const Report = sequelize.define('Report', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: DataTypes.UUID, | ||
defaultValue: DataTypes.UUIDV4, | ||
}, | ||
userId: { | ||
type: DataTypes.UUID, | ||
onDelete: 'CASCADE', | ||
allowNull: false | ||
}, | ||
novelId: { | ||
type: DataTypes.UUID, | ||
onDelete: 'CASCADE', | ||
allowNull: false | ||
}, | ||
type: DataTypes.STRING, | ||
body: DataTypes.STRING, | ||
isConfirmed: DataTypes.BOOLEAN, | ||
isHandled: DataTypes.BOOLEAN | ||
}, {}); | ||
Report.associate = (models) => { | ||
Report.belongsTo(models.User, { | ||
foreignKey: 'userId', | ||
onDelete: 'CASCADE', | ||
}); | ||
Report.belongsTo(models.Novel, { | ||
foreignKey: 'novelId', | ||
onDelete: 'CASCADE', | ||
}); | ||
}; | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const up = queryInterface => queryInterface.bulkInsert('Reports', [{ | ||
id: '67a68419-405a-45e6-8d4f-cb00cbff7a64', | ||
userId: '122a0d86-8b78-4bb8-b28f-8e5f7811c456', | ||
novelId: '7f45df6d-7003-424f-86ec-1e2b36e2fd14', | ||
type: 'general', | ||
body: 'i hate this novel', | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}, | ||
], {}); | ||
|
||
export const down = queryInterface => queryInterface.bulkDelete('Reports', null, {}); |
Oops, something went wrong.