-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·530 lines (442 loc) · 14.1 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
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
var express = require('express');
var bodyParser = require('body-parser');
// var mysql = require('./dbcon.js');
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10,
host : 'classmysql.engr.oregonstate.edu',
user : 'cs340_macabuha',
password : '****',
database : 'cs340_macabuha'
});
var app = express();
var handlebars = require('express-handlebars').create({defaultLayout: 'main'});
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
app.set('port', 8578);
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use('/public', express.static('public'));
app.set('mysql', mysql);
app.get('/', function(req, res) {
res.render('home');
});
app.get('/error', function(req, res) {
res.render('error');
});
//functions to display contents of tables
function selectTable1(res, sql, ctx, complete){
pool.query(sql, function(err, result, fields) {
if(err){
console.log(err);
res.end();
}
ctx.results = result;
complete();
});
}
function selectTable2(res, sql, ctx, complete){
pool.query(sql, function(err, result, fields) {
if(err){
console.log(err);
res.end();
}
ctx.selection = result;
complete();
});
}
function selectTable3(res, sql, ctx, complete){
pool.query(sql, function(err, result, fields) {
if(err){
console.log(err);
res.end();
}
ctx.display = result;
complete();
});
}
//delete from a table
function deleteQuery(res, sql, inserts) {
pool.query(sql, inserts, function(err, rows, fields){
if(err) {
console.log(err);
return;
}
console.log('Deleted ' + inserts);
res.status(202).end();
});
}
//function to add to a table
function addQuery(res, sql, page, inserts) {
pool.query(sql, inserts, function(err, rows, fields){
if(err){
console.log(err.code);
res.render('error');
} else {
res.redirect(page);
}
});
}
//update query function
function updateQuery(res, sql, inserts){
pool.query(sql, inserts, function(err, rows, fields){
if(err){
console.log(err);
return;
}
res.status(200);
res.end();
});
}
/*
****************************************
players table Get, Post, Delete routes
****************************************
*/
//display players
app.get('/players', function(req, res){
var ctx = {};
var count = 0;
var sql = 'SELECT players.id, players.player_fname, players.player_lname, players.age, players.height, teams.team_name FROM players ' +
'LEFT JOIN teams ON players.team_id = teams.id ORDER BY teams.id, players.player_fname';
var sql2 = 'SELECT id, team_name FROM teams ORDER BY team_name';
selectTable1(res, sql, ctx, complete);
selectTable2(res, sql2, ctx, complete);
function complete(){
count++;
if(count > 1){
res.render('players', ctx);
}
}
});
//add a player
app.post('/players', function(req, res){
var sql = "INSERT INTO players (player_fname, player_lname, age, height, team_id) VALUES (?,?,?,?,?)";
if(req.body.team_name === "NULL"){
req.body.team_name = null;
}
var inserts = [req.body.player_fname, req.body.player_lname, req.body.age, req.body.height, req.body.team_name];
addQuery(res, sql, '/players', inserts);
});
//delete a player
app.delete('/players/:id', function(req, res){
var sql = 'DELETE FROM players WHERE id=?';
var inserts = [req.params.id];
deleteQuery(res, sql, inserts);
});
//display 1 player to update
app.get('/players/:id', function(req, res){
var ctx = {};
var count = 0;
var sql = 'SELECT id, player_fname, player_lname, age, height, team_id FROM players WHERE id=?';
var sql2 = 'SELECT id, team_name FROM teams ORDER BY team_name';
var inserts = [req.params.id];
pool.query(sql, inserts, function(err, rows, fields){
if(err) {
console.log(err);
return;
}
selectTable2(res, sql2, ctx, complete);
ctx.players = rows[0];
function complete() {
count++;
if(count >= 1){
res.render('players_update', ctx);
}
}
});
});
//update player
app.put('/players/:id', function(req, res) {
var sql = 'UPDATE players SET player_fname=?, player_lname=?, age=?, height=?, team_id=? WHERE id=?';
if(req.body.team === "NULL"){
req.body.team = null;
}
var inserts = [req.body.player_fname, req.body.player_lname, req.body.age, req.body.height, req.body.team, req.params.id];
updateQuery(res, sql, inserts);
});
//render player search page
app.get('/search', function(req, res){
res.render('search');
});
//search for a player
app.post('/search', function(req, res){
var sql = 'SELECT players.player_fname, players.player_lname, players.height, players.age, teams.team_name FROM players ' +
'LEFT JOIN teams ON players.team_id = teams.id WHERE players.player_fname=? AND players.player_lname=?';
var inserts = [req.body.player_fname, req.body.player_lname];
var ctx = {};
pool.query(sql, inserts, function(err, result, fields) {
if(err){
console.log(err);
res.end();
}
//if player found, display player info
if(result[0] !== undefined){
ctx.results = result[0];
// console.log(ctx);
res.render('search', ctx);
}else {
res.send('Player Not Found! Go back previous page.');
}
});
});
/*
END players routes
*/
/*
**********************************************
teams table Get, Post, Update, Delete routes
**********************************************
*/
//display teams
app.get('/team', function(req, res){
var count = 0;
var ctx = {};
var sql = 'SELECT teams.id, teams.team_name, divisions.division_name FROM teams LEFT JOIN ' +
'divisions ON teams.div_id = divisions.id ORDER BY divisions.division_name, teams.team_name';
var sql2 = 'SELECT id, division_name FROM divisions';
selectTable1(res, sql, ctx, complete);
selectTable2(res, sql2, ctx, complete);
function complete() {
count++;
if(count > 1) {
res.render('team', ctx);
}
}
});
//add a team
app.post('/team', function(req, res){
var sql = "INSERT INTO teams (team_name, div_id) VALUES (?,?)";
var inserts = [req.body.team_name, req.body.division];
addQuery(res, sql, '/team', inserts);
});
//display 1 team name that is to be updated
app.get('/team/:id', function(req, res){
var ctx = {};
var count = 0;
var sql = 'SELECT id, team_name, div_id FROM teams WHERE id=?';
var sql2 = 'SELECT id, division_name FROM divisions';
var inserts = [req.params.id];
pool.query(sql, inserts, function(err, rows, fields){
if(err) {
console.log(err);
return;
}
selectTable2(res, sql2, ctx, complete);
ctx.teams = rows[0];
function complete() {
count++;
if(count >= 1){
res.render('teams_update', ctx);
}
}
});
});
//update a team
app.put('/team/:id', function(req, res) {
var sql = 'UPDATE teams SET team_name=?, div_id=? WHERE id=?';
var inserts = [req.body.team_name, req.body.division, req.params.id];
updateQuery(res, sql, inserts);
});
//delete a team
app.delete('/team/:id', function(req, res){
var sql = 'DELETE FROM teams WHERE id=?';
var inserts = [req.params.id];
deleteQuery(res, sql, inserts);
});
/*
END teams route
*/
/*
**********************************************
player_position table Get, Post, Delete routes
**********************************************
*/
//display positions and players
app.get('/positions', function(req, res){
var ctx = {};
var count = 0;
var sql = 'SELECT positions.id, positions.position_name FROM positions ORDER BY positions.id';
var sql1 = 'SELECT player_position.player_id, player_position.position_id, positions.position_name, players.player_fname, players.player_lname FROM player_position ' +
'INNER JOIN positions ON player_position.position_id = positions.id ' +
'INNER JOIN players ON player_position.player_id = players.id ORDER BY positions.id, players.player_fname';
var sql2 = 'SELECT players.id, players.player_fname, players.player_lname FROM players ORDER BY players.player_fname';
selectTable1(res, sql1, ctx, complete);
selectTable2(res, sql2, ctx, complete);
selectTable3(res, sql, ctx, complete);
function complete(){
count++;
if(count >= 3){
res.render('positions', ctx);
}
}
});
//add a player_position relationship
app.post('/positions', function(req, res) {
var sql = 'INSERT INTO player_position (player_id, position_id) VALUES (?,?)';
var inserts = [req.body.player, req.body.position];
console.log(inserts);
addQuery(res, sql, '/positions', inserts);
});
//delete a relationship
app.delete('/positions/:player_id/:position_id', function(req, res){
var sql = 'DELETE FROM player_position WHERE player_id=? AND position_id=?';
var inserts = [req.params.player_id, req.params.position_id];
deleteQuery(res, sql, inserts);
});
/*
END player_position routes
*/
/*
****************************************************
* conf_div table Get, Post, Update, Delete routes
****************************************************
*/
app.get('/conference', function(req, res){
var count = 0;
var ctx = {};
var sql = 'SELECT confID, confName FROM conference';
var sql2 = 'SELECT id, division_name FROM divisions WHERE NOT EXISTS (SELECT 1 FROM conf_div WHERE conf_div.divID = divisions.id) ORDER BY division_name';
var sql3 = 'SELECT conf_div.confID, conf_div.divID, divisions.id, divisions.division_name, conference.confID, conference.confName FROM conf_div ' +
'INNER JOIN divisions ON conf_div.divID = divisions.id ' +
'INNER JOIN conference ON conf_div.confID = conference.confID ORDER BY conference.confName, divisions.division_name';
selectTable1(res, sql, ctx, complete);
selectTable2(res, sql2, ctx, complete);
selectTable3(res, sql3, ctx, complete);
function complete() {
count++;
if(count > 2) {
res.render('conference', ctx);
}
}
});
//add to conf_div
app.post('/conference', function(req, res) {
var sql = 'INSERT INTO conf_div (confID, divID) VALUES (?,?)';
var inserts = [req.body.conference, req.body.divisions];
console.log(inserts);
addQuery(res, sql, '/conference', inserts);
});
//delete route for conf_div table
app.delete('/conference/:confID/:divID', function(req, res){
var sql = 'DELETE FROM conf_div WHERE confID=? AND divID=?';
var inserts = [req.params.confID, req.params.divID];
deleteQuery(res, sql, inserts);
});
/*
END conf_div
*/
/*
**********************************************
divisions table Get, Post, Delete routes
**********************************************
*/
//display all divisions
app.get('/divisions', function(req, res) {
var count = 0;
var sql = 'SELECT id, division_name FROM divisions';
var ctx = {};
selectTable1(res, sql, ctx, complete);
function complete(){
count++;
if(count >= 1){
res.render('divisions', ctx);
}
}
});
//add a division
app.post('/divisions', function(req, res) {
var sql = 'INSERT INTO divisions (division_name) VALUES (?)';
var inserts = [req.body.division_name];
console.log(inserts);
addQuery(res, sql, '/divisions', inserts);
});
//delete a division
app.delete('/divisions/:id', function(req, res){
var sql = 'DELETE FROM divisions WHERE id=?';
var inserts = [req.params.id];
deleteQuery(res, sql, inserts);
});
/*
END divisions
*/
/*
**********************************************
conference table Get, Post, Delete routes
**********************************************
*/
//display all conferences
app.get('/create_conf', function(req, res) {
var count = 0;
var sql = 'SELECT confID, confName FROM conference';
var ctx = {};
selectTable1(res, sql, ctx, complete);
function complete(){
count++;
if(count >= 1){
res.render('create_conf', ctx);
}
}
});
//add a conference
app.post('/create_conf', function(req, res) {
var sql = 'INSERT INTO conference (confName) VALUES (?)';
var inserts = [req.body.confName];
console.log(inserts);
addQuery(res, sql, '/create_conf', inserts);
});
//delete a conference
app.delete('/create_conf/:confID', function(req, res){
var sql = 'DELETE FROM conference WHERE confID=?';
var inserts = [req.params.confID];
deleteQuery(res, sql, inserts);
});
/*
END conference
*/
/*
**********************************************
positions table Get, Post, Delete routes
**********************************************
*/
//display all positions
app.get('/create_pos', function(req, res){
var count = 0;
var ctx = {};
var sql = 'SELECT id, position_name FROM positions ORDER BY id';
selectTable1(res, sql, ctx, complete);
function complete(){
count++;
if(count >= 1){
res.render('create_position', ctx);
}
}
});
//create new position
app.post('/create_pos', function(req, res){
var sql = 'INSERT INTO positions (position_name) VALUES (?)';
var inserts = [req.body.posName];
addQuery(res, sql, '/create_pos', inserts);
});
//delete a position
app.delete('/create_pos/:id', function(req, res){
var sql = 'DELETE FROM positions WHERE id=?';
var inserts = [req.params.id];
deleteQuery(res, sql, inserts);
});
/*
END positions routes
*/
app.use(function(req,res) {
res.status(404);
res.render('404');
});
app.use(function(err,req,res,next){
console.log(err.stack);
res.type('plain/text');
res.status(500);
res.render('500');
});
app.listen(app.get('port'), function(){
console.log('Express started on http://localhost:' + app.get('port') + '; press Ctrl-C to terminate.');
});