-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
245 lines (220 loc) · 6.41 KB
/
server.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
const express = require('express');
const mongoose = require('mongoose');
const { User, Thought } = require('./models');
const app = express();
const PORT = process.env.PORT || 3001;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'));
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost/social-network-api', {
useNewUrlParser: true,
useUnifiedTopology: true
});
// Use this to log mongo queries being executed!
mongoose.set('debug', true);
app.listen(PORT, () => console.log(`🌍 Connected on localhost:${PORT}`));
// get all users
app.get('/api/users', (req, res) => {
User.find({})
.then(dbUserData => res.json(dbUserData))
.catch(err => {
console.error(err);
res.sendStatus(400);
});
});
// get single user
app.get('/api/users/:id', ({ params, body }, res) => {
User.findOne({ _id: params.id })
.populate({
path: 'thoughts',
select: '-__v'
})
.populate({
path: 'friends',
select: '-__v'
})
.select('-__v')
.sort({ _id: -1 })
.then(dbUserData => res.json(dbUserData))
.catch(err => {
console.error(err);
res.sendStatus(400);
});
});
// create new user
app.post('/api/users', ({ body }, res) => {
User.create(body)
.then(dbUserData => res.json(dbUserData))
.catch(err => res.json(err));
});
// delete user
app.delete('/api/users/:id', async ({ params }, res) => {
const promise1 = await User.findOneAndDelete({ _id: params.id });
const promise2 = await Thought.deleteMany({ userId: { $in: params.id } }); // deletes all associated thoughts
Promise.all([promise1, promise2])
.then(dbUserData => res.json(dbUserData))
.catch(err => res.json(err));
});
// update user
app.put('/api/users/:id', ({ params, body }, res) => {
User.findOneAndUpdate({ _id: params.id }, body, { new: true, runValidators: true })
.then(dbUserData => res.json(dbUserData))
.catch(err => {
console.error(err);
res.sendStatus(400);
});
});
// add friend
app.post('/api/users/:userId/friends/:friendId', async ({ params, body }, res) => {
const promise1 = await User.findOneAndUpdate(
{ _id: params.userId },
{ $push: { friends: params.friendId } },
{ new: true }
);
const promise2 = await User.findOneAndUpdate(
{ _id: params.friendId },
{ $push: { friends: params.userId } },
{ new: true }
);
Promise.all([promise1, promise2]) // updates friends array for both users
.then(dbUserData => {
if (!dbUserData) {
res.status(404).json({ message: "No user found with this id!" });
}
res.json(dbUserData);
})
.catch(err => res.json(err));
});
// remove friend
app.delete('/api/users/:userId/friends/:friendId', async ({ params, body }, res) => {
const promise1 = await User.findOneAndUpdate(
{ _id: params.userId },
{ $pull: { friends: params.friendId } },
{ new: true }
);
const promise2 = await User.findOneAndUpdate(
{ _id: params.friendId },
{ $pull: { friends: params.userId } },
{ new: true }
);
Promise.all([promise1, promise2]) // updates friends array for both users
.then(dbUserData => {
if (!dbUserData) {
res.status(404).json({ message: "No user found with this id!" });
}
res.json(dbUserData);
})
.catch(err => res.json(err));
});
// get thoughts
app.get('/api/thoughts', (req, res) => {
Thought.find({})
.then(dbThoughtData => res.json(dbThoughtData))
.catch(err => {
console.error(err);
res.sendStatus(400);
});
});
// get single thought
app.get('/api/thoughts/:id', ({ params, body }, res) => {
Thought.findOne({ _id: params.id })
.populate({
path: 'reactions',
select: '-__v'
})
.select('-__v')
.sort({ _id: -1 })
.then(dbThoughtData => res.json(dbThoughtData))
.catch(err => {
console.error(err);
res.sendStatus(400);
});
});
// create new thought
app.post('/api/thoughts/:userId', async ({ params, body }, res) => {
let id;
const promise1 = await Thought.create(body)
.then(({ _id }) => {
id = _id; // moves id up in scope to be used in second promise
return User.findOneAndUpdate(
{ _id: params.userId },
{ $push: { thoughts: _id } },
{ new: true }
);
});
const promise2 = await Thought.findOneAndUpdate(
{ _id: id },
{ userId: params.userId },
{ new: true }
); // here we assign the userId field as the userId specified in the endpoint.
// this is so when a user is deleted all associated thoughts are as well
Promise.all([promise1, promise2])
.then(dbThoughtData => {
if(!dbThoughtData) {
res.status(404).json({ message: 'No user found with this id!' });
return;
}
res.json(dbThoughtData);
})
.catch(err => console.error(err));
});
// delete thought
app.delete('/api/thoughts/:userId/:thoughtId', ({ params }, res) => {
Thought.findOneAndDelete({ _id: params.thoughtId })
.then(({ _id }) => {
return User.findOneAndUpdate(
{ _id: params.userId },
{ $pull: { thoughts: _id } }, // also removes from thought array in the user model
{ new: true }
);
})
.then(dbThoughtData => {
if(!dbThoughtData) {
res.status(404).json({ message: 'No thought found with this id!' });
return;
}
res.json(dbThoughtData);
})
.catch(err => console.error(err));
});
// update thought
app.put('/api/thoughts/:thoughtId', ({ params, body }, res) => {
Thought.findOneAndUpdate({ _id: params.thoughtId }, body, { new: true, runValidators: true })
.then(dbThoughtData => {
if(!dbThoughtData) {
res.status(404).json({ message: 'No thought found with this id!' });
return;
}
res.json(dbThoughtData);
})
.catch(err => {
console.error(err);
res.sendStatus(400);
});
});
// create reaction
app.post('/api/thoughts/:thoughtId/reactions', ({ params, body }, res) => {
Thought.findOneAndUpdate(
{ _id: params.thoughtId },
{ $push: { reactions: body } },
{ new: true, runValidators: true }
)
.then(dbThoughtData => {
if(!dbThoughtData) {
res.status(404).json({ message: 'No thought data found with this id!' });
return
}
res.json(dbThoughtData);
})
.catch(err => res.json(err));
});
// delete reaction
app.delete('/api/thoughts/:thoughtId/reactions/:reactionId', ({ params, body }, res) => {
Thought.findOneAndUpdate(
{ _id: params.thoughtId },
{ $pull: { reactions: { reactionId: params.reactionId } } },
{ new: true }
)
.then(dbThoughtData => res.json(dbThoughtData))
.catch(err => res.json(err));
});