-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamazonManager.js
149 lines (138 loc) · 3.77 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
// Challenge Activity Level ADVANCED
const inquirer = require("inquirer");
const mysql = require("mysql");
// connects to mysql
var connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "Majesty88",
port: 3306,
database: "bamazon"
});
// if connection fails, error prompt
connection.connect(function (err) {
if (err) {
console.error("error connecting: " + err.stack);
return;
}
console.log("connected as id " + connection.threadId);
menuOptions();
});
var menuOptions = function () {
var inquirer = require("inquirer");
inquirer
.prompt({
/* Pass your questions in here */
name: "action",
message: "What would you like to do?",
type: "list",
choices: [
"View product for sale",
"View low inventory",
"Add to inventory",
"Add new product"
]
})
.then(answers => {
console.log(answers)
switch (answers.action) {
case "View product for sale":
viewProducts();
break;
case "View low inventory":
viewLowInventory();
break;
case "Add to inventory":
addToInventory();
break;
case "Add new product":
addNewProduct();
break;
}
// if (answers.action === "View product for sale") {
// viewProducts();
// } else if (answers.action === "View low inventory") {
// viewLowInventory();
// } else (answers.action === "Add to inventory") {
// addToInventory();
// } switch
});
};
function viewProducts() {
connection.query("SELECT * FROM products", function (err, result) {
console.table(result);
menuOptions();
});
}
function viewLowInventory() {
connection.query("SELECT * FROM products WHERE stock_quantity < 5 ", function (
err,
result
) {
console.table(result);
menuOptions();
});
}
function updateProduct(product) {
connection.query("UPDATE products SET stock_quantity = stock_quantity + ? WHERE item_id = ?",
[product.quantity, product.id],
function (err, res) {
if (err) throw err;
console.log("Congratulations manager! you have added more of an item to inventory.");
menuOptions();
}
);
}
function insertNewProduct(answers) {
connection.query("INSERT INTO products (product_name, department_name, price, stock_quantity) VALUES (?, ?, ?, ?)",
[answers.name, answers.department, answers.price, answers.quantity],
function (err, res) {
if (err) throw err;
console.log("Congratulations manager! you have added a NEW ITEM to inventory.");
menuOptions();
}
);
}
function addToInventory() {
var inquirer = require("inquirer");
inquirer
.prompt([{
/* Pass your questions in here */
name: "id",
message: "What is the product id of the item to be added?",
type: "input"
}, {
name: "quantity",
message: "What's the quantity of the item you'd like to add to inventory?",
type: "input"
}])
.then(answers => {
updateProduct(answers);
});
}
function addNewProduct () {
var inquirer = require("inquirer");
inquirer
.prompt([{
/* Pass your questions in here */
name: "name",
message: "What is the name of the product to be added?",
type: "input"
}, {
name: "department",
message: "What/'s the name of the department?",
type: "input"
}, {
name: "price",
message: "What is the price of the item?",
type: "input"
}, {
name: "quantity",
message: "What/'s the quantity of the item you'd like to add to inventory?",
type: "input"
}])
.then(answers => {
insertNewProduct(answers);
});
}
//(node:3000) UnhandledPromiseRejectionWarning: RangeError: Maximum call stack size exceeded