-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazoncustomer.js
More file actions
95 lines (74 loc) · 2.26 KB
/
bamazoncustomer.js
File metadata and controls
95 lines (74 loc) · 2.26 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
const mysql = require("mysql");
const inquirer = require("inquirer");
const connection = mysql.createConnection({
host: "localhost",
port: "3306",
user: "root",
password: "",
database: "bamazonDB",
});
connection.connect((err) =>{
if (err) throw err;
console.log("* * * * * * * * * * * * * * * * * * *");
console.log(`Welcome to Bamazon! What would you like to purchase?`);
runCustomer();
});
function runCustomer() {
inquirer
.prompt([
{
name: "id",
type: "input",
message: "Enter the item id: ",
validate: function(value) {
if (isNaN(value) === false) {
return true;
}
return false;
}
},
{
name: "quantity",
type: "input",
message: "Enter the item quantitiy for purchase: ",
validate: function(value) {
if (isNaN(value) === false) {
return true;
}
return false;
}
}
])
.then(function(answer) {
let id = answer.id;
let quantity = answer.quantity;
connection.query("SELECT * FROM products WHERE ?", { item_id: id }, function(err, res) {
if (err) throw err;
var item = res[0];
if (quantity < item.stock_quantity){
var currentStock = item.stock_quantity - quantity;
connection.query("UPDATE products SET ? WHERE ?",
[
{
stock_quantity: currentStock
},
{
item_id: item.item_id
}
], function(err) {
if (err) throw err;
var total = quantity * item.price;
console.log("* * * * * Purchase Processed * * * * *");
console.log(`You purchased ${quantity} of ${item.product_name}.`);
console.log(`Your total is: $${total}. Thank you for shopping with Bamazon!`)
console.log("-----------------------------------");
runCustomer();
});
}else{
console.log("Sorry, there is not enough inventory for this purchase.");
console.log("-----------------------------------");
runCustomer();
};
});
});
};