-
Notifications
You must be signed in to change notification settings - Fork 0
/
bamazonManager.js
178 lines (160 loc) · 5.12 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
var mysql = require("mysql");
var inquirer = require("inquirer");
var connection = mysql.createConnection({
host: "localhost",
// Your port; if not 3306
port: 3306,
// Your username
user: "root",
// Your password
password: "rootpass",
// Your db
database: "bamazon_db"
});
// connect to the mysql server
connection.connect(function (err) {
if (err) throw err;
//console.log("connected as id " + connection.threadId + "\n");
// Run function after the connection that starts the prompt for the user
viewInventory();
});
function viewInventory() {
inquirer
.prompt({
name: "action",
type: "list",
message: "SELECT ACTION",
choices: [
"View Products for Sale",
"View Low Inventory",
"Add to Inventory",
"Add New Product",
"Exit"
]
})
.then(function (answer) {
switch (answer.action) {
case "View Products for Sale":
viewProducts();
break;
case "View Low Inventory":
viewLowInv();
break;
case "Add to Inventory":
addInv();
break;
case "Add New Product":
addProduct();
break;
case "Exit":
connection.end();
break;
}
});
}
function viewProducts() {
connection.query("SELECT item_id AS 'ID', product_name AS 'Product', price AS 'Price', stock_quantity AS 'Inventory' FROM products", function (err, res) {
if (err) throw err;
console.table(res);
viewInventory();
});
}
function viewLowInv() {
connection.query("SELECT item_id AS 'ID', product_name AS 'Product', stock_quantity AS 'Inventory' FROM products WHERE stock_quantity < 5", function (err, res) {
if (err) throw err;
console.table(res);
viewInventory();
});
}
function addInv() {
inquirer.prompt([
{
name: "item_id",
type: "input",
message: "Please enter the item_id for the product you want to add inventory to"
},
{
name: "quantity",
type: "input",
message: "Please enter the number of products you want to add",
// Verifies input is a number
validate: function (value) {
if (isNaN(value) === false) {
return true;
}
return false;
}
}
])
.then(function (answer) {
// variables for user inputs
var item = answer.item_id;
var quantity = answer.quantity;
// Query db for the product
connection.query("SELECT * FROM products WHERE ?",
{ item_id: item }, function (err, results) {
connection.query("UPDATE products SET stock_quantity = stock_quantity + ? WHERE ?",
[quantity,
{
item_id: item
}],
function (err, results) {
if (err) throw err;
console.log("Inventory for increased by " + quantity);
viewInventory();
});
});
});
}
function addProduct() {
inquirer.prompt([
{
name: "product_name",
type: "input",
message: "Please enter the product name"
},
{
name: "department_name",
type: "input",
message: "Please enter the department name for this product"
},
{
name: "price",
type: "input",
message: "Please enter the price for this product"
},
{
name: "quantity",
type: "input",
message: "Please enter the quantity of products available",
// Verifies input is a number
validate: function (value) {
if (isNaN(value) === false) {
return true;
}
return false;
}
}
])
.then(function (answer) {
// variables for user inputs
var product = answer.product_name;
var dept = answer.department_name;
var price = answer.price;
var quantity = answer.quantity;
// Query db for all products
connection.query("INSERT INTO products SET ?",
{
product_name: product,
department_name: dept,
price: price,
stock_quantity: quantity
},
function (err, results) {
if (err) throw err;
console.log("PRODUCT ADDED");
console.table(results);
viewInventory();
});
});
}