Skip to content

Commit

Permalink
Merge e8a7b36 into 0e6cd2a
Browse files Browse the repository at this point in the history
  • Loading branch information
julietezekwe committed Jan 23, 2019
2 parents 0e6cd2a + e8a7b36 commit 09be4b4
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 3 deletions.
101 changes: 100 additions & 1 deletion src/controllers/CommentsController.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import models from '../db/models';
import Response from '../helpers/response';

const { Comment, Art } = models;
const { Comment, Art, UpdatedComment } = models;

/**
* Represents a CommentsController.
Expand Down Expand Up @@ -52,6 +52,105 @@ class CommentsController {
return res.status(response.code).json(response);
}
}

/**
* @static
* @param {Object} req
* @param {object} res
* @return {object} updated comment
*/
static async updateComment(req, res) {
try {
const { body } = req.body;
const { commentId } = req.params;
const { id } = req.verifyUser;
const findComment = await Comment.find({
where: {
id: commentId
}
});
if (findComment) {
if (findComment.userId === id) {
await UpdatedComment.create({
body: findComment.body,
commentId
});
await Comment.update({
body
},
{
where: {
id: commentId
}
});
const response = new Response(
'Ok',
200,
'Comment updated successfully',
);
return res.status(response.code).json(response);
}
const response = new Response(
'Unauthorized',
401,
'You are not authorized to update this comment',
);
return res.status(response.code).json(response);
}
const response = new Response(
'Not Found',
404,
'This comment does not exist',
);
return res.status(response.code).json(response);
} catch (err) {
const response = new Response(
'Not ok',
500,
`${err}`,
);
return res.status(response.code).json(response);
}
}

/**
* @static
* @param {Object} req
* @param {object} res
* @return {object} Edit history comment
*/
static async getEditHistory(req, res) {
try {
const { commentId } = req.params;
const getUpdatedComments = await UpdatedComment.findAll({
where: {
commentId,
}
});
if (getUpdatedComments.length < 1) {
const response = new Response(
'Not found',
404,
'This comment has not been edited',
);
return res.status(response.code).json(response);
}
const response = new Response(
'Ok',
200,
'Successfully retrieved updated comment history',
getUpdatedComments
);
return res.status(response.code).json(response);
} catch (err) {
const response = new Response(
'Not ok',
500,
`${err}`,
);
return res.status(response.code).json(response);
}
}
}

export default CommentsController;
26 changes: 26 additions & 0 deletions src/db/seeders/d_commentSeeder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
up: queryInterface => queryInterface.bulkInsert('Comments', [{
artId: 1,
body: 'Comment for user 1, art 1',
userId: 4,
createdAt: new Date(),
updatedAt: new Date()
},
{
artId: 2,
body: 'Comment for user 1, art 2',
userId: 4,
createdAt: new Date(),
updatedAt: new Date()
},
{
artId: 2,
body: 'Comment for user 2, art 2',
userId: 2,
createdAt: new Date(),
updatedAt: new Date()
}
], {}),

down: queryInterface => queryInterface.bulkDelete('Comments', null, {})
};
23 changes: 23 additions & 0 deletions src/db/seeders/e_updatedCommentSeeder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = {
up: queryInterface => queryInterface.bulkInsert('UpdatedComments', [{
commentId: 1,
body: 'Comment for user 1, art 1',
createdAt: new Date(),
updatedAt: new Date()
},
{
commentId: 1,
body: 'Comment for user 1, art 1',
createdAt: new Date(),
updatedAt: new Date()
},
{
commentId: 2,
body: 'Comment for user 1, art 1',
createdAt: new Date(),
updatedAt: new Date()
}
], {}),

down: queryInterface => queryInterface.bulkDelete('UpdatedComments', null, {})
};
3 changes: 2 additions & 1 deletion src/middlewares/ParamsChecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ParamsChecker {
* @returns {function} next
*/
static idChecker(req, res, next) {
const { userId, artId } = req.params;
const { userId, artId, commentId } = req.params;
const validId = /^[0-9]+$/;

const checkParam = (param) => {
Expand All @@ -28,6 +28,7 @@ class ParamsChecker {
};
if (userId) checkParam(userId);
if (artId) checkParam(artId);
if (commentId) checkParam(commentId);
}
}

Expand Down
16 changes: 15 additions & 1 deletion src/routes/commentRouter.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import express from 'express';
import TokenAuthenticate from '../helpers/TokenAuthenticate';
import ParamsChecker from '../middlewares/ParamsChecker';
import CommentsController from '../controllers/CommentsController';
import CommentValidator from '../middlewares/CommentValidator';
import TokenAuthenticate from '../helpers/TokenAuthenticate';

const commentRouter = express.Router();

Expand All @@ -14,5 +14,19 @@ commentRouter
CommentValidator.createCommentValidator,
CommentsController.createComment
);
commentRouter
.put(
'/:commentId',
TokenAuthenticate.tokenVerify,
ParamsChecker.idChecker,
CommentValidator.createCommentValidator,
CommentsController.updateComment
);
commentRouter
.get(
'/:commentId/history',
ParamsChecker.idChecker,
CommentsController.getEditHistory
);

export default commentRouter;
51 changes: 51 additions & 0 deletions test/controllers/commentControllerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,56 @@ describe('Comments Endpoint API Test', () => {
done(err);
});
});
it('it should update comment that exists', (done) => {
chai.request(app)
.put('/api/v1/arts/comments/1')
.set('Authorization', userToken)
.send(validComment)
.end((err, res) => {
expect(res.body.messages).eql('Comment updated successfully');
expect(res.status).to.equal(200);
done(err);
});
});
it('it should not update comment that does not exists', (done) => {
chai.request(app)
.put('/api/v1/arts/comments/100')
.set('Authorization', userToken)
.send(validComment)
.end((err, res) => {
expect(res.body.messages).eql('This comment does not exist');
expect(res.status).to.equal(404);
done(err);
});
});
it('it should allow only comment creator to update the comment ', (done) => {
chai.request(app)
.put('/api/v1/arts/comments/3')
.set('Authorization', userToken)
.send(validComment)
.end((err, res) => {
expect(res.body.messages).eql('You are not authorized to update this comment');
expect(res.status).to.equal(401);
done(err);
});
});
it('it should get updated comment history', (done) => {
chai.request(app)
.get('/api/v1/arts/comments/1/history')
.end((err, res) => {
expect(res.body.messages).eql('Successfully retrieved updated comment history');
expect(res.status).to.equal(200);
done(err);
});
});
it('it should not get updated comment history for comment that has not been edited', (done) => {
chai.request(app)
.get('/api/v1/arts/comments/100/history')
.end((err, res) => {
expect(res.body.messages).eql('This comment has not been edited');
expect(res.status).to.equal(404);
done(err);
});
});
});
});

0 comments on commit 09be4b4

Please sign in to comment.