-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazonCustomer.js
More file actions
142 lines (135 loc) · 5.02 KB
/
bamazonCustomer.js
File metadata and controls
142 lines (135 loc) · 5.02 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Require NPM packages
var mysql = require('mysql');
var inquirer = require('inquirer');
var colors = require('colors');
// Setup connection to SQL server
var connection = mysql.createConnection({
host: 'localhost',
port: 3306,
user: 'root',
password: '',
database: 'bamazon_DB'
});
// Set counter for total number of products
var numberOfProductTypes = 0;
// Connect to DB
connection.connect(function(err) {
// Throw error if it errors
if (err) throw err;
// New promise that selects all data from the table
new Promise(function(resolve, reject) {
connection.query('SELECT * FROM products', function(err, res) {
if (err) reject(err);
resolve(res);
console.log('Welcome to Bamazon! Here are our products:'.red.underline.bold);
});
// Console log each item and increment the number of products
}).then(function(result) {
result.forEach(function(item) {
numberOfProductTypes++;
console.log('Item ID: '.cyan + item.item_id + ' || Product Name: '.yellow + item.product_name + ' || Price: '.green + item.price);
});
// Enter the store
}).then(function() {
return enterStore();
// catch errors
}).catch(function(err) {
console.log(err);
});
});
// Function to enter the store
function enterStore() {
inquirer.prompt([{
name: 'entrance',
message: 'Would you like to shop with us today?'.yellow,
type: 'list',
choices: ['Yes', 'No']
}]).then(function(answer) {
// Go to the customer shopping menu if Yes
if (answer.entrance === 'Yes') {
menu();
} else {
// Exit CLI if No
console.log('Please come back soon! --Bamazon'.cyan.bold);
connection.destroy();
return;
}
});
}
// Function for the menu options for the customer
function menu() {
return inquirer.prompt([{
name: 'item',
message: 'Enter the item number of the product you would like to purchase.'.yellow,
type: 'input',
// Validator to ensure the product number is a number and it exists
validate: function(value) {
if ((isNaN(value) === false) && (value <= numberOfProductTypes)) {
return true;
} else {
console.log('\nPlease enter a valid ID.'.bgRed);
return false;
}
}
}, {
name: 'quantity',
message: 'How many would you like to buy?'.yellow,
type: 'input',
// Validator to ensure it is number
validate: function(value) {
if (isNaN(value) === false) {
return true;
} else {
console.log('\nPlease enter a valid quantity.'.bgRed);
return false;
}
}
// new promise to pull all data from SQL
}]).then(function(answer) {
return new Promise(function(resolve, reject) {
connection.query('SELECT * FROM products WHERE ?', { item_id: answer.item }, function(err, res) {
if (err) reject(err);
resolve(res);
});
// Then if selected quanitity is valid, save to a local object, else console log error
}).then(function(result) {
var savedData = {};
if (parseInt(answer.quantity) <= parseInt(result[0].stock_quantity)) {
savedData.answer = answer;
savedData.result = result;
} else if (parseInt(answer.quantity) > parseInt(result[0].stock_quantity)) {
console.log('Insufficient quantity!'.bgRed);
} else {
console.log('An error occurred, exiting Bamazon, your order is not complete.'.bgRed);
}
return savedData;
// Update the SQL DB and console log messages for completion.
}).then(function(savedData) {
if (savedData.answer) {
var updatedQuantity = parseInt(savedData.result[0].stock_quantity) - parseInt(savedData.answer.quantity);
var itemId = savedData.answer.item;
var totalCost = parseInt(savedData.result[0].price) * parseInt(savedData.answer.quantity);
connection.query('UPDATE products SET ? WHERE ?', [{
stock_quantity: updatedQuantity
}, {
item_id: itemId
}], function(err, res) {
if (err) throw err;
console.log('Your order total cost: '.green + '$' + totalCost + '. Thank you for shopping with Bamazon!'.cyan);
connection.destroy();
});
} else {
// Recursion to re-enter store
enterStore();
}
// catch errors
}).catch(function(err) {
console.log(err);
connection.destroy();
});
// catch errors
}).catch(function(err) {
console.log(err);
connection.destroy();
});
}