Skip to content

Commit

Permalink
Merge a685894 into 32413ad
Browse files Browse the repository at this point in the history
  • Loading branch information
McHardex committed Apr 13, 2019
2 parents 32413ad + a685894 commit b81b762
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 10 deletions.
99 changes: 97 additions & 2 deletions server/controllers/comment.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,59 @@ import serverError from '../helpers/server-error';
const { User, Comment, Comment_history: CommentHistory } = model;
const { databaseError, findArticle } = search;

const getCommentAndReplies = async (req, res) => {
if (!validations.verifyUUID(req.params.commentid)) {
return res.status(400).json({
errors: {
body: ['id not valid'],
},
});
}

const comment = await Comment.findOne({
where: {
id: req.params.commentid,
},
});

if (!comment) {
return res.status(404).json({
errors: {
body: ['This comment does not exist'],
},
});
}
const commentReplies = await Comment.findAll({
where: {
id: req.params.commentid,
},
include: [
{
model: CommentHistory,
required: false,
as: 'histories',
order: ['body'],
attributes: [
'id',
'comment_id',
'is_reply',
'reply',
'updatedAt',
'createdAt',
],
where: {
is_reply: true,
},
},
],
order: [['histories', 'updatedAt', 'asc']],
});

return res.status(200).json({
commentResponse: commentReplies,
});
};

