-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamazonCustomer.js
executable file
·147 lines (116 loc) · 4.32 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Pull in required dependencies
var inquirer = require('inquirer');
var mysql = require('mysql');
// Define the MySQL connection parameters
var connection = mysql.createConnection({
host: 'localhost',
port: 3306,
// Your username
user: 'root',
// Your password
password: 'password',
database: 'Bamazon'
});
// validateInput makes sure that the user is supplying only positive integers for their inputs
function validateInput(value) {
var integer = Number.isInteger(parseFloat(value));
var sign = Math.sign(value);
if (integer && (sign === 1)) {
return true;
} else {
return 'Please enter a whole non-zero number.';
}
}
// promptUserPurchase will prompt the user for the item/quantity they would like to purchase
function promptUserPurchase() {
// console.log('___ENTER promptUserPurchase___');
// Prompt the user to select an item
inquirer.prompt([
{
type: 'input',
name: 'item_id',
message: 'Please enter the Item ID which you would like to purchase.',
validate: validateInput,
filter: Number
},
{
type: 'input',
name: 'quantity',
message: 'How many do you need?',
validate: validateInput,
filter: Number
}
]).then(function(input) {
// console.log('Customer has selected: \n item_id = ' + input.item_id + '\n quantity = ' + input.quantity);
var item = input.item_id;
var quantity = input.quantity;
// Query db to confirm that the given item ID exists in the desired quantity
var queryStr = 'SELECT * FROM products WHERE ?';
connection.query(queryStr, {item_id: item}, function(err, data) {
if (err) throw err;
// If the user has selected an invalid item ID, data attay will be empty
// console.log('data = ' + JSON.stringify(data));
if (data.length === 0) {
console.log('ERROR: Invalid Item ID. Please select a valid Item ID.');
displayInventory();
} else {
var productData = data[0];
// console.log('productData = ' + JSON.stringify(productData));
// console.log('productData.stock_quantity = ' + productData.stock_quantity);
// If the quantity requested by the user is in stock
if (quantity <= productData.stock_quantity) {
console.log('Congratulations, the product you requested is in stock! Placing order!');
// Construct the updating query string
var updateQueryStr = 'UPDATE products SET stock_quantity = ' + (productData.stock_quantity - quantity) + ' WHERE item_id = ' + item;
// console.log('updateQueryStr = ' + updateQueryStr);
// Update the inventory
connection.query(updateQueryStr, function(err, data) {
if (err) throw err;
console.log('Your order has been placed! Your total is $' + productData.price * quantity);
console.log('Thank you for shopping with us!');
console.log("\n---------------------------------------------------------------------\n");
// End the database connection
connection.end();
})
} else {
console.log('Sorry, there is not enough product in stock, your order can not be placed as is.');
console.log('Please modify your order.');
console.log("\n---------------------------------------------------------------------\n");
displayInventory();
}
}
})
})
}
// displayInventory will retrieve the current inventory from the database and output it to the console
function displayInventory() {
// console.log('___ENTER displayInventory___');
// Construct the db query string
queryStr = 'SELECT * FROM products';
// Make the db query
connection.query(queryStr, function(err, data) {
if (err) throw err;
console.log('Existing Inventory: ');
console.log('...................\n');
var strOut = '';
for (var i = 0; i < data.length; i++) {
strOut = '';
strOut += 'Item ID: ' + data[i].item_id + ' // ';
strOut += 'Product Name: ' + data[i].product_name + ' // ';
strOut += 'Department: ' + data[i].department_name + ' // ';
strOut += 'Price: $' + data[i].price + '\n';
console.log(strOut);
}
console.log("---------------------------------------------------------------------\n");
//Prompt the user for item/quantity they would like to purchase
promptUserPurchase();
})
}
// runBamazon will execute the main application logic
function runBamazon() {
// console.log('___ENTER runBamazon___');
// Display the available inventory
displayInventory();
}
// Run the application logic
runBamazon();