-
Notifications
You must be signed in to change notification settings - Fork 0
/
bamazonSupervisor.js
107 lines (100 loc) · 3.26 KB
/
bamazonSupervisor.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
95
96
97
98
99
100
101
102
103
104
105
106
107
var cTable = require('console.table');
var mysql = require("mysql");
var inquirer = require("inquirer");
var connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "coppeR##6531",
database: "bamazonDB"
});
connection.connect(function (err) {
if (err) throw err;
console.log("connected as id " + connection.threadId);
promptChoice();
})
function promptChoice() {
inquirer.prompt([
{
name: "choice",
type: "list",
choices: ["SALES BY DEPARTMENT", "CREATE NEW DEPARTMENT", "EXIT"]
}
])
.then(function (inquirerResponse) {
if (inquirerResponse.choice == "SALES BY DEPARTMENT") {
salesByDept();
}
if (inquirerResponse.choice == "CREATE NEW DEPARTMENT") {
createDept();
}
if (inquirerResponse.choice == "EXIT") {
connection.end();
}
});
}
// departments table should be taking the sales from the customer and adding them together.
function salesByDept() {
connection.query("SELECT * FROM departments", function (err, res) {
if (err) throw err;
var price;
items = res.length;
var sales;
console.log("=====================================================================");
var tableArr = [];
var profit;
for (var i = 0; i < res.length; i++) {
sales = parseFloat(res[i].product_sales).toFixed(2);
price = parseFloat(res[i].price).toFixed(2);
profit = (sales - res[i].over_head_costs).toFixed(2);
tableArr.push(
{
ID: res[i].department_id,
DEPARTMENT: res[i].department_name,
OVERHEAD_COSTS: res[i].over_head_costs,
SALES: sales,
PROFIT: profit
}
)
}
console.table(tableArr);
console.log("=====================================================================");
promptChoice();
})
}
function createDept() {
inquirer.prompt([
{
name: "department",
type: "input",
message: "DEPARTMENT NAME:"
},
{
name: "overhead",
type: "input",
message: "OVERHEAD COSTS:"
}
])
.then(function (inquirerResponse) {
var department = inquirerResponse.department;
var overhead = inquirerResponse.overhead;
connection.beginTransaction(function (err) {
if (err) {
throw err;
}
connection.query('INSERT INTO departments SET ?', { department_name: department, over_head_costs: overhead}, function (error, results, fields) {
if (error) {
throw error;
}
connection.commit(function (err) {
if (err) {
return connection.rollback(function () {
throw err;
});
}
});
});
promptChoice();
});
});
}