Skip to content

Commit

Permalink
feat(User like specific comment)
Browse files Browse the repository at this point in the history
- Update comment lioke model
- Create functionality for user to like comment
- Create functionality for user to unlike comment
- Create functionality to get all likes for a comment
- Write tests for featue

finishes(162414287)
  • Loading branch information
Makwe-O committed Jan 29, 2019
1 parent da24335 commit 37b4db5
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 10 deletions.
87 changes: 86 additions & 1 deletion server/controllers/LikesController.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import db from '../models';
import response from '../helpers/response';

const { Article, ArticleLikesDislike } = db;
const { Article, ArticleLikesDislike, Comment, CommentLike } = db;

/**
* @class LikesDislikeController
Expand Down Expand Up @@ -94,5 +94,90 @@ class LikesController {
});
}
}

/**
*
* @description Method to likes and unlike a comment
* @static
* @param {*} req
* @param {*} res
* @returns {object} Json response
* @memberof LikesController
*/
static async likeComment(req, res) {
try {
const { commentId } = req.params;
const { userId } = req.user;
const commentFound = await Comment.findOne({
where: {
id: commentId
}
});
if (!commentFound) {
return response(res, 404, 'failure', 'Comment not found', null, null);
}

const unlikeTheComment = await CommentLike.findOne({
where: {
userId,
commentId
}
});

if (unlikeTheComment) {
await CommentLike.destroy({
where: {
userId,
commentId
}
});
return response(res, 200, 'success', 'You just unliked this comment', null, null);
}

const likeTheComment = await CommentLike.create({
userId,
commentId,
reaction: 'like'
});

if (likeTheComment) {
return response(res, 200, 'success', 'You just liked this comment', null, null);
}
} catch (error) {
return response(res, 500, 'failure', 'An error occured on the server', null, null);
}
}

/**
*
* @description Method to get all likes for a comment
* @static
* @param {*} req
* @param {*} res
* @returns {object} Json response
* @memberof LikesController
*/
static async commentLikes(req, res) {
const { commentId } = req.params;
const commentFound = await Comment.findOne({
where: {
id: commentId
}
});
if (!commentFound) {
return response(res, 404, 'failure', 'Comment not foundd', null, null);
}

const allLikes = await CommentLike.findAndCountAll({
where: {
commentId
}
});

if (allLikes.count === 0) {
return response(res, 404, 'failure', 'No likes for this comment', null, null);
}
return response(res, 200, 'success', `There are ${allLikes.count} Likes for this comment`, null, null);
}
}
export default LikesController;
2 changes: 1 addition & 1 deletion server/models/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default (sequelize, DataTypes) => {
articleId: {
type: DataTypes.UUID,
allowNull: false
}
},
},
{}
);
Expand Down
12 changes: 12 additions & 0 deletions server/routes/api/likes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Router } from 'express';
import LikesController from '../../controllers/LikesController';
import AuthManager from '../../middlewares/AuthManager';
import AuthMiddleware from '../../middlewares/AuthMiddleware';

const likeRoutes = Router();

Expand All @@ -14,6 +15,17 @@ likeRoutes.get(
AuthManager.checkIfUserIsLoggedIn,
LikesController.articleLikes
);
likeRoutes.post(
'/articles/:slug/comments/:commentId/likes',
AuthMiddleware.checkIfUserIsAuthenticated,
LikesController.likeComment
);

likeRoutes.get(
'/articles/:slug/comments/:commentId/likes',
AuthMiddleware.checkIfUserIsAuthenticated,
LikesController.commentLikes
);


export default likeRoutes;
8 changes: 7 additions & 1 deletion server/seeders/20190119190506-demoComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ export default {
content: 'Hallelujah',
userId: '45745c60-7b1a-11e8-9c9c-2d42b21b1a3e',
articleId: '95745c60-7b1a-11e8-9c9c-2d42b21b1a3e'
}], {}),
}, {
id: '09543c60-7b1a-11e8-9c9c-2d42b21b1a31',
content: 'Hallelujah',
userId: '45745c60-7b1a-11e8-9c9c-2d42b21b1a3e',
articleId: '85745c60-7b1a-11e8-9c9c-2d42b21b1a3e'
}
], {}),

down: (queryInterface, Sequelize) => queryInterface.bulkDelete('Comments', null, {})
};
79 changes: 72 additions & 7 deletions server/test/controllers/likeDislike.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@ import chai, { expect } from 'chai';
import chaiHttp from 'chai-http';
import app from '../../..';
import db from '../../models';
import { userToken, userToken2, invalidToken } from '../mockData/tokens'

