/
bamazonCustomer.js
204 lines (165 loc) · 5.6 KB
/
bamazonCustomer.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
require('dotenv').config();
var keys = require("./keys.js");
var mysql = require("mysql");
var inquirer = require('inquirer');
var divider = "\n--------------------------------------\n";
var connection = mysql.createConnection({
host: keys.host,
port: keys.port,
user: keys.user,
password: keys.password,
database: "bamazon"
});
connection.connect(function (err) {
if (err) throw err;
console.log("connected as id " + connection.threadId);
enterStore();
});
function enterStore() {
// logic to display all of the items available for sale. Include the ids, names, and prices of products for sale.
inquirer
.prompt([
{
name: "start",
message: "Do you want to enter the store?",
type: "list",
choices: [
"Oh yea!",
"No thank you..."
]
}
])
.then(answers => {
switch (answers.start) {
case "Oh yea!":
console.log("Entering the store...");
disaplyAllItems();
break;
case "No thank you...":
console.log(divider + "Well, that's a bummer");
connection.end();
break;
}
});
}
function disaplyAllItems() {
console.log(divider + "ALL ITEMS IN THE BAMAZON STORE: ")
// display all items
var query = "SELECT * FROM products"
connection.query(query, function (err, res) {
if (err) throw err;
res.forEach(element => {
var details = [
"ID: " + element.item_id,
"Product Name: " + element.product_name,
"Department: " + element.department_name,
"Price: $" + element.price,
"Quantity Available: " + element.stock_quantity
].join(" | ");
console.log("\n" + details);
});
console.log(divider)
whichItem()
})
}
function whichItem() {
inquirer
.prompt([
{
name: "whichID",
type: "input",
message: "What is the ID of the item you with to buy? (input number)",
validate: function (value) {
if (isNaN(value) === false) {
return true;
}
return false;
}
},
{
name: "quantityNeeded",
type: "input",
message: "How many of those would you like? (input number)",
validate: function (value) {
if (isNaN(value) === false) {
return true;
}
return false;
}
}
])
.then(answers => {
var ID = answers.whichID;
var quantityNeeded = answers.quantityNeeded;
connection.query("SELECT product_name, stock_quantity, price FROM products WHERE ?",
{
item_id: ID
}, function (err, res) {
if (err) throw err
res.forEach(element => {
var amountAvailable = element.stock_quantity;
var product = element.product_name;
var price = element.price;
checkQuantity(ID, product, quantityNeeded, amountAvailable, price);
})
})
});
}
function checkQuantity(ID, product, quantityNeeded, amountAvailable, price) {
if (amountAvailable >= quantityNeeded) {
console.log(divider);
console.log("Yay! We can fulfill your order for " + quantityNeeded + " " + product + "s!");
console.log(divider);
processOrder(ID, quantityNeeded, amountAvailable, price);
}
else {
var difference = quantityNeeded - amountAvailable
failedOrder(product, difference)
}
}
function failedOrder(product, difference) {
console.log(divider);
console.log("Drat! Your order failed becuause you want " + difference + " more " + product + "s than we have available right now");
console.log("Try placing your order again");
console.log(divider);
// prompt the user to start their order again
whichItem()
}
function processOrder(ID, quantityNeeded, amountAvailable, price) {
var newStock = amountAvailable - quantityNeeded;
connection.query("UPDATE products SET stock_quantity=? WHERE item_id=?",
[
newStock, ID
], function (err, res) {
if (err) throw err;
})
var total = quantityNeeded * price;
console.log(divider + "The total cost for this purchase is $" + total + divider);
startNewPurchase()
}
function startNewPurchase() {
inquirer
.prompt([
{
name: "newPurchase",
message: "Do you want to make annother purchase?",
type: "list",
choices: [
"Oh yea!",
"No thank you..."
]
}
])
.then(answers => {
switch (answers.newPurchase) {
case "Oh yea!":
console.log(divider + "Commencing another order..." + divider);
disaplyAllItems();
break;
case "No thank you...":
console.log(divider + "Well, that's a bummer");
connection.end();
break;
}
});
}