-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazonmanager.js
More file actions
206 lines (197 loc) · 5.55 KB
/
bamazonmanager.js
File metadata and controls
206 lines (197 loc) · 5.55 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
var mysql = require("mysql");
var inquirer = require("inquirer");
var cTable = require("console.table");
var connection = mysql.createConnection({
host: "localhost",
port: 3306,
// Your username
user: "root",
// Your password
password: "root",
database: "bamazonDB",
});
//connect to the database
connection.connect(function(err) {
if (err) throw err;
});
//run the manager function
managerFunction();
function managerFunction() {
console.log("");
//use inquirer to get info from the user
inquirer
.prompt([
{
type: "list",
name: "functions",
message: "What would you like to do?",
choices: [
"View Products for Sale",
"View Low Inventory",
"Add to Inventory",
"Add New Product",
],
},
])
//depending on what is selected, trigger the appropriate function
.then(function(answer) {
if (answer.functions === "View Products for Sale") {
viewProducts();
} else if (answer.functions === "View Low Inventory") {
lowInventory();
} else if (answer.functions === "Add to Inventory") {
addInventory();
} else {
addNew();
}
});
}
function viewProducts() {
//show all the items in the database
connection.query("SELECT * FROM products", function(err, results) {
if (err) {
console.log(err);
return;
}
var table = results;
console.log("");
//use the cTable package to improve the diplay of the table
console.table(table);
});
connection.end();
}
function lowInventory() {
//only show inventory that is lower than 5 items
connection.query("SELECT * FROM products WHERE stock_quantity<5", function(
err,
results
) {
if (err) {
console.log(err);
return;
}
var table = results;
//use cTable
console.table(results);
});
connection.end();
}
function addInventory() {
//show all the items in the database
connection.query("SELECT * FROM products", function(err, results) {
if (err) {
console.log(err);
return;
}
//place the items in an array
var productsArray = [];
for (var i = 0; i < results.length; i++) {
productsArray.push(results[i].product_name);
}
//ask the user to select which product they wnat to update
inquirer
.prompt([
{
type: "list",
name: "inventory_update",
message: "Select a product to update.",
choices: productsArray,
},
])
.then(function(answer) {
//store the items that the user wants updated into a variable
var whatUpdated = answer.inventory_update;
//find the item they want in the database
for (var i = 0; i < results.length; i++) {
if (whatUpdated === results[i].product_name) {
var currentInventory = results[i].stock_quantity;
//set the item's id as a variable
var id = results[i].ID;
}
}
inquirer
.prompt([
{
//find out how many the user wants to add to the inventory
name: "addInventory",
message:
"There are currently " +
currentInventory +
" " +
whatUpdated +
". How many would you like to add?",
},
])
.then(function(answer2) {
//set the ammount they want added as a variable
var addHowMuch =
parseInt(answer2.addInventory) + parseInt(currentInventory);
//update the database where the id's match to reflect the new inventory amount
connection.query(
"UPDATE products SET ? WHERE ?",
[{ stock_quantity: addHowMuch }, { ID: id }],
function(err, results) {
if (err) {
console.log(err);
return;
}
}
);
console.log("Your inventory has been updated.");
connection.end();
});
});
});
}
//this function will add a new item to the database
function addNew() {
//this will get the name of the product they want to add
inquirer
.prompt([
{
name: "new_item",
message: "What is the name of the product you would like to add?",
},
{
//this will find out what department they want the product to go into
name: "new_item_department",
type: "list",
message: "What department will this product be in?",
choices: [
"Sports and Recreation",
"Food and Beverage",
"Movies",
"Health",
"Outdoors",
"Clothing",
],
},
{
//this will find out how much the user wants to charge
name: "new_item_price",
message: "What is the price of the new item?",
},
{
//this will find out how many they want to add
name: "new_item_quantity",
message: "What is the quantity of stock available?",
},
])
.then(function(answer) {
//this will put the information from the user input into the database
connection.query(
"INSERT INTO products SET ?",
{
product_name: answer.new_item,
department_name: answer.new_item_department,
price: answer.new_item_price,
stock_quantity: answer.new_item_quantity,
},
function(err) {
if (err) throw err;
console.log("Your item was added.");
}
);
connection.end();
});
}