-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazonCustomer.js
More file actions
94 lines (83 loc) · 2.6 KB
/
bamazonCustomer.js
File metadata and controls
94 lines (83 loc) · 2.6 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
const inquirer = require('inquirer');
const mysql = require('mysql');
require('dotenv').config();
const dbPassword = process.env.DB_PASSWORD
const connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: dbPassword,
database: "bamazon"
})
connection.connect(function(err) {
if (err) throw err;
startShopping();
})
function startShopping() {
var query = "SELECT item_id, product_name, price FROM products";
connection.query(query, function(err, res) {
console.log('\n--- Available Items ---\n');
res.forEach(function(product) {
console.log(
'ID: ' +
product.item_id + ' -- ' +
product.product_name + ' || ' +
'$' + product.price.toFixed(2) );
})
console.log("\n----------------------\n");
inquirer.prompt([
{
type: 'input',
name: 'productID',
message: 'Which product would you like to buy? (Enter Product ID)'
},
{
type: 'input',
name: 'quantity',
message: 'How many would you like to purchase?'
}
]).then(function(resp) {
// get item by id
var productID = resp.productID;
var quantity = parseInt(resp.quantity, 10);
var query = "SELECT * FROM products WHERE item_id=?"
connection.query(query, resp.productID, function(err, res) {
var item = res[0];
if (item.stock_quantity >= quantity) {
var newQuantity = item.stock_quantity - quantity;
var queryUpdate = "UPDATE products SET stock_quantity=? WHERE item_id=?";
var total = quantity * item.price;
totalPrice = total.toFixed(2);
connection.query(queryUpdate, [newQuantity, productID], function(err, res) {
console.log("\n----------------------\n");
console.log("PURCHASE SUCCESS: You spent $" + totalPrice + "\n");
console.log("----------------------\n");
continueOrQuit();
})
} else {
console.log("\n----------------------\n");
console.log("Sorry, this item doesn't have enough in stock to fulfill your order.\n");
console.log("----------------------\n");
continueOrQuit();
}
})
})
})
}
function continueOrQuit() {
inquirer.prompt([
{
type: 'confirm',
name: 'continue',
message: 'Continue shopping?'
}
]).then(function(conf) {
if (conf.continue) {
startShopping();
} else {
console.log("\n----------------------\n");
console.log("Thanks for shopping. Goodbye!");
connection.end();
}
})
}