-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamazonCustomer.js
62 lines (57 loc) · 2.07 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
var mysql = require("mysql");
var inquirer = require('inquirer');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "Iltc110617#!",
database: "bamazon_db"
});
con.connect(function(err) { // leaves this js file and attempts to connect to MySQL and will return with success or error
if (err) throw err;
console.log("Welcome to Bamazon!");
showMenu(); // this is calling the function showMenu
});
function showMenu(){
var sql = "SELECT * FROM products";
con.query(sql, function (err, result) {
if (err) throw err;
for (var i = 0; i < result.length; i++){
console.log("Product: ", result[i].product_name, "ID: ", result[i].item_id)
}
askFirstQuestion(); // this is calling the function askFirstQuestion
});
};
function askFirstQuestion(){
inquirer.prompt([
{
type: "input",
name: "IDquestion",
message: "Enter ID of item you'd like to purchase"
},
{
type: "input",
name: "quantityQuestion",
message: "How many quantity to purchase?"
}
]).then(answers => {
var id = parseInt(answers.IDquestion); // turning string to integer for MySQL
var quantity = parseInt(answers.quantityQuestion); // turning string to integer for MySQL
var sql = "SELECT * FROM products WHERE item_id = " + id; // find product by id
con.query(sql, function (err, result) {
if (err) throw err;
if (quantity <= result[0].stock_quantity){ // if else for handling stock quantity
console.log("You bought the item " + result[0].product_name + " for " + result[0].price * quantity);
var newQuantity = result[0].stock_quantity - quantity;
var sql = "UPDATE products SET stock_quantity = " + newQuantity + " WHERE item_id = " + result[0].item_id; // update product with new stock quantity
con.query(sql, function (err, result){
console.log(result, "Updated Item Quantity: " + newQuantity);
});
showMenu(); // restart app
}else{
console.log("Item Quantity Not In Stock ");
showMenu(); // restart app
};
});
console.log(id, quantity);
});
};