-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazonCust.js
More file actions
119 lines (106 loc) · 2.85 KB
/
bamazonCust.js
File metadata and controls
119 lines (106 loc) · 2.85 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
var mysql = require('mysql');
var Table = require('cli-table2');
var inquirer = require('inquirer');
var showTable = require('./tableBuilder.js');
// Connect to the DB
var connection = mysql.createConnection({
host: 'localhost',
port: 3306,
user: 'root',
password: '',
database: 'bamazon'
});
// If connection fails, log error
connection.connect(function (err) {
if (err) {
console.log('Error connecting to DB');
throw err;
}
});
// Displays table
var displayForUser = function () {
var display = new showTable();
connection.query('SELECT * FROM products', function (err, results) {
display.displayTable(results);
purchaseItem();
});
};
// Asks user for item and quantity
var purchaseItem = function () {
console.log('\n');
inquirer.prompt([{
name: "id",
type: "input",
message: "Enter the ID of the item you would like to purchase.",
}, {
name: "quantity",
type: "input",
message: "How many would you like to purchase?",
// Pings DB for item details
}]).then(function (answer) {
connection.query('SELECT item, catagory, price, stock FROM products WHERE ?', {
id: answer.id
}, function (err, res) {
console.log('\nYou\'ve selected ' + answer.quantity + ' ' + res[0].item + ' at $' + res[0].price + ' each');
purchasePrompt(res, answer.quantity, answer.id);
});
});
};
// Asks user if they are ready to begin a purchase
var customerPrompt = function () {
inquirer.prompt({
name: "action",
type: "list",
message: "Are you ready to start shopping?\n",
choices: ["Yes", "No"]
}).then(function (answer) {
switch (answer.action) {
case 'Yes':
displayForUser();
break;
case 'No':
connection.end();
break;
};
});
};
// Asks user if the order is correct, processes if yes, takes user back to selection if no.
var purchasePrompt = function (res, quantity, id) {
inquirer.prompt({
name: "action",
type: "list",
message: "Is this the correct order?\n",
choices: ["Yes", "No"]
}).then(function (answer) {
let navTo = customerPrompt;
switch (answer.action) {
case 'Yes':
if (res[0].stock >= parseInt(quantity)) {
//If enough inventory to complete order, process order by updating database inventory and notifying customer that order is complete.
var itemQuantity = res[0].stock - parseInt(quantity);
connection.query("UPDATE products SET ? WHERE ?", [{
stock: itemQuantity
},
{
id: id
}
], function (err, res) {});
var cost = res[0].price * parseInt(quantity);
// Order complete
console.log('\nOrder complete. Your total is $' + cost.toFixed(2) + '\n');
// customerPrompt();
} else {
// Order incomplete
console.log('\nSorry, not enough inventory for fulfill your order.\n');
// customerPrompt();
};
break;
case 'No':
navTo = purchaseItem;
break;
};
navTo();
});
};
// Starts app
customerPrompt();