-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazonManajer.js
More file actions
260 lines (224 loc) · 9.51 KB
/
bamazonManajer.js
File metadata and controls
260 lines (224 loc) · 9.51 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// var inquirer = require("inquirer");
var mysql = require("mysql");
// var cTable = require('console.table');
var Table = require('cli-table');
var inquirer = require("inquirer");
var connection = mysql.createConnection({
host: "localhost",
// Your port; if not 3306
port: 3306,
// Your username
user: "root",
// Your password
password: "password",
database: "bamazon_db"
});
connection.connect(function (err) {
if (err) throw err;
console.log("connected as id " + connection.threadId);
askUser();
});
function askUser() {
inquirer
.prompt([
{
name: "Choices",
type: "rawlist",
message: "Hello Please choose from the options below?",
choices: ["View Products for Sale", "View Low Inventory", "Add to Inventory", "Add New Product"]
}
])
.then(function (answer) {
switch (answer.Choices) {
case "View Products for Sale":
productSale();
break;
case "View Low Inventory":
lowInv();
break;
case "Add to Inventory":
addInv();
break;
case "Add New Product":
addProd();
break;
}
})
}
function productSale() {
var table = new Table({
head: ["Id", "Item", "Price", "Stock"],
colWidths: [10, 30, 15, 10]
});
var query = connection.query("SELECT * FROM products", function (err, res) {
if (err) {
throw err
};
for (var i = 0; i < res.length; i++) {
// console.log(res[i].id+" | " + res[i].ProductName + " | " +res[i].DepartmentName+ " | " + res[i].Price + " | " + res[i].StockQuantity);
var itemId = res[i].id,
product = res[i].ProductName,
// department = res[i].DepartmentName,
price = res[i].Price,
quantity = res[i].StockQuantity;
table.push(
[itemId, product, price, quantity]
);
}
console.log("<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>");
console.log("<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>");
console.log(table.toString());
console.log("<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>");
console.log("<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>");
askUser()
// connection.end();
})
}
function lowInv() {
var table = new Table({
head: ["Id", "Item", "Stock"],
colWidths: [10, 30, 15]
});
connection.query("SELECT * FROM products WHERE StockQuantity < 5", function (err, res) {
if (err) throw err;
for (var i = 0; i < res.length; i++) {
var itemId = res[i].id,
product = res[i].ProductName,
quantity = res[i].StockQuantity;
table.push(
[itemId, product, quantity]
);
}
console.log("<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>");
console.log("<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>");
console.log("Our inventory is Lower in This PRoduct below!!");
console.log(table.toString());
console.log("<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>");
console.log("<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>");
askUser()
// connection.end();
})
}
function addInv() {
connection.query("SELECT * FROM products", function (err, res) {
inquirer
.prompt([
{
name: "Question",
type: "rawlist",
choices: function () {
var choiceArray = [];
for (var i = 0; i < res.length; i++) {
choiceArray.push(res[i].ProductName);
}
return choiceArray;
},
message: "On which product you want to add more on?"
},
{
name: "Question2",
type: "input",
message: "How many more you want to add on the prducts?",
validate: function (value) {
if (isNaN(value) === false) {
return true;
}
return false;
}
}
])
.then(function (answer) {
var chosenItem;
var addedInStock;
for (var i = 0; i < res.length; i++) {
if (res[i].ProductName === answer.Question) {
chosenItem = res[i];
addedInStock = parseInt(res[i].StockQuantity) + parseInt(answer.Question2);
}
}
if (answer.Question2 > 0) {
connection.query(
"UPDATE products SET ? WHERE ?",
[
{
StockQuantity: addedInStock
},
{
id: chosenItem.id
}
], function (err) {
if (err) throw err;
console.log("<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>");
console.log("<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>");
console.log("Product added successfully!");
console.log("<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>");
console.log("<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>");
askUser();
}
);
}
else {
console.log("<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>");
console.log("<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>");
console.log("You need a number thats more than 0!!");
console.log("<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>");
console.log("<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>");
addInv();
}
});
});
}
function addProd() {
inquirer
.prompt([
{
name: "item",
type: "input",
message: "What is the new product you want to add?"
},
{
name: "Department",
type: "input",
message: "What department would you like to place the product in?"
},
{
name: "price",
type: "input",
message: "What is the price of the product?",
validate: function (value) {
if (isNaN(value) === false) {
return true;
}
return false;
}
},
{
name: "Question2",
type: "input",
message: "How many more you want to add on the prducts?",
validate: function (value) {
if (isNaN(value) === false) {
return true;
}
return false;
}
}
])
.then(function (answer) {
connection.query("INSERT INTO products SET ?", {
ProductName: answer.item,
DepartmentName: answer.Department,
Price: answer.price,
StockQuantity: answer.Question2
}, function (err, res) {
if (err) throw err;
console.log("<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>");
console.log("<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>");
console.log("Your Item IS ADDED!!");
console.log("<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>");
console.log("<*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*><*>");
console.log("_________________________________________________________________________________________________________");
askUser();
})
})
}