-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathcomment.js
More file actions
37 lines (34 loc) · 1.08 KB
/
Copy pathcomment.js
File metadata and controls
37 lines (34 loc) · 1.08 KB
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
import { createError } from "../error.js";
import Comment from "../models/Comment.js";
import Video from "../models/Video.js";
export const addComment = async (req, res, next) => {
const newComment = new Comment({ ...req.body, userId: req.user.id });
try {
const savedComment = await newComment.save();
res.status(200).send(savedComment);
} catch (err) {
next(err);
}
};
export const deleteComment = async (req, res, next) => {
try {
const comment = await Comment.findById(res.params.id);
const video = await Video.findById(comment.videoId);
if (req.user.id === comment.userId || req.user.id === video.userId) {
await Comment.findByIdAndDelete(req.params.id);
res.status(200).json("The comment has been deleted.");
} else {
return next(createError(403, "You can delete ony your comment!"));
}
} catch (err) {
next(err);
}
};
export const getComments = async (req, res, next) => {
try {
const comments = await Comment.find({ videoId: req.params.videoId });
res.status(200).json(comments);
} catch (err) {
next(err);
}
};