-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbamazonCustomer.js
More file actions
110 lines (102 loc) · 3.45 KB
/
bamazonCustomer.js
File metadata and controls
110 lines (102 loc) · 3.45 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Dependencies
var mysql = require('mysql');
var wrap = require('word-wrap');
var Table = require('cli-table');
var inquirer = require('inquirer');
var colors = require('colors');
// sets connection param for database connection
var connection = mysql.createConnection({
host: "localhost",
port: 3306,
// sets username
user: "root",
// sets password
password: "",
// sets current database
database: "bamazon_db"
});
// makes connection with the server
connection.connect(function (err) {
if (err) throw err;
console.log("connected as id " + connection.threadId);
itemsForSale();
});
// A query which returns all items available for sale ().
function itemsForSale() {
connection.query("SELECT item_id, product_name, price, department_name FROM products WHERE price > 0;", function (err, result) {
// gets and builds the table header
var obj = result[0];
var header = [];
for (var prop in obj) {
header.push(prop);
}
// instantiate
var table = new Table({
head: header,
colWidths: [20, 55, 10, 20]
});
// gets and sets the data in the table
var item_ids = [];
for (var i = 0; i < result.length; i++) {
item_ids.push(result[i].item_id);
table.push([result[i].item_id, wrap(result[i].product_name), result[i].price.toFixed(2), result[i].department_name]);
}
var output = table.toString();
console.log(output);
purchaseItem(item_ids);
});
}
// sets function for cutomer to make a purchase
// list gets the items id's as an array and passed to the promt/choices param
function purchaseItem(list) {
inquirer
.prompt([{
name: "buy",
type: "list",
message: "Please indicate which item would you like to purchase?",
choices: list
},
{
name: "quantity",
type: "input",
message: "Please enter quantity?",
}])
.then(function (answer) {
// sets a query to select the item the user has chosen
var query = "SELECT item_id, stock_quantity, price FROM products WHERE ?";
connection.query(query, { item_id: answer.buy }, function (err, res) {
// console.log(res);
var inputQuantity = answer.quantity;
checkStock(res[0].stock_quantity, inputQuantity, res[0].price.toFixed(2), res[0].item_id);
});
})
}
// checks quantity against the stock
function checkStock(on_stock, buy_quantity, price, item_id) {
if (on_stock >= buy_quantity) {
var total_price = buy_quantity * price;
console.log(`Your total amount is $${total_price}.\nThank you for your purchase on BAMAZON!`.green);
// updates database
updateStock(buy_quantity, item_id);
} else {
console.log(`Insufficient quantity on stock!\nOnly ${on_stock} items on stock!`.red);
connection.end();
}
}
// updates stock_quantity in the database
function updateStock(quantity, item_id) {
var query = "UPDATE products SET stock_quantity = stock_quantity - ? WHERE ?";
connection.query(
query,
[
quantity,
{
item_id: item_id
}
],
function (error) {
if (error) throw error;
console.log("DB was succefully updated!");
connection.end();
});
}