-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamazonCustomer.js
184 lines (163 loc) · 5.8 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
// //////////////////////////////////////////////////////////////////////////////////////////////
// MySql Connection & Inquirer import
// //////////////////////////////////////////////////////////////////////////////////////////////
const mysql = require("mysql");
const inquirer = require("inquirer");
const connection = mysql.createConnection({
host: "localhost",
// Your port; if not 3306
port: 3306,
// Your username
user: "root",
// Your password
password: "root",
database: "bamazon"
});
// //////////////////////////////////////////////////////////////////////////////////////////////
// Global Variables
// //////////////////////////////////////////////////////////////////////////////////////////////
// Array of items in customer's cart (objects)
let cartArr = [];
// //////////////////////////////////////////////////////////////////////////////////////////////
// Upon connecting to the database, prompt user
// //////////////////////////////////////////////////////////////////////////////////////////////
connection.connect(function (err) {
if (err) throw err;
customerNavigate();
});
// //////////////////////////////////////////////////////////////////////////////////////////////
// Function to prompt user with a list of commands from which to choose
// //////////////////////////////////////////////////////////////////////////////////////////////
function customerNavigate() {
console.log
(`-------------------------------------------`)
inquirer
.prompt({
name: "navigation",
type: "list",
message: "What would you like to do?",
choices: ["Start Shopping", "View Cart", "Checkout", "Quit"]
})
.then(function (answer) {
if (answer.navigation === "Start Shopping") {
showProducts();
addToCart();
} else if (answer.navigation === "View Cart") {
viewCart();
} else if (answer.navigation === "Checkout") {
checkOut();
} else if (answer.navigation === "Quit") {
process.exit();
} else {
connection.end();
}
});
}
// //////////////////////////////////////////////////////////////////////////////////////////////
// Function to display the products in a table for the user
// //////////////////////////////////////////////////////////////////////////////////////////////
function showProducts() {
let query = "SELECT * FROM products";
connection.query(query, function (err, res) {
if (err) throw err;
console.table(res);
});
}
// //////////////////////////////////////////////////////////////////////////////////////////////
// Function to update quantity of product chosen in database
// //////////////////////////////////////////////////////////////////////////////////////////////
function addToCart() {
let query = "SELECT * FROM products";
connection.query(query, function (err, res) {
if (err) throw err;
inquirer
.prompt({
name: "choice",
type: "rawlist",
choices: function () {
var choiceArray = [];
for (var i = 0; i < res.length; i++) {
choiceArray.push(res[i].product_name);
}
return choiceArray;
},
message: "What item would you like purchase?"
})
.then(function (answer) {
let chosenItem;
for (var i = 0; i < res.length; i++) {
if (res[i].product_name === answer.choice) {
chosenItem = res[i];
}
}
if (chosenItem.stock_quantity >= 1) {
connection.query(
"UPDATE products SET stock_quantity = stock_quantity -1 WHERE item_id='" +
chosenItem.item_id +
"'",
function (error) {
if (error) console.log(error);
console.log(`${chosenItem.product_name} added to cart!
-----------------------------------------------------`);
cartArr.push({
product: chosenItem.product_name,
price: chosenItem.PRICE,
quantity: 1
});
customerNavigate();
}
);
} else {
console.log(`Sorry we are out of that product.
----------------------------------------------`);
customerNavigate();
}
});
});
}
// //////////////////////////////////////////////////////////////////////////////////////////////
// Function to display items in cart for user
// //////////////////////////////////////////////////////////////////////////////////////////////
function viewCart() {
if (cartArr.length < 1){
console.log(`Your cart is empty.
------------------------------------`);
customerNavigate();
}
else {
console.table(cartArr);
let totalPrice = cartArr.reduce(function (prev, cur) {
return prev + cur.price;
}, 0);
console.log(`Your total is $${Math.round(totalPrice * 100) / 100}
--------------------------------------------------------------`);
customerNavigate();
}
}
// //////////////////////////////////////////////////////////////////////////////////////////////
// Function to prompt user to complete order
// //////////////////////////////////////////////////////////////////////////////////////////////
function checkOut() {
let totalPrice = cartArr.reduce(function (prev, cur) {
return prev + cur.price;
}, 0);
console.log(`Your total is $${Math.round(totalPrice * 100) / 100}
----------------------------------------------------------------`);
inquirer
.prompt({
name: "confirm",
type: "confirm",
default: true,
message: "Would you like to complete your order?"
})
.then(function (answer) {
if (answer.confirm) {
console.log(`Thank you for shopping with us!
-----------------------------------------------------`);
cartArr = [];
customerNavigate();
} else {
customerNavigate();
}
});
}