Skip to content

Commit

Permalink
bug(fix-comment-id):To return the comment Id
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmykoya committed Aug 22, 2019
1 parent 5194467 commit d9e0c9e
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/controllers/article.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,10 @@ export default {
*/

async likeComment(request, response) {
const { id } = request.user;
const { id, email } = request.user;
const { slug, commentId } = request.params;

const result = await likeCommentService(slug, commentId, id);
const result = await likeCommentService(slug, commentId, id, email);

if (result === `The article with the specified slug does not exist`) {
return Helper.failResponse(response, 404, {
Expand Down
6 changes: 6 additions & 0 deletions src/db/migrations/20190712173952-create-comment-reaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ module.exports = {
}
}
},
email: {
type: Sequelize.STRING,
allowNull: {
args: false
}
},
commentId: {
type: Sequelize.INTEGER,
allowNull: {
Expand Down
5 changes: 5 additions & 0 deletions src/db/models/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ export default (sequelize, DataTypes) => {
onDelete: 'CASCADE'
});

Comment.hasMany(models.CommentReaction, {
foreignKey: 'commentId',
as: 'commentLikes'
});

Comment.belongsTo(models.Article, {
foreignKey: 'articleId',
as: 'articleComment',
Expand Down
6 changes: 6 additions & 0 deletions src/db/models/commentReaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ module.exports = (sequelize, DataTypes) => {
}
}
},
email: {
type: DataTypes.STRING,
allowNull: {
args: false
}
},
commentId: {
type: DataTypes.INTEGER,
allowNull: {
Expand Down
15 changes: 10 additions & 5 deletions src/services/article.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,12 +523,16 @@ export const getAllArticleCommentsService = async slug => {
const articleId = article.id;
const comments = await Comment.findAll({
where: { articleId },
attributes: ['body', 'highlightedText', 'slug', 'createdAt'],
attributes: ['id', 'body', 'highlightedText', 'slug', 'createdAt'],
include: [
{
model: User,
as: 'userComment',
attributes: ['firstName', 'lastName', 'userName']
attributes: ['firstName', 'lastName', 'userName', 'email']
},
{
model: CommentReaction,
as: 'commentLikes'
}
]
});
Expand Down Expand Up @@ -620,7 +624,7 @@ export const getCommentLikesCount = async commentId => {
where: { commentId }
});

return likeCount.count;
return likeCount;
};
/**
* @method likeCommentService
Expand All @@ -632,7 +636,7 @@ export const getCommentLikesCount = async commentId => {
* @param {string} reaction - the reaction value -like or dislike
*/

export const likeCommentService = async (slug, commentId, userId) => {
export const likeCommentService = async (slug, commentId, userId, email) => {
try {
const articleExist = await Article.findOne({ where: { slug } });

Expand All @@ -656,7 +660,8 @@ export const likeCommentService = async (slug, commentId, userId) => {
if (!findReaction) {
await CommentReaction.create({
userId,
commentId
commentId,
email
});

const likeCount = await getCommentLikesCount(commentId);
Expand Down
5 changes: 3 additions & 2 deletions src/services/auth.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,14 @@ export const loginService = async body => {
return bcrypt.compareSync(plainText, result.password);
};
if (result && validatePassword(password)) {
const { firstName, lastName } = result;
const { firstName, lastName, image } = result;
const token = await getToken(result);
const user = {
firstName,
lastName,
email: result.email,
token
token,
image
};
return { user };
}
Expand Down
Binary file removed uploads/2019-08-10T00:11:21.327Zimage.jpeg
Binary file not shown.
Binary file removed uploads/2019-08-10T00:11:21.698Zimage.jpeg
Binary file not shown.
Binary file removed uploads/2019-08-15T09:19:40.211Zimage.jpeg
Binary file not shown.
Binary file removed uploads/2019-08-15T09:19:40.567Zimage.jpeg
Binary file not shown.
Binary file removed uploads/2019-08-15T10:21:58.991Zimage.jpeg
Binary file not shown.
Binary file removed uploads/2019-08-15T10:21:59.315Zimage.jpeg
Binary file not shown.
Binary file removed uploads/2019-08-15T10:23:51.375Zimage.jpeg
Binary file not shown.
Binary file removed uploads/2019-08-15T10:23:51.799Zimage.jpeg
Binary file not shown.

0 comments on commit d9e0c9e

Please sign in to comment.