-
Notifications
You must be signed in to change notification settings - Fork 0
/
final.js
269 lines (231 loc) · 12.7 KB
/
final.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
/*******************************************************************************
* Filename: final.js *
* Author: Daniel Wilson *
* E-mail: [email protected] *
* Date: 6 August 2017 *
* *
* Description: Implements a single-page web application which is intended to be*
* used as a workout journal. The page will display a list of workout *
* routines, along with related information, in a tabular form and allow the *
* user to edit, add, and delete items from the table using asynchronous *
* JavaScript calls which allow the results to be shown without a page *
* refresh. This application will persist even after the user logs out, so it*
* will be an interface to a MySQL database, as opposed to keeping data local*
* in some cookies using sessions. *
* *
*******************************************************************************/
var express = require('express');
var app = express();
var mysql = require('./dbcon.js');
var handlebars = require('express-handlebars').create({defaultLayout:'main'});
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
app.set('port', /*PRIVATE*/);
app.use(express.static('public'));
/*******************************************************************************
******************************* ROUTE DEFINITIONS ******************************
*******************************************************************************/
/*******************************************************************************
* Route Address: "/" *
* Request Type: GET *
* *
* Description: Loads all previous entries from the workout database for the *
* user and displays all those entires in a formatted table. *
* At the top of the page is a form to add a new entry into the *
* database and will update the table without a page refresh. *
* Alongside each of the table rows, will be buttons that allow *
* the user to edit or delete each entry. *
*******************************************************************************/
app.get('/',function(req, res, next) {
var context = {};
var exerciseInfo = [];
context.script = '<script src="js/buttonScript.js"></script>';
context.css = '<link rel="stylesheet" href="css/table.css">';
mysql.pool.query('SELECT * FROM workouts', function(err, rows, fields) {
if(err){
next(err);
return;
}
rows.forEach(function(currentEntry) {
var weight = (currentEntry.weight === 0 ? "–" : currentEntry.weight);
var reps = (currentEntry.reps === 0 ? "–" : currentEntry.reps);
var units = (currentEntry.lbs === 0 ? "kgs" : "lbs");
exerciseInfo.push({"id" : currentEntry.id, "weight": weight,
"date" : currentEntry.date, "units" : units,
"name" : currentEntry.name, "reps" : reps});
});
context.exercises = exerciseInfo;
res.render('workoutTracker', context);
});
});
/*******************************************************************************
* Route Address: "/insert" *
* Request Type: GET *
* *
* Description: Takes a query string (requires a name) and uses that data to *
* create a new entry in our database. *
* *
* Credit: Modified from CS290 lecture examples *
*******************************************************************************/
app.get('/insert',function(req, res, next){
var context = {};
var columns = "(name, weight, lbs, reps, date)";
var values = "(?, ?, ?, ?, ?)";
var reqArray = [req.query.name, req.query.weight, req.query.units,
req.query.reps, req.query.date];
mysql.pool.query("INSERT INTO workouts " + columns + " VALUES " + values,
reqArray, function(err, result) {
if (err) {
next(err);
return;
}
/* Despite not actually wanting to go to this page, the homepage will
not dynamically update its HTML with a render call here, I'm unsure
why. Further research needed...
*/
res.render("insert", context);
});
});
/*******************************************************************************
* Route Address: "/edit" *
* Request Type: GET *
* *
* Description: Takes a query string (requires an id) and uses that data to *
* load a page with a pre-filled form of that id's database *
* contents and allows the user to update the values. *
* *
* Credit: Modified from CS290 lecture examples *
*******************************************************************************/
app.get('/edit', function(req, res, next) {
var context = {};
context.script = '<script src="js/editScript.js"></script>';
mysql.pool.query("SELECT * FROM workouts WHERE id=?", [req.query.id],
function(err, result) {
if (err) {
next(err);
return;
}
// Pre-fills out form with data from given id entry in database
context.name = result[0].name;
context.weight = result[0].weight;
context.reps = result[0].reps;
context.id = result[0].id;
context.date = (result[0].date === "" ? "0000-00-00" : result[0].date);
if (result[0].lbs === 1)
context.isPounds = true;
res.render("edit", context);
});
});
/*******************************************************************************
* Route Address: "/reset-table" *
* Request Type: GET *
* *
* Description: Removes all entries from the database when the user goes to *
* this URL. This is understood to be poor practice in a *
* professional setting. *
* *
* Credit: Modified from CS290 lecture examples *
*******************************************************************************/
app.get('/reset-table',function(req, res, next) {
mysql.pool.query("DROP TABLE IF EXISTS workouts", function(err) {
var createString = "CREATE TABLE workouts(" +
"id INT PRIMARY KEY AUTO_INCREMENT," +
"name VARCHAR(255) NOT NULL," +
"reps INT," +
"weight INT," +
"date DATE," +
"lbs BOOLEAN)";
mysql.pool.query(createString, function(err) {
res.render('home');
});
});
});
/*******************************************************************************
* Route Address: "/delete" *
* Request Type: GET *
* *
* Description: Takes a query string (requires an id) and uses that data to *
* remove an existing entry from our database. *
* *
* Credit: Modified from CS290 lecture examples *
*******************************************************************************/
app.get('/delete',function(req, res, next) {
mysql.pool.query("DELETE FROM workouts WHERE id=?", [req.query.id],
function(err, result) {
if(err){
next(err);
return;
}
res.render('delete');
});
});
/*******************************************************************************
* Route Address: "/safe-update" *
* Request Type: GET *
* *
* Description: Takes a query string (requires an id) and uses that data to *
* load a page with a pre-filled form of that id's database *
* contents and allows the user to update the values. *
* *
* Credit: Modified from CS290 lecture examples *
*******************************************************************************/
app.get('/safe-update',function(req, res, next) {
mysql.pool.query("SELECT * FROM workouts WHERE id=?", [req.query.id],
function(err, result){
if(err) {
next(err);
return;
}
if(result.length == 1) {
var curVals = result[0];
var updateThese = "name=?, reps=?, weight=?, date=?, lbs=?";
var updatedValues = [req.query.name || curVals.name,
req.query.reps || curVals.reps,
req.query.weight || curVals.weight,
req.query.date || curVals.date,
req.query.lbs || curVals.lbs,
req.query.id];
mysql.pool.query("UPDATE workouts SET " + updateThese + " WHERE id=?",
updatedValues, function(err, result) {
if (err) {
next(err);
return;
}
res.render('insert');
});
}
});
});
/*******************************************************************************
********************************* ERROR HANDLING *******************************
*******************************************************************************/
/*******************************************************************************
* Error Number: 404 *
* Error Type: Not Found *
* *
* Description: Takes the user to a predefined error 404 page. *
* *
* Credit: Taken from CS290 lecture examples *
*******************************************************************************/
app.use(function(req, res){
res.status(404);
res.render('404');
});
/*******************************************************************************
* Error Number: 500 *
* Error Type: Internal Server Error *
* *
* Description: Takes the user to a predefined error 500 page. *
* *
* Credit: Taken from CS290 lecture examples *
*******************************************************************************/
app.use(function(err, req, res, next){
console.error(err.stack);
res.status(500);
res.render('500');
});
// Sets up the server to listen in for traffic on the preset port
app.listen(app.get('port'), function(){
console.log('Express started on http://localhost:' + app.get('port') +
'; press Ctrl-C to terminate.');
});