-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
115 lines (89 loc) · 2.86 KB
/
app.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
'use strict';
const express = require('express')
const app = express()
const port = 3000
const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded())
global.orders=[]
let states=[]
states.push("ordered")
states.push("cooked")
states.push("served")
states.push("paid")
app.post('/PlaceOrder', (req, res) => {
const order={}
order.state= "ordered"
order.tableNumber = req.body["tableNumber"]
delete req.body.tableNumber
order.items = req.body
order.number = global.orders.length+1 //Note, the order number is 1 more than the orders index in the array (becuase we don't want an order #0)
global.orders.push (order)
res.send(`
<html>
<body>
Order Accepted # ${order.number}
</body>
</html>
`)
})
app.get('/view', (req, res) => {
outputOrders(req,res)
})
app.get('/setState',(req, res) => {
setOrderState(req,res)
outputOrders(req, res)
})
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
const path=require("path")
app.use(express.static(path.join(__dirname, 'public')));
function outputOrders(req,res){
let filter=req.query["filter"]
let ordersHTML=[]
ordersHTML.push("<html><head><link type='text/css' rel='stylesheet' href='/css/style.css'></head><body>")
ordersHTML.push("<table id='ordersTable'>")
ordersHTML.push("<tr><th>Order#</th><th>Table#</th><th>Items</th><th>State</th><th>Mark as..</th></tr>")
for (const order of global.orders){
if (filter==null || order.state==filter){
ordersHTML.push(orderHTML(order))
}
}
ordersHTML.push('</table>')
ordersHTML.push('</body></html>')
res.send(ordersHTML.join(''))
}
function orderHTML(order){
let elements=[]
elements.push("<tr>")
elements.push(`<td>${order.number}</td>`)
elements.push(`<td>${order.tableNumber}</td>`)
elements.push("<td>")
for (const key in order.items){
const quantity=order.items[key]
if (quantity>0){
elements.push( quantity + " * " + key + "<br>" )
}
}
elements.push("</td>")
elements.push(`<td class=${order.state}>${order.state}</td>`)
elements.push("<td>" + stateButtons(order) + "</td>")
elements.push("</tr>")
console.log (order);
return (elements.join(""))
}
function stateButtons(order){
let buttons=[]
for(const state of states){
buttons.push(`<a href=/setState?orderNumber=${order.number}&state=${state}><button>${state}</button></a>`)
}
return (buttons.join('<br>'))
}
function setOrderState(req,res){
//transition state - based on a ?state=ordernum NameValue pair
let order = global.orders[parseInt(req.query["orderNumber"])-1]
order.state=req.query["state"]
}