-
Notifications
You must be signed in to change notification settings - Fork 0
/
bamazonManager.js
222 lines (195 loc) · 6.52 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
213
214
215
216
217
218
219
220
221
222
var mysql = require("mysql");
var inquirer = require("inquirer");
var connection = mysql.createConnection({
host: "localhost",
// Your port; if not 3306
port: 3306,
// Your username
user: "root",
// Your password
password: "",
database: "bamazon"
});
connection.connect(function(err) {
if (err) throw err;
runOptions();
});
var line = "----------------------------------------------------------------------------------------------------------------------------------";
function runOptions() {
console.log("Menu options:")
console.log(line)
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":
queryProducts('sale', continueView);
break;
case "View Low Inventory":
queryProducts('lowInventory', continueView);
break;
case "Add to Inventory":
queryProducts('sale', addInventory);
break
case "Add New Product":
createProduct(continueView);
break;
default:
queryProducts('sale', continueView);
}
// console.log(`\n${answer.action}\n`);
});
}
function queryProducts(type = 'sale', callback) {
var query = "SELECT * FROM products ";
if(type == 'lowInventory'){
query += "WHERE stock_quantity < 5";
}
var result = connection.query(query, function(err, res, fields) {
if(res.length == 0){
if(type == 'lowInventory') {
console.log("We don't have Low Inventory at the moment");
}else{
console.log("Out of Inventary");
}
// return;
}else{
console.log("\n" + line);
console.log("Item Id | " + "Product | " + "Deparment name | " + "Price |"+ "Quantity")
console.log(line);
for (var i = 0; i < res.length; i++) {
console.log(res[i].item_id + " | " + res[i].product_name + " | " + res[i].department_name + " | " + `$${res[i].price}` + " | " + res[i].stock_quantity );
}
}
console.log("\n" + line);
callback()
})
};
function continueView(){
inquirer.prompt({
name: "continue",
type: "confirm",
message: "Would you like to continue? Yes|No",
}).then(function(answer) {
if(answer.continue){
runOptions()
}else{
console.log("Bamazon wish you a Good Day!!! ")
return;
}
})
}
function addInventory() {
var questions = [
{ name: "item_id",
type: "input",
message: "Which product's ID would you like to add inventory?"},
{ name: "quantity",
type: "input",
message: "How many items would you like to add?"}
]
inquirer
.prompt(questions)
.then(function(answer) {
// based on their answer, either call the bid or the post functions
var quantityWanted = answer.quantity;
var query = "SELECT * FROM products WHERE item_id = ?";
connection.query(query, [answer.item_id], function(err, res) {
if(res != undefined) {
var product = res[0];
var stockQuantity = parseInt(product.stock_quantity);
stockQuantity = stockQuantity + parseInt(quantityWanted)
console.log(line);
console.log("Product \t\t\t\t\t\t" + "Price\t"+ "Stock Quantity")
console.log(`${product.product_name} \t ${product.price}\t ${product.stock_quantity}`)
console.log(line);
updateProduct(product, stockQuantity)
}
});
});
// }
}
var ids=[]
function updateProduct(product, quantity ) {
// console.log("\n Updating quantities...\n");
var query = connection.query(
"UPDATE products SET ? WHERE ?",
[
{
stock_quantity: quantity
},
{
item_id: product.item_id
}
],
function(err, res) {
console.log(res.affectedRows + " products updated!\n");
if(res.changedRows === 1){
ids.push(product.item_id);
console.log("\n")
continueView()
}
}
);
}
function createProduct(callback) {
var product_details = [
{ name: "product_name",
type: "input",
message: "Please enter product name"
},
{ name: "department_name",
type: "input",
message: "Please enter deparment name"
},
{ name: "price",
type: "input",
message: "Please enter price"
},
{ name: "quantity",
type: "input",
message: "Please enter stock quantity"
},
]
inquirer
.prompt(product_details)
.then(function(answer) {
// based on their answer, either call the bid or the post functions
console.log("Inserting a new product...\n");
connection.beginTransaction(function(err) {
if (err) { throw err; }
connection.query("INSERT INTO products SET ?",
{
product_name: answer.product_name,
department_name: answer.department_name,
price: answer.price,
stock_quantity: answer.quantity
},
function (error, results, fields) {
if (error) {
return connection.rollback(function() {
throw error;
});
}
connection.commit(function(err) {
if (err) {
return connection.rollback(function() {
throw err;
});
}
console.log('success!');
callback()
});
});
});
});
// }
}