-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmazonManager.js
211 lines (185 loc) · 6.48 KB
/
bmazonManager.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
const mysql = require('mysql');
const inquirer = require('inquirer');
const connection = mysql.createConnection({
host: 'localhost', port: 3306, user: 'root', password: 'Midnight*1', database: 'bmazon'
});
const prompt = inquirer.createPromptModule();
start()
function cancelled() {
console.log('Thanks for stopping by!')
connection.end()
}
function start() {
connection.connect(function (err) {
if (err) throw err;
console.log('connected as id ' + connection.threadId + '\n');
displayOptions();
})
};
function displayOptions() {
prompt({
type: 'list',
name: 'managerOptions',
message: 'What would you like to do?',
choices: ['View Products for Sale', 'View Low Inventory',
'Add to Inventory', 'Add New Product to Inventory', 'EXIT']
}).then(function (answer) {
switch (answer.managerOptions) {
case 'View Products for Sale':
viewProducts()
break;
case 'View Low Inventory':
viewLowInventory()
break;
case 'Add to Inventory':
addtoInventory();
break;
case 'Add New Product to Inventory':
addNewProduct()
break;
default: connection.end()
}
})
}
function viewProducts() {
connection.query("SELECT * FROM products", function (err, results) {
if (err) throw err;
console.table(results);
displayOptions();
})
}
function viewLowInventory() {
connection.query("SELECT * FROM products WHERE stock_quantity < 5", function (err, results) {
if (err) throw err;
console.table(results);
console.log("Displaying items with less than five in stock.")
displayOptions();
})
}
function addtoInventory() {
connection.query("SELECT * FROM products", function (err, results) {
if (err) throw err
prompt([
{
type: 'number',
name: 'id',
message: 'Please enter the ID # of the item you would like to add inventory for.',
validate: function (value) {
if (isNaN(value) === false) {
return true;
}
return 'Please enter a valid product ID number using digits 0-9.';
}
},
{
type: 'number',
name: 'quantity',
message: 'How many to add to inventory?',
validate: function (value) {
if (isNaN(value) === false) {
return true;
}
return 'Please enter a valid product ID number using digits 0-9.';
}
},
]).then(function (answer) {
let chosenProduct;
for (let i = 0; i < results.length; i++) {
if (results[i].id === answer.id) {
chosenProduct = results[i];
}
}
if (answer.quantity > 0) {
let newInventory = chosenProduct.stock_quantity + answer.quantity
connection.query(
"UPDATE products SET ? WHERE ?", [
{
stock_quantity: newInventory
},
{
id: chosenProduct.id
}
],
function (error, response) {
if (error) throw err;
console.log(chosenProduct.product_name + " stock_quantity has been updated to: " + newInventory)
displayOptions();
});
}
else {
console.log("I'm sorry. The ID you entered is either not in our database or the quantity you entered is invalid.")
displayOptions();
}
})
})
}
function addNewProduct() {
connection.query("SELECT department_name FROM departments", function (err, results) {
if (err) throw err
prompt([
{
type: 'input',
name: 'product_name',
message: 'Please enter a name for the product to add to database.',
validate: function (value) {
if (isNaN(value) === true) {
return true;
}
return 'Please enter a valid product name.';
}
},
{
type: 'number',
name: 'quantity',
message: 'How many to add to inventory?',
validate: function (value) {
if (isNaN(value) === false) {
return true;
}
return 'Please enter a valid product quantity using digits 0-9.';
}
},
{
type: 'list',
name: 'department',
message: 'What department would you like it added to?',
choices: function() {
var choiceArray = [];
for (var i = 0; i < results.length; i++) {
choiceArray.push(results[i].department_name);
}
return choiceArray;
}
},
{
type: 'number',
name: 'cost',
message: 'How much should one of these items cost?',
validate: function (value) {
if (isNaN(value) === false) {
return true;
}
return 'Please enter a valid product cost using digits 0-9.';
}
},
]).then(function (answer) {
let newCost = answer.cost;
let newProduct = answer.product_name;
let newQuantity = answer.quantity;
let newDepartment = answer.department;
console.log("Inserting a new product...\n");
connection.query("INSERT INTO products SET ?",
{
product_name: newProduct,
department_name: newDepartment,
price: newCost,
stock_quantity: newQuantity,
},
function (err, res) {
if (err) throw err;
viewProducts();
}
)
})
})
}