-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazon.js
More file actions
108 lines (99 loc) · 3.11 KB
/
bamazon.js
File metadata and controls
108 lines (99 loc) · 3.11 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
var mysql = require("mysql");
var inquirer = require("inquirer");
var Table = require("cli-table2");
var connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "violeta86",
database: "bamazon",
port: 3306
});
connection.connect();
var display = function() {
connection.query("SELECT * FROM products", function(err, res) {
if (err) throw err;
console.log("-----------------------------");
console.log(" Welcome To Bamazon ");
console.log("-----------------------------");
console.log("");
console.log("Find below our Products List");
console.log("");
var table = new Table({
head: ["Product Id", "Product Description", "Cost"],
colWidths: [12, 50, 8],
colAligns: ["center", "left", "right"],
style: {
head: ["aqua"],
compact: true
// 'padding-right' : 1,
}
});
for (var i = 0; i < res.length; i++) {
table.push([res[i].item_id, res[i].product_name, res[i].price]);
}
console.log(table.toString());
console.log("");
shopping();
}); //End Connection to products
};
var shopping = function() {
inquirer
.prompt({
name: "productToBuy",
type: "input",
message: "Please enter the Product Id of the item you wish to purchase.!"
})
.then(function(answer1) {
var selection = answer1.productToBuy;
connection.query("SELECT * FROM products WHERE item_id=?", selection, function(
err,
res
) {
if (err) throw err;
if (res.length === 0) {
console.log(
"That Product doesn't exist, Please enter a Product Id from the list above"
);
shopping();
} else {
inquirer
.prompt({
name: "quantity",
type: "input",
message: "How many items woul you like to purchase?"
})
.then(function(answer2) {
var quantity = answer2.quantity;
if (quantity > res[0].stock_quantity) {
console.log(
"Our Apologies we only have " +
res[0].stock_quantity +
" items of the product selected"
);
shopping();
} else {
console.log("");
console.log(res[0].product_name + " purchased");
console.log(quantity + " qty @ $" + res[0].price);
var newQuantity = res[0].stock_quantity - quantity;
connection.query(
"UPDATE products SET stock_quantity = " +
newQuantity +
" WHERE id = " +
res[0].id,
function(err, resUpdate) {
if (err) throw err;
console.log("");
console.log("Your Order has been Processed");
console.log("Thank you for Shopping with us...!");
console.log("");
connection.end();
}
);
}
});
}
});
});
};
display();