-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazonCustomer.js
More file actions
98 lines (93 loc) · 3.14 KB
/
Copy pathbamazonCustomer.js
File metadata and controls
98 lines (93 loc) · 3.14 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
98
var mysql = require('mysql');
var table = require('console.table');
var inquirer = require('inquirer');
var connection = mysql.createConnection({
host: 'localhost',
port: 8889,
user: 'root',
password: 'root',
database: 'bamazon'
});
connection.connect(function(err) {
if (err) throw err;
queryProducts();
});
function queryProducts() {
var query =
'SELECT item_id AS SKU, product_name AS "Product Description", price AS Price FROM products';
connection.query(query, function(err, res) {
console.table(res);
goShopping();
});
}
function goShopping() {
inquirer
.prompt([
{
type: 'input',
message: 'What product id would you like to buy?',
name: 'id',
validate: function(value) {
if (isNaN(value) == false) {
return true;
} else {
return false;
}
}
},
{
type: 'input',
message: 'How many units would you like to buy?',
name: 'quantity',
validate: function(value) {
if (isNaN(value) == false) {
return true;
} else {
return false;
}
}
}
])
.then(function(order) {
var quantity = order.quantity;
var itemId = order.id;
connection.query(
'SELECT * FROM products WHERE item_id=' + itemId,
function(err, selectedItem) {
if (err) throw err;
if (selectedItem[0].stock_quantity - quantity >= 0) {
// console.log("Bamazon's shelves have enough of that item ("selectedItem[0].ProductName + ")!");
console.log(
'\n\n°º¤ø,¸¸,ø¤º°`°º¤ø,¸,ø¤°º¤ø,¸¸,ø¤º°`°º¤ø,¸°º¤ø,¸¸,ø¤º°`°º¤ø,¸,ø¤°\n\nQuantity in Stock: ' +
selectedItem[0].stock_quantity +
' Order Quantity: ' +
quantity
);
console.log(
'You will be charged ' +
quantity * selectedItem[0].price +
' dollars. Thank you for shopping at Bamazon.\n\n°º¤ø,¸¸,ø¤º°`°º¤ø,¸,ø¤°º¤ø,¸¸,ø¤º°`°º¤ø,¸°º¤ø,¸¸,ø¤º°`°º¤ø,¸,ø¤°\n\n'
);
connection.query(
'UPDATE products SET stock_quantity=? WHERE item_id=?',
[selectedItem[0].stock_quantity - quantity, itemId],
function(err, inventory) {
if (err) throw err;
queryProducts();
}
);
} else {
console.log(
'\n\n°º¤ø,¸¸,ø¤º°`°º¤ø,¸,ø¤°º¤ø,¸¸,ø¤º°`°º¤ø,¸°º¤ø,¸¸,ø¤º°`°º¤ø,¸,ø¤°\n\nInsufficient inventory to fulfill this order. Please order less of that item, as Bamazon only has ' +
selectedItem[0].stock_quantity +
' ' +
selectedItem[0].product_name +
' in stock at this time.\n\n°º¤ø,¸¸,ø¤º°`°º¤ø,¸,ø¤°º¤ø,¸¸,ø¤º°`°º¤ø,¸°º¤ø,¸¸,ø¤º°`°º¤ø,¸,ø¤°\n\n'
);
queryProducts();
}
}
);
});
}
// queryProducts();