-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamazonManager.js
172 lines (159 loc) · 7.32 KB
/
bamazonManager.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
var mysql = require("mysql");
var inquirer = require("inquirer");
var connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "PYP02kky",
database: "bamazon"
});
connection.connect(function (err) {
if (err) throw err;
start();
});
function start() {
inquirer
.prompt({
name: "manager",
type: "list",
message: "Would you like to do today?",
choices: ["View Products for Sale", "View Low Inventory", "Add to Inventory", "Add New Product", "Exit"]
})
.then(function (answer) {
switch (answer.manager) {
case "View Products for Sale":
connection.query("SELECT * FROM products", function (err, results) {
if (err) throw err;
for (var i = 0; i < results.length; i++) {
var showData = [
"ID: " + results[i].id,
"Prduct Name: " + results[i].product_name,
"Price: " + results[i].price,
"Quantity: " + results[i].stock_quantity,
"---------------------------------------"
].join("\n");
console.log(showData);
};
start();
});
break;
case "View Low Inventory":
connection.query("SELECT * FROM products", function (err, results) {
if (err) throw err;
var numLow = 0;
for (var i = 0; i < results.length; i++) {
var showData;
if (results[i].stock_quantity < 5) {
showData = [
"ID: " + results[i].id,
"Prduct Name: " + results[i].product_name,
"Price: " + results[i].price,
"Quantity: " + results[i].stock_quantity,
"---------------------------------------"
].join("\n");
numLow++
console.log(showData);
}
};
if (numLow === 0) {
console.log("All items are well Stocked.");
}
start();
});
break;
case "Add to Inventory":
inquirer
.prompt([{
name: "item",
type: "number",
message: "What is the ID of the product you would like to restock?"
},
{
name: "addedQuantity",
type: "number",
message: "How much of the product you would like to restock?"
}])
.then(function (answer) {
connection.query("SELECT * FROM products", function (err, results) {
if (err) throw err;
var chosenItem;
for (var i = 0; i < results.length; i++) {
if (results[i].id === answer.item) {
chosenItem = results[i];
}
}
var newStock = chosenItem.stock_quantity + answer.addedQuantity;
connection.query(
"UPDATE products SET ? WHERE ?",
[
{
stock_quantity: newStock
},
{
id: chosenItem.id
}
],
function (error) {
if (error) throw err;
console.log("You successfully added " + answer.addedQuantity + " items!");
start();
}
);
});
})
break;
case "Add New Product":
var departments = []
connection.query("SELECT department_name FROM departments", function (err, results) {
if (err) throw err;
for (var i = 0; i < results.length; i++) {
departments.push( results[i].department_name);
}
inquirer
.prompt([
{
name: "item",
type: "input",
message: "What is the name of the product you would like to add?"
},
{
name: "department",
type: "list",
message: "What department does the product belong to?",
choices: departments
},
{
name: "price",
type: "number",
message: "What is its price?"
},
{
name: "stock",
type: "number",
message: "How much would you like to stock?"
}
])
.then(function (answer) {
connection.query(
"INSERT INTO products SET ?",
{
product_name: answer.item,
department_name: answer.department,
price: answer.price,
stock_quantity: answer.stock
},
function (err) {
if (err) throw err;
console.log("The product was added and stocked successfully!");
start();
}
);
});
})
break;
case "Exit":
connection.end();
break;
}
});
}