Skip to content

Commit

Permalink
Merge d8fdf32 into 6589e7a
Browse files Browse the repository at this point in the history
  • Loading branch information
salviosage committed Aug 27, 2019
2 parents 6589e7a + d8fdf32 commit 137d5e5
Show file tree
Hide file tree
Showing 15 changed files with 946 additions and 23 deletions.
20 changes: 3 additions & 17 deletions src/controllers/articles.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,10 @@ class Articles {
*/
static async getAllArticles(req, res) {
const counter = await db.count();
let page = parseInt(req.query.page, 10);
if (isNaN(page) || page < 1) {
page = 1;
if (req.offset >= counter) {
req.offset = 0;
}
let limit = parseInt(req.query.limit, 10);
if (isNaN(limit)) {
limit = 10;
} else if (limit > 50) {
limit = 50;
} else if (limit < 1) {
limit = 1;
}
let offset = (page - 1) * limit;
if (offset >= counter) {
offset = 0;
}

const { searchQueries } = req;
const { searchQueries, offset, limit } = req;
const articles = await articleService.getAllArticles(offset, limit, searchQueries);
if (!articles) {
return res.status(200).json({ status: 200, message: 'There is no article.' });
Expand Down
178 changes: 178 additions & 0 deletions src/controllers/report.comtroller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import 'dotenv/config';
import models from '../models';
import reportService from '../services/report.service';
import Util from '../helpers/util';

const util = new Util();

const db = models.reporting;
/**
*
*
* @class Report
*/
class Report {
/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} Reports
* @memberof Report
*/
static async getAllReport(req, res) {
const counter = await db.count();
if (counter === 0) {
util.setError(200, 'No reports found');
return util.send(res);
}
if (req.offset >= counter) {
req.offset = 0;
}
const { offset, limit } = req;
const reports = await reportService.getAllReport(offset, limit);
const response = {
count: counter,
reports,
};
util.setSuccess(200, 'Reports retrieved successfully', response);
return util.send(res);
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} Reports
* @memberof Report
*/
static async getMyReport(req, res) {
try {
const counter = await db.count({ where: { userId: req.auth.id } });
if (counter === 0) {
util.setError(200, 'No reports found');
return util.send(res);
}
if (req.offset >= counter) {
req.offset = 0;
}
const { offset, limit } = req;
const reports = await reportService.getMyReport(offset, limit, req.auth.id,);
const response = {
count: counter,
yourReport: reports,
};
util.setSuccess(200, 'Your reports retrieved successfully', response);
return util.send(res);
} catch (error) {
util.setError(500, 'server error contact admin');
return util.send(res);
}
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} Report for an article
* @memberof Report
*/
static async getReportsForArticle(req, res) {
try {
const counter = await db.count({ where: { articleSlug: req.params.Article } });
if (counter === 0) {
util.setError(200, 'This article is not yet Reported');
return util.send(res);
}
if (req.offset >= counter) {
req.offset = 0;
}
const { offset, limit } = req;
const articleSlug = req.params.Article;
const reports = await reportService.getAllReportForOneArticle(offset, limit, articleSlug);
const response = {
count: counter,
reports,
};
util.setSuccess(200, 'Reports retrieved successfully', response);
return util.send(res);
} catch (error) {
util.setError(500, 'server error contact admin');
return util.send(res);
}
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {object} delete your report
* @memberof Report
*/
static async deleteReport(req, res) {
try {
const findReport = await db.findOne({
where: { id: req.params.reportId }
});
if (!findReport) {
return res.status(200).json({
status: 200,
message: 'That report does not exist!'
});
}
if (req.auth.id !== findReport.userId && req.auth.role !== 'admin') {
return res.status(403).json({
status: 403,
message: 'Sorry you have no access to delete this Report.'
});
}
await db.destroy({
where: { id: req.params.reportId }
});
util.setSuccess(200, 'Report deleted succussfully!');
return util.send(res);
} catch (error) {
util.setError(500, 'server error contact admin');
return util.send(res);
}
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {Object} Report an article
* @memberof Report
*/
static async reportArticle(req, res) {
try {
const report = {
reason: req.body.reason,
userId: req.auth.id,
articleSlug: req.params.Article
};
const reportArticle = await reportService.report(report);
const newReport = {
reason: reportArticle.reason,
articleSlug: reportArticle.articleSlug,
id: reportArticle.id
};
util.setSuccess(200, 'Article Successfully reported', newReport);
return util.send(res);
} catch (error) {
util.setError(500, 'server error contact admin');
return util.send(res);
}
}
}

export default Report;
24 changes: 24 additions & 0 deletions src/helpers/pagination.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@


export default async (page, limit, counter) => {
let pageN = parseInt(page, 10);
if (isNaN(pageN) || page < 1) {
pageN = 1;
}
let limitS = parseInt(limit, 10);
if (isNaN(limitS)) {
limitS = 10;
} else if (limitS > 50) {
limitS = 50;
} else if (limitS < 1) {
limitS = 1;
}
let offset = (page - 1) * limit;
if (offset >= counter) {
offset = 0;
}
return {
limit,
offset
};
};
4 changes: 0 additions & 4 deletions src/middlewares/likes.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ const util = new Util();
export default async (req, res, next) => {
try {
const user = await models.user.findOne({ where: { email: req.auth.email } });
if (!user) {
util.setError(404, 'you are anonimous');
return util.send(res);
}
const userId = user.id;
const ArticleSlug = req.params.Article;
const post = await models.Article.findOne({ where: { slug: ArticleSlug } });
Expand Down
19 changes: 17 additions & 2 deletions src/middlewares/query.check.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export const searchArticleQuerybuilder = (searchQueries) => {
};

export const checkQuery = (req, res, next) => {
const {
limit, offset, page, ...searchQueries
let {
limit, page, ...searchQueries
} = req.query;
const validQueries = ['author', 'keyword', 'tag', 'title'];
let isValidRequest = true;
Expand All @@ -43,9 +43,24 @@ export const checkQuery = (req, res, next) => {
}
});
}
page = parseInt(page, 10);
if (isNaN(page) || page < 1) {
page = 1;
}
limit = parseInt(limit, 10);
if (isNaN(limit)) {
limit = 10;
} else if (limit > 50) {
limit = 50;
} else if (limit < 1) {
limit = 1;
}
const offset = (page - 1) * limit;

if (isValidRequest) {
req.searchQueries = searchArticleQuerybuilder(searchQueries);
req.limit = limit;
req.offset = offset;
return next();
}
return res.status(400).json({
Expand Down
32 changes: 32 additions & 0 deletions src/middlewares/report.middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Util from '../helpers/util';
import models from '../models/index';

const util = new Util();

export default async (req, res, next) => {
try {
const ArticleSlug = req.params.Article;
const post = await models.Article.findOne({ where: { slug: ArticleSlug } });

if (!post) {
util.setError(404, 'post not found');
return util.send(res);
}
const user = await models.user.findOne({ where: { email: req.auth.email } });
const userId = user.id;
if (userId === post.authorId) {
util.setError(403, 'Sorry you can not report your article.');
return util.send(res);
}

const Report = await models.reporting.findOne({ where: { articleSlug: ArticleSlug, userId } });
if (Report) {
util.setError(403, 'You arleady Reported this article.');
return util.send(res);
}
next();
} catch (error) {
util.setError(500, 'server error contact admin');
return util.send(res);
}
};
21 changes: 21 additions & 0 deletions src/middlewares/validators/report.validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Joi from 'joi';
import Util from '../../helpers/util';

const util = new Util();

export default (req, res, next) => {
const { reason } = req.body;

const schema = {
reason: Joi.string().valid('Rules Violation', 'Spam', 'Harassment').required(),
};
const { error } = Joi.validate({
reason
}, schema);

if (!error) return next();
const errorMessageFromJoi = error.details[0].message;
const removeEscapeCharacters = errorMessageFromJoi.replace(/['"]+/g, '');
util.setError(400, removeEscapeCharacters);
return util.send(res);
};
47 changes: 47 additions & 0 deletions src/migrations/20190823185735-create-reporting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('reportings', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
userId: {
type: Sequelize.INTEGER,
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
references: {
model: 'users',
key: 'id'
}
},
articleSlug: {
type: Sequelize.STRING,
allowNull: false,
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
references: {
model: 'Articles',
key: 'slug'
}
},
reason: {
type: Sequelize.ENUM,
values: ['Rules Violation', 'Spam', 'Harassment'],
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('reportings');
}
};
Loading

0 comments on commit 137d5e5

Please sign in to comment.