-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
95 lines (90 loc) · 2.44 KB
/
app.js
File metadata and controls
95 lines (90 loc) · 2.44 KB
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
const mysql = require("mysql");
const inquirer = require("inquirer");
const Table = require("cli-table");
const connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "root",
database: "bamazon"
});
connection.connect(function(err) {
if (err) throw err;
console.log("connected as id " + connection.threadId + "\n");
currentItems();
});
function currentItems() {
console.log("Items currently avaialble for sell:");
connection.query("SELECT * FROM products", function(err, res) {
if (err) throw err;
let table = new Table({
head: ["Item ID", "Product Name", "Department", "Price", "# in Stock"],
colWidths: [15, 35, 20, 10, 15]
});
for (let i = 0; i < res.length; i++) {
table.push([
res[i].item_id,
res[i].product_name,
res[i].department_name,
res[i].price,
res[i].stock_quantity
]);
}
console.log(table.toString());
goShopping();
});
}
function goShopping() {
inquirer
.prompt([
{
name: "item",
type: "input",
message: "What is the item ID of the item you would like to purchase?"
},
{
name: "quantity",
type: "input",
message: "How many of this item do you want to buy?"
}
])
.then(function(answer) {
const purchaseItemID = answer.item;
const purchaseItemQuantity = answer.quantity;
purchaseItems(purchaseItemID, purchaseItemQuantity);
});
}
function purchaseItems(itemID, itemQuantity) {
connection.query("SELECT * FROM products WHERE item_id = " + itemID, function(
err,
res
) {
if (err) throw err;
const itemPrice = res[0].price;
const stock_quantity = res[0].stock_quantity;
const new_stock_quantity = stock_quantity - itemQuantity;
if (new_stock_quantity <= 0) {
console.log("\nWe don't have that many items to sell, sorry!");
currentItems();
} else {
let query = connection.query(
"UPDATE products SET ? WHERE ?",
[
{
stock_quantity: new_stock_quantity
},
{
item_id: itemID
}
],
function(err, res) {
if (err) throw err;
const totalCost = itemPrice * itemQuantity;
console.log("\nYou just bought " + itemQuantity + " items!");
console.log("Total cost: $" + totalCost + "\n");
currentItems();
}
);
}
});
}