Skip to content

Commit

Permalink
Merge 2a1de0a into 77e20c2
Browse files Browse the repository at this point in the history
  • Loading branch information
olajide1234 committed Mar 26, 2019
2 parents 77e20c2 + 2a1de0a commit 68b6981
Show file tree
Hide file tree
Showing 16 changed files with 546 additions and 176 deletions.
57 changes: 56 additions & 1 deletion controllers/comment.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Comment, User } from '../models';
import Sequelize from 'sequelize';
import { Comment, User, commentLikes } from '../models';

/**
* @class CommentController
Expand Down Expand Up @@ -58,10 +59,20 @@ export default class CommentController {
where: {
articleSlug
},
group: ['User.username', 'User.bio', 'User.email', 'Comment.id'],
include: [
{
model: User,
attributes: ['username', 'bio', 'email']
},
{
as: 'likes',
model: commentLikes,
attributes: [
[
Sequelize.fn('COUNT', Sequelize.col('likes.id')), 'commentCount'
]
]
}
]
});
Expand Down Expand Up @@ -152,4 +163,48 @@ export default class CommentController {
});
}
}

/**
* @description - Like/unlike an article comment
* @static
*
* @param {object} req - HTTP Request
* @param {object} res - HTTP Response
*
* @memberof CommentController
*
* @returns {string} Like/unlike comment result
*/
static async likeComment(req, res) {
const { id } = req.params;
const { id: userId } = req.user;
const currentUser = await User.findOne({ where: { id: userId } });
const currentComment = await Comment.findOne({ where: { id } });
const userHasLiked = await currentUser.hasCommentLiked(currentComment.id);
try {
if (userHasLiked) {
await commentLikes.destroy({
where: {
userId, commentId: id
}
});
return res.status(201).json({
success: true,
message: 'Comment unliked successfully',
comment: currentComment
});
}
await currentUser.addCommentLiked(currentComment.id);
return res.status(201).json({
success: true,
message: 'Comment liked successfully',
comment: currentComment
});
} catch (error) {
return res.status(500).json({
success: false,
errors: [error.message]
});
}
}
}
40 changes: 39 additions & 1 deletion doc.json
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,45 @@
}
}
}

},
"/comments{id}/like": {
"put": {
"tags": [
"CommentLike"
],
"summary": "Like a comment article",
"description": "",
"operationId": "Like comment",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"name": "authorization",
"in": "header",
"description": "A token to verify the user",
"required": true,
"type": "string"
},
{
"in": "header",
"name": "id",
"description": "Id of comment to like",
"required": true
}
],
"responses": {
"201": {
"description": "Article liked/unliked successfully"
},
"404": {
"description": "Comment not found"
}
}
}
}
},
"/articles/{slug}/comments": {
Expand Down
24 changes: 24 additions & 0 deletions middleware/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,30 @@ export const validateCommentUser = async (req, res, next) => {
});
}
};
export const validateCommentExist = async (req, res, next) => {
const {
params: { id }
} = req;
try {
const comment = await Comment.findOne({
where: {
id
}
});
if (!comment) {
return res.status(404).json({
success: false,
errors: ['Comment not found.']
});
}
return next();
} catch (error) {
return res.status(500).json({
success: false,
error: [error.message]
});
}
};

export const validateArticleExist = async (req, res, next) => {
const { slug } = req.params;
Expand Down
3 changes: 1 addition & 2 deletions migrations/20190313092713-create-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ module.exports = {
},
comment: {
type: Sequelize.STRING,
allowNull: false,
unique: true
allowNull: false
},
articleSlug: {
type: Sequelize.STRING
Expand Down
26 changes: 26 additions & 0 deletions migrations/20190318175823-create-comment-likes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('commentLikes', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
commentId: {
type: Sequelize.INTEGER
},
userId: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),

down: queryInterface => queryInterface.dropTable('commentLikes'),
};
6 changes: 6 additions & 0 deletions models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ module.exports = (sequelize, DataTypes) => {
as: 'view',
through: models.stats
});
User.belongsToMany(models.Comment, {
as: 'commentLiked',
through: models.commentLikes,
foreignKey: 'userId',
targetKey: 'commentId'
});
};

User.hook('beforeValidate', (user) => {
Expand Down
10 changes: 6 additions & 4 deletions models/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ module.exports = (sequelize, DataTypes) => {
comment: {
allowNull: false,
type: DataTypes.STRING,
unique: {
args: true,
msg: 'The comment already exists'
}
},
articleSlug: DataTypes.STRING,
userId: DataTypes.INTEGER
Expand All @@ -21,6 +17,12 @@ module.exports = (sequelize, DataTypes) => {
foreignKey: 'userId',
onDelete: 'CASCADE',
});
Comment.hasMany(models.commentLikes, {
as: 'likes',
foreignKey: 'commentId',
onDelete: 'CASCADE'
});
Comment.belongsToMany(models.User, { as: 'commentLiker', through: models.commentLikes, foreignKey: 'commentId' });
};
return Comment;
};
7 changes: 7 additions & 0 deletions models/commentlikes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = (sequelize, DataTypes) => {
const commentLikes = sequelize.define('commentLikes', {
commentId: DataTypes.INTEGER,
userId: DataTypes.INTEGER
}, {});
return commentLikes;
};
Loading

0 comments on commit 68b6981

Please sign in to comment.