const { ArticleLikesDislike } = db;

chai.use(chaiHttp);

describe('Likes and Dislike feature', () => {
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI0NTc0NWM2MC03YjFhLTExZTgtOWM5Yy0yZDQyYjIxYjFhM2UiLCJ1c2VyRW1haWwiOiJqZXNzZWluaXRAbm93LmNvbSIsInJvbGVJZCI6IjNjZWI1NDZlLTA1NGQtNGMxZC04ODYwLWUyN2MyMDlkNGFlMyIsImlhdCI6MTU0ODAxOTUyMCwiZXhwIjoxNTc5MTIzNTIwfQ.Bw4qjXM4bTQGW4GWwiOIr_iIJHhP7JWvquwvC8P0AhM';

before(async () => {});

after(async () => {
await ArticleLikesDislike.destroy({ where: {} });
});

describe('User can like articles', () => {
it('should return error for article not found', async () => {
const response = await chai
Expand Down Expand Up @@ -59,4 +53,75 @@ describe('Likes and Dislike feature', () => {
expect(response.body.status).to.equal('success');
});
});

describe('Comment Like and Unlike', () => {
it('should return error for comment not found', async () => {
const response = await chai
.request(app)
.post('/api/v1/articles/how-to-be-a-10x-dev-sGNYfURm/comments/09543c60-7b1a-11e8-9c9c-2d42b21b1a3f/likes')
.set('authorization', `Bearer ${userToken}`);
expect(response.status).to.equal(404);
expect(response.body.data).to.have.property('message');
expect(response.body.data.message).to.be.a('string');
expect(response.body.data.message).to.be.eql('Comment not found');
expect(response.body.data).to.have.property('statusCode');
expect(response.body.data.statusCode).to.be.a('Number');
expect(response.body.status).to.equal('failure');
});

it('Correct message when comment has no likes', async () => {
const response = await chai
.request(app)
.get('/api/v1/articles/how-to-google-in-2019/comments/09543c60-7b1a-11e8-9c9c-2d42b21b1a31/likes')
.set('authorization', `Bearer ${userToken}`);
expect(response.status).to.equal(404);
expect(response.body.data).to.have.property('message');
expect(response.body.data.message).to.be.a('string');
expect(response.body.data.message).to.be.eql('No likes for this comment');
expect(response.body.data).to.have.property('statusCode');
expect(response.body.data.statusCode).to.be.a('Number');
expect(response.body.status).to.equal('failure');
});

it('like comment', async () => {
const response = await chai
.request(app)
.post('/api/v1/articles/how-to-google-in-2019/comments/09543c60-7b1a-11e8-9c9c-2d42b21b1a31/likes')
.set('authorization', `Bearer ${userToken}`);
expect(response.status).to.equal(200);
expect(response.body.data).to.have.property('message');
expect(response.body.data.message).to.be.a('string');
expect(response.body.data.message).to.be.eql('You just liked this comment');
expect(response.body.data).to.have.property('statusCode');
expect(response.body.data.statusCode).to.be.a('Number');
expect(response.body.status).to.equal('success');
});

it('Correct message when comment has likes', async () => {
const response = await chai
.request(app)
.get('/api/v1/articles/how-to-google-in-2019/comments/09543c60-7b1a-11e8-9c9c-2d42b21b1a31/likes')
.set('authorization', `Bearer ${userToken}`);
expect(response.status).to.equal(200);
expect(response.body.data).to.have.property('message');
expect(response.body.data.message).to.be.a('string');
expect(response.body.data).to.have.property('statusCode');
expect(response.body.data.statusCode).to.be.a('Number');
expect(response.body.status).to.equal('success');
});

it('unlike comment', async () => {
const response = await chai
.request(app)
.post('/api/v1/articles/how-to-google-in-2019/comments/09543c60-7b1a-11e8-9c9c-2d42b21b1a31/likes')
.set('authorization', `Bearer ${userToken}`);
expect(response.status).to.equal(200);
expect(response.body.data).to.have.property('message');
expect(response.body.data.message).to.be.a('string');
expect(response.body.data.message).to.be.eql('You just unliked this comment');
expect(response.body.data).to.have.property('statusCode');
expect(response.body.data.statusCode).to.be.a('Number');
expect(response.body.status).to.equal('success');
});
});
});

0 comments on commit 37b4db5

Please sign in to comment.