-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomments.js
107 lines (104 loc) · 2.94 KB
/
comments.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import Argument from "../models/Argument.js";
import Comment from "../models/Comment.js";
import User from "../models/user.js";
export const createComment = async (req, res) => {
try {
const { argumentId } = req.params;
const { content } = req.body;
const { userId } = req.user;
const comment = new Comment({
userId,
argumentId,
content,
votes: {},
replies: [],
});
await comment.save();
res.status(201).json(comment);
} catch (err) {
res.status(500).json({ error: err.message });
}
};
export const getComments = async (req, res) => {
try {
const { argumentId } = req.params;
const argumentsData = await Argument.findById(argumentId)
.select("comments")
.populate({
path: "comments",
populate: {
path: "userId",
select: "firstName lastName picture createdAt _id",
},
});
if(!argumentsData) {
return res.status(404).json({msg: "Not found"})
}
res.status(200).json(argumentsData.comments);
} catch (err) {
res.status(400).json({ error: err.message });
}
};
export const deleteComment = async (req, res) => {
try {
const { commentId } = req.params;
const { userId } = req.user;
const comment = await Comment.findById(commentId);
if (comment.userId.toString() !== userId)
return res.status(401).json({ msg: "Unauthorized" });
await Comment.findByIdAndDelete(commentId);
res.status(204).json({});
} catch (err) {
res.status(400).json({ error: err.message });
}
};
export const voteComment = async (req, res) => {
try {
const { commentId } = req.params;
const { vote } = req.body;
const { userId } = req.user;
const comment = await Comment.findById(commentId);
const isVoted = comment.votes.has(userId);
if (isVoted && comment.votes.get(userId) === (vote === "true"))
comment.votes.delete(userId);
else {
comment.votes.set(userId, vote);
}
await comment.save();
res.status(200).json(comment);
} catch (err) {
res.status(500).json({ error: err.message });
}
};
export const addReply = async (req, res) => {
try {
const { commentId } = req.params;
const { userId } = req.user;
const { content } = req.body;
const comment = await Comment.findById(commentId);
const user = await User.findById(userId).select("firstName lastName");
if (!user) return res.status(404);
const { firstName, lastName } = user;
comment.replies.push({ userId, content, firstName, lastName });
comment.save();
res.status(200).json(comment);
} catch (err) {
res.status(500).json({ error: err.message });
}
};
export const deleteReply = async (req, res) => {
try {
const { commentId, replyId } = req.params;
const comment = await Comment.findById(commentId);
let { replies } = comment;
const { userId } = req.body;
replies = replies.filter(
(reply) => reply._id !== replyId || reply.userId.toString() !== userId
);
comment.replies = replies;
await comment.save();
res.status(200).json(comment);
} catch (err) {
res.status(400).json({ error: err.message });
}
};