This repository has been archived by the owner on Jul 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
53 lines (47 loc) · 1.46 KB
/
router.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
var express = require('express');
var router = express.Router();
var db = []
router.get('/', function (req, res) {
res.send('Welcome to FIT2095 Lab 3 Home Page');
});
router.get('/newItem/:item/:quantity/:price', function (req, res) {
item = {
id: Math.round(Math.random() * 1000),
item: req.params.item,
quantity: req.params.quantity,
price: req.params.price
}
db.push(item)
res.send("Items added");
});
router.get('/listAllItems', function (req, res) {
let output = "ID Item Quantity Price Cost</br>";
for (let i = 0; i < db.length; i++) {
output += db[i].id + ' | ' + db[i].item + ' | ' + db[i].quantity + ' | ' + db[i].price + ' | ' + (parseFloat(db[i].price) * parseFloat(db[i].quantity)) + ' </br>';
}
res.send(output);
});
router.get('/deleteItem/:id', function (req, res) {
let itemID = req.params.id;
let msg = "Element ID does not exist!";
db.forEach(function (entry) {
if (entry.id == itemID) {
let index = db.indexOf(entry)
elem = db[index];
db.splice(index, 1);
msg = "Element " + elem.item + " removed";
return
}
});
res.send(msg);
}
);
router.get('/totalValue', function (req, res) {
let total = 0
db.forEach(function (entry) {
total += entry.price * entry.quantity;
})
res.send("Total warehouse value: R" + total);
});
//export this router
module.exports = router;