-
Notifications
You must be signed in to change notification settings - Fork 2
/
bamazonCustomer.js
74 lines (66 loc) · 1.95 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
var mysql = require("mysql");
var inquirer = require("inquirer");
var Table = require("cli-table");
var connection = mysql.createConnection({
host:"localhost",
port:3306,
user:"root",
password:"",
database:"bamazon"
});
connection.connect(function(err){
if(err)throw err;
console.log("connected as id" + connection.threadId);
});
var displayProducts = function(){
var query = "Select * FROM products";
connection.query(query, function(err, res){
if(err) throw err;
var displayTable = new Table ({
head: ["Item ID", "Product Name", "Catergory", "Price", "Quantity"],
colWidths: [10,25,25,10,14]
});
for(var i = 0; i < res.length; i++){
displayTable.push(
[res[i].item_id,res[i].product_name, res[i].department_name, res[i].price, res[i].stock_quantity]
);
}
console.log(displayTable.toString());
purchasePrompt();
});
}
function purchasePrompt(){
inquirer.prompt([
{
name: "ID",
type: "input",
message:"Please enter Item ID you like to purhcase.",
filter:Number
},
{
name:"Quantity",
type:"input",
message:"How many items do you wish to purchase?",
filter:Number
},
]).then(function(answers){
var quantityNeeded = answers.Quantity;
var IDrequested = answers.ID;
purchaseOrder(IDrequested, quantityNeeded);
});
};
function purchaseOrder(ID, amtNeeded){
connection.query('Select * FROM products WHERE item_id = ' + ID, function(err,res){
if(err){console.log(err)};
if(amtNeeded <= res[0].stock_quantity){
var totalCost = res[0].price * amtNeeded;
console.log("Good news your order is in stock!");
console.log("Your total cost for " + amtNeeded + " " +res[0].product_name + " is " + totalCost + " Thank you!");
connection.query("UPDATE products SET stock_quantity = stock_quantity - " + amtNeeded + "WHERE item_id = " + ID);
} else{
console.log("Insufficient quantity, sorry we do not have enough " + res[0].product_name + "to complete your order.");
};
displayProducts();
});
};
displayProducts();