-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazonManager.js
More file actions
216 lines (196 loc) · 6.69 KB
/
bamazonManager.js
File metadata and controls
216 lines (196 loc) · 6.69 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
var mysql = require("mysql");
var inquirer = require("inquirer");
var Table = require('cli-table');
var connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "password",
database: "bamazonDB"
});
connection.connect(function (err) {
if (err) throw err;
console.log("connected as id " + connection.threadId + "\n");
managerMenu()
})
function managerMenu() {
inquirer.prompt([
{
name: "managerChoice",
type: "list",
message: "What would you like to do?",
choices: ["View Products for Sale", "View Low Inventory", "Add to Inventory", "Add New Product", "Quit"]
}
]).then(function (answer) {
switch (answer.managerChoice) {
case "View Products for Sale":
viewProducts();
break;
case "View Low Inventory":
checkInventory();
break;
case "Add to Inventory":
addToInventory();
break;
case "Add New Product":
addNewProduct();
break;
default:
connection.end();
}
})
}
function availableProducts() {
connection.query("select * from products", function (err, res) {
// instantiate
var table = new Table({
head: ['ID', 'Product', 'Department', 'Price', 'In Stock']
, colWidths: [10, 12, 15, 10, 12]
});
// table is an Array, so you can `push`, `unshift`, `splice` and friends
for (var 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());
managerMenu();
})
}
// If a manager selects View Products for Sale, the app should list every
//available item: the item IDs, names, prices, and quantities.
function viewProducts() {
console.log("**************************************************")
availableProducts();
}
// If a manager selects View Low Inventory, then it should list all items with an
//inventory count lower than five.
function checkInventory() {
console.log("**************************************************")
connection.query("select * from products", function (err, res) {
var table = new Table({
head: ['ID', 'Product', 'Department', 'Price', 'In Stock']
, colWidths: [10, 12, 15, 10, 12]
});
// table is an Array, so you can `push`, `unshift`, `splice` and friends
for (var i = 0; i < res.length; i++) {
if (res[i].Stock_quantity < 10) {
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());
managerMenu();
})
}
// If a manager selects Add to Inventory, your app should display a prompt
//that will let the manager "add more" of any item currently in the store.
function addToInventory(res) {
inquirer.prompt([
{
name: "addToItemId",
type: "input",
message: "Which product do you want to add to (enter the ID)?",
validate: function (value) {
if (isNaN(value) === false) {
return true;
}
console.log("\nInvalid Input. Please try again!")
}
},
{
name: "quantityOfItem",
type: "input",
message: "How many of that product would you like to add to inventory?",
validate: function (value) {
if (isNaN(value) === false) {
return true;
}
console.log("\nInvalid Input. Please try again!")
}
}
]).then(function (answer) {
connection.query("SELECT * FROM products where ?",
{
Item_id: answer.addToItemId
}, function (err, res) {
connection.query("UPDATE products SET ? WHERE ?",
[
{
Stock_quantity: parseInt(res[0].Stock_quantity) + parseInt(answer.quantityOfItem)
},
{
Item_id: answer.addToItemId
}
],
function (err) {
if (err) {
console.log("err", err);
throw err;
} else {
console.log(res[0].Product_name + " products updated!\n");
managerMenu();
}
}
)
})
})
}
// If a manager selects Add New Product, it should allow the manager to add
//a completely new product to the store.
function addNewProduct() {
inquirer.prompt([
{
name: "productName",
type: "input",
message: "What is the name of the product you would like to add?"
},
{
name: "department",
type: "list",
message: "Which department does this product fall into?",
choices: ["electronic", "outdoor", "shoe", "sport", "accessories", "makeup"]
},
{
name: "price",
type: "input",
message: "how much does it cost?",
validate: function (value) {
if (isNaN(value) === false) {
return true;
}
console.log("\nInvalid Input. Please try again!")
}
},
{
name: "quantity",
type: "input",
message: "How many do we have?",
validate: function (value) {
if (isNaN(value) === false) {
return true;
}
console.log("\nInvalid Input. Please try again!")
}
}
]).then(function (answer) {
//console.log(answer);
connection.query("INSERT INTO products SET ?",
{
Product_name: answer.productName,
Department_name: answer.department,
Price: answer.price,
Stock_quantity: answer.quantity
},
function (err, res) {
if (err) {
throw err;
} else {
console.log(res.affectedRows + " product inserted!\n");
managerMenu();
}
}
)
})
}