-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazonManager.js
More file actions
230 lines (210 loc) · 7.76 KB
/
bamazonManager.js
File metadata and controls
230 lines (210 loc) · 7.76 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
var inquirer = require("inquirer");
var mysql = require("mysql");
const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2
});
var connectionData = {
// debug: true,
host: "localhost",
port: 3306,
user: "root",
password: "password",
database: "bamazon"
};
var connection = mysql.createConnection(connectionData);
// connect to the mysql server and sql database
connection.connect(function (err) {
if (err) throw err;
// console.log(`Connected to ${dbName}`);
});
function viewProducts() {
// DB logic
connection.query("SELECT id, product_name, department_name, FORMAT(price,2), stock_quantity, FORMAT(product_sales,2) FROM products", function (err, results) {
if (err) throw err;
console.table(results);
prompt();
});
}
function viewLowInventory() {
console.log(`View Low Inventory`);
connection.query("SELECT id, product_name, department_name, FORMAT(price,2), stock_quantity, FORMAT(product_sales,2) FROM products WHERE stock_quantity < 5", function (err, results) {
if (err) throw err;
// for (var i = 0; i < results.length; i++) {
// console.log(`${results[i].product_name}: ${results[i].stock_quantity} units left.`);
// }
console.table(results);
prompt();
});
}
function addToInventory(results) {
inquirer
.prompt([
{
name: "choice",
type: "list",
choices: function () {
var choiceArray = [];
for (var i = 0; i < results.length; i++) {
choiceArray.push(`Product ID: ${results[i].id}, Item: ${results[i].product_name}, Price: ${results[i].price}, Quantity: ${results[i].stock_quantity}`);
}
return choiceArray;
},
message: "Select item to update quantity."
},
{
name: "quantity",
type: "input",
message: `How many units?`,
validate: function validateQty(value) {
var pass = value.match(/^[0-9]+$/);
if (pass) {
return true;
}
return "Must be a number (use backspace to re-enter a valid number)";
}
}
])
.then(function (answer) {
// get the information of the chosen item
var chosenItem;
var qty = 0;
// console.log(`Answer =${answer.choice.split(",")[1]}`);
for (var i = 0; i < results.length; i++) {
// console.log(`Item = Item: ${results[i].product_name}`);
if ("Item: " + results[i].product_name === answer.choice.split(",")[1].trim()) {
chosenItem = results[i];
qty = parseInt(answer.quantity) + chosenItem.stock_quantity;
// console.log("ChosenItem = " + JSON.stringify(results[i]));
// console.log(`qty = ${qty}`);
break;
}
}
connection.query(
"UPDATE products SET ? WHERE ?",
[
{
stock_quantity: qty
},
{
id: chosenItem.id
}
],
function (error) {
if (error) throw err;
console.log(`${answer.quantity} units added to product id ${chosenItem.id}, ${chosenItem.product_name}\n`);
prompt();
}
);
});
}
function addNewProduct() {
inquirer
.prompt([
{
name: "product",
type: "input",
message: `Item Name?`
},
{
name: "department",
type: "input",
message: `Department Name?`
},
{
name: "price",
type: "input",
message: `Unit Price?`,
validate: function validateQty(value) {
// var pass = value.match(/^[0-9]+$/);
var pass = value.match(/^\$?[0-9]+(\.[0-9][0-9])?$/);
if (pass) {
return true;
}
return "Must be a number (use backspace to re-enter a valid number)";
}
},
{
name: "quantity",
type: "input",
message: `How many units?`,
validate: function validateQty(value) {
var pass = value.match(/^[0-9]+$/);
if (pass) {
return true;
}
return "Must be a number (use backspace to re-enter a valid number)";
}
}
])
.then(function (answer) {
// get the information of the chosen item
var currPrice = parseFloat(answer.price).toFixed(2);
// console.log(`Answer =${answer.product}`);
// for (var i = 0; i < results.length; i++) {
// if (results[i].product_name === answer.product.trim()) {
// console.log(`Entry for ${answer.product} already exists in table.`);
// return;
// }
// }
connection.query(
`INSERT INTO products (product_name, department_name, price, stock_quantity) VALUES (\"${answer.product.trim()}\",\"${answer.department.trim()}\",${currPrice},${answer.quantity}); `,
function (error) {
if (error) {
if (error.code === 'ER_DUP_ENTRY') {
console.log(`\nProduct \'${answer.product.trim()}\' already exists.\n`);
prompt();
} else {
throw error;
}
} else {
console.log(`Added: ${answer.product}, ${answer.department}, ${currPrice}, ${answer.quantity}\n`);
prompt();
}
}
);
});
}
function prompt() {
connection.query("SELECT * FROM products", function (err, results) {
if (err) throw err;
// once we have the items, prompt the user for which they'd like to purchase
inquirer
.prompt([
{
name: "task",
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) {
// Perform the chosen task
// console.log(`Task was ${answer.task}`);
switch (answer.task) {
case "View Products for Sale":
viewProducts();
break;
case "View Low Inventory":
viewLowInventory();
break;
case "Add to Inventory":
addToInventory(results);
break;
case "Add New Product":
addNewProduct();
break;
case "Exit":
default:
connection.end();
return;
}
});
});
}
function start() {
process.stdout.write('\033c'); // Refresh the screen
prompt();
}
start();