-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazonManager.js
More file actions
182 lines (141 loc) · 4.93 KB
/
bamazonManager.js
File metadata and controls
182 lines (141 loc) · 4.93 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
var Table = require('cli-table');
var mysql = require('mysql');
var inquirer = require('inquirer');
var connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "password",
database: "bamazonDB"
});
connection.connect(function(err) {
if (err) throw err;
console.log("connected as id " + connection.threadId);
startPrompt();
});
function startPrompt() {
inquirer.prompt([{
type: "list",
name: "actionList",
message: "Welcome Manager. Please review.",
choices: ["View Products For Sale", "View Low Inventory", "Add To Inventory", "Add New Product"]
}]).then(function(user) {
if (user.actionList === "View Products For Sale") {
inventoryView();
} else if (user.actionList === "View Low Inventory") {
lowInventory();
} else if (user.actionList === "Add To Inventory") {
addInventory();
} else {
addProduct();
}
});
}
function inventoryView() {
var table = new Table({
head: ['ID', 'Item', 'Department', 'Price', 'Stock'],
colWidths: [10, 30, 30, 30, 30]
});
listInventory();
function listInventory() {
connection.query("SELECT * FROM products", function(err, res) {
for (var i = 0; i < res.length; i++) {
var itemId = res[i].item_id,
productName = res[i].product_name,
departmentName = res[i].department_name,
price = res[i].price,
stockQuantity = res[i].stock_quantity;
table.push(
[itemId, productName, departmentName, price, stockQuantity]
);
}
console.log("");
console.log("====================================================== Current Bamazon Inventory ======================================================");
console.log("");
console.log(table.toString());
console.log("");
startPrompt();
});
}
}
function lowInventory() {
// instantiate
var table = new Table({
head: ['ID', 'Item', 'Department', 'Price', 'Stock'],
colWidths: [10, 30, 30, 30, 30]
});
listLowInventory();
function listLowInventory() {
connection.query("SELECT * FROM products", function(err, res) {
for (var i = 0; i < res.length; i++) {
if (res[i].stock_quantity <= 5) {
var itemId = res[i].item_id,
productName = res[i].product_name,
departmentName = res[i].department_name,
price = res[i].price,
stockQuantity = res[i].stock_quantity;
table.push(
[itemId, productName, departmentName, price, stockQuantity]
);
}
}
console.log("");
console.log("============================================= Low Bamazon Inventory (5 or Less in Stock) ===============================================");
console.log("");
console.log(table.toString());
console.log("");
startPrompt();
});
}
}
function addInventory() {
inquirer.prompt([{
type: "input",
name: "inputId",
message: "Please enter the ID number of the item you would like to add inventory to.",
},
{
type: "input",
name: "inputNumber",
message: "How many units of this item would you like to have in the in-store stock quantity?",
}
]).then(function(managerAdd) {
connection.query("UPDATE products SET ? WHERE ?", [{
stock_quantity: managerAdd.inputNumber
}, {
item_id: managerAdd.inputId
}], function(err, res) {});
startPrompt();
});
}
function addProduct() {
inquirer.prompt([{
type: "input",
name: "inputName",
message: "Please enter the item name of the new product.",
},
{
type: "input",
name: "inputDepartment",
message: "Please enter which department name of which the new product belongs.",
},
{
type: "input",
name: "inputPrice",
message: "Please enter the price of the new product (0.00).",
},
{
type: "input",
name: "inputStock",
message: "Please enter the stock quantity of the new product.",
}
]).then(function(managerNew) {
connection.query("INSERT INTO products SET ?", {
product_name: managerNew.inputName,
department_name: managerNew.inputDepartment,
price: managerNew.inputPrice,
stock_quantity: managerNew.inputStock
}, function(err, res) {});
startPrompt();
});
}