-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazon.js
More file actions
79 lines (73 loc) · 2.19 KB
/
bamazon.js
File metadata and controls
79 lines (73 loc) · 2.19 KB
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
var mysql = require('mysql');
var inquirer = require('inquirer');
var cTable = require('console.table');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'guate@09',
database: 'bamazondb',
});
connection.connect(function(err){
if(err) throw err;
start();
});
function start(){
connection.query('SELECT * FROM products', function (err, res){
if (err) throw err;
var table=[];
for(var i=0; i< res.length; i++){
output =
[
[res[i].itemID], [res[i].productName], [res[i].price]
];
table.push(output)
}
console.table(['ID', 'Name', 'Price'], table);
questions();
})
}
function questions(){
inquirer.prompt([
{
message: "Type in the product ID of the item you'd like to order.",
type: "input",
name: "productID"
},{
message: "how many would you like to purchase?",
type: "input",
name: "QTY"
}
]).then(function(answer){
var productID = parseInt(answer.productID);
var QTY = parseInt(answer.QTY);
newOrder( productID, QTY);
});
}
function newOrder(productID, QTY){
connection.query('SELECT * FROM products', function (err, res){
if (err) throw err;
var product;
for(var i=0; i < res.length; i++){
if(res[i].itemID == productID){
product=res[i]
}
}
if(product.stockQTY >= QTY){
var totalPrice = QTY * product.price;
var newQTY = product.stockQTY - QTY;
var query = 'UPDATE products SET stockQTY = ? where ?';
console.log(product.productName + " is in stock! Order now complete." + "\r\n" + "Your grand total is: $" + totalPrice);
connection.query(
query,
[newQTY,
{itemID: productID}
],
function(err,res){
})
connection.end()
}else{
console.log("Sorry insufficient product in stock, your order has been cancelled.")
connection.end()
}
})
};