-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamazonCustomer.js
119 lines (82 loc) · 2.36 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
var mysql = require("mysql");
var Table = require("easy-table");
var prompt = require("prompt");
var inquirer = require("inquirer");
var connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "Poofey01!",
database: "bamazon"
});
//connection to mysql
connection.connect(function(err) {
if (err){
console.log("Error: " + err);
}
console.log("connection as id" + connection.threadId);
});
connection.query("SELECT * FROM products", function(err, res)
{
// var end = function(){
// inquirer.prompt([{
// name: "startOver",
// type: "confirm",
// message: "would you like to make another purchase?"
// //default: False
// }]).then(function(answer){
// if (answer.startOver === "Yes"){
// customerPrompts();
// }else if(answer.startOver === "No"){
// connection.end();
// }
// });
// }
//function to ask customer for sale
var customerPrompts = function(){
//item to be bought
inquirer.prompt([{
name: "buyItem",
type: "input",
message: "Welcome to Bamazon! Enter the number of the item you would like to purchase today.",
//check if entry is a number
validate: function(value) {
if (isNaN(value)=== true){
console.log("Try again with a valid number.");
}
return true;
}
},
//how many?
{
name: "quantity",
type: "input",
message: "How many would you like to buy?"
}
]).then(function(answer) {
//easier or different way?
var i = (answer.buyItem) - 1;
if (res.stock_quantity < answer.quantity)
{
console.log("We only have " + res[i].stock_quantity + " in stock. Please try again.");
customerPrompts();
}
console.log("Your total is $" + answer.quantity*res[i].price + " Thank you for your purchase.");
//updates mySql
var newQuantity = res[i].stock_quantity- answer.quantity;
connection.query("UPDATE products set stock_quantity=?, WHERE item_id=?", [newQuantity, answer.buyItem], function(err, res) {});
});
end();
}
var newT = new Table;
//shortened for loop
res.forEach(function(product){
newT.cell("Item ID", product.item_id);
newT.cell("Category", product.department_name);
newT.cell("Product Name", product.product_name);
newT.cell("Price", product.price, Table.number(2));
newT.newRow();
});
console.log(newT.toString());
customerPrompts();
});