Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
746 lines (650 loc) · 25.4 KB
/
Copy pathapp.js
File metadata and controls
746 lines (650 loc) · 25.4 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
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
// Source: CS 340 node starter code - app.js
// https://github.com/osu-cs340-ecampus/nodejs-starter-app/blob/main/Step%208%20-%20Dynamically%20Updating%20Data/app.js
// Scope: Whole file
// Originality: Structure of routes and commenting style based on starter code;
// SQL queries are our own.
// Date: 3/16/2024
/*
SETUP
*/
// Use dotenv so we can get the port number from a .env file
require('dotenv').config()
const express = require('express')
const exphbs = require('express-handlebars')
const app = express()
// enable express to handle JSON data and form data
app.use(express.json())
app.use(express.urlencoded({extended: true}))
app.use(express.static(__dirname + "/public"))
app.engine("handlebars", exphbs.engine({ defaultLayout: "main" }))
app.set("view engine", "handlebars")
app.use(express.json())
// Get the port number from the .env file
PORT = process.env.PORT
const db = require('./database/db-connector')
/*
ROUTES
*/
// Render homepage
app.get('/', function(req, res) {
res.status(200).render("home", {
title: "Home"
})
})
// Create routes for static files
app.use(express.static(__dirname + "/public"))
// Users ------------------------------------------------------------------------------------------------------------------
// Create functionality
app.post('/insert-user-form-ajax', function(req, res) {
// Capture the incoming data and parse it back to a JS object
let data = req.body
let username = data.username
let password = data.password
// Create the query and run it on the database
let insert_user = `INSERT INTO Users (username, password) VALUES (?, ?);`
db.pool.query(insert_user, [username, password], function(error, rows, fields) {
// Check to see if there was an error
if (error) {
// Log the error to the terminal so we know what went wrong, and send the visitor an HTTP response 400 indicating it was a bad request.
console.log(error)
res.sendStatus(400)
}
else
{
// If there was no error, get the newly added User
let show_users = `
SELECT userID, username, password FROM Users
WHERE username = '${username}';
`
db.pool.query(show_users, function(error, rows, fields) {
// If there was an error on the second query, send a 400
if (error) {
// Log the error to the terminal so we know what went wrong, and send the visitor an HTTP response 400 indicating it was a bad request.
console.log(error)
res.sendStatus(400)
}
// If all went well, send the results of the query back.
else
{
res.send(rows)
}
})
}
})
})
// Read functionality
app.get('/users', function(req, res) {
const browse_query = `
SELECT userID, username, password FROM Users
ORDER BY userID;
`
db.pool.query(browse_query, function(error, rows, fields) {
if (error) {
console.error(error)
}
res.status(200).render("users", {
title: "Users",
data: rows
})
})
})
// Delete functionality
app.delete('/delete-user/', function(req, res){
const data = req.body
const userID = parseInt(data.id)
const deleteQuery = `DELETE FROM Users where userID = ?`
db.pool.query(deleteQuery, [userID], function(error, rows, fields) {
if (error) {
console.log(error)
res.sendStatus(400)
}
else {
res.sendStatus(204)
}
})})
// Questions -------------------------------------------------------------------------------------------------------------
// Create functionality
app.post('/insert-question', function(req, res)
{
// Capture the incoming data and parse it back to a JS object
let data = req.body
let questionText = data.questionText
let typeID = data.typeID
// Create the query and run it on the database
let insert_question = `INSERT INTO Questions (questionText, typeID) VALUES ( ? , ? )`
db.pool.query(insert_question, [questionText, typeID], function(error, rows, fields){
// Check to see if there was an error
if (error) {
// Log the error to the terminal so we know what went wrong, and send the visitor an HTTP response 400 indicating it was a bad request.
console.log(error)
res.sendStatus(400)
}
else
{
// If there was no error, get the newly added Question
let show_questions = `
SELECT Questions.questionID, Questions.questionText, Question_Types.typeName FROM Questions
LEFT JOIN Question_Types ON Questions.typeID = Question_Types.typeID
WHERE Questions.typeID = ${typeID};
`
db.pool.query(show_questions, function(error, rows, fields){
// If there was an error on the second query, send a 400
if (error) {
// Log the error to the terminal so we know what went wrong, and send the visitor an HTTP response 400 indicating it was a bad request.
console.log(error)
res.sendStatus(400)
}
// If all went well, send the results of the query back.
else
{
res.send(rows)
}
})
}
})
})
// Read functionality
app.get('/questions', function(req, res)
{
const browse_query = `
SELECT Questions.questionID, Questions.questionText, Question_Types.typeName FROM Questions
INNER JOIN Question_Types ON Questions.typeID = Question_Types.typeID
ORDER BY Questions.questionID;
`
let get_typeNames = `SELECT typeID, typeName from Question_Types ORDER BY typeID;`
db.pool.query(browse_query, function(error, rows, fields) {
if (error) {
console.error(error)
}
// save Questions
let questions = rows
db.pool.query(get_typeNames, (error, rows, fields) => {
// save the usernames
let typeNames = rows
return res.status(200).render("questions", {
title: "Questions",
data: questions,
typeNames: typeNames
})
})
})
})
// Delete functionality
app.delete('/delete-question/', function(req, res)
{
const data = req.body
const questionID = parseInt(data.id)
console.log(data)
const deleteQuery = `DELETE FROM Questions where questionID = ?`
db.pool.query(deleteQuery, [questionID], function(error, rows, fields) {
if (error) {
console.log(error)
res.sendStatus(400)
}
else {
res.sendStatus(204)
}
})
})
// Answers ----------------------------------------------------------------------------------------------------------------
// Create functionality
app.post('/insert-answer-form-ajax', function(req, res){
// Capture the incoming data and parse it back to a JS object
let data = req.body
let answerText = data.answerText
let questionID = data.questionID
let correctness = data.correctness
// Create the query and run it on the database
let insert_answer = `
INSERT INTO Answers (answerText, questionID, correctness)
VALUES (?, ?, ?)
;`
db.pool.query(insert_answer, [answerText, questionID, correctness], function(error, rows, fields) {
// Check to see if there was an error
if (error) {
// Log the error to the terminal so we know what went wrong, and send the visitor an HTTP response 400 indicating it was a bad request.
console.log(error)
res.sendStatus(400)
}
else
{
// If there was no error, get the newly added Answer
let show_answers = `
SELECT Answers.answerID, Answers.answerText, Questions.questionText, Answers.correctness
FROM Answers
INNER JOIN Questions
ON Answers.questionID = Questions.questionID
WHERE Answers.answerText = '${answerText}';
`
db.pool.query(show_answers, function(error, rows, fields) {
// If there was an error on the second query, send a 400
if (error) {
// Log the error to the terminal so we know what went wrong, and send the visitor an HTTP response 400 indicating it was a bad request.
console.log(error)
res.sendStatus(400)
}
// If all went well, send the results of the query back.
else
{
res.send(rows)
}
})
}
})
})
// Read functionality
app.get('/answers', function(req, res)
{
const browse_answers = `
SELECT Answers.answerID, Answers.answerText, Questions.questionText, Answers.correctness FROM Answers
INNER JOIN Questions ON Answers.questionID = Questions.questionID
ORDER BY Answers.answerID;
`
let get_questionTexts = `SELECT questionID, questionText FROM Questions ORDER BY questionID;`
db.pool.query(browse_answers, function(error, rows, fields) {
if (error) {
console.error(error)
}
// save Answers
let answers = rows
db.pool.query(get_questionTexts, (error, rows, fields) => {
let questionTexts = rows
if (error) {
console.error(error)
}
res.status(200).render("answers", {
title: "Answers",
data: answers,
questionTexts: questionTexts
})
})
})
})
// Question_Types ---------------------------------------------------------------------------------------------------------
// Create functionality
app.post('/insert-question-type', function(req, res){
// Capture the incoming data and parse it back to a JS object
let data = req.body
let typeName = data.name
// Create the query and run it on the database
let insert_question_type = `INSERT INTO Question_Types (typeName) VALUES ( ? )`
db.pool.query(insert_question_type, [typeName], function(error, rows, fields){
// Check to see if there was an error
if (error) {
// Log the error to the terminal so we know what went wrong, and send the visitor an HTTP response 400 indicating it was a bad request.
console.log(error)
res.sendStatus(400)
}
else
{
// If there was no error, get the newly added Question_Type
let show_question_types = `
SELECT typeID, typeName FROM Question_Types
WHERE typeName = '${typeName}';
`
db.pool.query(show_question_types, function(error, rows, fields){
// If there was an error on the second query, send a 400
if (error) {
// Log the error to the terminal so we know what went wrong, and send the visitor an HTTP response 400 indicating it was a bad request.
console.log(error)
res.sendStatus(400)
}
// If all went well, send the results of the query back.
else
{
res.send(rows)
}
})
}
})
})
// Read functionality
app.get('/question_types', function(req, res)
{
const browse_question_types = `
SELECT typeID, typeName FROM Question_Types
ORDER BY typeID;
`
db.pool.query(browse_question_types, function(error, rows, fields) {
res.status(200).render("question_types", {
title: "Question Types",
data: rows
})
})
})
// Game_Rounds ------------------------------------------------------------------------------------------------------------
// Create functionality
app.post('/insert-game-round-form-ajax', function(req, res) {
// Capture the incoming data and parse it back to a JS object
let data = req.body
// Capture NULL values
let userID = parseInt(data.userID)
if (isNaN(userID))
{
userID = null
}
let score = parseInt(data.score)
if (isNaN(score))
{
score = null
}
let time = data.time
if (time == '') {
time = null
}
// Create the query and run it on the database
let insert_game_rounds = `INSERT INTO Game_Rounds (userID, score, time) VALUES ( ? , ? , ? )`
db.pool.query(insert_game_rounds, [userID, score, time], function(error, rows, fields){
// Check to see if there was an error
if (error) {
// Log the error to the terminal so we know what went wrong, and send the visitor an HTTP response 400 indicating it was a bad request.
console.log(error)
res.sendStatus(400)
}
else
{
// If there was no error, get the newly added Game_Round
let show_game_rounds = `
SELECT Game_Rounds.roundID, Users.username, Game_Rounds.score, Game_Rounds.time FROM Game_Rounds
LEFT JOIN Users ON Game_Rounds.userID = Users.userID
WHERE Game_Rounds.userID = ${userID};
`
// We added this check for when userID is null, since = does not work with null
if (userID === null) {
show_game_rounds = `
SELECT Game_Rounds.roundID, Users.username, Game_Rounds.score, Game_Rounds.time FROM Game_Rounds
LEFT JOIN Users ON Game_Rounds.userID = Users.userID
WHERE Game_Rounds.userID IS NULL;
`
}
db.pool.query(show_game_rounds, function(error, rows, fields){
// If there was an error on the second query, send a 400
if (error) {
// Log the error to the terminal so we know what went wrong, and send the visitor an HTTP response 400 indicating it was a bad request.
console.log(error)
res.sendStatus(400)
}
// If all went well, send the results of the query back.
else
{
res.send(rows)
}
})
}
})
})
// Read functionality
app.get('/game_rounds', function(req, res) {
const browse_query = `
SELECT Game_Rounds.roundID, Users.username, Game_Rounds.score, Game_Rounds.time FROM Game_Rounds
LEFT JOIN Users ON Game_Rounds.userID = Users.userID
ORDER BY Game_Rounds.roundID;
`
let get_usernames = `SELECT userID, username from Users ORDER BY userID;`
db.pool.query(browse_query, function(error, rows, fields) {
if (error) {
console.error(error)
}
// save Game Rounds
let game_rounds = rows
db.pool.query(get_usernames, (error, rows, fields) => {
// save the usernames
let usernames = rows
if (error) {
console.error(error)
}
return res.status(200).render("game_rounds", {
title: "Game Rounds",
data: game_rounds,
usernames: usernames
})
})
})
})
// Update functionality
app.put('/put-round', function(req, res) {
const data = req.body
const id = parseInt(data.id)
const username = data.username
const score = data.score
const time = data.time
const updateQuery = `
UPDATE Game_Rounds SET
userID = ? ,
score = ? ,
time = ?
WHERE roundID = ?
;`
const selectQuery = `
SELECT Game_Rounds.roundID, Users.username, Game_Rounds.score, Game_Rounds.time FROM Game_Rounds
LEFT JOIN Users ON Game_Rounds.userID = Users.userID
WHERE roundID = ?
;`
// Run the 1st query
db.pool.query(updateQuery, [username, score, time, id], function(error, rows, fields) {
if (error) {
// Log the error to the terminal so we know what went wrong, and send the visitor an HTTP response 400 indicating it was a bad request.
console.log(error)
res.sendStatus(400)
}
// If there was no error, we run our second query to and return the newly updated data so we can use it to update the
// table on the front-end
else {
// Run the second query
db.pool.query(selectQuery, [id], function(error, rows, fields) {
if (error) {
console.log(error)
res.sendStatus(400)
}
else {
console.log(rows)
res.send(rows)
}
})
}
})
})
// Delete functionality
app.delete('/delete-round/', function(req, res) {
const data = req.body
const roundID = parseInt(data.id)
const deleteQuery = `DELETE FROM Game_Rounds where roundID = ?`
db.pool.query(deleteQuery, [roundID], function(error, rows, fields) {
if (error) {
console.log(error)
res.sendStatus(400)
}
else {
res.sendStatus(204)
}
})
})
// Rounds_Questions -------------------------------------------------------------------------------------------------------
// Create functionality
app.post('/insert-round-question', function(req, res) {
// Capture the incoming data and parse it back to a JS object
let data = req.body
let roundID = data.roundID
let questionID = data.questionID
let insert_round_question = `
INSERT INTO Rounds_Questions (roundID, questionID)
VALUES (?, ?)
;`
db.pool.query(insert_round_question, [roundID, questionID], function(error, rows, fields) {
// Check to see if there was an error
if (error) {
// Log the error to the terminal so we know what went wrong, and send the visitor an HTTP response 400 indicating it was a bad request.
console.log(error)
res.sendStatus(400)
}
else
{
// If there was no error, get the newly added Rounds_Question
let show_rounds_questions = `
SELECT Rounds_Questions.roundID, Users.username, Game_Rounds.score, Game_Rounds.time, Questions.questionText, Rounds_Questions.questionID
FROM Rounds_Questions
INNER JOIN Game_Rounds ON Rounds_Questions.roundID = Game_Rounds.roundID
LEFT JOIN Users ON Game_Rounds.userID = Users.userID
INNER JOIN Questions ON Rounds_Questions.questionID = Questions.questionID
WHERE Rounds_Questions.roundID = ${roundID} AND Questions.questionID = ${questionID};
`
db.pool.query(show_rounds_questions, function(error, rows, fields) {
// If there was an error on the second query, send a 400
if (error) {
// Log the error to the terminal so we know what went wrong, and send the visitor an HTTP response 400 indicating it was a bad request.
console.log(error)
res.sendStatus(400)
}
// If all went well, send the results of the query back.
else
{
res.send(rows)
}
})
}
})
})
// Read functionality
app.get('/rounds_questions', function(req, res) {
const browse_rounds_questions = `
SELECT Game_Rounds.roundID, Questions.questionText
FROM Game_Rounds
INNER JOIN Rounds_Questions ON Game_Rounds.roundID = Rounds_Questions.roundID
INNER JOIN Questions ON Rounds_Questions.questionID = Questions.questionID
ORDER BY Rounds_Questions.roundID, Questions.questionID
;`
const get_roundID_insert_dropdown = `
SELECT Game_Rounds.roundID, Users.username, Game_Rounds.score, Game_Rounds.time
FROM Game_Rounds
LEFT JOIN Users ON Game_Rounds.userID = Users.userID
ORDER BY Game_Rounds.roundID
;`
let get_questionTexts = `SELECT questionID, questionText FROM Questions ORDER BY questionID;`
const get_roundID_update_dropdown = `
SELECT DISTINCT Game_Rounds.roundID, Users.username, Game_Rounds.score, Game_Rounds.time
FROM Game_Rounds
INNER JOIN Rounds_Questions ON Game_Rounds.roundID = Rounds_Questions.roundID
LEFT JOIN Users ON Game_Rounds.userID = Users.userID
ORDER BY Game_Rounds.roundID
;`
const get_questionTexts_update_dropdown = `
SELECT DISTINCT Questions.questionText, Questions.questionID
FROM Questions
INNER JOIN Rounds_Questions ON Questions.questionID = Rounds_Questions.questionID
ORDER BY Questions.questionID
;`
db.pool.query(browse_rounds_questions, function(error, rows, fields) { // Execute the query
if(error) {
console.error(error)
}
// save Rounds_Questions
let rounds_questions = rows
db.pool.query(get_questionTexts, function(error, rows, fields) {
// save questionTexts
let questionTexts = rows
if(error) {
console.error(error)
}
db.pool.query(get_roundID_insert_dropdown, function(error, rows, fields) {
// save rounds info
let roundDropdown = rows
if (error) {
console.error(error)
}
db.pool.query(get_roundID_update_dropdown, function(error, rows, fields) {
// save updatable rounds info
let roundUpdateDropdown = rows
if (error) {
console.error(error)
}
db.pool.query(get_questionTexts_update_dropdown, function(error, rows, fields) {
// save updatable questionTexts info
let questionTextsUpdateDropdown = rows
if (error) {
console.error(error)
}
res.status(200).render("rounds_questions", {
title: "Rounds Questions",
data: rounds_questions,
questionTexts: questionTexts,
roundDropdown: roundDropdown,
questionTextsUpdateDropdown: questionTextsUpdateDropdown,
roundUpdateDropdown: roundUpdateDropdown
})
})
})
})
})
})
})
// Update functionality
app.put('/put-round-question', function(req, res) {
const data = req.body
console.log(data)
const inputRoundID = parseInt(data.inputRoundID)
const inputQuestionID = parseInt(data.inputQuestionID)
const newRoundID = parseInt(data.newRoundID)
const newQuestionID = parseInt(data.newQuestionID)
const updateQuery = `
UPDATE Rounds_Questions
SET roundID = ?, questionID = ?
WHERE roundID = ? AND questionID = ?
;`
const selectQuery = `
SELECT Rounds_Questions.roundID, Users.username, Game_Rounds.score, Game_Rounds.time, Questions.questionText
FROM Rounds_Questions
INNER JOIN Game_Rounds ON Rounds_Questions.roundID = Game_Rounds.roundID
LEFT JOIN Users ON Game_Rounds.userID = Users.userID
INNER JOIN Questions ON Rounds_Questions.questionID = Questions.questionID
WHERE Rounds_Questions.roundID = ? AND Rounds_Questions.questionID = ?
;`
// Run the 1st query
db.pool.query(updateQuery, [newRoundID, newQuestionID, inputRoundID, inputQuestionID], function(error, rows, fields) {
if (error) {
// Log the error to the terminal so we know what went wrong, and send the visitor an HTTP response 400 indicating it was a bad request.
console.log(error)
res.sendStatus(400)
}
// If there was no error, we run our second query to and return the newly updated data so we can use it to update the
// table on the front-end
else {
// Run the second query
db.pool.query(selectQuery, [newRoundID, newQuestionID], function(error, rows, fields) {
if (error) {
console.log(error)
res.sendStatus(400)
}
else {
console.log(rows)
res.send(rows)
}
})
}
})
})
// Delete functionality
app.delete('/delete-rounds-question/', function(req, res) {
const data = req.body
const roundID = parseInt(data.roundID)
const questionText = data.questionText
const deleteQuery = `
DELETE Rounds_Questions FROM Rounds_Questions
JOIN Questions ON Rounds_Questions.questionID = Questions.questionID
WHERE Rounds_Questions.roundID = ? AND Questions.questionText = ?
;`
db.pool.query(deleteQuery, [roundID, questionText], function(error, rows, fields) {
if (error) {
console.log(error)
res.sendStatus(400)
}
else {
res.sendStatus(204)
}
})
})
/*
LISTENER
*/
app.listen(PORT, function() {
console.log('Express started on http://localhost:' + PORT + '; press Ctrl-C to terminate.')
})