-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
209 lines (189 loc) · 4.51 KB
/
main.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
var mysql = require("mysql");
var inquirer = require("inquirer");
var table;
var connection = mysql.createConnection({
host: "localhost",
port: 3306,
// Your username
user: "root",
// Your password
password: "",
database: "bamazon"
});
connection.connect(function(err) {
if (err) throw err;
console.log("connected as id " + connection.threadId + "\n");
greeter();
});
function greeter(){
inquirer.prompt([
{
type: "list",
name: "initialResponse",
message: "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ \nWelcome to Bamazon. We offer a wide variety of high-end products. If you are poor, leave or our drone army will find you. \nCare to window shop?",
choices: ["List an Item","Buy an Item", "get the fuck out"]
}
]).then(function(first){
if(first.initialResponse == "List an Item") {
addCLI();
}
else if (first.initialResponse == "Buy an Item") {
toTable();
}
else {
connection.end()
}
})
}
function addCLI(){
inquirer.prompt([
{
type: "input",
name: "itemResponse",
message: "What possible item could you contribute to the top 1%?"
},
{
type: 'imput',
name: "priceResponse",
message: "sigh....\nAnd just how much do you think its worth?"
},
{
type: 'imput',
name: 'quantityRepsonse',
message: 'And how much of that crap do you have?'
}
]).then(function(add){
console.log("we will review your item. No, we'll call you.")
var item = add.itemResponse;
var price = add.priceResponse;
var quantity = add.quantityRepsonse;
create(item, price, quantity);
// reiterate("would you like to add another item?");
})
}
function reiterate(message){
inquirer.prompt([
{
type: "confirm",
name: "reiterate",
message: message
}
]).then(function(again){
if(again.reiterate){
addCLI();
}
else{
greeter();
}
})
}
function buyAgain(message){
inquirer.prompt([
{
type: "confirm",
name: "reiterate",
message: message
}
]).then(function(again){
if(again.reiterate){
toTable();
}
else{
greeter();
}
})
}
function toTable(){
connection.query("SELECT * FROM productlist", function(err, res) {
if (err) throw err;
table = res;
printTable();
askBuy();
});
}
function printTable(){
for (var i = 0; i < table.length; i++) {
console.log(table[i].item_id + " | Item: " + table[i].product_name + " | Price : $" + table[i].price + " | quantity " + table[i].quantity);
}
}
function askBuy(){
inquirer.prompt([
{
type: "input",
name: "choosenid",
message: "Choose an item by ID you would like to buy"
},
{
type: "input",
name: "choosenQuantity",
message: "just how many can you afford?"
}
]).then(function(buy){
var choosenid = table[buy.choosenid -1].product_name;
var choosenQuantity = buy.choosenQuantity;
console.log("choosenid: "+ buy.choosenid+"choosenQuantity: "+choosenQuantity)
if(choosenQuantity > table[buy.choosenid-1].quantity){
console.log("not even I cannot provide what does not exist");
}
else {
var newQuantity = table[buy.choosenid -1].quantity - choosenQuantity;
update(newQuantity, choosenid);
console.log('you now owe us '+choosenQuantity*table[buy.choosenid -1].price+' dollars.')
}
})
}
function create(item, price, quantity) {
console.log("Inserting a new product...\n");
var query = connection.query(
"INSERT INTO productlist SET ?",
{
product_name: item,
price: price,
quantity: quantity,
},
function(err, res) {
console.log(res.affectedRows + " product inserted!\n");
}
);
// logs the actual query being run
console.log(query.sql);
reiterate("Care to waste more of my time?");
}
function read() {
console.log("Selecting all products...\n");
connection.query("SELECT * FROM productlist", function(err, res) {
if (err) throw err;
console.log(res);
});
}
function update(quantity, item) {
var query = connection.query(
"UPDATE productlist SET ? WHERE ?",
[
{
quantity: quantity
},
{
product_name: item
}
],
function(err, res) {
console.log(res.affectedRows + " products updated!\n");
buyAgain("Would you like to buy another Item?");
}
);
// logs the actual query being run
console.log(query.sql);
}
// function delete(item) {
// // console.log("Deleting all strawberry icecream...\n");
// connection.query(
// "DELETE FROM list WHERE ?",
// {
// item: item,
// },
// function(err, res) {
// console.log(res.affectedRows + " products deleted!\n");
// }
// );
// }