-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomer.js
More file actions
79 lines (72 loc) · 2.5 KB
/
Copy pathcustomer.js
File metadata and controls
79 lines (72 loc) · 2.5 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 Table = require("cli-table");
// create the connection information for the sql database
var connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "",
database: "bamazon"
});
// connect to the mysql server and sql database
connection.connect(function(err) {
if (err) throw err;
});
//disply the table and items
function displayTable() {
connection.query('SELECT * from products', function(error, results, fields) {
if (error) throw error;
var table = new Table({
head: ['Item ID', 'Product Name', 'Department Name', 'Price', "Stock"]
});
results.forEach(function(columns) {
var obj = new Object;
obj[columns.item_id] = [columns.product_name, columns.department_name, "$" + columns.price, columns.stock_quantity];
table.push(obj);
});
console.log(table.toString());
selectItem();
});
}
// function to ask user what Item the wish to buy
function selectItem() {
inquirer.prompt([{
type: "input",
name: "itemID",
message: "Please enter an item-id you would like to purchase?",
},
{
type: "input",
name: "quantity",
message: "How many items would you like to buy?",
}
]).then(function(response) {
connection.query('SELECT * FROM products WHERE item_id = ? ', [parseInt(response.itemID)], function(err, ress) {
if (err) throw err;
if (ress[0].stock_quantity >= parseInt(response.quantity)) {
var newStock = ress[0].stock_quantity - parseInt(response.quantity);
var sale = parseFloat(response.quantity) * ress[0].price;
updateTable(newStock, sale, parseInt(response.itemID));
} else if (ress[0].stock_quantity < parseInt(response.quantity)) {
console.log("not enough item on stock!!!");
}
});
});
}
function updateTable(newStock, sale, id) {
connection.query('UPDATE products SET stock_quantity = ?, product_sale = ? WHERE item_id = ? ', [newStock, sale, id], function(errors, result) {
if (errors) throw errors;
//Displays customer cost
console.log("Thank you, your total cost today is: $" + sale);
console.log("");
console.log("");
console.log("-------------------");
//if there isnt enough inventory diaply message then rerun displayTable
console.log("database updated ");
});
//after table updates rerun displayTable
displayTable();
}
// run the start function when the file is loaded to prompt the user
displayTable();