-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazonCustomer.js
More file actions
128 lines (116 loc) · 3.37 KB
/
bamazonCustomer.js
File metadata and controls
128 lines (116 loc) · 3.37 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
126
127
128
var mysql = require('mysql');
var inquirer = require('inquirer');
var connection = mysql.createConnection({
host:"localhost",
port: 3306,
user: "root",
password: "12345678",
database: "bamazon"
});
connection.connect(function (err) {
if (err) throw err;
console.log('connected as id ' + connection.threadId);
askAction();
})
function readProducts(){
connection.query("SELECT * FROM products", function (err, res){
if (err) throw err;
console.log('****** Our products! ******\n');
for (let i = 0; i < res.length; i++){
console.log('ID: ' + res[i].item_id
+ ' - ' + res[i].product_name
+ ' - ' + res[i].department_name
+ ' - $ ' + res[i].price
+ ' - ' + res[i].stock_quantity) + ' unit(s).';
}
console.log('******\n');
askAction();
}); //connection.query
}
function askAction(){
inquirer.prompt([
{
type: "list",
name: "action",
message: "What are you up to?",
choices: [
"See all products",
"Buy an item",
"Exit"
]
}
]).then(function(answers){ // inquirer.prompt
switch (answers.action){
case 'See all products':
readProducts();
break;
case 'Buy an item':
promptBuyItem();
break;
case 'Exit':
exit();
break;
}
}); // inquirer.prompt().then()
}
/**
* Buying item
*/
function promptBuyItem(){
inquirer.prompt([
{
type: 'input',
name: 'itemToBuy',
message: 'Please introduce the id of the item you want to buy.'
},
{
type: 'input',
name: 'amountToBuy',
message: 'Please introduce the amount of items you want to buy.'
}
]).then(function(answers){ // inquirer.prompt()
buyItem(answers.itemToBuy, answers.amountToBuy);
}); // inquirer.prompt().then();
}
function buyItem(id, amount) {
connection.query('SELECT * FROM products WHERE ?',
[
{
item_id: id
}
], function (err, res) {
if (err) throw err;
if (parseInt(res[0].stock_quantity) >= parseInt(amount)) {
var total = parseInt(res[0].price) * amount;
updateProduct(id, res[0].stock_quantity, amount);
console.log("You bought " + amount + ' ' + res[0].product_name + " for a total of: $ " + total + "\n");
console.log("****** Thanks for buying! ******")
}else {
console.log("We don't have enough units of the requested item, try again later!\n");
askAction();
}
});
}
function updateProduct(id, originalStock, amountToReduce) {
console.log("****** Updating stock ******\n");
var remaining = parseInt(originalStock) - parseInt(amountToReduce);
connection.query("UPDATE products SET ? WHERE ?",
[
{
stock_quantity: remaining
},
{
item_id: id
}
],
function (err, res) {
askAction();
}
);
}
/**
* Exit
*/
function exit() {
connection.end();
}