-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamazonManager.js
162 lines (151 loc) · 4.92 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
var mysql = require("mysql");
var cTable = require("console.table");
var inquirer = require("inquirer");
// create the connection information for the sql database
var connection = mysql.createConnection({
host: "localhost",
// Your port; if not 3306
port: 8889,
// Your username
user: "root",
// Your password
password: "root",
database: "bamazonDB"
});
connection.connect(function (err) {
if (err) throw err;
console.log("Server is listening and connected" + "\n");
showCommands();
});
function showCommands() {
inquirer.prompt([{
name: "selection",
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.selection) {
case "View Products for Sale":
listProducts();
break;
case "View Low Inventory":
viewLow();
break;
case "Add to Inventory":
addInv();
break;
case "Add New Product":
addProd();
break;
default:
break;
}
});
}
function listProducts() {
connection.query("SELECT * FROM products", function (err, results) {
if (err) throw err;
console.log("\n")
console.table(results);
showCommands()
});
}
function viewLow() {
connection.query("SELECT * FROM products WHERE stock_quantity < 3", function (err, results) {
if (err) throw err;
console.log("\n")
console.table(results)
showCommands()
});
}
function addInv() {
connection.query("SELECT * FROM products", function (err, results) {
if (err) throw err;
inquirer.prompt([{
name: "products",
type: "list",
choices: function () {
var choiceArray = []
for (var i = 0; i < results.length; i++) {
choiceArray.push(results[i].product_name)
}
return choiceArray;
},
message: "Which product do you need to add inventory to?"
},
{
name: "quantity",
type: "input",
validate: function (value) {
if (isNaN(value) === false) {
return true;
} else {
return false;
}
},
message: "How many do you want to add?"
}
]).then(function (answer) {
connection.query("UPDATE products SET stock_quantity = stock_quantity + ? WHERE product_name = ?", [answer.quantity, answer.products], function (err) {
if (err) throw err
console.log("Successfully added " + answer.quantity + " " + answer.products + "(s)")
showCommands()
})
})
})
}
function addProd() {
connection.query("SELECT * FROM products WHERE stock_quantity < 3", function (err, results) {
if (err) throw err;
inquirer.prompt([{
name: "product",
type: "input",
message: "What is the product?"
},
{
name: "dept",
type: "list",
choices: ["Bathroom", "Kitchen", "Home", "ETC"],
message: "Which department does this product belong in?"
},
{
name: "price",
type: "input",
validate: function (value) {
if (isNaN(value) === false) {
return true;
} else {
return false;
}
},
message: "Price this item - make sure to format it correctly - e.g 2.00"
},
{
name: "stock",
type: "input",
validate: function (value) {
if (isNaN(value) === false) {
return true;
} else {
return false;
}
},
message: "How many are in stock?"
}
]).then(function (answer) {
connection.query(
"INSERT INTO products SET ?", {
product_name: answer.product,
department_name: answer.dept,
price: answer.price,
stock_quantity: answer.stock
},
function (err) {
if (err) throw err;
console.log("\n" + answer.product + " successfully added into inventory log! \n");
showCommands()
}
);
})
});
}