Skip to content

Commit

Permalink
worked on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
salviosage committed Aug 28, 2019
1 parent 819fc16 commit 001ae6a
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 39 deletions.
18 changes: 7 additions & 11 deletions src/controllers/report.comtroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Report {
static async getAllReport(req, res) {
const counter = await db.count();
if (counter === 0) {
util.setError(200, 'No reports found');
util.setError(404, 'No reports found');
return util.send(res);
}
if (req.offset >= counter) {
Expand Down Expand Up @@ -53,7 +53,7 @@ class Report {
try {
const counter = await db.count({ where: { userId: req.auth.id } });
if (counter === 0) {
util.setError(200, 'No reports found');
util.setError(404, 'No reports found');
return util.send(res);
}
if (req.offset >= counter) {
Expand Down Expand Up @@ -86,7 +86,7 @@ class Report {
try {
const counter = await db.count({ where: { articleSlug: req.params.Article } });
if (counter === 0) {
util.setError(200, 'This article is not yet Reported');
util.setError(404, 'This article is not yet Reported');
return util.send(res);
}
if (req.offset >= counter) {
Expand Down Expand Up @@ -122,16 +122,12 @@ class Report {
where: { id: req.params.reportId }
});
if (!findReport) {
return res.status(200).json({
status: 200,
message: 'That report does not exist!'
});
util.setError(404, 'That report does not exist!');
return util.send(res);
}
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.'
});
util.setError(403, 'Sorry you have no access to delete this Report.');
return util.send(res);
}
await db.destroy({
where: { id: req.params.reportId }
Expand Down
24 changes: 0 additions & 24 deletions src/helpers/pagination.helper.js

This file was deleted.

4 changes: 4 additions & 0 deletions src/middlewares/likes.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export default async (req, res, next) => {
try {
const user = await models.user.findOne({ where: { email: req.auth.email } });
const userId = user.id;
if (!user) {
util.setError(404, 'you are anonimous');
return util.send(res);
}
const ArticleSlug = req.params.Article;
const post = await models.Article.findOne({ where: { slug: ArticleSlug } });

Expand Down
159 changes: 159 additions & 0 deletions src/routes/api/reports/doc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@

/reports/{articleSlug}:
post:
summary: report an article
description: >
report article which is not yours
tags:
- Report
produces:
- application/json
parameters:
- name: x-access-token
in: header
schema:
type: string
required:
- authorization
- name: reason
in: body
description: reason for reporting article
responses:
200:
description: OK
schema:
$ref: '#definitions/Report'
400:
$ref: '#responses/BadRequest'
get:
summary: get reports for a certain article
description: >
admin can get report made for a specific article
tags:
- Report
produces:
- application/json
parameters:
- name: x-access-token
in: header
schema:
type: string
required:
- authorization
responses:
200:
description: Reports for an article retrieved successfully
schema:
$ref: '#definitions/Report'
400:
$ref: '#responses/BadRequest'
/reports/all:
get:
summary: get all reports
description: >
admin can get all reports including pagination for it
tags:
- Report
produces:
- application/json
parameters:
- name: x-access-token
in: header
schema:
type: string
required:
- authorization
responses:
200:
description: Reports retrieved successfully
schema:
$ref: '#definitions/Report'
400:
$ref: '#responses/BadRequest'
/reports:
get:
summary: get your reports
description: >
get all the reports you have made
tags:
- Report
produces:
- application/json
parameters:
- name: x-access-token
in: header
schema:
type: string
required:
- authorization
responses:
200:
description: Your reports retrieved successfully
schema:
$ref: '#definitions/Report'
400:
$ref: '#responses/BadRequest'
/reports/{reportid}:
delete:
summary: delete your report
description: >
user can delete his/her report or admin can
tags:
- Report
produces:
- application/json
parameters:
- name: x-access-token
in: header
schema:
type: string
required:
- authorization
responses:
200:
description: Report successfully deleted
schema:
$ref: '#definitions/Report'
400:
$ref: '#responses/BadRequest'
tags:
- name: Report
description: Operations related to Report
responses:
success:
description: Success
schema:
$ref: '#/definitions/Report'
BadRequest:
description: Bad request
schema:
$ref: '#/definitions/Error'
Notfound:
description: Not found
schema:
$ref: '#/definitions/Error'
definitions:
Report:
type: object
properties:
status:
type: string
description: username
message:
type: string
description: description of data retrieved
data:
type: object
description: data retrieved
required:
- body
- image
Error:
type: object
properties:
status:
type: string
description: status code
message:
type: string
description: description of error
8 changes: 4 additions & 4 deletions test/reporting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe('/Report an article', () => {
.set('Authorization', admintoken)
.end((error, res) => {
expect(res).to.be.an('object');
expect(res.status).to.equal(200);
expect(res.status).to.equal(404);
expect(res.body).to.have.keys('message', 'status');
expect(res.body.message).to.deep.equal('No reports found');
done();
Expand All @@ -131,7 +131,7 @@ describe('/Report an article', () => {
.set('Authorization', admintoken)
.end((error, res) => {
expect(res).to.be.an('object');
expect(res.status).to.equal(200);
expect(res.status).to.equal(404);
expect(res.body).to.have.keys('message', 'status');
expect(res.body.message).to.deep.equal('No reports found');
done();
Expand Down Expand Up @@ -256,7 +256,7 @@ describe('/Report an article', () => {
.set('Authorization', admintoken)
.end((error, res) => {
expect(res).to.be.an('object');
expect(res.status).to.equal(200);
expect(res.status).to.equal(404);
expect(res.body).to.have.keys('message', 'status');
expect(res.body.message).to.deep.equal('That report does not exist!');
done();
Expand Down Expand Up @@ -295,7 +295,7 @@ describe('/Report an article', () => {
.set('Authorization', admintoken)
.end((error, res) => {
expect(res).to.be.an('object');
expect(res.status).to.equal(200);
expect(res.status).to.equal(404);
expect(res.body).to.have.keys('message', 'status');
expect(res.body.message).to.deep.equal('This article is not yet Reported');
done();
Expand Down

0 comments on commit 001ae6a

Please sign in to comment.