-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamazonCustomer.js
83 lines (58 loc) · 2.1 KB
/
bamazonCustomer.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
//DEPENDENCIES
const mysql = require('mysql');
const inquirer = require('inquirer');
const connection = mysql.createConnection({
host: 'localhost',
port: 8889,
user: 'root',
password: 'root',
database: 'bamazonDB'
});
connection.connect((err) => {
if (err) throw err;
console.log('WELCOME TO BAMAZON!');
console.log('-------------------------------------------');
showTable();
});
//SHOWS INITIAL TABLE OF AVAILABLE PRODUCTS
showTable = () => {
connection.query('SELECT * FROM products', (err, res) => {
for (let i = 0; i < res.length; i++) {
console.log(res[i].id + ' | ' + res[i].product_name + ' | ' + res[i].department_name + ' | $' + res[i].price + ' | ' + res[i].stock_quantity);
}
console.log('-------------------------------------------');
userPrompt();
});
}
//CUSTOMER PROMPTS
userPrompt = () => {
inquirer.prompt([
{
name: 'product_id',
type: 'input',
message: 'Which product would you like to purchase? [Use product ID]'
},
{
name: 'quantity',
type: 'input',
message: 'How many would you like to purchase?'
}
]).then((answer) => {
let product = answer.product_id;
let quantity = answer.quantity;
connection.query('SELECT * FROM products WHERE ?', {id: product}, (err, data) => {
if (err) throw (err);
if (quantity <= data[0].stock_quantity) {
console.log('Item[s] purchased!');
console.log('Your total is: $' + (quantity * data[0].price));
console.log('-------------------------------------------');
connection.query('UPDATE products SET stock_quantity= ' + (data[0].stock_quantity - quantity) + ' WHERE id= ' + product);
showTable();
} else {
console.log('Insufficient quantity!');
console.log('-------------------------------------------');
userPrompt();
}
});
});
}