Skip to content

Commit

Permalink
feature(package.lock):deleted[finished:167313424] (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
nshutijonathan authored and Mnickii committed Aug 29, 2019
1 parent c0e940d commit cca1648
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ typings/

#ignore vscode settings file
.vscode/settings.json

#ignore DS_Store
.DS_Store

Expand Down
64 changes: 26 additions & 38 deletions src/controllers/comments.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import commentsService from '../services/comments.service';
import UserService from '../services/user.service';
import models from '../models';
import NotificationServices from '../services/notification.service';
import Util from '../helpers/util';

const util = new Util();

const { notifyUsersWhoFavorited } = NotificationServices;

Expand Down Expand Up @@ -39,11 +42,8 @@ class Comments {
};
const createdComment = await commentsService.addComment(comment);
await notifyUsersWhoFavorited(req, res, getArticle.id, req.params.slug);
return res.status(201).send({
status: 201,
message: 'Comment successfully created',
comment: createdComment
});
await util.setSuccess(201, 'Comment successfully created', createdComment);
return util.send(res);
} catch (error) {
return res.send({
message: error.message
Expand All @@ -67,24 +67,18 @@ class Comments {
const { id } = req.params;

if (!Number(id)) {
return res.status(400).send({
status: 400,
message: 'Please provide numeric value'
});
await util.setError(400, 'Please provide numeric value');
return util.send(res);
}
if (!(req.auth.id === commentAuthor)) {
return res.status(403).send({
status: 403,
message: 'comment is not yours'
});
await util.setError(403, 'comment is not yours');
return util.send(res);
}
try {
const CommentTODelete = await commentsService.deleteComment(id);
if (CommentTODelete) {
return res.status(200).send({
status: 200,
message: `Comment with id ${id} is successfully deleted`
});
await util.setSuccess(200, `Comment with id ${id} is successfully deleted`);
return util.send(res);
}

return res.status(404).send({
Expand All @@ -110,15 +104,11 @@ class Comments {
static async getComments(req, res) {
const comments = await CommentsDb.findAll();
if (!comments) {
return res.status(200).send({
message: 'No comments found'
});
await util.setError(200, 'No comments found');
return util.send(res);
}
return res.status(200).send({
status: 200,
message: 'All comments successfully retrieved',
comments
});
await util.setSuccess(200, 'All comments successfully retrieved', comments);
return util.send(res);
}

/**
Expand All @@ -134,23 +124,21 @@ class Comments {
const getComment = await CommentsDb.findOne({ where: { id: req.params.id } });
const gottenComent = getComment && getComment.get().id;
const { id } = req.params;
if (!id) {
return res.status(400).send({
status: 400,
message: 'Please provide valid numeric value'
});
}

if (!gottenComent) {
return res.status(200).json({ status: 200, message: 'That comment does not exist' });
await util.setSuccess(200, 'That comment does not exist');
return util.send(res);
}
if (!Number(id)) {
await util.setError(400, 'Please provide numeric value');
return util.send(res);
}

const { body } = req.body;
const updateComment = await commentsService.updateComment(req.params.id, { body });
return res.status(200).send({
status: 200,
message: 'Updation is successfully',
updateComment
});
const commentRevisions = getComment.dataValues.body;
const updateComment = await commentsService.updateComment(req.params.id, { body, commentRevisions });
await util.setSuccess(200, 'Update is successfully', updateComment);
return util.send(res);
}
}
export default Comments;
7 changes: 7 additions & 0 deletions src/migrations/20190816101310-create-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ module.exports = {
body: {
type: Sequelize.STRING
},
commentRevisions: {
type: Sequelize.STRING,
allowNull: true
},
parentCommentId: {
type: Sequelize.INTEGER,
allowNull: true
Expand All @@ -33,7 +37,10 @@ module.exports = {
}
}) .then(() => sequelize.query(`ALTER TABLE "Comments"
ADD CONSTRAINT fk_parentReference FOREIGN KEY ("parentCommentId")
REFERENCES "Comments" (id) ON DELETE CASCADE`),(`ALTER TABLE "Comments"
ADD CONSTRAINT fk_parentReference FOREIGN KEY ("commentRevisions")
REFERENCES "Comments" (id) ON DELETE CASCADE`));

},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Comments');
Expand Down
13 changes: 12 additions & 1 deletion src/models/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,22 @@ module.exports = (sequelize, DataTypes) => {
onUpdate: 'CASCADE',
onDelete: 'CASCADE'
},
commentRevisions:{
type: DataTypes.STRING,
allowNull: true,
references: {
model: 'Comment',
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE'

},
parentCommentId: {
type: DataTypes.INTEGER,
allowNull: true
},
body: DataTypes.STRING
body: DataTypes.STRING,
}, {});
Comment.associate = function(models) {
// associations can be defined here
Expand Down
65 changes: 65 additions & 0 deletions test/comments.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { chai, server, expect } from './test-setup';
import Helper from '../src/helpers/helper';

const adminToken = Helper.generateToken({
id: 1,
email: 'admin@gmail.com',
username: 'admin',
verified: true,
});
const FakeToken = Helper.generateToken({
id: 1,
email: '',
username: 'admin',
verified: true,
});
describe('Comments', () => {
it('should retrieve all comments', (done) => {
chai
.request(server)
.get('/api/v1/comments')
.set('x-access-token', adminToken)
.end((error, res) => {
expect(res.status).to.be.equal(200);
expect(res.body).to.have.deep.property('message');
done();
});
});
it('should not retrieve all comments', (done) => {
chai
.request(server)
.get('/api/v1/comments')
.set('x-access-token', FakeToken)
.end((error, res) => {
expect(res.status).to.be.equal(200);
expect(res.body).to.have.deep.property('message');
done();
});
});
it('should return resource not found message if endpoint does not exist', (done) => {
chai
.request(server)
.get('/api/v1/commentss')
.set('x-access-token', adminToken)
.end((error, res) => {
expect(res.status).to.be.equal(404);
expect(res.body).to.have.deep.property('error', 'Resource not found');
done();
});
});
it('should comment on article', (done) => {
chai
.request(server)
.post('/api/v1/comments/fakeslug')
.send({
body: 'I commented',
})
.set('x-access-token', adminToken)
.end((error, res) => {
expect(res.status).to.be.equal(201);
expect(res.body).to.have.deep.property('message');
expect(res.body).to.have.deep.property('data');
done();
});
});
});
File renamed without changes.

0 comments on commit cca1648

Please sign in to comment.