Skip to content

Commit

Permalink
Merge 63f87a7 into 5f1f18c
Browse files Browse the repository at this point in the history
  • Loading branch information
benisi committed Aug 23, 2019
2 parents 5f1f18c + 63f87a7 commit 6d6bdc8
Show file tree
Hide file tree
Showing 20 changed files with 1,009 additions and 9 deletions.
69 changes: 69 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,75 @@ paths:
error:
type: string
example: no comment found with the provided id
/novels/{slug}/report:
post:
summary: Report a novel
description: Replort a novel that violates the terms and condition of the house
requestBody:
content:
application/json:
schema:
type: object
properties:
type:
type: string
body:
type: string
parameters:
- name: slug
in: path
required: true
schema:
type: string
responses:
201:
description: succesfully reported an article
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: report was created successfully
400:
description: bad request
content:
application/json:
schema:
type: object
properties:
errors:
type: array
items:
type: object
properties:
field:
type: string
example: body
message:
type: string
example: body cannot be empty
401:
description: unauthorized access
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: invalid token
404:
description: entity not found
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: novel not found
'/profiles':
patch:
tags:
Expand Down
69 changes: 69 additions & 0 deletions src/controllers/ReportController.js
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
};
50 changes: 50 additions & 0 deletions src/database/migrations/20190814223425-create-report.js
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 src/database/migrations/20190814224806-add-column-to-novel.js
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',
);
5 changes: 5 additions & 0 deletions src/database/models/novel.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export default (Sequelize, DataTypes) => {
allowNull: false,
type: DataTypes.TEXT
},
isBanned: {
allowNull: false,
type: DataTypes.BOOLEAN,
defaultValue: false
},
createdAt: {
allowNull: false,
type: DataTypes.DATE
Expand Down
35 changes: 35 additions & 0 deletions src/database/models/report.js
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;
};
2 changes: 1 addition & 1 deletion src/database/seeders/20190724090406-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const up = queryInterface => queryInterface.bulkInsert('Users', [{
phoneNo: null,
isVerified: true,
isSubscribed: true,
roleId: 'f2dec928-1ff9-421a-b77e-8998c8e2e720',
roleId: '2c4dfb3f-1798-43d4-8eb6-1c125994a263',
createdAt: new Date(),
updatedAt: new Date()
},
Expand Down
12 changes: 12 additions & 0 deletions src/database/seeders/20190815133001-report.js
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, {});
Loading

0 comments on commit 6d6bdc8

Please sign in to comment.