forked from cis350/350S20-36
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
748 lines (690 loc) · 22 KB
/
index.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
var express = require('express');
var bodyParser = require('body-parser');
var cors = require('cors');
var path = require('path');
var crypto = require('crypto');
var multer = require('multer');
var GridFsStorage = require('multer-gridfs-storage');
var Grid = require('gridfs-stream');
var mongoose = require('mongoose');
var app = express();
app.use(bodyParser.json());
app.use(cors());
var lost_item = require('./Schemas/lost_item');
var found_item = require('./Schemas/found_item');
var user = require('./Schemas/user');
var admin = require('./Schemas/admin');
var ban = require('./Schemas/ban');
var warning = require('./Schemas/warning');
var report = require('./Schemas/report');
const Chat = require('./Schemas/chat');
const Message = require('./Schemas/message');
app.listen(3000, () => {
console.log('Listening on port 3000');
});
//Import Routes
const chatRoute = require('./routes/chatRoute')
const messageRoute = require('./routes/messageRoute')
//Use middleware chat and messaging routes
app.use('/chat', chatRoute);
app.use('/message', messageRoute);
const mongoURI = 'mongodb+srv://Access:ilovelukeyeagley@cluster0-elsk0.mongodb.net/test?retryWrites=true&w=majority';
const conn = mongoose.createConnection(mongoURI);
let gfs;
conn.once('open', () => {
gfs = Grid(conn.db, mongoose.mongo);
gfs.collection('uploads');
})
const storage = new GridFsStorage({
url: mongoURI,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err);
}
const filename = buf.toString('hex') + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'uploads'
};
resolve(fileInfo);
});
});
}
});
const upload = multer({ storage });
// filename important to link to item! POST image
// post {'file': img}
// RESPONDS W/ THE FILENAME which will be saved as attachmentLoc in android
app.post('/upload', upload.single('file'), (req, res) => {
res.json({'filename': req.file.filename});
});
// GET image: /image/d21d6cc9dd815da8b84742d06a1e4b23.jpg
// GET as a stream, android needs to read as input stream and maybe
// convert to bitmap
app.use('/image/:filename', (req, res) => {
var search = req.params.filename;
console.log(search)
gfs.files.findOne({filename: search}, (err, image) => {
if (err) {
res.json({'status': err});
}
else if (!image) {
res.json({'status': 'no image'});
}
else {
const readstream = gfs.createReadStream(image.filename);
console.log('successfully gotten image');
readstream.pipe(res);
}
})
});
// GET route to create a new user, ex: 'http://localhost:3000/create-user...' and get request
// query has parameters that are the user object json attributes
app.use('/create-user', (req, res) => {
var lost_items_array= JSON.parse(req.query.lost_items)
var found_items_array= JSON.parse(req.query.found_items)
console.log(lost_items_array)
var newUser = new user ({
id: parseInt(req.query.id),
username: req.query.username,
password: req.query.password,
last_login: Date.parse(req.query.last_login),
lost_items: lost_items_array,
found_items: found_items_array,
status: parseInt(req.query.status)
});
newUser.save( (err) => {
if (err) {
res.json({'status' : err})
console.log(err)
}
else {
res.json({'status': 'success'});
console.log('successfully created new user')
}
})
});
// GET all users, ex: 'http://localhost:3000/all-users'
app.use('/all-users', (req, res) => {
user.find ( (err, allItems) => {
if (err) {
res.json({'status' : err});
}
else if (allItems.length == 0) {
res.json({'status' : 'no users'});
}
else {
res.json({'status' : 'success', 'items' : allItems});
console.log('successfully gotten all users');
}
});
});
// GET specific user, ex: 'http://localhost:3000/get-user?id=1'
app.use('/get-user', (req, res) => {
var searchId = parseInt(req.query.id);
user.findOne({id: searchId}, (err, item) => {
if (err) {
res.json({'status': err});
}
else if (!item) {
res.json({'status': 'no item'});
}
else {
res.json({'status': 'success', 'user': item});
console.log('successfully gotten user');
}
})
});
// GET route to update the db, ex: address is 'http://localhost:3000/update-user' and
// query params contains new object's json attributes
app.use('/update-user', (req, res) => {
var updateId = parseInt(req.query.id);
user.findOne({id: updateId}, (err, item) => {
if (err) {
res.json({'status': err});
}
else if (!item) {
res.json({'status': 'no item'});
}
else {
if (req.query.username) {
item.username = req.query.username;
}
if (req.query.password) {
item.password = req.query.password;
}
if (req.query.last_login) {
item.last_login = Date.parse(req.query.last_login);
}
if (req.query.lost_items) {
var lost_items_array= JSON.parse(req.query.lost_items);
item.lost_items = lost_items_array;
}
if (req.query.found_items) {
var found_items_array= JSON.parse(req.query.found_items);
item.found_items = found_items_array;
}
if (req.query.status) {
item.status = parseInt(req.query.status)
}
item.save((err) => {
if (err) {
res.json({'status': err});
}
else {
res.json({'status': 'success'});
console.log('successfully updated user');
}
})
}
})
});
// GET to create a lost item, ex: 'http://localhost:3000/create-lost-item' and get request
// has query params being lost-item object json attributes
app.use('/create-lost-item', (req, res) => {
var newLostItem = new lost_item ({
id: parseInt(req.query.id),
posterId: parseInt(req.query.posterId),
category: req.query.category,
date: Date.parse(req.query.date),
latitude: parseFloat(req.query.latitude),
longitude: parseFloat(req.query.longitude),
around: req.query.around,
description: req.query.description,
attachmentLoc: req.query.attachmentLoc,
additionalInfo: req.query.additionalInfo
});
newLostItem.save( (err) => {
if (err) {
res.json({'status' : err});
console.log(err)
}
else {
res.json({'status' : 'success'});
console.log('successfully posted lost item');
}
})
});
// GET all lost items, ex: 'http://localhost:3000/all-lost-items'
app.use('/all-lost-items', (req, res) => {
lost_item.find ( (err, allItems) => {
if (err) {
res.json({'status' : err});
}
else if (allItems.length == 0) {
res.json({'status' : 'no items'});
}
else {
res.json({'status' : 'success', 'items' : allItems});
console.log('successfully gotten all lost items');
}
});
});
// GET specific lost item, ex: 'http://localhost:3000/get-lost-item?id=1'
app.use('/get-lost-item', (req, res) => {
var searchId = parseInt(req.query.id);
lost_item.findOne({id: searchId}, (err, item) => {
if (err) {
res.json({'status': err});
}
else if (!item) {
res.json({'status': 'no item'});
}
else {
res.json({'status': 'success', 'lost-item': item});
console.log('successfully gotten lost item');
}
})
});
app.use('/get-poster-lost-items', (req, res) => {
var searchId = parseInt(req.query.id);
lost_item.find({posterId: searchId}, (err, items) => {
if (err) {
res.json({'status': err});
}
else if (!items) {
res.json({'status': 'no item'});
}
else {
res.json({'status': 'success', 'lost_items': items});
console.log('successfully gotten lost items');
}
})
});
// GET route to update the db, ex: address is 'http://localhost:3000/update-lost-item' and
// query params contains new object's json attributes
app.use('/update-lost-item', (req, res) => {
var updateId = parseInt(req.query.id);
lost_item.findOne({id: updateId}, (err, item) => {
if (err) {
res.json({'status': err});
}
else if (!item) {
res.json({'status': 'no item'});
}
else {
if (req.query.posterId) {
item.posterId = parseInt(req.query.posterId);
}
if (req.query.category) {
item.category = req.query.category;
}
if (req.query.date) {
item.date = Date.parse(req.query.date);
}
if (req.query.latitude) {
item.latitude = parseFloat(req.query.latitude);
}
if (req.query.longitude) {
item.longitude = parseFloat(req.query.longitude);
}
if (req.query.around) {
item.around = req.query.around;
}
if (req.query.description) {
item.description = req.query.description;
}
if (req.query.attachmentLoc) {
item.attachmentLoc = req.query.attachmentLoc;
}
if (req.query.additionalInfo) {
item.additionalInfo = req.query.additionalInfo;
}
item.save((err) => {
if (err) {
res.json({'status': err});
}
else {
res.json({'status': 'success'});
console.log('successfully updated lost item');
}
})
}
})
});
// GET to remove a lost item
app.use('/remove-lost-item', async (req, res) => {
id = parseInt(req.query.id);
const result = await lost_item.deleteOne({"id" : id}).exec();
if (result.deletedCount == 0) {
console.log('no lost item with id exists');
res.json({'status': 'not an item'});
} else {
console.log('successfully removed lost item with id ' + id);
res.json({'status' : 'success'});
}
});
// GET to create a found item, ex: 'http://localhost:3000/create-found-item' and get request
// with query params being found-item object json attributes
app.use('/create-found-item', (req, res) => {
var newFoundItem = new found_item ({
id: parseInt(req.query.id),
posterId: parseInt(req.query.posterId),
category: req.query.category,
date: Date.parse(req.query.date),
latitude: parseFloat(req.query.latitude),
longitude: parseFloat(req.query.longitude),
around: req.query.around,
});
newFoundItem.save( (err) => {
if (err) {
res.json({'status' : err});
console.log(err)
}
else {
res.json({'status' : 'success'});
console.log('successfully posted found item');
}
})
});
// GET all found items, ex: 'http://localhost:3000/all-found-items'
app.use('/all-found-items', (req, res) => {
found_item.find ( (err, allItems) => {
if (err) {
res.json({'status' : err});
}
else if (allItems.length == 0) {
res.json({'status' : 'no items'});
}
else {
res.json({'status' : 'success', 'items' : allItems});
console.log('successfully gotten all found items');
}
});
});
// GET specific found item, ex: 'http://localhost:3000/get-found-item?id=1'
app.use('/get-found-item', (req, res) => {
var searchId = parseInt(req.query.id);
found_item.findOne({id: searchId}, (err, item) => {
if (err) {
res.json({'status': err});
}
else if (!item) {
res.json({'status': 'no item'});
}
else {
res.json({'status': 'success', 'found-item': item});
console.log('successfully gotten found item');
}
})
});
app.use('/get-poster-found-items', (req, res) => {
var searchId = parseInt(req.query.id);
found_item.find({posterId: searchId}, (err, items) => {
if (err) {
res.json({'status': err});
}
else if (!items) {
res.json({'status': 'no item'});
}
else {
res.json({'status': 'success', 'found_items': items});
console.log('successfully gotten found items');
}
})
});
// GET to update to db, ex: address is 'http://localhost:3000/update-lost-item' and
// query params contains new object's json attributes
app.use('/update-found-item', (req, res) => {
var updateId = parseInt(req.query.id);
found_item.findOne({id: updateId}, (err, item) => {
if (err) {
res.json({'status': err});
}
else if (!item) {
res.json({'status': 'no item'});
}
else {
if (req.query.posterId) {
item.posterId = parseInt(req.query.posterId);
}
if (req.query.category) {
item.category = req.query.category;
}
if (req.query.date) {
item.date = Date.parse(req.query.date);
}
if (req.query.latitude) {
item.latitude = parseFloat(req.query.latitude);
}
if (req.query.longitude) {
item.longitude = parseFloat(req.query.longitude);
}
if (req.query.around) {
item.around = req.query.around;
}
item.save((err) => {
if (err) {
res.json({'status': err});
}
else {
res.json({'status': 'success'});
console.log('successfully updated found item');
}
})
}
})
});
// GET to remove a found item
app.use('/remove-found-item', async (req, res) => {
id = parseInt(req.query.id);
const result = await found_item.deleteOne({"id" : id}).exec()
if (result.deletedCount == 0) {
console.log(id);
console.log('no found item with id exists');
res.json({'status': 'not an item'});
} else {
console.log('successfully removed found item with id ' + id);
res.json({'status' : 'success'});
}
});
// GET specific admin user
app.use('/get-admin', (req, res) => {
var searchUsername = req.query.username;
var reqPassword = req.query.password;
console.log(searchUsername);
admin.findOne({username: searchUsername}, (err, item) => {
if (err) {
res.json({'status': err});
}
else if (!item) {
res.json({'status': 'no admin'});
}
else {
console.log('successfully gotten admin');
if (reqPassword.localeCompare(item.password) == 0) {
console.log('admin pass correct');
res.json({'status': 'success'});
} else {
console.log('admin pass incorrect');
res.json({'status': 'incorrect password'})
}
}
})
});
// GET to create a warning
// ex: 'http://localhost:3000/warn?userId=2&message=hey'
app.use('/warn', (req, res) => {
var newWarning = new warning ({
userId: parseInt(req.query.userId),
seen: false,
message: req.query.message,
});
newWarning.save( (err) => {
if (err) {
res.json({'status' : err});
console.log(err)
}
else {
res.json({'status' : 'success'});
console.log('successfully created warning');
}
})
});
// GET specific warnings for the user
app.use('/get-warnings', (req, res) => {
var id = parseInt(req.query.userId);
warning.find({userId: id}, (err, item) => {
if (err) {
res.json({'status': err});
console.log(err);
}
else if (item.length == 0) {
res.json({'status': 'no warnings'});
console.log('no warnings found');
}
else {
res.json({'status': 'success', 'warnings': item});
console.log('successfully gotten warnings');
}
})
});
// GET to update warnings for a user to be seen
app.use('/see-warnings', (req, res) => {
var id = parseInt(req.query.userId);
warning.find({userId : id}, (err, allItems) => {
if (err) {
res.json({'status' : err});
}
else if (allItems.length == 0) {
res.json({'status' : 'no warnings'});
}
else {
allItems.forEach(function (w) {
w.seen = true;
w.save((err) => {
if (err) {
console.log(err);
} else {
console.log('successfully updated warning');
}
});
});
res.json({'status' : 'success', 'warnings' : allItems});
console.log('successfully seen warnings');
}
});
});
// GET all warnings
app.use('/get-all-warnings', (req, res) => {
console.log("in /get-all-warnings");
warning.find( (err, allWarns) => {
if (err) {
res.json({ status: err });
} else if (allWarns.length == 0) {
res.json({ status: 'no warnings' });
} else {
res.json({ 'status' : 'success',
'warns': allWarns });
console.log('successfully got all warnings ');
}
})
});
// GET to create a ban
// ex: 'http://localhost:3000/ban'
app.use('/ban', (req, res) => {
var newBan = new ban ({
userId: parseInt(req.query.userId),
until: Date.parse(req.query.until),
message: req.query.message,
});
newBan.save( (err) => {
if (err) {
res.json({'status' : err});
console.log(err)
}
else {
res.json({'status' : 'success'});
console.log('successfully created ban');
}
})
});
// GET to unban a user
app.use('/unban', async (req, res) => {
id = parseInt(req.query.userId);
const result = await ban.deleteOne({"userId" : id}).exec()
if (result.deletedCount == 0) {
console.log('user is not currently banned');
res.json({'status': 'not currently banned'});
} else {
console.log('successfully unbanned user with id ' + id);
res.json({'status' : 'success'});
}
});
// GET a specific ban
app.use('/get-ban', (req, res) => {
var id = parseInt(req.query.userId);
ban.findOne({userId: id}, (err, item) => {
if (err) {
res.json({'status': err});
}
else if (!item) {
res.json({'status': 'no ban'});
}
else {
res.json({'status': 'success', 'ban': item});
console.log('successfully gotten ban');
}
})
});
//GET all bans
app.use('/get-all-bans', (req, res) => {
console.log("in /get-all-bans");
ban.find( (err, allBans) => {
if (err) {
res.json({ status: err });
} else if (allBans.length == 0) {
res.json({ status: 'no bans' });
} else {
res.json({ 'status' : 'success',
'bans': allBans });
console.log('successfully got all bans ');
}
})
});
// GET to create a report
// ex: 'http://localhost:3000/report' and get request
// with query params being report object json attributes
app.use('/report', (req, res) => {
var newReport = new report ({
reporterId: parseInt(req.query.reporterId),
violatorId: parseInt(req.query.violatorId),
category: req.query.category,
message: req.query.message,
});
newReport.save( (err) => {
if (err) {
res.json({'status' : err});
console.log(err)
}
else {
res.json({'status' : 'success'});
console.log('successfully created report');
}
})
});
// GET reports for a specific user
app.use('/get-reports', (req, res) => {
var id = parseInt(req.query.userId);
report.find({violatorId : id}, (err, item) => {
if (err) {
res.json({'status': err});
console.log(err);
}
else if (item.length == 0) {
res.json({'status': 'no reports'});
console.log('no reports found');
}
else {
res.json({'status': 'success', 'reports': item});
console.log('successfully gotten reports');
}
})
});
// GET all reports
app.use('/all-reports', (req, res) => {
report.find((err, allItems) => {
if (err) {
res.json({'status': err});
console.log(err);
}
else if (allItems.length == 0) {
res.json({'status': 'no reports'});
console.log('no reports found');
}
else {
res.json({'status': 'success', 'reports': allItems});
console.log('successfully gotten all reports');
}
})
});
// GET to remove a report
app.use('/remove-report', async (req, res) => {
id = parseInt(req.query.userId);
const result = await lost_item.deleteOne({"violatorId" : id}).exec();
if (result.deletedCount == 0) {
console.log('no report with id exists');
res.json({'status': 'not a report'});
} else {
console.log('successfully removed report with violatorId ' + id);
res.json({'status' : 'success'});
}
});
//Get back chats for a user
app.use('/get-user-chats', (req, res) => {
console.log("in chat/ path to get all chats")
userId = parseInt(req.query.id);
Chat.find({$or:[{'initiatorId':userId}, {'receiverId':userId}]}, (err, allChats) => {
if (err) {
res.json({ 'status': err });
} else if (allChats.length == 0) {
res.json({ 'status': 'no chats' });
} else {
res.json({ 'status' : 'success',
'chats': allChats });
console.log('successfully got all chats: ');
}
})
});