-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamazonCustomer.js
119 lines (95 loc) · 3.33 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
const dotenv = require("dotenv").config();
const mysql = require("mysql");
const inquirer = require("inquirer");
const cTable = require("console.table");
const connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "",
database: "itsmagic_db"
});
connection.connect(function(error) {
if (error) throw error;
console.log("Connected as ID " + connection.threadId + "\n");
accio();
});
function accio() {
console.log("🔍 🔍 🔍 Loading Magical Products 🔍 🔍 🔍 \n");
connection.query("SELECT item_id, product_name, department_name, price FROM inventory", function(error, response) {
if (error) throw error;
console.table(response);
prompts();
})
};
function prompts() {
inquirer.prompt({
name: "productID",
type: "input",
message: "What is the ID of the product that you would like to buy?"
}).then(function(answer) {
if (answer.productID) {
let userRequest = parseInt(answer.productID);
console.log("Customer Request: Product " + userRequest);
console.log(" ");
inquirer.prompt({
name: "howMany",
type: "input",
message: "How many would you like to buy?"
}).then(function(answer) {
let userQuantity = parseInt(answer.howMany);
console.log("Desired quantity: " + userQuantity);
console.log(" ");
function checkStock() {
connection.query(
"SELECT * FROM inventory WHERE ?",
{
item_id: userRequest
},
function(error, response) {
if (error) throw error;
if (userQuantity > response[0].stock_quantity) {
console.log("Whoops!", " We don't have enough " + response[0].product_name + "s in stock. :-( Try again.");
console.log(" ");
prompts()
}
else {
console.log("Lucky you!", " We have " + response[0].product_name + "s in stock.");
console.log(" ");
let stockAvail = response[0].stock_quantity;
let productPrice = response[0].price;
let purchaseTotal = productPrice * userQuantity;
updateInventory(stockAvail, userQuantity, userRequest);
totalCost(purchaseTotal, userQuantity);
connection.end();
}
})
}
checkStock()
})
}
})
}
function updateInventory(response, userQuantity, userRequest) {
console.log("🧠 🧠 🧠 Updating inventory and calculating your total 🧠 🧠 🧠");
console.log(" ");
connection.query(
"UPDATE inventory SET ? WHERE ?",
[
{
stock_quantity: response - userQuantity
},
{
item_id: userRequest
}
],
function(error, res) {
if (error) throw error;
console.log(res.affectedRows + " inventory updated.");
console.log(" ");
}
)
}
function totalCost(purchaseTotal) {
console.log("Your total is: $" + purchaseTotal);
}