-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathindex.js
297 lines (255 loc) · 10.4 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
const express = require('express')
const path = require('path')
const PORT = process.env.PORT || 5000
const { Pool } = require('pg');
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});
express()
.use(express.static(path.join(__dirname, 'public')))
.use(express.urlencoded({extended: true}))
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'ejs')
.get('/test', (req, res) => res.render('pages/test', {users: ["John", "Paul", "Ringo"]}))
.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
})
.get('/order', (req, res) => {
const first_name = (req.query.first) ? req.query.first : "";
const last_name = (req.query.last) ? req.query.last : "";
let entree = "";
let sideList = ""
let order = "";
if (req.query.entree) {
entree = req.query.entree;
sideList = getSides(req.query);
order = getOrderText(entree, sideList);
}
let menu_info = {first: first_name, last: last_name,
order: order}
if (validateMenu(first_name, last_name, entree, sideList)) {
let confirm_info = menu_info;
confirm_info.streetaddress = "";
confirm_info.cityaddress = "";
res.render('pages/confirmation', confirm_info);
} else {
res.render('pages/menu', menu_info);
}
})
.post('/confirm', async (req, res) => {
const first_name = req.body.first;
const last_name = req.body.last;
const street_address = req.body.streetaddress;
const city_state = req.body.cityaddress;
const order = req.body.order;
let confirm_info = {first: first_name, last: last_name, streetaddress: street_address,
cityaddress: city_state, order: order};
if (validateConfirm(first_name, last_name, street_address, city_state, order)) {
// Push the new information to the database
// and get the result for the new order number
//
// example insert
// INSERT INTO order_table (first_name, last_name, street_address,
// city_address, food_order, order_time, order_status)
// VALUES ('Hope', 'Dog', '12 Street St', 'Northampton, MA',
// 'Fake order foods 4', now(), 'Received')
// RETURNING id;
let query_text = "INSERT INTO order_table (first_name, last_name, street_address, ";
query_text += "city_address, food_order, order_time, order_status) ";
query_text += "VALUES ('" + first_name + "', '" + last_name + "', '" + street_address + "', '";
query_text += city_state + "', '" + order + "', now(), 'Received') RETURNING id;";
try {
const client = await pool.connect();
// INSERT the new order information
const result = await client.query(query_text);
// get the new ID number returned from the INSERT query
const order_number = (result) ? result.rows[0].id : null;
// with the new order number, get the appropriate customer info
const select_result = await client.query('SELECT * FROM order_table WHERE id = ' + order_number);
const results = (select_result) ? select_result.rows[0] : null;
const order_status = results.order_status;
const first_name = results.first_name;
const last_name = results.last_name;
const street_address = results.street_address;
const city_state = results.city_address;
const order = results.food_order;
let customer_info = {first: first_name, last: last_name, streetaddress: street_address,
cityaddress: city_state, order: order, ordernumber: order_number,
orderstatus: order_status};
res.render('pages/customerstatus', customer_info);
client.release();
} catch (err) {
console.error(err);
res.send("Error " + err);
}
} else {
res.render('pages/confirmation', confirm_info);
}
})
// /status is the customer facing status page
.get('/status', async (req, res) => {
// replace first_name and everything from body with only the order number
// the order number should be used to retrieve everything from the database.
const order_number = req.query.ordernumber;
// retrieve order info from database, determined by ordernumber
//
try {
const client = await pool.connect();
const result = await client.query('SELECT * FROM order_table WHERE id = ' + order_number);
const results = (result) ? result.rows[0] : null;
// assemble the local variables for the order status
const order_status = results.order_status;
const first_name = results.first_name;
const last_name = results.last_name;
const street_address = results.street_address;
const city_state = results.city_address;
const order = results.food_order;
let customer_info = {first: first_name, last: last_name, streetaddress: street_address,
cityaddress: city_state, order: order, ordernumber: order_number,
orderstatus: order_status};
res.render('pages/customerstatus', customer_info);
client.release();
} catch (err) {
console.error(err);
res.send("Error " + err);
}
})
.get('/service', async (req, res) => {
try {
// query the db for all the orders
const client = await pool.connect();
const result = await client.query('SELECT * FROM order_table');
const results = (result) ? result.rows : null;
// format the db results into orders for rendering
let orders = [];
for( let i=0; i<results.length; i++ ) {
let o = results[i];
orders.push({ timestamp: o.order_time,
order: o.food_order,
id: o.id,
first: o.first_name,
last: o.last_name,
streetaddress: o.street_address,
cityaddress: o.city_address,
orderstatus: o.order_status});
}
// render the page with the orders
res.render('pages/servicestatus', {orders: orders});
client.release();
} catch (err) {
console.error(err); res.send("Error " + err);
}
})
.post('/service', async (req, res) => {
try {
const order_number = req.body.id;
// GET THE CURRENT ORDER_STATUS
const client = await pool.connect();
const old_status_result = await client.query('SELECT order_status FROM order_table WHERE id=' + order_number);
old_status = old_status_result.rows[0].order_status;
// EXAMPLE UPDATE
// update order_table set order_status='Cooking' where id=1;
//
// 'Received' -> 'Cooking'
// 'Cooking' -> 'Out For Delivery'
// 'Out For Delivery' -> 'Delivered'
// 'Delivered' -> 'Delivered'
let new_status = "";
if (old_status === 'Received')
new_status = 'Cooking';
else if (old_status === 'Cooking')
new_status = 'Out For Delivery';
else
new_status = 'Delivered';
// update the db with the new status
await client.query("UPDATE order_table set order_status='" + new_status + "' where id=" + order_number);
// query the db for all the orders
const order_result = await client.query('SELECT * FROM order_table');
const results = (order_result) ? order_result.rows : null;
// format the db results into orders for rendering
let orders = [];
for( let i=0; i<results.length; i++ ) {
let o = results[i];
orders.push({ timestamp: o.order_time,
order: o.food_order,
id: o.id,
first: o.first_name,
last: o.last_name,
streetaddress: o.street_address,
cityaddress: o.city_address,
orderstatus: o.order_status});
}
// render the page with the orders
res.render('pages/servicestatus', {orders: orders});
client.release();
} catch (err) {
console.error(err); res.send("Error " + err);
}
})
// /db is a debugging view into the complete order_table database table
.get('/db', async (req, res) => {
try {
const client = await pool.connect();
const result = await client.query('SELECT * FROM order_table');
const results = { 'results': (result) ? result.rows : null};
res.render('pages/db', results );
client.release();
} catch (err) {
console.error(err);
res.send("Error " + err);
}
})
.listen(PORT, () => console.log(`Listening on ${ PORT }`))
/* HELPER FUNCTIONS BELOW
*/
// server side validation for the menu page submissions
function validateMenu(first_name, last_name, entree, sideList) {
let valid = false;
if (first_name.length != 0 &&
last_name.length != 0 &&
entree != undefined &&
sideList.length === 3) {
valid = true;
}
return valid;
}
// server side validaiton for the confirm page submissions
function validateConfirm(first_name, last_name, street_address, city_state, order) {
let valid = false;
if (first_name.length != 0 &&
last_name.length != 0 &&
street_address.length != 0 &&
city_state.length != 0 &&
order.length != 0 ) {
valid = true;
}
return valid;
}
// build a single string formatted order from the
// entree and sides
function getOrderText(entree, sideList) {
order = entree;
sideList.forEach(function(r) {
order += ", " + r;
});
return order;
}
// convert the item's buttons into strings
// for each of the side dishes
function getSides(body) {
sides = [];
if (body.item0 === "on")
sides.push("Corn Bread")
if (body.item1 === "on")
sides.push("Creamed Corn")
if (body.item2 === "on")
sides.push("Green Beans")
if (body.item3 === "on")
sides.push("Mashed Potatos")
if (body.item4 === "on")
sides.push("Baked Beans")
return sides;
}