Skip to content

Commit

Permalink
feat(comment): implement slug on route
Browse files Browse the repository at this point in the history
  • Loading branch information
tejiri4 committed Jan 21, 2019
1 parent 71dda05 commit 0dd73bc
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 100 deletions.
62 changes: 13 additions & 49 deletions server/controllers/CommentController.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,28 @@ class CommentController {
try {
const { userId } = req.user;
const { content } = req.body;
const { slug: articleId } = req.params;
const { slug } = req.params;
if (!content) {
return response(res, 400, 'failure', 'Enter a comment', null, null);
}
const articleFound = await Article.findOne({
where: {
id: articleId
slug
}
});
if (articleFound) {
const commentCreated = await Comment.create({
content,
userId,
articleId
articleId: slug
});
if (commentCreated) {
return response(res, 201, 'success', 'Comment created', null, null);
}
}
} catch (error) {
if (error.name === 'SequelizeDatabaseError') {
} else {
return response(res, 404, 'failure', 'Article with the id not found', null, null);
}
} catch (error) {
return response(
res, 500, 'failure',
'server error',
Expand All @@ -62,54 +61,19 @@ class CommentController {
*/
static async getComments(req, res) {
try {
const { slug: articleId } = req.params;
const { slug } = req.params;
const commentsQuery = await Comment.findAll({
where: {
articleId
articleId: slug
}
});
if (commentsQuery) {
const comments = [];
commentsQuery.map(comment => comments.push(comment.dataValues));
return response(res, 200, 'success', 'Comment found', null, comments);
}
} catch (error) {
if (error.name === 'SequelizeDatabaseError') {
if (commentsQuery.length === 0) {
return response(res, 404, 'failure', 'Comment with the articleId not found', null, null);
}
return response(
res, 500, 'failure',
'server error',
{ message: 'Something went wrong on the server' }, null
);
}
}

/**
*
* @description Retrive a comment for an article
* @static
* @param {*} req Express Request object
* @param {*} res Express Response object
* @returns {object} Json response
* @memberof CommentController
*/
static async getAComment(req, res) {
try {
const { slug: articleId, commentId } = req.params;
const commentQuery = await Comment.findOne({
where: {
articleId,
id: commentId
}
});
if (commentQuery) {
return response(res, 200, 'success', 'Comment found', null, commentQuery);
}
const comments = [];
commentsQuery.map(comment => comments.push(comment.dataValues));
return response(res, 200, 'success', 'Comment found', null, comments);
} catch (error) {
if (error.name === 'SequelizeDatabaseError') {
return response(res, 404, 'failure', 'Comment with the commentId not found', null, null);
}
return response(
res, 500, 'failure',
'server error',
Expand All @@ -130,14 +94,14 @@ class CommentController {
static async updateComment(req, res) {
try {
const { userId } = req.user;
const { slug: articleId, commentId } = req.params;
const { slug, commentId } = req.params;
const { content } = req.body;
const getCommentUpdate = await Comment.findOne({
where: {
id: commentId
}
});
if (getCommentUpdate.dataValues.articleId !== articleId) {
if (getCommentUpdate.dataValues.articleId !== slug) {
return response(res, 404, 'failure', 'Comment not found for article id', null, null);
}
if (getCommentUpdate.dataValues.userId !== userId) {
Expand Down
2 changes: 1 addition & 1 deletion server/migrations/20190111130326-create-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default {
allowNull: false
},
articleId: {
type: Sequelize.UUID,
type: Sequelize.STRING,
allowNull: false
},
createdAt: {
Expand Down
14 changes: 0 additions & 14 deletions server/migrations/20190115104506-add-userId-constraint-Comments.js

This file was deleted.

1 change: 0 additions & 1 deletion server/routes/api/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const commentRoutes = Router();

commentRoutes.post('/articles/:slug/comments', AuthMiddleware.checkIfUserIsAuthenticated, ContentValidate, CommentController.addComment);
commentRoutes.get('/articles/:slug/comments', AuthMiddleware.checkIfUserIsAuthenticated, CommentController.getComments);
commentRoutes.get('/articles/:slug/comments/:commentId', AuthMiddleware.checkIfUserIsAuthenticated, CommentController.getAComment);
commentRoutes.put('/articles/:slug/comments/:commentId', AuthMiddleware.checkIfUserIsAuthenticated, CommentController.updateComment);
commentRoutes.delete('/articles/:slug/comments/:commentId', AuthMiddleware.checkIfUserIsAuthenticated, CommentController.deleteComment);

Expand Down
2 changes: 1 addition & 1 deletion server/seeders/20190115151322-demoArticle.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default {
up: (queryInterface, Sequelize) => queryInterface.bulkInsert('Articles', [{
id: '95745c60-7b1a-11e8-9c9c-2d42b21b1a3e',
slug: 'What-a-mighty-God',
slug: 'dhdhdhhdhdhhdhdk',
title: 'Mighty God',
content: 'Hallelujah',
banner: 'banner_url',
Expand Down
2 changes: 1 addition & 1 deletion server/seeders/20190119190506-demoComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default {
id: '09543c60-7b1a-11e8-9c9c-2d42b21b1a3e',
content: 'Hallelujah',
userId: '45745c60-7b1a-11e8-9c9c-2d42b21b1a3e',
articleId: '95745c60-7b1a-11e8-9c9c-2d42b21b1a3e'
articleId: 'dhdhdhhdhdhhdhdk'
}], {}),

down: (queryInterface, Sequelize) => queryInterface.bulkDelete('Comments', null, {})
Expand Down
49 changes: 16 additions & 33 deletions server/test/controllers/comment.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Comment Model', () => {
it('It should be able to create a comment', async () => {
const response = await chai
.request(app)
.post('/api/v1/articles/95745c60-7b1a-11e8-9c9c-2d42b21b1a3e/comments')
.post('/api/v1/articles/dhdhdhhdhdhhdhdk/comments')
.set('Authorization', `Bearer ${token}`)
.send({
content: 'Nice write up'
Expand All @@ -26,7 +26,7 @@ describe('Comment Model', () => {
it('It should not be able to create a comment when content is empty', async () => {
const response = await chai
.request(app)
.post('/api/v1/articles/95745c60-7b1a-11e8-9c9c-2d42b21b1a3e/comments')
.post('/api/v1/articles/dhdhdhhdhdhhdhdk/comments')
.set('Authorization', `Bearer ${token}`)
.send({
content: ''
Expand All @@ -41,7 +41,7 @@ describe('Comment Model', () => {

const response = await chai
.request(app)
.post('/api/v1/articles/95745c60-7b1a-11e8-9c9c-2d42b21b1a3e/comments')
.post('/api/v1/articles/dhdhdhhdhdhhdhdk/comments')
.set('Authorization', `Bearer ${token}`)
.send({
content: 'Nice write up'
Expand All @@ -52,7 +52,7 @@ describe('Comment Model', () => {
it('It should return an error with incorrect token', async () => {
const response = await chai
.request(app)
.post('/api/v1/articles/95745c60-7b1a-11e8-9c9c-2d42b21b1a3e/comments')
.post('/api/v1/articles/dhdhdhhdhdhhdhdk/comments')
.send({
content: 'Nice write up'
});
Expand All @@ -63,7 +63,7 @@ describe('Comment Model', () => {
const expiredToken = 'jdjdjdjdjdj';
const response = await chai
.request(app)
.post('/api/v1/articles/95745c60-7b1a-11e8-9c9c-2d42b21b1a3e/comments')
.post('/api/v1/articles/dhdhdhhdhdhhdhdk/comments')
.set('Authorization', `Bearer ${expiredToken}`)
.send({
content: 'Nice write up'
Expand All @@ -76,33 +76,16 @@ describe('Comment Model', () => {
it('It should be able to get all comments', async () => {
const response = await chai
.request(app)
.get('/api/v1/articles/95745c60-7b1a-11e8-9c9c-2d42b21b1a3e/comments')
.get('/api/v1/articles/dhdhdhhdhdhhdhdk/comments')
.set('Authorization', `Bearer ${token}`);
expect(response.status).to.eqls(200);
expect(response.body.status).to.eqls('success');
expect(response.body.data.message).to.eqls('Comment found');
});
it('It should be able to get a single comment', async () => {
const response = await chai
.request(app)
.get('/api/v1/articles/95745c60-7b1a-11e8-9c9c-2d42b21b1a3e/comments/09543c60-7b1a-11e8-9c9c-2d42b21b1a3e')
.set('Authorization', `Bearer ${token}`);
expect(response.status).to.eqls(200);
expect(response.body.status).to.eqls('success');
expect(response.body.data.message).to.eqls('Comment found');
});
it('It should throw an error when a single comment is not found', async () => {
const response = await chai
.request(app)
.get('/api/v1/articles/95745c60-7b1a-11e8-9c9c-2d42b21b1a3e/comments/0953c60-7b1a-11e8-9c9c-2d42b21b1a3e')
.set('Authorization', `Bearer ${token}`);
expect(response.status).to.eqls(404);
expect(response.body.status).to.eqls('failure');
});
it('It should throw an error when article id is not found', async () => {
const response = await chai
.request(app)
.get('/api/v1/articles/95745c60-7b1a-11e8-9c9c-2d42b21b1a/comments')
.get('/api/v1/articles/dhdhdhhdhdhhdhd/comments')
.set('Authorization', `Bearer ${token}`);
expect(response.status).to.eqls(404);
expect(response.body.status).to.eqls('failure');
Expand All @@ -114,7 +97,7 @@ describe('Comment Model', () => {

const response = await chai
.request(app)
.post('/api/v1/articles/95745c60-7b1a-11e8-9c9c-2d42b21b1a3e/comments')
.post('/api/v1/articles/dhdhdhhdhdhhdhdk/comments')
.set('Authorization', `Bearer ${token}`)
.send({
content: 'Nice write up'
Expand All @@ -127,7 +110,7 @@ describe('Comment Model', () => {
it('It should not be able to update a comment created by another user', async () => {
const response = await chai
.request(app)
.put('/api/v1/articles/95745c60-7b1a-11e8-9c9c-2d42b21b1a3e/comments/09543c60-7b1a-11e8-9c9c-2d42b21b1a3e')
.put('/api/v1/articles/dhdhdhhdhdhhdhdk/comments/09543c60-7b1a-11e8-9c9c-2d42b21b1a3e')
.set('Authorization', `Bearer ${tokenTwo}`);
expect(response.status).to.eqls(401);
expect(response.body.status).to.eqls('failure');
Expand All @@ -136,7 +119,7 @@ describe('Comment Model', () => {
it('It should be able to update a comment created by only a user', async () => {
const response = await chai
.request(app)
.put('/api/v1/articles/95745c60-7b1a-11e8-9c9c-2d42b21b1a3e/comments/09543c60-7b1a-11e8-9c9c-2d42b21b1a3e')
.put('/api/v1/articles/dhdhdhhdhdhhdhdk/comments/09543c60-7b1a-11e8-9c9c-2d42b21b1a3e')
.set('Authorization', `Bearer ${token}`)
.send({
content: 'Nice write up'
Expand All @@ -148,7 +131,7 @@ describe('Comment Model', () => {
it('It should throw an error when you want to update a comment when comment id is wrong', async () => {
const response = await chai
.request(app)
.put('/api/v1/articles/95745c60-7b1a-11e8-9c9c-2d42b21b1a3e/comments/09543c60-7b1a-11e8-9c9c-2d42b21b1')
.put('/api/v1/articles/dhdhdhhdhdhhdhdk/comments/09543c60-7b1a-11e8-9c9c-2d42b21')
.set('Authorization', `Bearer ${token}`)
.send({
content: 'Nice write up'
Expand All @@ -163,7 +146,7 @@ describe('Comment Model', () => {

const response = await chai
.request(app)
.put('/api/v1/articles/95745c60-7b1a-11e8-9c9c-2d42b21b1a3e/comments/09543c60-7b1a-11e8-9c9c-2d42b21b1')
.put('/api/v1/articles/dhdhdhhdhdhhdhdk/comments/09543c60-7b1a-11e8-9c9c-2d42b21b1')
.set('Authorization', `Bearer ${token}`)
.send({
content: 'Nice write up'
Expand All @@ -188,7 +171,7 @@ describe('Comment Model', () => {
it('It should not be able to delete a comment created by another user', async () => {
const response = await chai
.request(app)
.delete('/api/v1/articles/95745c60-7b1a-11e8-9c9c-2d42b21b1a3e/comments/09543c60-7b1a-11e8-9c9c-2d42b21b1a3e')
.delete('/api/v1/articles/dhdhdhhdhdhhdhdk/comments/09543c60-7b1a-11e8-9c9c-2d42b21b1a3e')
.set('Authorization', `Bearer ${tokenTwo}`);
expect(response.status).to.eqls(401);
expect(response.body.status).to.eqls('failure');
Expand All @@ -206,15 +189,15 @@ describe('Comment Model', () => {
it('It should not be able to delete a comment when comment id is wrong', async () => {
const response = await chai
.request(app)
.delete('/api/v1/articles/95745c60-7b1a-11e8-9c9c-2d42b21b1a3e/comments/09543c60-7b1a-11e8-9c9c-2d42b21b')
.delete('/api/v1/articles/dhdhdhhdhdhhdhdk/comments/09543c60-7b1a-11e8-9c9c-2d42b21b')
.set('Authorization', `Bearer ${token}`);
expect(response.status).to.eqls(404);
expect(response.body.status).to.eqls('failure');
});
it('It should be able to delete a comment created by only a user', async () => {
const response = await chai
.request(app)
.delete('/api/v1/articles/95745c60-7b1a-11e8-9c9c-2d42b21b1a3e/comments/09543c60-7b1a-11e8-9c9c-2d42b21b1a3e')
.delete('/api/v1/articles/dhdhdhhdhdhhdhdk/comments/09543c60-7b1a-11e8-9c9c-2d42b21b1a3e')
.set('Authorization', `Bearer ${token}`);
expect(response.status).to.eqls(200);
expect(response.body.status).to.eqls('success');
Expand All @@ -227,7 +210,7 @@ describe('Comment Model', () => {

const response = await chai
.request(app)
.delete('/api/v1/articles/95745c60-7b1a-11e8-9c9c-2d42b21b1a3e/comments/09543c60-7b1a-11e8-9c9c-2d42b21b1')
.delete('/api/v1/articles/dhdhdhhdhdhhdhdk/comments/09543c60-7b1a-11e8-9c9c-2d42b21b1')
.set('Authorization', `Bearer ${token}`)
.send({
content: 'Nice write up'
Expand Down

0 comments on commit 0dd73bc

Please sign in to comment.