-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamazonCustomer.js
130 lines (119 loc) · 3.79 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
require("dotenv").config()
var mysql = require("mysql");
var inquirer = require("inquirer");
var connection = mysql.createConnection({
host: process.env.host,
port: process.env.PORT || 4000,
user: process.env.user,
password: process.env.db_password,
database: process.env.database
});
connection.connect(function(err){
if (err) throw err;
console.log("connected as id " + connection.threadId);
inventoryList();
});
function inventoryList(){
connection.query("SELECT item_id, product_name, department_name, price, stock_quantity FROM products", (err, res) => {
if(err) throw err;
console.table(res);
shopping(res);
})
};
function shopping(val){
inquirer
.prompt([
{
type: "input",
name: "choice",
message: "Here are all items for purchase. Would you like to buy a product?\n Type yes or no\n",
choices: [
"yes",
"no"
]
}
])
.then(answer =>{
switch (answer.choice){
case "yes":
console.log("Alright, choose desired product by id and enter it");
productSelection(val);
break;
case "no":
console.log("Alright, goodbye...");
connection.end;
process.exit(0)
break;
default:
console.log("Please type yes or no");
shopping();
}
});
}
function productSelection(inventory){
inquirer
.prompt([
{
type: "input",
name: "choice",
message: "Please enter Id number of the item you'd like to purchase :",
validate: (val)=> {
return !isNaN(val);
}
}
])
.then((val)=>{
let productId = parseInt(val.choice);
let product = checkInventory(productId, inventory);
if (product) {
console.log(`You've selected ${product.product_name}.
This item costs \$${product.price} each and there are currently ${product.stock_quantity} in stock.`);
orderStart(product);
}
else{
console.log("Product you've selected does not exist. Please try again.")
productSelection();
}
})
};
function orderStart(product){
inquirer
.prompt([
{
type: "input",
name: "quantity",
message: "How many would you like? :",
validate: function(val) {
return val >= 0;
}
}
])
.then(function(val){
var quantity = parseInt(val.quantity);
if (quantity > product.stock_quantity){
console.log(`There are only ${product.stock_quantity} and not enough to fulfill the order.. Please choose a lesser amount. `);
orderStart(product);
}
else {
checkout(product, quantity)
}
})
}
function checkout(product, quantity){
connection.query(
"UPDATE products SET stock_quantity = stock_quantity - ? WHERE item_id = ?",
[quantity, product.item_id],
function(err, res) {
console.log(`\nCongrats on completing your order of ${product.item_id} ${product.product_name} for total of \$` + product.price*quantity);
inventoryList();
}
)
}
function checkInventory(productId, inventory){
for (var i = 0; i < inventory.length; i++){
if (inventory[i].item_id === productId) {
return inventory[i];
}
}
return null;
}