-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBamazonCustomer.js
More file actions
130 lines (112 loc) · 4.13 KB
/
BamazonCustomer.js
File metadata and controls
130 lines (112 loc) · 4.13 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
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
var mysql = require("mysql");
var inquirer = require('inquirer');
var displayTable = require('./TableDisplay.js');
var connection = mysql.createConnection({
host: "localhost",
// Your port; if not 3306
port: 3306,
// Your username
user: "root",
// Your password
password: "root",
database: "bamazon"
});
connection.connect(function (err) {
if (err) throw err;
// Start app by Prompting the customer
customerPrompt();
});
var displayProducts = function () {
var display = new displayTable();
connection.query('SELECT * FROM products', function (error, response) {
if (error) throw error;
console.log("connected as id " + connection.threadId + "\n");
display.displayInventoryTable(response);
// connection.end();
itemPurchase();
});
}
var customerPrompt = function () {
inquirer.prompt({
name: "action",
type: "list",
message: "Would like to continue shopping?\n",
choices: ["Yes", "No"]
}).then(function (answer) {
switch (answer.action) {
case 'Yes':
displayProducts();
break;
case 'No':
connection.end();
break;
default:
break;
}
})
};
var itemPurchase = function () {
inquirer.prompt([{
name: "id",
type: "input",
message: "Enter the Item ID That you would like to puchase",
validate: function (value){
var pass = value.match(
/[1-9]/g
);
if (pass) {
return true;
}
return 'Please enter a valid number';
}
}, {
name: "quantity",
type: "input",
message: "How many would you like to purchase?",
validate: function (value){
var pass = value.match(
/[1-9]/g
);
if (pass) {
return true;
}
return 'Please enter a valid number';
}
}]).then(function (answer) {
// Query the database for info about the item including the quantity currently in stock.
connection.query('SELECT * FROM products WHERE ?', { item_id: answer.id }, function (error, response) {
console.log('\n You would like to buy ' + answer.quantity + ' ' + response[0].product_name + ' ' + response[0].department_name + ' at $' + response[0].price + ' each');
// customerPrompt();
if (response[0].stock_quantity >= answer.quantity) {
let purchasedItemQuantity = response[0].stock_quantity - parseFloat(answer.quantity);
let productsSold = response[0].products_sold + parseFloat(answer.quantity);
let productSalesPerQuantitiy = response[0].products_revenue +(response[0].price * answer.quantity);
// catName = response[0].department_name
connection.query("UPDATE products SET ? WHERE ?", [
{
stock_quantity: purchasedItemQuantity,
products_sold: productsSold,
products_revenue: productSalesPerQuantitiy
}, {
item_id: answer.id
}], function (error, response) {
});
connection.query("UPDATE departments SET ? WHERE ?", [
{
product_sales: productSalesPerQuantitiy,
}, {
department_name: response[0].department_name
}], function (error, response) {
});
let itemCost = response[0].price * answer.quantity;
console.log('\n Order has been fulfilled! Your total cost is $' + itemCost.toFixed(2) + '\n');
// Order completed
customerPrompt();
} else {
console.log('\n Sorry, Insufficient quantity in our stock.\n');
// Order not completed
customerPrompt();
}
});
});
}