-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
327 lines (288 loc) · 10.2 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/**
* Express webserver / controller
*/
// import express
const express = require('express'); // eslint-disable-line
// import the cors -cross origin resource sharing- module
const cors = require('cors'); // eslint-disable-line
// eslint-disable-next-line import/no-extraneous-dependencies
const cloudinary = require('cloudinary').v2;
cloudinary.config({
cloud_name: 'dgyhizrvt',
api_key: '665667247954395',
api_secret: 'SLHkQi0yP4V4V9C5dzOHdBpgPus',
});
const bodyParser = require('body-parser') // eslint-disable-line
// create a new express app
const webapp = express();
webapp.use(bodyParser.json());
// enable cors
webapp.use(cors());
// webapp.use((req, res, next) => {
// res.header('Access-Control-Allow-Origin', '*');
// res.header('Access-Control-Allow-Methods', 'GET, PUT');
// res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
// res.header('Access-Control-Allow-Credentials', 'true');
// next();
// });
// configure express to parse request bodies
webapp.use(express.urlencoded({ extended: true }));
// import path
const path = require('path');
// tell express where to find static files
webapp.use(express.static(path.join(__dirname,'./frontend/build')));
// import the db function
const dbLib = require('./model/chatDB');
const dbLib2 = require('./model/reportsDB');
const dbLibLeaderboad = require('./model/leaderboardDB');
webapp.get('/leaderboard', async (req, resp) => {
const res = await dbLibLeaderboad.getLeaders();
resp.status(200).json(res);
});
webapp.get('/Reports/lastreporter/:user', async (req, resp) => {
const res = await dbLib2.getLastReporter(req.params.user);
resp.status(200).json(res);
});
webapp.get('/Reports/mostreporter/:user', async (req, resp) => {
const res = await dbLib2.getMostReporter(req.params.user);
resp.status(200).json(res);
});
webapp.get('/Reports/reports/:user', async (req, resp) => {
const res = await dbLib2.getTotalReports(req.params.user);
resp.status(200).json(res);
});
webapp.get('/Reports/reportsweekly/:user', async (req, resp) => {
const res = await dbLib2.getWeeklyReports(req.params.user);
resp.status(200).json(res);
});
webapp.get('/Reports/getUserData/:user', async (req, resp) => {
const res = await dbLib2.getUserHistory(req.params.user);
resp.status(200).json({ data: res });
});
webapp.get('/Schedule/totalclasses/:user', async (req, resp) => {
const res = await dbLib2.getTotalClasses(req.params.user);
resp.status(200).json({ data: res });
});
webapp.get('/Reports/:reportId/getReportData', async (req, resp) => {
const res = await dbLib2.getReportData(req.params.reportId);
resp.status(200).json({ data: res });
});
webapp.put('/Reports/:reportId/sendComment', async (req, resp) => {
console.log('Sending Comment REQUEST BODY', req.body);
if (!req.body.commenterid || !req.body.content) {
console.log('Send Comment: BAD BODY DATA');
resp.status(404).json({ message: 'missing input fields' });
return;
}
try {
// create the new student object
const newComment = {
commenterid: req.body.commenterid,
content: req.body.content,
timestamp: Date.now(),
};
const result = await dbLib2.sendComment(req.params.reportId, newComment);
resp.status(201).json({ data: { id: result } });
} catch (err) {
resp.status(400).json({ message: 'There was an error in sending comment' });
console.log(err);
}
});
webapp.put('/Reports/:reportId/updateLikes', async (req, resp) => {
console.log('server.js Updating Likes REQUEST BODY', req.body);
if (!req.body.userId || !req.body.reportId) {
console.log('Update Likes: BAD BODY DATA');
resp.status(404).json({ message: 'missing input fields' });
return;
}
try {
const result = await dbLib2.updateLikes(req.body.reportId, req.body.userId, req.body.isLiked);
resp.status(201).json({ data: { id: result } });
} catch (error) {
console.log(error);
resp.status(400).json({ message: 'There was an error in updating likes.' });
}
});
/**
* route implementation POST / chat/sendMessage
*/
webapp.post('/chat/sendMessage', async (req, resp) => {
// parse the body
console.log('Sending Message REQUEST BODY', req.body);
if (!req.body.sender || !req.body.message || !req.body.chatId) {
console.log('Send Message BAD BODY DATA');
resp.status(404).json({ message: 'missing users or message' });
return;
}
try {
// create the new student object
const newMessage = {
chatId: req.body.chatId,
sender: req.body.sender,
message: req.body.message,
timestamp: Date.now(),
};
const result = await dbLib.addMessage(newMessage);
resp.status(201).json({ data: { id: result } });
} catch (err) {
resp.status(400).json({ message: 'There was an error' });
}
});
webapp.get('/chat/user/:user1/:user2/:chat', async (req, resp) => {
const res = await dbLib.getMessages(req.params.user1, req.params.user2, req.params.chat);
resp.status(200).json({ data: res });
});
webapp.get('/chat/getChatId/:user1/:user2', async (req, resp) => {
const res = await dbLib.getChatId(req.params.user1, req.params.user2);
resp.status(200).json({ data: res });
});
webapp.get('/chat/getFriends/:user', async (req, resp) => {
const res = await dbLib.getFriends(req.params.user);
resp.status(200).json({ data: res });
});
webapp.get('/user/:user', async (req, resp) => {
if (!req.params.user) {
resp.status(404).json({ message: 'missing user' });
return;
}
const res = await dbLib.getUserData(req.params.user);
if (res.length === 0) {
resp.status(404).json({ message: 'user not found' });
return;
}
resp.status(200).json({ data: res });
});
webapp.get('/user/:userId/getFriendReports', async (req, resp) => {
if (!req.params.userId) {
resp.status(404).json({ message: 'missing user' });
return;
}
try {
const res = await dbLib2.getFriendReports(req.params.userId);
resp.status(200).json({ data: res });
} catch (error) {
resp.status(400).json({ message: 'There was an error' });
}
});
webapp.get('/user/:userId/getPersonalReports', async (req, resp) => {
if (!req.params.userId) {
resp.status(404).json({ message: 'missing user' });
return;
}
console.log(req.params);
try {
const res = await dbLib2.getPersonalReports(req.params.userId);
resp.status(200).json({ data: res });
} catch (error) {
resp.status(400).json({ message: 'There was an error' });
}
});
webapp.post('/user/', async (req, resp) => {
console.log('Creating User REQUEST BODY', req.body);
if (!req.body.id || !req.body.password || !req.body.email
|| !req.body.firstName || !req.body.lastName) {
resp.status(404).json({ message: 'missing data' });
return;
}
try {
const newUser = {
id: req.body.id,
password: req.body.password,
firstName: req.body.firstName,
lastName: req.body.lastName,
friends: req.body.friends || [],
friendReqs: req.body.friendReqs || [],
email: req.body.email,
pfp: 'https://static.vecteezy.com/system/resources/thumbnails/009/734/564/small/default-avatar-profile-icon-of-social-media-user-vector.jpg',
};
const result = await dbLib.addUser(newUser);
resp.status(201).json({ data: { id: result } });
} catch (err) {
resp.status(400).json({ message: 'There was an error' });
}
});
webapp.put('/user/addfriend/:user/:friend', async (req, resp) => {
if (!req.params.user || !req.params.friend) {
resp.status(404).json({ message: 'missing user' });
return;
}
const res = await dbLib.addFriend(req.params.user, req.params.friend);
resp.status(200).json({ data: res });
});
webapp.put('/user/removefriend/:user/:friend', async (req, resp) => {
if (!req.params.user || !req.params.friend) {
resp.status(404).json({ message: 'missing user' });
return;
}
const res = await dbLib.removeFriend(req.params.user, req.params.friend);
resp.status(200).json({ data: res });
});
webapp.put('/user/sendfriendreq/:user/:friend', async (req, resp) => {
if (!req.params.user || !req.params.friend) {
resp.status(404).json({ message: 'missing user' });
return;
}
const res = await dbLib.sendFriendReq(req.params.user, req.params.friend);
resp.status(200).json({ data: res });
});
webapp.put('/user/removefriendreq/:user/:friend', async (req, resp) => {
if (!req.params.user || !req.params.friend) {
resp.status(404).json({ data: 'missing user' });
return;
}
const res = await dbLib.removeFriendReq(req.params.user, req.params.friend);
resp.status(200).json({ data: res });
});
webapp.put('/user/changePfp', async (req, resp) => {
try {
const imageResp = cloudinary.uploader.upload(req.body.pfp);
const imgUrl = (await imageResp).secure_url;
console.log('imgUrl', imgUrl);
const result = await dbLib.changePfp(req.body.user, imgUrl);
resp.status(201).json({ data: result });
} catch (err) {
resp.status(400).json({ message: 'There was an error' });
}
});
webapp.post('/report', async (req, resp) => {
console.log('Reporting User', req.body.img);
try {
const imageResp = cloudinary.uploader.upload(req.body.img, { public_id: req.body.reporteeid });
// create the new student object
const newReport = {
_id: req.body.id,
reporterid: req.body.reporterid,
reporteeid: req.body.reporteeid,
caption: req.body.caption,
date: req.body.date,
comments: [],
likes: [],
img: (await imageResp).secure_url,
};
const result = await dbLib.reportUser(newReport);
resp.status(201).json({ data: { id: result } });
} catch (err) {
resp.status(400).json({ message: 'There was an error' });
}
});
webapp.get('/schedule/:user', async (req, resp) => {
if (!req.params.user) {
resp.status(404).json({ message: 'missing user' });
return;
}
const res = await dbLib.getSchedule(req.params.user);
resp.status(200).json({ data: res });
});
webapp.put('/classes/:user/:day', async (req, resp) => {
if (!req.params.user) {
resp.status(404).json({ message: 'missing user' });
return;
}
const res = await dbLib.addClass(req.params.user, req.body, req.params.day);
resp.status(201).json({ data: res });
});
webapp.get('*',(req, resp) =>{
console.log("GLOBAL GET DIRNAME:", __dirname);
resp.sendFile(path.join(__dirname, './frontend/build/index.html'));
});
module.exports = webapp;