-
Notifications
You must be signed in to change notification settings - Fork 0
/
bamazonManager.js
212 lines (180 loc) · 5.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
var mysql = require("mysql");
var inquirer = require("inquirer");
var Table = require('cli-table');
var connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "password",
database: "bamazon"
});
// main menu loads upon starting node
connection.connect(function (err) {
if (err) throw err;
runSearch();
});
// main menu
function runSearch() {
inquirer
.prompt({
name: "action",
type: "list",
message: "What would you like to do, boss?",
choices: [
"Search Inventory",
"View All Products",
"'Last Chance' Products",
"Add to Inventory",
"Add New Product",
"Exit"
]
})
.then(function (answer) {
switch (answer.action) {
case "Search Inventory":
productSearch();
break;
case "View All Products":
selectProducts();
break;
case "'Last Chance' Products":
lowSearch();
break;
case "Add to Inventory":
addInventory();
break;
case "Add New Product":
newProduct();
break;
case "exit":
console.log("See ya later, boss.");
connection.end();
process.exit()
break;
}
});
}
function showTable(c) {
var table = new Table({
head: ['ID', 'Product Name', 'Department', 'Price', 'Quantity']
})
c.forEach(i => {
table.push([
i.id,
i.product_name,
i.dept_name,
i.price,
i.stock
])
});
console.log(table.toString());
}
// displays all products in table
function selectProducts() {
var query = "SELECT * FROM products;"
connection.query(query, function (err, res) {
if (err) throw err;
showTable(res);
// runSearch();
});
}
// Allows user to search for a product
function productSearch() {
inquirer
.prompt({
name: "product",
type: "input",
message: "What is the name of the product you're looking for?"
})
.then(function (answer) {
// searches
var query = "SELECT * FROM products WHERE ?";
connection.query(query, {
// result from inquirer is sent to query sql
product_name: answer.product
}, function (err, res) {
if (err) throw err;
// console.log(res)
console.log("Here's what I found:");
showTable(res);
});
});
}
// finds the lowest inventory items
function lowSearch() {
var query = "SELECT * FROM products WHERE stock < 5";
connection.query(query, function (err, res) {
showTable(res);
// runSearch();
});
}
// update stock quantity via item id
function addInventory() {
inquirer.prompt([{
name: "id",
type: "input",
message: "Enter the ID of an item to add more of:",
filter: Number
},
{
name: "quantity",
type: "input",
message: "How many to add?",
filter: Number
}
])
.then(function (input) {
var selected = input.id;
var quan = input.quantity;
var query = 'SELECT * FROM products WHERE ?';
connection.query(query, {
id: selected
}, function (err, data) {
if (err) throw err;
var thisData = data[0];
var updated = 'UPDATE products SET stock = ' + (thisData.stock + quan) + ' WHERE id = ' + selected;
connection.query(updated, function (err, res) {
if (err) throw err;
console.log("Successfully updated stock for item with ID " + selected + ". Current stock: " + (thisData.stock + quan));
})
})
})
}
// allows manager to add a completely new product to the store
function newProduct() {
inquirer.prompt([{
name: "product_name",
type: "input",
message: "Tell me what the new product is called: "
},
{
name: "dept_name",
type: "input",
message: "What department is it in?"
},
{
name: "price",
type: "input",
message: "Price (e.g. 40.25):"
},
{
name: "stock",
type: "input",
message: "Quantity:",
filter: Number
}
])
.then(function (input) {
var name = input.product_name;
var dept = input.dept_name;
var price = input.price;
var stock = input.stock;
// mysql update
var query = 'INSERT INTO products SET ?';
connection.query(query, input, function (err, res) {
if (err) throw err;
console.log("Success!");
console.log("Added " + stock + " " + name + "(s) to the " + dept + " department at $" + price + ".");
})
})
}