-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazonManager.js
More file actions
148 lines (141 loc) · 3.56 KB
/
bamazonManager.js
File metadata and controls
148 lines (141 loc) · 3.56 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
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
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");
actionOptions();
});
function actionOptions() {
inquirer
.prompt([
{
name: "action",
type: "list",
message: "What would you like to do?",
choices: [
"View Products for Sale",
"View Low Inventory",
"Add to Inventory",
"Add New Product"
]
}
])
.then(function(answer) {
switch (answer.action) {
case "View Products for Sale":
currentItems();
break;
case "View Low Inventory":
lowInventory();
break;
case "Add to Inventory":
addToInventory();
break;
case "Add New Product":
// New Product Function
break;
default:
console.log("Unknown option somehow selected... That's magic.");
break;
}
});
}
function currentItems() {
console.log("\nItems 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());
actionOptions();
});
}
function lowInventory() {
console.log("\nItems with less than 20 in stock:");
connection.query("SELECT * FROM products WHERE stock_quantity < 20", 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());
actionOptions();
});
}
function addToInventory() {
inquirer
.prompt([
{
name: "item",
type: "input",
message: "What is the item ID of the item you would like to restock?"
},
{
name: "quantity",
type: "input",
message: "How many of this item do you want to add?"
}
])
.then(function(answer) {
const stockItemID = answer.item;
const stockItemQuantity = answer.quantity;
addItems(stockItemID, stockItemQuantity);
});
}
function addItems(itemID, itemQuantity) {
connection.query("SELECT * FROM products WHERE item_id = " + itemID, function(
err,
res
) {
if (err) throw err;
const stock_quantity = Number(res[0].stock_quantity);
const new_stock_quantity = stock_quantity + parseInt(itemQuantity);
let query = connection.query(
"UPDATE products SET ? WHERE ?",
[
{
stock_quantity: new_stock_quantity
},
{
item_id: itemID
}
],
function(err, res) {
if (err) throw err;
console.log("\nYou just added " + itemQuantity + " items!");
actionOptions();
}
);
});
}