Skip to content

Commit

Permalink
bug(Comment)Get comment likes
Browse files Browse the repository at this point in the history
create route to get comment likes

create a controller class for the route
Write tests to test for the funtionality of the feature
Change route of like comment
  • Loading branch information
Peace Oyedeji authored and omoefe-dukuye committed Mar 21, 2019
1 parent cc9d64d commit c0d5961
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import auth from './server/api/middlewares/authentication/authenticate';
import userRoute from './server/api/routes/user';
import articleRoute from './server/api/routes/article';
import bookmarkRoutes from './server/api/routes/bookmark';
import { commentRoutes } from './server/api/routes/comment';
import tagsRoute from './server/api/routes/tags';
import handleSocket from './server/config/socket';

Expand Down Expand Up @@ -43,6 +44,7 @@ app.use('/api/articles', articleRoute);
app.use('/api/tags', tagsRoute);
app.use('/docs', swagger.serve, swagger.setup(swaggerDocument));
app.use('/api/bookmarks', bookmarkRoutes);
app.use('/api/comments', commentRoutes);

app.get('/', (req, res) => res.status(200).send({
status: 'connection successful',
Expand Down
38 changes: 38 additions & 0 deletions server/api/controllers/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,41 @@ export const likeComment = async (req, res) => {
});
}
};


/**
* @export
* @function getCommentLikes
* @param {Object} req - request received
* @param {Object} res - response object
* @returns {Object} JSON object (JSend format)
*/
export const getCommentLikes = async (req, res) => {
const {
params: {
commentId
}
} = req;
try {
const foundComment = await Comment.findByPk(commentId);

if (!foundComment) {
return res.status(404).send({
status: 'fail',
message: 'Commment not found'
});
}

const commentLikes = await foundComment.getCommentLikes();

return res.status(200).send({
status: 'success',
data: commentLikes.length
});
} catch (e) {
return res.status(500).send({
status: 'error',
message: 'Internal Server error'
});
}
};
2 changes: 1 addition & 1 deletion server/api/routes/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '../controllers/article';
import { reportArticle } from '../controllers/reports';
import { shareArticle } from '../controllers/shares';
import commentsRouter from './comment';
import { commentsRouter } from './comment';
import highlightRouter from './highlight';

const articlesRouter = Router();
Expand Down
8 changes: 5 additions & 3 deletions server/api/routes/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Router } from 'express';
import passport from 'passport';
import { newCommentValidator, deleteCommentValidator } from '../middlewares/validation/comment';
import {
postComment, getArticleComments, updateComment, deleteComment, likeComment
postComment, getArticleComments, updateComment, deleteComment, likeComment, getCommentLikes
} from '../controllers/comments';

const commentsRouter = Router({ mergeParams: true });
const commentRoutes = Router();

commentsRouter.post('/', passport.authenticate('jwt', { session: false }), newCommentValidator, postComment);
commentsRouter.get('/', getArticleComments);
Expand All @@ -15,6 +16,7 @@ commentsRouter.patch('/:commentId', passport.authenticate('jwt', { session: fals
commentsRouter.delete('/:commentId', passport.authenticate('jwt', { session: false }),
deleteCommentValidator,
deleteComment);
commentsRouter.post('/:commentId/like', passport.authenticate('jwt', { session: false }), likeComment);
commentRoutes.post('/:commentId/likes', passport.authenticate('jwt', { session: false }), likeComment);
commentRoutes.get('/:commentId/likes', getCommentLikes);

export default commentsRouter;
export { commentsRouter, commentRoutes };
36 changes: 32 additions & 4 deletions tests/article/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import chai, { expect } from 'chai';
import chaiHttp from 'chai-http';
import sinon from 'sinon';
import app from '../../index';
import models, { sequelize, Article, Tag } from '../../server/models';
import models, { sequelize, Article } from '../../server/models';
import {
user1, user2, user3, user5, user6, user8, user9
} from '../mocks/mockUsers';
Expand Down Expand Up @@ -300,7 +300,7 @@ describe('Tests for article resource', () => {
describe('Tests for like comment', () => {
it('Should like comment if request is correct', async () => {
const res = await chai.request(app)
.post(`/api/articles/${articleId}/comments/${commentId2}/like`)
.post(`/api/comments/${commentId2}/likes`)
.set('Authorization', `Bearer ${userToken}`);
const { body: { status, message } } = res;
expect(res).to.have.status(201);
Expand All @@ -310,7 +310,7 @@ describe('Tests for article resource', () => {

it('Should unlike comment if comment has already been liked by the user', async () => {
const res = await chai.request(app)
.post(`/api/articles/${articleId}/comments/${commentId2}/like`)
.post(`/api/comments/${commentId2}/likes`)
.set('Authorization', `Bearer ${userToken}`);
const { body: { status, message } } = res;
expect(res).to.have.status(200);
Expand All @@ -320,14 +320,42 @@ describe('Tests for article resource', () => {

it('Should return error if comment id is incorrrect', async () => {
const res = await chai.request(app)
.post(`/api/articles/${articleId}/comments/${fakeCommentId}/like`)
.post(`/api/comments/${fakeCommentId}/likes`)
.set('Authorization', `Bearer ${userToken}`);
const { body: { status, message } } = res;
expect(res).to.have.status(500);
expect(status).to.equal('error');
expect(message).to.equal('Internal Server error occured');
});
});
describe('Tests for retreiving comment likes', () => {
it('Should get comments if request is correct', async () => {
const res = await chai.request(app)
.get(`/api/comments/${commentId2}/likes`);
const { body: { status, data } } = res;
expect(res).to.have.status(200);
expect(status).to.equal('success');
expect(data).to.equal(0);
});

it('Should return error if comment does not exist', async () => {
const res = await chai.request(app)
.get(`/api/comments/${fakeUUIDCommentId}/likes`);
const { body: { status, message } } = res;
expect(res).to.have.status(404);
expect(status).to.equal('fail');
expect(message).to.equal('Commment not found');
});

it('Should return error if comment id is invalid', async () => {
const res = await chai.request(app)
.get(`/api/comments/${fakeCommentId}/likes`);
const { body: { status, message } } = res;
expect(res).to.have.status(500);
expect(status).to.equal('error');
expect(message).to.equal('Internal Server error');
});
});
});

describe('Tests for liking/disliking Articles', () => {
Expand Down

0 comments on commit c0d5961

Please sign in to comment.