-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory_route.js
182 lines (152 loc) · 4.2 KB
/
inventory_route.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
const express = require('express');
const inventoryRouter = express.Router();
const db = require('./db.js');
function itemExistsInTable (itemName) {
const query = `SELECT * FROM Inventory WHERE item = $itemName LIMIT 1`
return new Promise((resolve, reject) => {
db.get(query, {
$itemName: itemName
},function(err, row) {
if(err) {
reject(err);
}
if (row) {
resolve(true)
}
resolve(false)
});
})
}
function addSpacesToUrl(url) {
return url.replace("%20", " ")
}
inventoryRouter.get('/', (req, res, next) => {
// offset - what row to start at
// limit - how many results to return
const query = `SELECT * from Inventory ORDER BY id ASC LIMIT $limit OFFSET $offset`;
let limit = req.query.limit
let offset = ((req.query.page - 1) * limit)
db.all(query, {$limit: limit, $offset: offset, }, function(err, rows) {
if(err) {
throw err;
}
res.json(rows)
})
})
inventoryRouter.post('/add',async function(req,res,next) {
const item = req.body;
itemExists = await itemExistsInTable(item.item)
if (itemExists) {
const error = new Error(`"${item.item}" item already exists. Update the item instead!`)
error.status = 409;
return next(error);
}
let sql = `INSERT INTO Inventory (item, quantity, price)
VALUES ($item, $quantity, $price)`;
db.run(sql,{
$item: item.item,
$quantity: item.quantity,
$price: item.price
}, function(err) {
if (err) {
console.log('SERVER SIDE ERROR - Unable to add item')
const error = new Error('Unable to add item!')
error.status = 400;
return next(error);
}
res.json({'rowId':this.lastID})
})
})
inventoryRouter.put('/update',async function(req,res,next) {
const item = req.body;
itemExists = await itemExistsInTable(item.item)
if (!itemExists) {
const error = new Error(`"${item.item}" does not exist. Add the item instead!`)
error.status = 404;
return next(error);
}
sql = `UPDATE Inventory
SET quantity = $quantity,
price = $price
WHERE
item = $item`
db.run(sql,{
$item: item.item,
$quantity: item.quantity,
$price: item.price
}, function(err) {
if (err) {
console.log('SERVER SIDE ERROR - Unable to update item')
const error = new Error('Unable to update item!')
error.status = 400;
return next(error);
}
res.json({'rowId':this.lastID})
})
})
inventoryRouter.delete('/delete/:item',async function(req,res,next) {
item = addSpacesToUrl(req.params.item)
itemExists = await itemExistsInTable(item)
if (!itemExists) {
const error = new Error(`"${item}" item does not exist. Nothing to delete!`)
error.status = 404;
return next(error);
}
sql = `DELETE FROM Inventory WHERE item = "${item}"`
db.run(sql,{
$item: item.item
}, function(err) {
if (err) {
console.log(err)
console.log('SERVER SIDE ERROR - Unable to delete item')
const error = new Error('Unable to delete item!')
return next(error);
}
res.json({'rowId':this.lastID})
})
})
inventoryRouter.get('/facts/totalItems', (req, res, next) => {
const query = `SELECT COUNT(*) FROM Inventory`;
db.get(query, function(err, row) {
if(err) {
throw err;
}
res.json({
totalItems: row["COUNT(*)"]
})
});
})
inventoryRouter.get('/facts/totalQuantity', (req, res, next) => {
const query = `SELECT SUM(quantity) FROM Inventory`;
db.get(query, function(err, row) {
if(err) {
throw err;
}
res.json({
totalItems: row["SUM(quantity)"]
})
});
})
inventoryRouter.get('/facts/mostInStock', (req, res, next) => {
const query = `SELECT *, MAX(quantity) FROM Inventory`;
db.get(query, function(err, row) {
if(err) {
throw err;
}
res.json({
mostInStock: row['item']
})
});
})
inventoryRouter.get('/facts/leastInStock', (req, res, next) => {
const query = `SELECT *, MIN(quantity) FROM Inventory`;
db.get(query, function(err, row) {
if(err) {
throw err;
}
res.json({
leastInStock: row['item']
})
});
})
module.exports = inventoryRouter;