Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
272 lines (208 loc) · 6.76 KB
/
Copy pathserver.js
File metadata and controls
272 lines (208 loc) · 6.76 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// const mongoose = require('mongoose');
// const mondoDB = require('mongoDB');
const express = require('express');
const db = require('./config/connection');
const app = express();
const User = require('./models/user');
const Thought = require('./models/thought');
app.use(express.urlencoded({extended: false}));
app.use(express.json());
//api/users = GET all users
app.get('/api/users', async (req, res) => {
try {
const result = await User.find();
res.status(200).json(result);
} catch (err) {
res.status(500).send({ message: err })
}
});
///api/users = GET a single user, by ID and thought n friend data
app.get('/api/users/:userId', async (req, res) => {
try {
const result = await User.findOne({ _id: req.params.userId });
res.status(200).json(result);
} catch (err) {
res.status(500).send({ message: err })
}
});
//POST a new user
app.post('/api/users', async (req, res) => {
console.log('test');
console.log(req.body);
try {
var user = await User.create(req.body);
res.status(200).send("user created successfully: " + user._id);
} catch (err) {
res.status(500).send({ message: err })
}
});
//PUT update User by ID,
app.put('/api/users/:userId', async (req, res) => {
try {
// find and update user heredo updates here
const user = await User.findOneAndUpdate(
{ _id: req.params.userId },
{ $set: req.body },
{ runValidators: true, new: true }
)
if (!user) {
return res.status(404).json({ message: "User no here!" })
}
res.status(200).json(user);
} catch (err) {
console.error(err);
res.status(500).send({ message: err })
}
});
//DELETE user by ID,
app.delete('/api/users/:userId', async (req, res) => {
try {
//delete user with userID
const deleteUser = await User.findOneAndDelete({ _id: req.params.userId });
if (!deleteUser) {
return res.status(404).json({ message: "No here!" })
}
//BONUS delete users thoughts on delete:user CASCADE
//await Thought.deleteMany({ userId: req.params.userId });
res.status(200).json(deleteUser);
} catch (err) {
res.status(500).send({ message: err })
}
});
app.post('/api/users/:userId/friends/:friendId', async (req, res) => {
const exampleURL = "localholst1111/api/users/potatoe/friends/678910"
try {
const userId = req.params.userId;//12345
const friendId = req.params.friendId;
//find user by id
const user = await User.findById(userId);
if (!user) {
return res.status(404).json({ message: "No here!" });
}
//find friend by id
const friend = await User.findById(friendId);
if (!friend) {
return res.status(404).json({ message: "No here!" });
}
const userUpdate = await User.findOneAndUpdate(
{ _id: userId },
{ $addToSet: { friends: friendId } },
{ new: true }
);
res.status(200).json({ message: "Friend added!" })
} catch (err) {
res.status(500).send({ message: err })
}
});
app.delete('/api/users/:userId/friends/:friendId', async (req, res) => {
try {
//remove friend from User list.
const userId = req.params.userId;
const friendId = req.params.friendId;
const user = await User.findById(userId);
const userDelete = await User.findOneAndUpdate(
{ _id: userId },
{ $pull: { friends: friendId } },
{ new: true }
);
res.status(200).json({ message: "fuck that friend." })
} catch (err) {
res.status(500).send({ message: err })
}
});
app.get('/api/thoughts', async (req, res) => {
try {
const result = await Thought.find();
res.status(200).json(result);
} catch (err) {
res.status(500).send({ message: err })
}
});
app.get('/api/thoughts/:thoughtId', async (req, res) => {
try {
const result = await Thought.findOne({ _id: req.params.thoughtId })
res.status(200).json(result);
} catch (err) {
res.status(500).send({ message: err })
}
});
app.post('/api/thoughts', async (req, res) => {
try {
//create thought
const thought = await Thought.create(req.body);
const user = await User.findOneAndUpdate(
{_id: req.body.userId},
{ $push: { thoughts: thought._id } },
{ new: true }
)
//respond with created thought
res.status(201).json(user);
} catch (err) {
console.log(err)
res.status(500).send({ message: err })
}
});
app.put('/api/thoughts/:thoughtId', async (req, res) => {
try {
const thoughtId = req.params.thoughtId;
const { thoughtText } = req.body;
//find and update thought by id
const thought = await Thought.findByIdAndUpdate(
thoughtId,
{ thoughtText },
{ new: true }
);
if (!thought) {
return res.status(404).json({ Message: "No THOTS here!" })
}
res.status(200).json(thought);
} catch (err) {
res.status(500).send({ message: err })
}
});
app.delete('/api/thoughts/:thoughtId', async (req, res) => {
try {
//delete by thoughtId
const thoughtID = req.params.thoughtId;
const deletedThought = await Thought.findByIdAndDelete(thoughtId);
if (!deletedThought) {
return res.status(404).json({ message: "No THOTS here!" })
}
} catch (err) {
res.status(500).send({ message: err })
}
});
app.post('/api/thoughts/:thoughtId/reactions', async (req, res) => {
try {
//create reaction stored in thoughts reactions array
const thoughtId = req.params.thoughtId;
const thought = await Thought.findOneAndUpdate(
{_id: thoughtId},
{$push: {reactions: req.body}},
{new: true}
)
res.json(thought)
} catch (err) {
res.status(500).send({ message: err })
}
});
app.delete('/api/thoughts/:thoughtId/reactions/:reactionId', async (req, res) => {
try {
//create reaction stored in thoughts reactions array
const thoughtId = req.params.thoughtId;
const thought = await Thought.findOneAndUpdate(
{_id: thoughtId},
{$pull: {reactions: {reactionID: req.params.reactionId}}},
{new: true}
)
res.json(thought)
} catch (err) {
res.status(500).send({ message: err })
}
});
db.once('open', () => {
app.listen(3001, () => {
console.log("API is watching you");
console.log("yay!");
});
});