Skip to content

Commit 77a1053

Browse files
feat: 🎸 reply comment in blog
1 parent f3def11 commit 77a1053

File tree

5 files changed

+87
-2
lines changed

5 files changed

+87
-2
lines changed

‎src/controllers/Comment.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
getCommentList,
3-
createComment
3+
createComment,
4+
replyComment
45
} from "../services/crudDatabase/comment.js";
56

67
function CommentController() {
@@ -34,6 +35,21 @@ function CommentController() {
3435
error: error
3536
});
3637
};
38+
39+
this.replyComment = async (req, res, next) => {
40+
const comment = await replyComment(req.body);
41+
comment
42+
? res.status(200).json({
43+
message: "Successfully",
44+
data: comment,
45+
error: null
46+
})
47+
: res.status(400).json({
48+
message: "Failed",
49+
data: null,
50+
error: error
51+
});
52+
};
3753
}
3854

3955
export default new CommentController();

‎src/models/Comment.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ const CommentSchema = new mongoose.Schema(
2929
type: String,
3030
trim: true,
3131
required: true
32+
},
33+
children: {
34+
type: Array,
35+
default: []
3236
}
3337
},
3438
{ timestamps: true, versionKey: false }

‎src/routes/comment.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ import CommentController from "../controllers/Comment.js";
44

55
router.get("/list", CommentController.getCommentList);
66
router.post("/create", CommentController.createComment);
7+
router.post("/reply", CommentController.replyComment);
78

89
export default router;

‎src/services/crudDatabase/comment.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,38 @@ export const createComment = async ({ blogId, userId, content }) => {
1414
userWalletAddress: userInfo.walletAddress,
1515
userAvatar: userInfo.avatar,
1616
userFullName: userInfo.fullName,
17-
content: content
17+
content: content,
18+
children: []
1819
};
1920
const createdComment = await CommentModel.create(newComment);
2021
return createdComment;
2122
};
23+
24+
export const replyComment = async ({
25+
blogId,
26+
userId,
27+
content,
28+
parentCommentId
29+
}) => {
30+
const userInfo = await UserModel.findOne({ _id: userId });
31+
const comment = await CommentModel.findOne({ _id: parentCommentId });
32+
if (comment) {
33+
const newComment = {
34+
blogId: blogId,
35+
userId: userId,
36+
userWalletAddress: userInfo.walletAddress,
37+
userAvatar: userInfo.avatar,
38+
userFullName: userInfo.fullName,
39+
content: content,
40+
children: null,
41+
createdAt: new Date()
42+
};
43+
return await CommentModel.findOneAndUpdate(
44+
{ _id: parentCommentId },
45+
{ children: [...comment.children, newComment] },
46+
{ new: true }
47+
);
48+
} else {
49+
return false;
50+
}
51+
};

‎src/swaggers/comment.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,37 @@
4646
description: Successfully
4747
400:
4848
description: Failed
49+
50+
/comment/reply:
51+
post:
52+
summary: Reply comment
53+
tags:
54+
- Comments
55+
requestBody:
56+
content:
57+
application/json:
58+
schema:
59+
type: object
60+
properties:
61+
blogId:
62+
type: String
63+
required: true
64+
userId:
65+
type: String
66+
required: true
67+
content:
68+
type: string
69+
required: true
70+
parentCommentId:
71+
type: string
72+
required: false
73+
example:
74+
blogId: "64325f973f4e558a06a58e26"
75+
userId: "0x...."
76+
content: "Bài viết quá hay !!"
77+
parentCommentId: "64325f973f4e558a06a58e26"
78+
responses:
79+
200:
80+
description: Successfully
81+
400:
82+
description: Failed

0 commit comments

Comments
 (0)