-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
281 lines (256 loc) · 9.4 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
/***********************
Load Components!
Express - A Node.js Framework
Body-Parser - A tool to help use parse the data in a post request
Pg-Promise - A database tool to help use connect to our PostgreSQL database
***********************/
var express = require('express'); //Ensure our express framework has been added
var app = express();
var bodyParser = require('body-parser'); //Ensure our body-parser tool has been added
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
//Create Database Connection
var pgp = require('pg-promise')();
/**********************
Database Connection information
host: This defines the ip address of the server hosting our database. We'll be using localhost and run our database on our local machine (i.e. can't be access via the Internet)
port: This defines what port we can expect to communicate to our database. We'll use 5432 to talk with PostgreSQL
database: This is the name of our specific database. From our previous lab, we created the football_db database, which holds our football data tables
user: This should be left as postgres, the default user account created when PostgreSQL was installed
password: This the password for accessing the database. You'll need to set a password USING THE PSQL TERMINAL THIS IS NOT A PASSWORD FOR POSTGRES USER ACCOUNT IN LINUX!
**********************/
const dbConfig = {
host: 'localhost',
port: 5432,
database: 'football_db',
user: 'postgres',
password: 'user'
};
var db = pgp(dbConfig);
// set the view engine to ejs
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/'));//This line is necessary for us to use relative paths and access our resources directory
/*********************************
Below we'll add the get & post requests which will handle:
- Database access
- Parse parameters from get (URL) and post (data package)
- Render Views - This will decide where the user will go after the get/post request has been processed
Web Page Requests:
Login Page: Provided For your (can ignore this page)
Registration Page: Provided For your (can ignore this page)
Home Page:
/home - get request (no parameters)
This route will make a single query to the favorite_colors table to retrieve all of the rows of colors
This data will be passed to the home view (pages/home)
/home/pick_color - post request (color_message)
This route will be used for reading in a post request from the user which provides the color message for the default color.
We'll be "hard-coding" this to only work with the Default Color Button, which will pass in a color of #FFFFFF (white).
The parameter, color_message, will tell us what message to display for our default color selection.
This route will then render the home page's view (pages/home)
/home/pick_color - get request (color)
This route will read in a get request which provides the color (in hex) that the user has selected from the home page.
Next, it will need to handle multiple postgres queries which will:
1. Retrieve all of the color options from the favorite_colors table (same as /home)
2. Retrieve the specific color message for the chosen color
The results for these combined queries will then be passed to the home view (pages/home)
/team_stats - get request (no parameters)
This route will require no parameters. It will require 3 postgres queries which will:
1. Retrieve all of the football games in the Fall 2018 Season
2. Count the number of winning games in the Fall 2018 Season
3. Count the number of lossing games in the Fall 2018 Season
The three query results will then be passed onto the team_stats view (pages/team_stats).
The team_stats view will display all fo the football games for the season, show who won each game,
and show the total number of wins/losses for the season.
/player_info - get request (no parameters)
This route will handle a single query to the football_players table which will retrieve the id & name for all of the football players.
Next it will pass this result to the player_info view (pages/player_info), which will use the ids & names to populate the select tag for a form
************************************/
// login page
app.get('/', function(req, res) {
res.render('pages/login',{
local_css:"signin.css",
my_title:"Login Page"
});
});
// registration page
app.get('/register', function(req, res) {
res.render('pages/register',{
my_title:"Registration Page"
});
});
/*Add your other get/post request handlers below here: */
app.listen(3000);
console.log('3000 is the magic port');
app.get('/home', function(req, res) {
var query = 'select * from favorite_colors;';
db.any(query)
.then(function (rows) {
res.render('pages/home',{
my_title: "Home Page",
data: rows,
color: '',
color_msg: ''
})
})
.catch(function (err) {
// display error message in case an error
request.flash('error', err);
response.render('pages/home', {
title: 'Home Page',
data: '',
color: '',
color_msg: ''
})
})
});
app.get('/home/pick_color', function(req, res) {
var color_choice = req.query.color_selection;
var color_options = 'select * from favorite_colors;';
var color_message = "select color_msg from favorite_colors where hex_value = '" + color_choice + "';";
db.task('get-everything', task => {
return task.batch([
task.any(color_options),
task.any(color_message)
]);
})
.then(info => {
res.render('pages/home',{
my_title: "Home Page",
data: info[0],
color: color_choice,
color_msg: info[1][0].color_msg
})
})
.catch(error => {
// display error message in case an error
console.log(err);
response.render('pages/home', {
title: 'Home Page',
data: '',
color: '',
color_msg: ''
})
});
});
app.post('/home/pick_color', function(req, res) {
var color_hex = req.body.color_hex;
var color_name = req.body.color_name;
var color_message = req.body.color_message;
var insert_statement = "INSERT INTO favorite_colors(hex_value, name, color_msg) VALUES('" + color_hex + "','" +
color_name + "','" + color_message +"') ON CONFLICT DO NOTHING;";
var color_select = 'select * from favorite_colors;';
db.task('get-everything', task => {
return task.batch([
task.any(insert_statement),
task.any(color_select)
]);
})
.then(info => {
res.render('pages/home',{
my_title: "Home Page",
data: info[1],
color: color_hex,
color_msg: color_message
})
})
.catch(error => {
// display error message in case an error
request.flash('error', err);
response.render('pages/home', {
title: 'Home Page',
data: '',
color: '',
color_msg: ''
})
});
});
app.get('/team_stats', function(req, res)
{
var query = 'select * from football_games;';
var wins = 'select count(*) from football_games where home_score > visitor_score;';
var losses = 'select count(*) from football_games where home_score < visitor_score;';
db.task('get-everything', task => {
return task.batch ([
task.any(query),
task.any(wins),
task.any(losses)
]);
})
.then(data => {
res.render('pages/team_stats', {
my_title: "Team Stats",
games: data[0],
wins: data[1],
losses: data[2],
color: '',
color_msg: ''
})
})
.catch(error => {
// display error message in case an error
console.log(err);
res.render('pages/team_stats', {
title: 'Team Stats',
games: '',
wins: '',
losses: '',
color: '',
color_msg: ''
})
});
});
app.get('/player_info', function(req, res)
{
var query = 'select * from football_players;';
db.any(query)
.then(function (rows) {
res.render('pages/player_info', {
my_title: "Player Info",
data: rows,
color: '',
color_msg: ''
})
})
.catch(function (err){
// display error message in case an error
console.log(err);
res.render('pages/player_info', {
title: 'Player Info',
data: '',
color: '',
color_msg: ''
})
})
});
app.get('/player_info/post', function(req, res)
{
var players = 'select * from football_players;';
var player = req.query.player_choice;
var games = 'select count(*) from football_games where players = any(' + player + ');';
db.task('get-everything', task => {
return task.batch ([
task.any(players),
task.any(games)
]);
})
.then(data => {
res.render('pages/player_info', {
my_title: "Player Info",
players: data[0],
games: data[1],
color: '',
color_msg: ''
})
})
.catch(error => {
// display error message in case an error
console.log(err);
res.render('pages/player_info', {
title: 'Player Info',
players: '',
games: '',
color: '',
color_msg: ''
})
});
});