-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcomment.js
94 lines (86 loc) · 2.42 KB
/
comment.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
const express = require("express");
const router = express.Router();
const Comment = require("../model/comment.model");
let middleware = require("../middleware");
//all comment
router.route("/all").get(async (req, res) => {
await Comment.find({}, (err, comments) => {
if (err) {
res.status(400).json({ error: err });
} else {
res.json(comments);
}
});
});
//getting a single comment of single user
router.route("/").get(middleware.checkToken, async (req, res) => {
await Comment.find({ username: req.decoded.username }, (err, comments) => {
if (err) {
res.status(400).json({ error: err });
} else {
res.json(comments);
}
});
});
router.route("/getcomments:postid").get(async (req, res) => {
await Comment.find({ postid: req.params.postid }, (err, comments) => {
if (err) {
res.status(400).json({ error: err });
} else {
res.json(comments);
}
});
});
//adding a new user profile data
router.route("/add/:postid").post(middleware.checkToken, async (req, res) => {
const comment = new Comment({
username: req.decoded.username,
postid: req.params.postid,
comment: req.body.comment,
});
comment
.save()
.then(() =>
res.json({ Message: "comment added successfully !", data: comment })
)
.catch((err) => res.status(400).json({ Error: err }));
});
//deleteing the comment data
router.route("/delete/:id").delete(middleware.checkToken, async (req, res) => {
await Comment.findByIdAndRemove({ _id: req.params.id }, (err, comment) => {
if (err) return res.status(500).send(err);
const response = {
message: "comment successfully deleted",
username: comment.username,
};
return res.status(200).send(response);
});
});
//increment like
router.route("/updatelike/:id").patch(async (req, res) => {
Comment.update(
{ _id: req.params.id },
{ $inc: { like: 1 } },
(err, comment) => {
if (err) return res.status(500).send(err);
const response = {
message: "ok",
};
return res.status(200).send(response);
}
);
});
//get like
router.route("/getlike/:id").get(async (req, res) => {
Comment.findById({ _id: req.params.id })
.select("like")
.exec((err, like) => {
if (err) return res.status(500).send(err);
const response = {
message: "ok",
like: like,
};
return res.status(200).send(response);
});
});
module.exports = router;