-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamazonCustomer.js
104 lines (99 loc) · 3.08 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//Challenge level REQUIRED/ENTRY
const inquirer = require("inquirer");
const mysql = require("mysql");
// connects to mysql
var connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "Majesty88",
port: 3306,
database: "bamazon"
});
// if connection fails, error prompt
connection.connect(function (err) {
if (err) {
console.error("error connecting: " + err.stack);
return;
}
console.log("connected as id " + connection.threadId);
});
// brings back data from mysql- loads database into application.
function inventory() {
connection.query("SELECT * FROM products", function (err, response) {
if (err) throw err;
console.table(response);
orderProducts();
});
}
// utilizes inquirer and directs user to input product ID of what they'd like to order.
function orderProducts() {
var inquirer = require("inquirer");
inquirer
.prompt([{
/* Pass your questions in here */
name: "orderID",
message: "What is the product ID that you'd like to order?",
type: "input"
},
{
name: "quantity",
message: "How many units of that product would you like to purchase?",
type: "input"
}
])
.then(answers => {
// Use user feedback for... whatever!!
console.log(answers.orderID);
// Finds item in inventory
getProduct(answers.orderID, function (requestedProduct) {
console.log(requestedProduct);
console.log(typeof (requestedProduct.stock_quantity));
var quantity = parseInt(answers.quantity);
if (requestedProduct === undefined) {
console.log(
"Sorry, that product doesn't exist right now. Please pick another product."
);
} else if (
// checks if there's anough inventory for order
requestedProduct.stock_quantity > quantity
) {
// if enough is there, you have to update the data base through a sql query
// requestedProduct.quantity - parseInt(answers.quantity);
purchaseProduct(requestedProduct, quantity);
} else {
console.log("Sorry, insufficient quantity.");
orderProducts();
}
});
});
}
// Given the primary key of a product, query MySQL and return that product record.
function getProduct(productID, callback) {
connection.query("SELECT * FROM products WHERE item_id = ?", [productID], function (error, results) {
if (error) throw error;
callback(results[0]);
});
}
function purchaseProduct(requestedProduct, quantity) {
connection.query(
"UPDATE products SET stock_quantity = stock_quantity - ? WHERE item_id =?",
[quantity, requestedProduct.item_id],
function (err, response) {
console.log("Purchase successful");
displayTable();
}
);
};
function productSales() {
}
// displays table for user to view in command line
function displayTable() {
connection.query("SELECT * FROM products", function (error, results) {
if (error) throw error;
// connected!
inventory = results;
console.table(results);
orderProducts();
});
}
displayTable();