-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazonManager.js
More file actions
182 lines (166 loc) · 5.13 KB
/
Copy pathbamazonManager.js
File metadata and controls
182 lines (166 loc) · 5.13 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
/* eslint-disable */
const mysql = require("mysql");
const inquirer = require("inquirer");
const Table = require('cli-table');
// create the connection information for the sql database
const connection = mysql.createConnection({
host: "localhost",
// Your port; if not 3306
port: 3306,
// Your username
user: "root",
// Your password
password: "",
database: "bamazon_db"
});
// connect to the mysql server and sql database
connection.connect(function(err) {
if (err) throw err;
// run the start function after the connection is made to prompt the user
// console.log("We are connected");
menuOptions();
});
function menuOptions() {
connection.query("SELECT * FROM products", function(err) {
if (err) throw err;
inquirer
.prompt([
{
name: "options",
type: "list",
choices: ["View Products for Sale", "View Low Inventory", "Add to Inventory", "Add New Product"],
message: "What would you like to do?"
},
])
.then(function(answer) {
switch(answer.options) {
case 'View Products for Sale':
viewProducts();
break;
case 'View Low Inventory':
viewLowInventory();
break;
case 'Add to Inventory':
addInventory();
break;
case 'Add New Product':
addNewProduct();
break;
}
})
})
}
function viewProducts() {
connection.query("SELECT * FROM products", function(err, res) {
if (err) throw err;
const table = new Table({
head: ['ID', 'Product', 'Department', 'Price', 'In Stock', 'Product Sales'],
colWidths: [10, 30, 15, 10, 10, 15]
});
for (var i = 0; i < res.length; i++) {
table.push([res[i].id, res[i].product_name, res[i].department_name, res[i].price, res[i].stock_quantity, res[i].product_sales]);
}
console.log(table.toString());
connection.end();
});
}
function viewLowInventory() {
connection.query("SELECT * FROM products", function(err, res) {
if (err) throw err;
const table = new Table({
head: ['ID', 'Product', 'Department', 'Price', 'In Stock', 'Product Sales'],
colWidths: [10, 30, 15, 10, 10, 15]
});
for (var i = 0; i < res.length; i++) {
if (res[i].stock_quantity < 5) {
table.push([res[i].id, res[i].product_name, res[i].department_name, res[i].price, res[i].stock_quantity, res[i].product_sales]);
}
}
console.log(table.toString());
connection.end();
});
}
function addInventory() {
connection.query("SELECT * FROM products", function(err, results) {
if (err) throw err;
inquirer
.prompt([
{
name: "addMoreID",
type: "number",
message: "Enter the ID for the product you would like to increase the quantity of:"
},
{
name: "addMoreAmount",
type: "number",
message: "How many would you like to add?"
},
])
.then(function(answer) {
let chosenProduct;
for (var i = 0; i < results.length; i++) {
if (results[i].id === answer.addMoreID) {
chosenProduct = results[i];
}
}
updatedStockQuantity = chosenProduct.stock_quantity + answer.addMoreAmount;
connection.query(
"UPDATE products SET ? WHERE ?",
[
{
stock_quantity: updatedStockQuantity
},
{
product_name: chosenProduct.product_name
}
],
function(err) {
if (err) throw err;
connection.end();
}
);
console.log(chosenProduct.product_name + " stock quantity has been updated to " + updatedStockQuantity);
})
})
}
function addNewProduct() {
inquirer
.prompt([
{
name: "newProductName",
type: "input",
message: "Enter the name of the new product you would like to add:"
},
{
name: "newProductDept",
type: "input",
message: "Enter the name of the new product's department:"
},
{
name: "newProductPrice",
type: "number",
message: "How much does the new product cost?"
},
{
name: "newProductQuantity",
type: "number",
message: "How many units of the new product will be available?"
},
])
.then(function(answer) {
connection.query(
"INSERT INTO products SET ?",
{
product_name: answer.newProductName,
department_name: answer.newProductDept,
price: answer.newProductPrice,
stock_quantity: answer.newProductQuantity
},
function(err) {
if (err) throw err;
connection.end();
}
);
console.log(answer.newProductName + " has been added to the database!");
})
}