-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamazon-manager.js
183 lines (175 loc) · 5.55 KB
/
bamazon-manager.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
var mysql = require("mysql");
var inquirer = require("inquirer");
var connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "",
database: "bamazon_DB"
});
connection.connect(function (err) {
if (err) throw err;
console.log("Connection is successful");
console.log("\nWelcome to Bamazon - Manager Ver.\n");
start();
});
function start() {
inquirer.prompt([
{
type: "list",
name: "options",
message: "Select Option Below",
choices: [
"View Products for Sale",
"View Low Inventory",
"Add to Inventory",
"Add New Product",
"Exit"
]
}
])
.then(function (response) {
switch (response.options) {
case "View Products for Sale":
viewProducts();
break;
case "View Low Inventory":
viewLowInventory();
break;
case "Add to Inventory":
addInventory();
break;
case "Add New Product":
addProduct();
break;
case "Exit":
process.exit();
break;
}
});
};
function viewProducts() {
connection.query("SELECT * FROM products", function (err, data) {
if (err) throw err;
for (var i = 0; i < data.length; i++) {
console.log(data[i].itemid + " | " + data[i].department_name + " | " + data[i].product_name + " | " + data[i].price + " | " + data[i].stock_quantity + " | ");
}
start();
});
};
function viewLowInventory() {
connection.query("SELECT * FROM products WHERE stock_quantity<=5", function (err, data) {
if (err) throw err;
for (var i = 0; i < data.length; i++) {
console.log(data[i].itemid + " | " + data[i].department_name + " | " + data[i].product_name + " | " + data[i].price + " | " + data[i].stock_quantity + " | ");
}
start();
});
};
function addInventory() {
connection.query("SELECT * FROM products", function (err, data) {
if (err) throw err;
inquirer.prompt([
{
type: "list",
message: "Choose item to Update Inventory",
name: "update",
type: "list",
choices: function () {
var updateArray = [];
for (var i = 0; i < data.length; i++) {
updateArray.push(data[i].itemid + " | " + data[i].product_name);
}
return updateArray;
}
},
{
name: "quantity",
type: "input",
message: "Enter quantity to add: ",
valide: function (value) {
if (isNaN(value) === false) {
return true;
}
return false;
}
}
])
.then(function (answer) {
var itemArr = (answer.choice).split("-");
var id = parseInt(itemArr[0]);
var chosenItem;
for (var i = 0; i < data.length; i++) {
if (data[i].itemid === id) {
chosenItem = data[i];
}
}
connection.query(
"UPDATE products SET ? WHERE ?",
[
{
stock_quantity: chosenItem.stock_quantity + parseInt(answer.quantity)
},
{
itemid: chosenItem.itemid
}
],
function (error) {
if (error) throw err;
console.log(answer.quantity + " units have been added to " + chosenItem.product_name);
start();
}
);
});
});
};
function addProduct() {
inquirer.prompt([
{
type: "input",
name: "product_name",
message: "Enter Product Name: "
},
{
type: "input",
name: "department_name",
message: "Enter Department: "
},
{
type: "input",
name: "price",
message: "Enter Item Price: ",
validate: function (value) {
if (isNaN(value) === false) {
return true;
}
return false;
}
},
{
type: "input",
name: "quantity",
message: "Enter Quantity: ",
valide: function (value) {
if (isNaN(value) === false) {
return true;
}
return false;
}
}
])
.then(function (answer) {
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) {
console.log(answer.product_name + " has been added onto Bamazon.");
start();
}
);
})
}