const getCommentAndHistories = async (req, res) => {
if (!validations.verifyUUID(req.params.commentid)) {
return res.status(400).json({
Expand Down Expand Up @@ -36,12 +89,16 @@ const getCommentAndHistories = async (req, res) => {
include: [
{
model: CommentHistory,
as: 'updatedComments',
required: false,
as: 'histories',
order: ['body'],
attributes: ['id', 'comment_id', 'body', 'updatedAt', 'createdAt'],
where: {
is_updated: true,
},
},
],
order: [['updatedComments', 'updatedAt', 'desc']],
order: [['histories', 'updatedAt', 'desc']],
});

return res.status(200).json({
Expand Down Expand Up @@ -180,6 +237,7 @@ const updateComment = async (req, res) => {
await CommentHistory.create({
comment_id: comment.id,
body: comment.body,
is_updated: true,
});
}

Expand Down Expand Up @@ -230,12 +288,49 @@ const deleteComment = async (req, res) => {
});
};

const replyComment = async (req, res) => {
if (!validations.verifyUUID(req.params.commentid)) {
return res.status(400).json({
errors: {
body: ['id not valid'],
},
});
}

const comment = await Comment.findOne({
where: {
id: req.params.commentid,
},
});

if (!comment) {
return res.status(404).json({
errors: {
body: ['This comment does not exist'],
},
});
}

const reply = await CommentHistory.create({
comment_id: comment.id,
body: comment.body,
is_reply: true,
reply: req.body.reply,
});

res.status(201).json({
response: reply,
});
};

const comments = {
post,
toggleLike,
getCommentAndHistories,
getCommentAndReplies,
updateComment,
deleteComment,
replyComment,
};

export default comments;
16 changes: 15 additions & 1 deletion server/joiSchema/commentSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,19 @@ const editCommentSchema = () => {
return schema;
};

const commentSchema = { postCommentSchema, editCommentSchema };
const replyCommentSchema = () => {
const schema = Joi.object().keys({
reply: Joi.string()
.min(1)
.max(250)
.required(),
});
return schema;
};

const commentSchema = {
postCommentSchema,
editCommentSchema,
replyCommentSchema,
};
export default commentSchema;
21 changes: 20 additions & 1 deletion server/middlewares/comment.middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,23 @@ const validateEditComment = (req, res, next) => {
return next();
};

export default { validatePostComment, validateEditComment };
const validateReplyComment = (req, res, next) => {
const { error } = Joi.validate(req.body, commentSchema.replyCommentSchema());

if (error) {
const { message } = error.details[0];
const formatedMessage = joiFormater(message);
return res.status(400).json({
errors: {
body: [formatedMessage],
},
});
}
return next();
};

export default {
validatePostComment,
validateEditComment,
validateReplyComment,
};
12 changes: 12 additions & 0 deletions server/migrations/20190409194718-create-comment-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ module.exports = {
type: Sequelize.STRING,
allowNull: false,
},
is_updated: {
type: Sequelize.BOOLEAN,
defaultValue: false,
},
is_reply: {
type: Sequelize.BOOLEAN,
defaultValue: false,
},
reply: {
type: Sequelize.STRING,
allowNull: true,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
Expand Down
11 changes: 10 additions & 1 deletion server/models/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
* format: uuid
* body:
* type: string
* reply:
* type: string
* reply-comment:
* type: object
* required:
* - reply
* properties:
* reply:
* type: string
*/

module.exports = (sequelize, DataTypes) => {
Expand Down Expand Up @@ -53,7 +62,7 @@ module.exports = (sequelize, DataTypes) => {
Comment.hasMany(Comment_history, {
foreignKey: 'comment_id',
onDelete: 'CASCADE',
as: 'updatedComments',
as: 'histories',
});
};
return Comment;
Expand Down
12 changes: 12 additions & 0 deletions server/models/comment_history.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.STRING,
allowNull: false,
},
is_updated: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
is_reply: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
reply: {
type: DataTypes.STRING,
allowNull: true,
},
});
Comment_history.associate = models => {
Comment_history.hasMany(models.Comment, {
Expand Down
77 changes: 77 additions & 0 deletions server/routes/comment.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,42 @@ router.get(
commentController.getCommentAndHistories
);

/**
* @swagger
*
* /comment/{commentid}/replies:
* get:
* tags:
* - comment
* description: get all comments and respective replies to each comment
* produces:
* - application/json
* parameters:
* - in: path
* name: commentid
* description: Requires comment id
* required: true
* schema:
* $ref: '#/definitions/comment'
* responses:
* 200:
* description: Success
* 400:
* description: Bad request
* 401:
* description: unauthorized
* 403:
* description: Forbidden
* 500:
* description: ran
*/

router.get(
'/comment/:commentid/replies',
tokenValidator.verifyToken,
commentController.getCommentAndReplies
);

/**
* @swagger
*
Expand Down Expand Up @@ -116,6 +152,47 @@ router.patch(
commentController.updateComment
);

/**
* @swagger
*
* /comment/{commentid}/reply:
* post:
* tags:
* - comment
* description: reply to a specific comment
* produces:
* - application/json
* parameters:
* - in: path
* name: commentid
* description: Requires comment id
* required: true
* - in: body
* name: reply
* description: reply a comment
* required: true
* schema:
* $ref: '#/definitions/reply-comment'
* responses:
* 201:
* description: Success
* 400:
* description: Bad request
* 401:
* description: unauthorized
* 403:
* description: Forbidden
* 500:
* description: ran
*/

router.post(
'/comment/:commentid/reply',
tokenValidator.verifyToken,
commentMiddleware.validateReplyComment,
commentController.replyComment
);

/**
* @swagger
*
Expand Down
5 changes: 5 additions & 0 deletions server/seeders/20190409195758-comment-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@ module.exports = {
id: 'c125a9a9-f6d0-4e8f-ac39-6964f7b6a2c5',
comment_id: '15a2628f-ecf7-4098-8db5-95ecaf24847e',
body: 'deep write up',
is_updated: true,
},
{
id: '10ed2582-0e52-4645-a135-cd22b030ef41',
comment_id: '15a2628f-ecf7-4098-8db5-95ecaf24847e',
body: 'updated comment',
is_reply: true,
reply: 'replying to you',
},
{
id: 'a1adb042-5111-4d16-bb5c-ef2f4b57bf65',
comment_id: '0b29d287-0ad0-42ca-8f74-3159bbe304af',
body: 'Nice',
is_updated: true,
},
{
id: '4432824c-dd6e-43e7-a154-f4a95c3734a2',
comment_id: '0b29d287-0ad0-42ca-8f74-3159bbe304af',
body: 'Hmmm',
is_updated: true,
},
]),
};
Loading

0 comments on commit b81b762

Please sign in to comment.