-
Notifications
You must be signed in to change notification settings - Fork 0
/
bamazonManager.js
212 lines (194 loc) · 5.86 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
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
const inquirer = require("inquirer");
const mysql = require("mysql");
const Table = require("cli-table");
let moreProduct;
const connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "yuffie",
database: "bamazonDB"
});
connection.connect(function(err){
if (err) throw err;
console.log("connected as id " + connection.threadId + "\n");
menu();
});
const table = new Table({
head: ['Id', 'Product', 'Price', 'In Stock'], colWidths: [5, 20, 10, 10]
});
// [x] display menu (list)
// [x] View Products for Sale
// [x] View Low Inventory
// [x] Add to Inventory
// [x] Add New Product
function menu(){
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",
"exit"
]
}).then(function(answer){
switch(answer.action){
case "View Products for Sale":
products();
break;
case "View Low Inventory":
lowInventory();
break;
case "Add to Inventory":
addToInventory();
break;
case "Add New Product":
addProduct();
break;
case "exit":
connection.end();
break;
}
});
}
// View Products for Sale
// [x] display all items available for sale
// [x] displays item ids
// [x] displays item name
// [x] displays item price
// [x] displays item quantity
function products(){
connection.query("SELECT * from products", function (err, res) {
if (err) throw err;
console.log("The available items are:");
res.forEach((res) => {
table.push( [`${res.item_id}`, `${res.product_name}`, `$${res.price}`, `${res.stock_quantity}`])
});
console.log(table.toString());
connection.end();
});
}
// View Low Inventory
// [x] display all items with an inventory lower than 5
function lowInventory(){
let query = "SELECT item_id, product_name, stock_quantity FROM products WHERE stock_quantity <= 5";
const lowInv = new Table ({
head: ['Id', 'Product', 'In Stock'], colWidths: [5,20,10]
})
connection.query(query, function(err, res) {
if (err) throw err;
console.log("The following products have an inventory less than 5: ");
res.forEach((res) => {
lowInv.push([`${res.item_id}`, `${res.product_name}`, `${res.stock_quantity}`]);
});
console.log(lowInv.toString());
connection.end();
})
}
// Add to Inventory
// [x] prompt user to "add more" of item
function addToInventory(){
inquirer.prompt({
name: "item_id",
type: "number",
message: "What is the item number for the product?"
}).then(function(response){
let query = "SELECT item_id, product_name, stock_quantity FROM products WHERE ?";
connection.query(query, { item_id: response.item_id}, function(err, res){
if(err) throw err;
for (let i =0; i < res.length; i++){
moreProduct = res[i];
console.log(
"The following item has been selected:\n" +
res[i].product_name +
"\nIn stock: " +
res[i].stock_quantity
);
}
increaseQuant(moreProduct);
});
});
}
function increaseQuant(item){
inquirer.prompt({
name: "amount",
type: "number",
message: "How much is being added?"
}).then(function(response){
let input = response.amount;
let query = "SELECT stock_quantity FROM products";
connection.query(query, function(err, res){
if (err) throw err;
let newQuantity = item.stock_quantity + input;
connection.query( "UPDATE products SET ? WHERE ?",
[
{
stock_quantity: newQuantity
},
{
item_id: item.item_id
}
], function (err, res){
if(err) throw err;
console.log("Stock quantity updated.");
});
connection.end();
});
});
}
// Add New Product
// [x] will allow user to add a new product
function addProduct(){
inquirer.prompt([
{
name: "productName",
type: "input",
message: "What is the name of the product?"
},
{
name: "department",
type: "input",
message: "What department will this product be in?"
},
{
name: "price",
type: "number",
message: "How much does this product cost?",
validate: function(value) {
if (isNaN(value) === false){
return true;
}
return false;
}
},
{
name: "stock",
type: "number",
message: "How much product?",
validate: function(value) {
if (isNaN(value) === false){
return true;
}
return false;
}
}
]).then(function(answer){
connection.query(
"INSERT INTO products SET ?",
{
product_name: answer.productName,
department_name: answer.department,
price: answer.price,
stock_quantity: answer.stock
},
function(err){
if(err) throw err;
console.log("Product added!");
}
)
connection.end();
})
}