-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazoncustomer.js
More file actions
97 lines (89 loc) · 2.22 KB
/
bamazoncustomer.js
File metadata and controls
97 lines (89 loc) · 2.22 KB
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
var mysql = require("mysql");
var inquirer = require("inquirer");
var cTable = require("console.table");
var productsArray = [];
var userProduct;
var quantityDesired = 0;
var connection = mysql.createConnection({
host: "localhost",
port: 3306,
// Your username
user: "root",
// Your password
password: "root",
database: "bamazonDB",
});
connection.connect(function(err) {
if (err) throw err;
});
//this triggers the showItems functions
showItems();
//
function showItems() {
connection.query("SELECT * FROM products", function(err, results) {
if (err) {
console.log(err);
return;
}
productsArray = results;
var table = results;
console.log("");
console.table(table);
purchaseItem();
});
}
function purchaseItem() {
inquirer
.prompt([
{
name: "id",
message:
"What do you want to bbbuy? Please enter the ID of the product you want.",
},
{
name: "quantity",
message: "How many would you like to burchase?",
},
])
.then(function(answer) {
var userSelect = answer.id;
for (var i = 0; i < productsArray.length; i++) {
if (productsArray[i].ID == userSelect) {
userProduct = productsArray[i];
if (productsArray[i].stock_quantity < answer.quantity) {
console.log(
"Insufficient quantity! Please contact customer service."
);
connection.end();
return;
} else {
quantityDesired = answer.quantity;
fulfillOrder();
}
}
}
});
}
function fulfillOrder() {
var quantityLeft = userProduct.stock_quantity - quantityDesired;
var userTotal = userProduct.price * quantityDesired;
connection.query(
"UPDATE products SET ? WHERE ?",
[{ stock_quantity: quantityLeft }, { ID: userProduct.ID }],
function(err, results) {
if (err) {
console.log(err);
return;
}
}
);
console.log(
"Thank you for your purchase of " +
userProduct.product_name +
". \nYour total is $" +
userTotal +
". https://paypal.com"
);
connection.end();
//console.log("Good job getting this far! Quantity: " + quantityDesired);
}