-
Notifications
You must be signed in to change notification settings - Fork 0
/
bamazonCustomer.js
115 lines (102 loc) · 4.08 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
var mysql = require("mysql");
var inquirer = require("inquirer");
var connection = mysql.createConnection({
host: "localhost",
// Your port; if not 3306
port: 3306,
// Your username
user: "root",
// Your password
password: "rootpass",
// Your db
database: "bamazon_db"
});
// connect to the mysql server
connection.connect(function (err) {
if (err) throw err;
//console.log("connected as id " + connection.threadId + "\n");
// Run function after the connection that starts the prompt for the user
displayProducts();
});
function displayProducts() {
connection.query("SELECT item_id AS 'ID', product_name AS 'Product', price AS 'Price' FROM products", function (err, results) {
if (err) throw err;
console.table(results);
placeOrder();
});
}
// function that prompts user to select item to pruchase
function placeOrder() {
// once products are received, prompt user which item they want to buy
inquirer
.prompt([
{
name: "item_id",
type: "input",
message: "Please enter the item_id for the product you want to buy"
},
{
name: "quantity",
type: "input",
message: "Please enter the number of products you want to buy",
// Verifies input is a number
validate: function (value) {
if (isNaN(value) === false) {
return true;
}
return false;
}
}
])
.then(function (answer) {
// variables for user inputs
var item = answer.item_id;
var quantity = answer.quantity;
//var totalCost = 0;
// Query db for all products
connection.query("SELECT * FROM products WHERE ?", { item_id: item }, function (err, results) {
if (err) throw err;
// determine if stock is available for number of purchases
if (quantity <= results[0].stock_quantity) {
connection.query(
"UPDATE products SET stock_quantity = ?, product_sales = product_sales + ? WHERE ?",
[
results[0].stock_quantity - quantity,
results[0].price * quantity,
{
item_id: item
}
],
function (err) {
if (err) throw err;
//var totalCost = results[0].price * quantity;
console.log("Order placed successfully! Total cost of purchase(s) is $" + results[0].price * quantity + "\n-----------------------------\n");
inquirer.prompt({
name: "action",
type: "list",
message: "CONTINUE SHOPPING?",
choices: [
"YES",
"NO"
]
}).then(function (answer) {
switch (answer.action) {
case "YES":
displayProducts();
break;
case "NO":
connection.end();
break;
}
});
}
);
}
else {
// if there isn't enough stock for number of purchases, tell user order cannot be placed
console.log("Sorry but there is in sufficient stock for your order. Please select a lower number of items.");
displayProducts();
}
});
});
}