-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamazonCustomer.js
94 lines (82 loc) · 2.66 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
var mysql = require("mysql");
var inquirer = require("inquirer");
var cTable = require("console.table");
var connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
port: 3306,
database: "bamazon_db"
});
connection.connect(function (err) {
if (err) throw err;
console.log("Connected with id: ", connection.threadId, "\n-----------------\n");
displayProducts();
});
function start() {
inquirer.prompt([
{
type: "input",
name: "item",
message: "ID of desired product: ",
validate: function (value) {
if (!isNaN(value) === true) {
return true;
}
return "Please type a valid item ID.";
}
},
{
type: "input",
name: "quantity",
message: "Quantity to be bought",
validate: function (value) {
if (!isNaN(value) === true) {
return true;
}
return "Please type a quantity";
}
}
]).then(function (input) {
var quant = parseInt(input.quantity);
var total = 0;
var query = connection.query("SELECT * FROM products WHERE ?", [{item_id: input.item}], function(err, result){
if(err) throw err;
var res = result[0];
console.log("------------------------\nItem id: ", res.item_id, " || Product Name: ", res.product_name, " || Price: ", res.price, " || In Stock: ", res.stock_quantity, "\n------------------------\n");
if(res.stock_quantity >= quant){
total = quant * res.price;
console.log("Total: $" + total);
var updatedStock = res.stock_quantity - quant;
console.log(`\n=======\nUpdated Stock: ${updatedStock}\n=======\n`);
updateStock(input.item, updatedStock);
}else{
console.log("Insufficient quantity. Please make another order with a smaller quantity.\n");
displayProducts();
}
});
});
}
function updateStock(item, quantity) {
connection.query("UPDATE products SET ? WHERE ?",
[
{
stock_quantity: quantity
},
{
item_id: item
}
]
);
console.log("update");
displayProducts();
}
function displayProducts() {
var itemsNum = 0;
connection.query("SELECT * FROM products", function (err, response) {
if (err) throw err;
console.table(response, "-------------------------------\n");
start();
});
// return itemsNum;
}