-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamazonClient.js
175 lines (150 loc) · 4.49 KB
/
bamazonClient.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//connect dependicies in package JSON to the file via require
var mysql= require("mysql");
var inquirer= require("inquirer");
var Table = require("cli-table3");
var colors =require("colors");
// establish mysql connection to bamazon database
var connection= mysql.createConnection({
host:"localhost",
port:3306,
user:"root",
password:"Zolula30%",
database:"bamazon_db"
});
// connecting to mySQL
connection.connect(function(err){
if(err) throw err;
//console.log("Connected as id", connection.threadId);
options();
});
// function that brings up the options menu
function options(){
//give the options to the user
inquirer
.prompt([{
type:"list",
name:"option",
message:"What would you like to do?",
choices:["View Products", "Buy a Product", "Exit"]
}
])
.then(function(answer){
//depending on what the user choses the switch case will deploy the right action
switch(answer.option){
case "View Products":
displayTable();
break;
case "Buy a Product":
displayTable();
break;
case "Exit":
exit();
break;
}
})
};
function displayTable(){
//CLI-table3 language to create a custom table
var table = new Table({
head:[colors.green("ID"), colors.brightBlue("Product"),colors.magenta("Price"), colors.cyan("Department"),colors.yellow("Stock Quantity")],
chars: { 'top': '═' , 'top-mid': '╤' , 'top-left': '╔' , 'top-right': '╗'
, 'bottom': '═' , 'bottom-mid': '╧' , 'bottom-left': '╚' , 'bottom-right': '╝'
, 'left': '║' , 'left-mid': '╟' , 'mid': '─' , 'mid-mid': '┼'
, 'right': '║' , 'right-mid': '╢' , 'middle': '│' }
});
// ask SQL to send the info from the table
connection.query("SELECT * FROM bamazon_db.products", function(err,res){
if(err) throw(err)
// console.log(res);
//create a loop to eiterate through the result from the query
for(var i=0; i<res.length;i++){
// //assign a variable to hold the result from the query
var items= res[i];
//transform the object sent from mySQL table into an array
items=Object.values(items)
//turn that array into a table that can be displayed in the terminal
table.push(items);
}
//display the table in the terminal
console.log(table.toString());
//run an inquirer to allow user to make a purchase or exit the app
inquirer
.prompt({
type:"confirm",
name:"purchase",
message:"Want to make a purchase?",
default:true
}) .then(function(resp){
if(resp.purchase===true){
buyProd();
}else{
exit();
}
})
})
};
//function that allows user to buy a product
function buyProd(){
inquirer
.prompt([{
type:"input",
name:"product",
message:"What's the ID of the product you want to purchase?"
},
{
type:"input",
name:"quantity",
message:"How many items would you like to purchase?"
},
{
type:"confirm",
name:"complete",
message:"Complete Purchase?",
default:true
}
])
.then(function(ans){
var prod=ans.product;
var qty=ans.quantity;
connection.query("SELECT * FROM products WHERE id= "+ prod, function(err,res){
if(err) throw(err)
//console.log(res);
if(qty<=res[0].stock_quantity){
var updateQuery= "UPDATE products SET stock_quantity= stock_quantity -" + qty + " WHERE id=" + prod
//console.log(updateQuery)
connection.query(updateQuery, function(err,result){
if(err)throw(err)
console.log("Thank you for your purchase! Your", colors.magenta("total") ,"price is: $", colors.brightYellow((res[0].price * qty).toFixed(2)));
leave();
})
}else{
console.log("Sorry, insufficient quantity");
leave();
}
})
})
};
function leave(){
inquirer
.prompt({
type:"list",
name:"leave",
message:"Are you done?",
choices:["Back to main menu", "Exit"]
})
.then(function(choice){
switch(choice.leave){
case "Back to main menu":
options();
break;
case "Exit":
exit();
break;
}
})
};
//function that ends the connection
function exit(){
connection.end();
console.log("Thank you for your visit!")
};