-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
104 lines (96 loc) · 2.15 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
const pg = require("pg")
const client = new pg.Client('postgres://localhost/warehouse')
const express = require("express")
const app = express();
const port = 3000;
app.listen(port, () => {
console.log(`listening on port ${port}`)
})
app.get('/', async(req, res, next) => {
try {
const response = await client.query("SELECT * FROM inventory")
const inv = response.rows
res.send(`
<html>
<title> Warehouse Inventory </title>
<h1>
<a href='/'>
Inventory
</a>
</h1>
<body>
Products
</body>
<ul>
${inv.map(product => `
<li>
<a href='/products/${product.location}'>
${product.item}
</a>
</li>
`).join('')}
</ul>
</html>
`)
}
catch(ex){
next(ex)
}
})
app.get('/products/:location', async(req, res, next) => {
try {
const response = await client.query("SELECT * FROM inventory WHERE location=$1", [req.params.location])
const inv = response.rows
res.send(`
<html>
<title> Warehouse Inventory </title>
<h1>
<a href='/'>
Inventory
</a>
</h1>
<body>
Products
</body>
<ul>
${inv.map(product => `
<li>
<a href='/products/${product.location}'>
${product.item}
</a>
</li>
`).join('')}
</ul>
</html>
`)
}
catch(ex){
next(ex)
}
})
const syncAndSeed = async() => {
const SQL = `
DROP TABLE IF EXISTS inventory;
CREATE TABLE inventory(
ORG varchar(255),
SUBINVENTORY varchar(255),
ITEM varchar(255),
LOCATION varchar(255)
);
INSERT INTO inventory(ORG, SUBINVENTORY, ITEM, LOCATION) VALUES('PAD', 'BP-A', 'HY144-D', '570801');
INSERT INTO inventory(ORG, SUBINVENTORY, ITEM, LOCATION) VALUES('PAD', 'BP-A', 'HY256-TL', '570802');
INSERT INTO inventory(ORG, SUBINVENTORY, ITEM, LOCATION) VALUES('PAD', 'BP-A', 'NY64-D', '570601');
`
await client.query(SQL)
}
const setUp = async() => {
try {
await client.connect()
await syncAndSeed();
console.log("connected to database")
}
catch (ex) {
console.log(ex)
}
}
setUp();