-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamazonManager.js
184 lines (175 loc) · 5.32 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
var mysql = require("mysql");
var inquirer = require("inquirer");
var Table = require("cli-table");
var conn = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "1289",
database: "bamazon"
});
conn.connect(function(err) {
if(err) throw err;
menu();
});
function menu() {
inquirer.prompt([
{
name: "options",
type: "list",
message: "What would you like to do next?",
choices: ["VIEW PRODUCTS FOR SALE", "VIEW LOW INVENTORY", "ADD TO INVENTORY", "ADD NEW PRODUCT", "EXIT"]
}
]).then(function(ans) {
switch(ans.options) {
case "VIEW PRODUCTS FOR SALE":
forSale();
break;
case "VIEW LOW INVENTORY":
lowInv();
break;
case "ADD TO INVENTORY":
addInv();
break;
case "ADD NEW PRODUCT":
newProd();
break;
case "EXIT":
conn.end();
break;
}
});
}
function forSale() {
var sql = "select * from products";
conn.query(sql, function(err, res) {
if(err) throw err;
var table = new Table({
head: ["Item ID", "Product", "Department", "Price", "Quantity"],
colWidths: [9, 35, 16, 9, 10]
});
for (var i = 0; i < res.length; i++) {
table.push(
[res[i].item_id, res[i].product_name, res[i].department_name, res[i].price, res[i].stock_quantity]
);
}
console.log(table.toString());
menu();
});
}
function lowInv() {
var sql = "select * from products having stock_quantity < 5";
conn.query(sql, function(err, res) {
if(err) throw err;
var table = new Table({
head: ["Item ID", "Product", "Department", "Price", "Quantity"],
colWidths: [9, 35, 16, 9, 10]
});
for (var i = 0; i < res.length; i++) {
table.push(
[res[i].item_id, res[i].product_name, res[i].department_name, res[i].price, res[i].stock_quantity]
);
}
console.log(table.toString());
menu();
});
}
function addInv() {
var sql = "select * from products";
conn.query(sql, function(err, res) {
if(err) throw err;
var table = new Table({
head: ["Item ID", "Product", "Department", "Price", "Quantity"],
colWidths: [9, 35, 16, 9, 10]
});
for (var i = 0; i < res.length; i++) {
table.push(
[res[i].item_id, res[i].product_name, res[i].department_name, res[i].price, res[i].stock_quantity]
);
}
console.log(table.toString());
addInvPrompt();
});
}
function addInvPrompt() {
inquirer.prompt([
{
name: "item_id",
type: "input",
message: "Please enter the Item ID of the product:"
},
{
name: "units",
type: "input",
message: "How many units of this item would you like to add to inventory?"
}
]).then(function(ans) {
conn.query("select * from products where ?", {item_id: ans.item_id}, function(err, res) {
if(err) throw err;
if(res[0] === undefined) {
console.log("We do not carry an item with that ID.");
setTimeout(function() { menu(); }, 1500);
} else {
var add = res[0].stock_quantity += ans.units;
conn.query("update products set ? where ?",
[
{
stock_quantity: add
},
{
item_id: ans.item_id
}
], function(err) {
if(err) throw err;
console.log("You have added " + ans.units + " unit(s) of the item " + res[0].product_name + ".\nIts total quantity is now " + res[0].stock_quantity + ".");
setTimeout(function() { menu(); }, 1500);
});
}
});
});
}
function newProd() {
inquirer.prompt([
{
name: "id",
type: "input",
message: "Create a five digit Item ID:"
},
{
name: "name",
type: "input",
message: "What is the name of the product?"
},
{
name: "dep",
type: "list",
message: "To which department would you like to add this item?",
choices: ["Grocery", "Children/Toys", "Entertainment", "Electronics", "Medicines"]
},
{
name: "price",
type: "input",
message: "Create a price for this item:"
},
{
name: "quantity",
type: "input",
message: "Please enter the quantity you would like to add:"
}
]).then(function(ans) {
var sql = "insert into products set ?";
conn.query(sql,
[
{
item_id: ans.id,
product_name: ans.name,
department_name: ans.dep,
price: ans.price,
stock_quantity: ans.quantity
}
], function(err) {
if(err) throw err;
forSale();
});
});
}