-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposts.js
227 lines (210 loc) · 6.13 KB
/
posts.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const express = require("express");
const router = express.Router();
const auth = require("../../middleware/auth");
const { check, validationResult } = require("express-validator/check");
const Post = require("../../models/Post");
const Profile = require("../../models/Profile");
const User = require("../../models/User");
//@route POST api/posts
//@desc Create a post
//@access Private
router.post(
"/",
[auth, [check("text", "Text is required").not().isEmpty()]],
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
try {
const user = await User.findById(req.user.id).select("-password");
const newPost = new Post({
text: req.body.text,
name: user.name,
avatar: user.avatar,
user: req.user.id,
});
const post = await newPost.save();
return res.json(post);
} catch (err) {
console.error(err.message);
return res.status(500).send("Server error");
}
}
);
//@route GET api/posts
//@desc Get all post
//@access Private
router.get("/", auth, async (req, res) => {
try {
const posts = await Post.find().sort({ date: -1 });
res.json(posts);
} catch (err) {
console.error(err.message);
return res.status(500).send("Server error");
}
});
//@route GET api/posts/:id
//@desc Get post by ID
//@access Private
router.get("/:id", auth, async (req, res) => {
try {
const post = await Post.findById(req.params.id);
if (!post) {
return res.status(404).json({ msg: "Post not found" });
}
res.json(post);
} catch (err) {
console.error(err.message);
if (err.kind === "ObjectId") {
return res.status(404).json({ msg: "Post not found" });
}
return res.status(500).send("Server error");
}
});
//@route DELETE api/posts/:id
//@desc delete a post by id
//@access Private
router.delete("/:id", auth, async (req, res) => {
try {
const post = await Post.findById(req.params.id);
//if post doesn't exist
if (!post) {
return res.status(404).json({ msg: "Post not found" });
}
//make sure the deleter owns the post
if (post.user.toString() !== req.user.id) {
return res.status(401).json({ msg: "User not authorized" });
}
await post.remove();
return res.json({ msg: "Post removed" });
} catch (err) {
console.error(err.message);
if (err.kind === "ObjectId") {
return res.status(404).json({ msg: "Post not found" });
}
return res.status(500).send("Server error");
}
});
//@route PUT api/posts/like/:id
//@desc Like a post
//@access Private
router.put("/like/:id", auth, async (req, res) => {
try {
const post = await Post.findById(req.params.id);
//if post doesn't exist
if (!post) {
return res.status(404).json({ msg: "Post not found" });
}
//check if already liked by user
if (
post.likes.filter((like) => like.user.toString() === req.user.id).length >
0
) {
return res.status(400).json({ msg: "Post already liked" });
}
post.likes.unshift({ user: req.user.id });
await post.save();
return res.json(post.likes);
} catch (err) {
console.error(err.message);
if (err.kind === "ObjectId") {
return res.status(404).json({ msg: "Post not found" });
}
return res.status(500).send("Server error");
}
});
//@route PUT api/posts/like/:id
//@desc Like a post
//@access Private
router.put("/unlike/:id", auth, async (req, res) => {
try {
const post = await Post.findById(req.params.id);
//if post doesn't exist
if (!post) {
return res.status(404).json({ msg: "Post not found" });
}
//check if user have never liked the post
if (
post.likes.filter((like) => like.user.toString() === req.user.id)
.length === 0
) {
return res.status(400).json({ msg: "Post has not yet been liked" });
}
//Get remove index
const removeIndex = post.likes
.map((like) => like.user.toString())
.indexOf(req.user.id);
post.likes.splice(removeIndex, 1);
await post.save();
return res.json(post.likes);
} catch (err) {
console.error(err.message);
if (err.kind === "ObjectId") {
return res.status(404).json({ msg: "Post not found" });
}
return res.status(500).send("Server error");
}
});
//@route POST api/posts/comment/:id
//@desc Comment on post
//@access Private
router.post(
"/comment/:id",
[auth, [check("text", "Text is required").not().isEmpty()]],
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
try {
const user = await User.findById(req.user.id).select("-password");
const post = await Post.findById(req.params.id);
const newComment = {
text: req.body.text,
name: user.name,
avatar: user.avatar,
user: req.user.id,
};
post.comments.unshift(newComment);
await post.save();
return res.json(post.comments);
} catch (err) {
console.error(err.message);
return res.status(500).send("Server error");
}
}
);
//@route DLETE api/posts/comment/:id/:comment_id
//@desc Delete comment
//@access Private
router.delete("/comment/:id/:comment_id", auth, async (req, res) => {
try {
const post = await Post.findById(req.params.id);
//if post doesn't exist
if (!post) {
return res.status(404).json({ msg: "Post not found" });
}
//get comment
const comment = post.comments.find(
(comment) => comment.id === req.params.comment_id
);
if(!comment){
return res.status(404).json({ msg: "Comment not found" });
}
if(req.user.id!==comment.user.toString()){
return res.status(401).json({ msg: "User not authorized" });
}
//Get remove index
const removeIndex = post.comments
.map((comment) => comment.user.toString())
.indexOf(req.user.id);
post.comments.splice(removeIndex, 1);
await post.save();
return res.json(post.comments);
} catch (err) {
console.error(err.message);
return res.status(500).send("Server error");
}
});
module.exports = router;