-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
699 lines (665 loc) · 31.1 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
// Application Dependencies
const { SicknessDAO } = require('./lib/app/database/SicknessDAO.js')
const { UserDAO } = require('./lib/app/database/UserDAO.js')
const cors = require("cors");
const bodyParser = require('body-parser');
// Create instance of an Express Application on Port 3000
const express = require('express');
const { Sicknesses } = require('./lib/app/models/Sicknesses.js');
const { User } = require('./lib/app/models/Users.js');
const app = express();
const port = 3000;
app.use(bodyParser.json());
app.use(cors());
var loggly = require('loggly');
var logger = loggly.createClient({ token:"c699c451-68e8-4a6d-a403-b19343297144", subdomain:"Cared4", sendConsoleErrors: false, tag:"Cared4-API-index" });
//Adding CryptoJS for encrypting the passwords
const CryptoJS = require('crypto-js');
//API Password
const API_KEY = "*B^NB8p44v1a0fqAiFS1gCJ4ugFoe#du%yPDZ^oTGM3fL79XeI";
// Database configuration
const dbHost = "cared4-db.clkzoscxfp7p.us-west-1.rds.amazonaws.com"
const dbPort = 3306;
const dbUsername = "admin"
const dbPassword = "rZyVIklrlBsY81m2G90AwJ1RVWd1"
// Set location of static resources and use the JSON body parser
app.use(express.static('app/images'))
// =-=-=-= Route code begins =-=-=-=
/**
* Get route at Root '/' that returns a Test Text message
* @param _req User request
* @param res Function response
*/
app.get('/', function (_req, res)
{
//Sending a log to the logging handler
logger.log("ENTERING: default route");
// Log the location
console.log('In GET / Route');
//Sending a log to the logging handler
logger.log("EXITING: default route");
// Return Test Text
res.send('This is the default root Route.');
})
/**
* GET Route at '/sickness' that returns all sicknesses from the database
* @param _req User request
* @param res Function response
*/
app.get('/sicknesses', function (req, res)
{
//Sending a log to the logging handler
logger.log("ENTERING: GET /sickness Route");
// Return sickness List as JSON, call SicknassDAO.findSickness(), and return JSON array of Sickness (a string)
// Log the location
console.log('In GET /sickness Route');
// Create a new instance of the DAO
let dao = new SicknessDAO(dbHost, dbPort, dbUsername, dbPassword);
// Using the findSickness function return a JSON of all sicknesses
dao.findSickness(function(sickness)
{
//Sending a log to the logging handler
logger.log("EXITING: GET /sickness Route");
res.json(sickness);
});
})
/**
* GET Route that does a wildcard search for all sicknesses searching by id from the database
* @param req User request
* @param res Function response
*/
app.get('/sicknesses/search/sickness/:id', function (req, res)
{
//Sending a log to the logging handler
logger.log("ENTERING: GET /sicknesses/search/sickness/:id Route for " + req.params.id);
// Return sicknesses List as JSON, call SicknessDAO.findSicknessById(), and return JSON array of sicknesses
// Log the location and the request parameters
console.log('In GET /sicknesses/search/sickness/id Route for ' + req.params.id);
// Create a new instance of the DAO
let dao = new SicknessDAO(dbHost, dbPort, dbUsername, dbPassword);
// Using the findSicknessById function and the Id in the parameters to find a sickness
dao.findSicknessById(req.params.id, function(sickness)
{
if (sickness == null)
{
//Sending a log to the logging handler
logger.log("ERROR: INVALID SICKNESS ID");
logger.log("EXITING: GET /sicknesses/search/sickness/:id Route");
res.status(200).json({error: "INVALID SICKNESS ID"}); // If the DAO does not return a sickness then the ID is invalid
} else {
//Sending a log to the logging handler
logger.log("EXITING: GET /sicknesses/search/sickness/:id Route");
res.status(200).json(sickness); // If the DAO returns a sickness then add it to the response
}
});
})
/**
* GET Route that does a wildcard search for all sicknesses searching by symptoms from the database
*/
app.post('/sicknesses/search/symptoms/:symptoms', function (req, res)
{
//Sending a log to the logging handler
logger.log("ENTERING: POST /sicknesses/search/symptoms/:symptoms Route for " + req.params.symptoms);
// Return sicknesses List as JSON, call SicknessDAO.findSicknessBySymptoms(), and return JSON array of sicknesses
// Log the location and the request parameters
console.log(
"In GET /sicknesses/search/sickness/symptoms Route for " +
req.params.symptoms
);
// Create a new instance of the DAO
let dao = new SicknessDAO(dbHost, dbPort, dbUsername, dbPassword);
//Getting necessary values to update rarity and severity
let conditions, birthday, sex;
if (req.body.conditions)
{
conditions = req.body.conditions;
birthday = req.body.birthday;
sex = req.body.sex;
}
// Using the findSicknessBySymptoms function and the Id in the parameters to find a sickness
dao.findSicknessBySymptoms(req.params.symptoms, conditions, birthday, sex, function(sickness)
{
if (sickness == null) {
//Sending a log to the logging handler
logger.log("ERROR: NO ILLNESS FOUND");
logger.log("EXITING: POST /sicknesses/search/symptoms/:symptoms Route");
res.status(200).json({ error: "NO ILLNESS FOUND" }); // If the DAO does not return a sickness then it was not found in the database
} else {
//Sending a log to the logging handler
logger.log("EXITING: POST /sicknesses/search/symptoms/:symptoms Route");
res.status(200).json(sickness); // If the DAO returns a sickness then add it to the response
}
});
})
/**
* GET Route that does a wildcard search for all sicknesses searching by name from the database
*/
app.get('/sicknesses/search/name/:name', function (req, res)
{
//Sending a log to the logging handler
logger.log("ENTERING: GET /sicknesses/search/name/:name Route for " + req.params.name);
// Return sicknesses List as JSON, call SicknessDAO.findSicknessByName(), and return JSON array of sicknesses
// Log the location and the request parameters
console.log('In GET /sicknesses/search/sickness/name Route for ' + req.params.name);
// Create a new instance of the DAO
let dao = new SicknessDAO(dbHost, dbPort, dbUsername, dbPassword);
// Using the findSicknessByName function and the name in the parameters to find a sickness
dao.findSicknessByName(req.params.name, function (sickness) {
if (sickness == null) {
//Sending a log to the logging handler
logger.log("ERROR: NO ILLNESS FOUND");
logger.log("EXITING: GET /sicknesses/search/name/:name Route");
res.status(201).json({ error: "NO ILLNESS FOUND" }); // If the DAO does not return a sickness then it was not found in the database
} else {
//Sending a log to the logging handler
logger.log("EXITING: GET /sicknesses/search/name/:name Route");
res.status(200).json(sickness); // If the DAO returns a sickness then add it to the response
}
});
})
/**
* GET Route that does a wildcard search for random sicknesses in the database
*/
app.get('/sicknesses/random', function (req, res)
{
//Sending a log to the logging handler
logger.log("ENTERING: GET /sicknesses/random Route");
// Return sicknesses List as JSON, call SicknessDAO.findSicknessByRandom(), and return JSON array of sicknesses
// Log the location and the request parameters
console.log('In GET /sicknesses/random Route');
// Create a new instance of the DAO
let dao = new SicknessDAO(dbHost, dbPort, dbUsername, dbPassword);
// Using the findSicknessByName function and the name in the parameters to find a sickness
dao.findSicknessByRandom(function (sickness) {
if (sickness == null) {
//Sending a log to the logging handler
logger.log("ERROR: SOMETHING WENT WRONG. NO ILLNESSES RETURNED.");
logger.log("EXITING: GET /sicknesses/random Route");
res.status(201).json({ error: "SOMETHING WENT WRONG" }); // If the DAO does not return a sickness then it was not found in the database
} else {
//Sending a log to the logging handler
logger.log("EXITING: GET /sicknesses/random Route");
res.status(200).json(sickness); // If the DAO returns a sickness then add it to the response
}
});
})
/**
* POST Route at '/sickness' that adds a sickness to the database
* @param req User request
* @param res Function response
*/
app.post('/sickness', function (req, res)
{
//Sending a log to the logging handler
logger.log("ENTERING: POST /sickness Route");
// Checking to see if the access is authorized.
if (req.body.key === API_KEY)
{
//Logging the request body
console.log(req.body);
// If invalid POST Body then return 400 response else add Sickness to the database
// Log the location and the data being inserted into the database
console.log('In POST /sickness Route with Post of ' + JSON.stringify(req.body));
// Check for valid POST Body, note this should validate EVERY field of the POST
if(!req.body)
{
//Sending a log to the logging handler
logger.log("ERROR: INVALID SICKNESS POSTED");
logger.log("EXITING: POST /sickness Route");
// Return the error in the response
res.status(400).json({error: "Invalid Sickness Posted"});
}
else
{
// New Sickness model
let sickness = new Sicknesses(req.body.id, req.body.name, req.body.commonName, req.body.symptoms, req.body.description, req.body.rarity, req.body.severity, req.body.treatment, req.body.strongAgainst, req.body.requirements, req.body.commontargets);
// Call sicknessDAO.create() to create a sickness from Posted Data and return an OK response
let dao = new SicknessDAO(dbHost, dbPort, dbUsername, dbPassword);
// Using the create function in the DAO with the sickness model to add a new sickness into the database
dao.create(sickness, function(sicknessId)
{
if(sicknessId == -1)
{
//Sending a log to the logging handler
logger.log("ERROR: INVALID SICKNESS POSTED");
logger.log("EXITING: POST /sickness Route");
res.status(200).json({"error" : "Creating Sickness failed"}) // If the sickness ID is set to -1 then no sickness was created
}
else{
//Sending a log to the logging handler
logger.log("EXITING: POST /sickness Route");
res.status(200).json({"success" : "Creating Sickness passed with an ID of " + sicknessId}); // If a sickness ID is returned then the sickness was created
}
});
}
} else {
//Sending a log to the logging handler
logger.log("ERROR: UNAUTHORIZED ACCESS");
logger.log("EXITING: POST /sickness Route");
//If the access is not authorized return error.
res.status(403).json({error: "UNAUTHORIZED ACCESS"});
}
})
/**
* DELETE Route at '/sickness/:id' that deletes sicknesses at a given sickness ID from the database
* @param req User request
* @param res Function response
*/
app.delete('/sickness/:id', function (req, res)
{
//Sending a log to the logging handler
logger.log("ENTERING: DELETE /sickness/:id Route with ID of " + req.params.id);
// Checking to see if the access is authorized.
if (req.body.key === API_KEY)
{
// Log the location and the request parameters
console.log('In DELETE /sickness Route with ID of ' + req.params.id);
// Create a new variable to hold the sickness ID passed in as a parameter
let sicknessId = Number(req.params.id);
// Call SicknessDAO.delete() to delete a sickness from the database and return if passed
let dao = new SicknessDAO(dbHost, dbPort, dbUsername, dbPassword);
// Using the delete function and the sicknessId to delete a sickness from the database
dao.delete(sicknessId, function(changes)
{
if(changes == 0)
{
//Sending a log to the logging handler
logger.log("ERROR: DELETE SICKNESS FAILED");
logger.log("EXITING: DELETE /sickness Route");
res.status(200).json({"error" : "Delete Sickness failed"}) // If no changes have been made, this error will be logged
}
else
{
//Sending a log to the logging handler
logger.log("EXITING: DELETE /sickness Route");
res.status(200).json({"success" : "Delete Sickness passed"}) // If changes were made then the delete was a success
}
});
} else {
//Sending a log to the logging handler
logger.log("ERROR: UNAUTHORIZED ACCESS");
logger.log("EXITING: DELETE /sickness Route");
//If the access is not authorized return error.
res.status(403).json({error: "UNAUTHORIZED ACCESS"});
}
})
/**
* PUT Route at '/sickness' that updates a sickness in the database
* @param req User request
* @param res Function response
*/
app.put('/sickness', function (req, res)
{
//Sending a log to the logging handler
logger.log("ENTERING: PUT /sickness Route");
// Checking to see if the access is authorized.
if (req.body.key === API_KEY)
{
//Logging the request body
console.log(req.body);
// Logging the location and the request body
console.log('In PUT /sickness Route with Post of ' + JSON.stringify(req.body));
// Check for valid PUT Body, note this should validate EVERY field of the POST
if(!req.body)
{
// If invalid PUT Body then return 400 response else update sickness in the database
res.status(400).json({error: "Invalid sickness Posted"});
}
else
{
// New sickness model from Posted Data
let sickness = new Sicknesses(req.body.id, req.body.name, req.body.commonName, req.body.symptoms, req.body.description, req.body.rarity, req.body.severity, req.body.treatment, req.body.strongAgainst, req.body.requirements, req.body.commontargets);
// Call SicknessDAO.update() to update a sickness from Posted Data and return an OK response
let dao = new SicknessDAO(dbHost, dbPort, dbUsername, dbPassword);
// Using the update function and the sickness model to update a sickness currently in the database
dao.update(sickness, function(changes)
{
if(changes == 0)
{
//Sending a log to the logging handler
logger.log("WARNING: Updating sickness passed but nothing was changed");
logger.log("EXITING: PUT /sickness Route");
res.status(200).json({error : "Updating Sickness passed but nothing was changed"}) // If no changes were made then return this error
}
else
{
//Sending a log to the logging handler
logger.log("EXITING: PUT /sickness Route");
res.status(200).json({success : "Updating Sickness passed and data was changed"}); // If changes were made then return this message
}
});
}
} else {
//Sending a log to the logging handler
logger.log("ERROR: UNAUTHORIZED ACCESS");
logger.log("EXITING: PUT /sickness Route");
//If the access is not authorized return error.
res.status(403).json({error: "UNAUTHORIZED ACCESS"});
}
})
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// USER REQUESTS
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
/**
* GET Route at '/users' that returns all users from the database
* @param _req User request
* @param res Function response
*/
app.get('/users', function (req, res)
{
//Sending a log to the logging handler
logger.log("ENTERING: GET /users Route");
// Checking to see if the access is authorized.
if (req.body.key === API_KEY)
{
// Return User List as JSON, call SicknassDAO.findUsers(), and return JSON array of Users (a string)
// Log the location
console.log('In GET /users Route');
// Create a new instance of the DAO
let dao = new UserDAO(dbHost, dbPort, dbUsername, dbPassword);
// Using the findUsers function, return all of the users
dao.findUsers(function(user)
{
//Sending a log to the logging handler
logger.log("EXITING: GET /users Route");
res.json(user); // Responding with a JSON of all users
});
} else {
//Sending a log to the logging handler
logger.log("ERROR: UNAUTHORIZED ACCESS");
logger.log("EXITING: GET /users Route");
//If the access is not authorized return error.
res.status(403).json({error: "UNAUTHORIZED ACCESS"});
}
})
/**
* POST Route that does a wildcard search for all users searching by id from the database
* @param req User request
* @param res Function response
*/
app.post('/users/search/user/:id', function (req, res)
{
//Sending a log to the logging handler
logger.log("ENTERING: POST /users/search/user/:id Route for " + req.params.id);
// Checking to see if the access is authorized.
if (req.body.key === API_KEY)
{
// Return users List as JSON, call UserDAO.findUserById(), and return JSON array of Users
// Log the location and the request parameters
console.log('In POST /users/searach/user Route for ' + req.params.id);
// Create a new instance of the DAO
let dao = new UserDAO(dbHost, dbPort, dbUsername, dbPassword);
// Using the findUserById function and using the ID sent in as a parameter to find a user in the database
dao.findUserById(req.params.id, function(user)
{
if (user == null)
{
//Sending a log to the logging handler
logger.log("ERROR: INVALID USER ID");
logger.log("EXITING: POST /users/search/user/:id Route");
res.status(201).json({error: "INVALID USER ID"}); // If no user was returned by the DAO then the ID was invalid
} else {
//Sending a log to the logging handler
logger.log("EXITING: POST /users/search/user/:id Route");
res.status(200).json(user); // If a user was returned by the DAO, put the user in the response
}
});
} else {
//Sending a log to the logging handler
logger.log("ERROR: UNAUTHORIZED ACCESS");
logger.log("EXITING: POST /users/search/user/:id Route");
//If the access is not authorized return error.
res.status(403).json({error: "UNAUTHORIZED ACCESS"});
}
})
/**
* GET Route that does a wildcard search for all users searching by email from the database
* @param req User request
* @param res Function response
*/
app.post('/users/search/foruser/email/', function (req, res)
{
//Sending a log to the logging handler
logger.log("ENTERING: GET /users/search/user/email/ Route for " + req.body.email);
// Checking to see if the access is authorized.
if (req.body.key === API_KEY)
{
// Return users List as JSON, call UserDAO.findUserById(), and return JSON array of Users
// Log the location and the request parameters
console.log('In GET /users/searach/user/email Route for ' + req.body.email);
// Create a new instance of the DAO
let dao = new UserDAO(dbHost, dbPort, dbUsername, dbPassword);
// Using the findUserByEmail function and using the email sent in as a parameter to find a user in the database
dao.findUserByEmail(req.body.email, function(user)
{
if (user == null)
{
//Sending a log to the logging handler
logger.log("ERROR: USER NOT FOUND");
logger.log("EXITING: POST /users/search/user/email Route");
res.status(201).json({error: "USER NOT FOUND"}); //If the user was not returned by the DAO, then the user was not found
} else {
//Sending a log to the logging handler
logger.log("EXITING: POST /users/search/user/email Route");
res.status(200).json(user); //If the user was returned by the DAO, put the user into the response
}
});
} else {
//Sending a log to the logging handler
logger.log("ERROR: UNAUTHORIZED ACCESS. USER TRIED ACCESSING WITH AN API KEY OF: " + req.body.key);
logger.log("EXITING: GET /users/search/user/email/ Route");
//If the access is not authorized return error.
res.status(403).json({ error: "UNAUTHORIZED ACCESS" });
}
})
/**
* POST Route at '/user' that adds a user to the database
* @param req User request
* @param res Function response
*/
app.post('/users', function (req, res)
{
//Sending a log to the logging handler
logger.log("ENTERING: POST /users Route");
// Checking to see if the access is authorized.
if (req.body.key === API_KEY)
{
//Logging the request body
console.log(req.body);
// If invalid POST Body then return 400 response else add User to the database
console.log('In POST /users Route with Post of ' + JSON.stringify(req.body));
if(!req.body)
{
//Sending a log to the logging handler
logger.log("ERROR: INVALID USER POSTED");
logger.log("EXITING: POST /users Route");
// Check for valid POST Body, note this should validate EVERY field of the POST
res.status(400).json({error: "Invalid User Posted"});
}
else
{
//Hashing the user's password before placing it into the database
const hash = CryptoJS.SHA256(req.body.password);
// New User model
let user = new User(req.body.id, req.body.firstName, req.body.lastName, req.body.email, hash.toString(), req.body.birthday, req.body.sex, req.body.conditions, req.body.image);
// Call userDAO.create() to create a User from Posted Data and return an OK response
let dao = new UserDAO(dbHost, dbPort, dbUsername, dbPassword);
dao.create(user, function(userId)
{
if(userId == -1)
{
//Sending a log to the logging handler
logger.log("ERROR: CREATING USER FAILED");
logger.log("EXITING: POST /users Route");
res.status(201).json({"error" : "Creating User failed"}) //If the response is -1 then something went wrong and the user was not created
}
else
{
//Sending a log to the logging handler
logger.log("EXITING: POST /users Route");
res.status(200).json({"success" : "Creating User passed with an ID of " + userId}); //If the response was anything other than -1 the user was created
}
});
}
} else {
//Sending a log to the logging handler
logger.log("ERROR: UNAUTHORIZED ACCESS");
logger.log("EXITING: POST /users Route");
//If the access is not authorized return error.
res.status(403).json({ error: "UNAUTHORIZED ACCESS" });
}
})
/**
* POST Route that does a wildcard search for all users searching by email from the database
* @param req User request
* @param res Function response
*/
app.post('/users/login', function (req, res)
{
//Sending a log to the logging handler
logger.log("ENTERING: POST /users/login Route");
// Checking to see if the access is authorized.
if (req.body.key === API_KEY)
{
// Return users List as JSON, call UserDAO.findUserById(), and return JSON array of Users
// Log the location and the request parameters
console.log('In POST /users/login Route');
//Hashing the user's password to test it against the one saved in the database
const hash = CryptoJS.SHA256(req.body.password);
// Create a new instance of the DAO
let dao = new UserDAO(dbHost, dbPort, dbUsername, dbPassword);
// Using the findUserByEmail function and using the Email sent in as a parameter to find a user in the database
dao.findUserByEmail(req.body.email, function(user){
if (user == null)
{
//Sending a log to the logging handler
logger.log("ERROR: USER NOT FOUND");
logger.log("EXITING: POST /users/login Route");
res.status(202).json({error: "USER NOT FOUND"}); //If the user was not returned by the DAO, then the user was not found
} else {
if (user.password == hash) //Testing if the password matches the password of the user found in the database
{
//Sending a log to the logging handler
logger.log("EXITING: POST /users/login Route");
res.status(200).json(user); //If the user was returned by the DAO and the password was correct, put the user into the response
} else {
//Sending a log to the logging handler
logger.log("ERROR: INCORRECT PASSWORD");
logger.log("EXITING: POST /users/login Route");
res.status(201).json({error: "INCORRECT PASSWORD"}); // If this error is thrown then the user was found but the password was incorrect
}
}
});
} else {
//Sending a log to the logging handler
logger.log("ERROR: UNAUTHORIZED ACCESS");
logger.log("EXITING: POST /users/login Route");
//If the access is not authorized return error.
res.status(403).json({ error: "UNAUTHORIZED ACCESS" });
}
})
/**
* DELETE Route at '/user/:id' that deletes users at a given user ID from the database
* @param req User request
* @param res Function response
*/
app.delete('/users/:id', function (req, res)
{
//Sending a log to the logging handler
logger.log("ENTERING: DELETE /users/:id Route with ID of " + req.params.id);
// Checking to see if the access is authorized.
if (req.body.key === API_KEY)
{
// Get the user
console.log('In DELETE /users Route with ID of ' + req.params.id);
let userId = Number(req.params.id);
// Call UserDAO.delete() to delete a user from the database and return if passed
let dao = new UserDAO(dbHost, dbPort, dbUsername, dbPassword);
dao.delete(userId, function(changes)
{
if(changes == 0)
{
//Sending a log to the logging handler
logger.log("ERROR: DELETE USER FAILED");
logger.log("EXITING: DELETE /users/:id Route");
res.status(200).json({"error" : "Delete User failed"}) //If zero changes were made to the database then the user was not deleted
}
else
{
//Sending a log to the logging handler
logger.log("EXITING: DELETE /users/:id Route");
res.status(200).json({"success" : "Delete User passed"}) //If changes were made to the database then the user was deleted
}
});
} else {
//Sending a log to the logging handler
logger.log("ERROR: UNAUTHORIZED ACCESS");
logger.log("EXITING: DELETE /users/:id Route");
//If the access is not authorized return error.
res.status(403).json({ error: "UNAUTHORIZED ACCESS" });
}
})
/**
* PUT Route at '/user' that updates a user in the database
* @param req User request
* @param res Function response
*/
app.put('/users', function (req, res)
{
//Sending a log to the logging handler
logger.log("ENTERING: PUT /users Route");
// Checking to see if the access is authorized.
if (req.body.key === API_KEY)
{
//Logging the request body
console.log(req.body);
// If invalid PUT Body then return 400 response else update user in the database
console.log('In PUT /users Route with Post of ' + JSON.stringify(req.body));
if(!req.body)
{
//Sending a log to the logging handler
logger.log("ERROR: INVALID USER POSTED");
logger.log("EXITING: PUT /users Route");
// Check for valid PUT Body, note this should validate EVERY field of the POST
res.status(400).json({error: "Invalid user Posted"});
}
else
{
// New User model from Posted Data
let user = new User(req.body.id, req.body.firstName, req.body.lastName, req.body.email, req.body.password, req.body.birthday, req.body.sex, req.body.conditions, req.body.image);
// Call UserDAO.update() to update a user from Posted Data and return an OK response
let dao = new UserDAO(dbHost, dbPort, dbUsername, dbPassword);
dao.update(user, function(changes)
{
if(changes == 0)
{
//Sending a log to the logging handler
logger.log("WARNING: UPDATING USER PASSED BUT NOTHING WAS CHANGED");
logger.log("EXITING: PUT /users Route");
res.status(201).json({error : "Updating User passed but nothing was changed"}) //If there are no changes made to the database, then it will return this status
}
else
{
//Sending a log to the logging handler
logger.log("EXITING: PUT /users Route");
res.status(200).json({success : "Updating User passed and data was changed"}); //If there are changes made to the database, then it will return this status
}
});
}
} else {
//Sending a log to the logging handler
logger.log("ERROR: UNAUTHORIZED ACCESS");
logger.log("EXITING: PUT /users Route");
//If the access is not authorized return error.
res.status(403).json({ error: "UNAUTHORIZED ACCESS" });
}
})
// =-=-=-= Route code ends =-=-=-=
/**
* Start the Server
* @param port The port where the API will sit and listen for a request
*/
app.listen(port, () =>
{
console.log(`Example app listening on port ${port}!`);
});