-
Notifications
You must be signed in to change notification settings - Fork 0
/
bamazonSupervisor.js
105 lines (100 loc) · 3.17 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
var mysql = require("mysql");
var inquirer = require("inquirer");
var Table = require("cli-table");
var conn = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "1289",
database: "bamazon"
});
conn.connect(function(err) {
if(err) throw err;
menu();
});
function menu() {
inquirer.prompt([
{
name: "options",
type: "list",
message: "What would you like to do?",
choices: ["VIEW PRODUCT SALES BY DEPARTMENT", "CREATE NEW DEPARTMENT", "EXIT"]
}
]).then(function(ans) {
switch(ans.options) {
case "VIEW PRODUCT SALES BY DEPARTMENT":
prodSales();
break;
case "CREATE NEW DEPARTMENT":
newDep();
break;
case "EXIT":
conn.end();
break;
}
});
}
function prodSales() {
var sql = "select departments.department_id, departments.department_name, departments.over_head_costs, sum(products.product_sales) as sales,";
sql += " sum(products.product_sales)-departments.over_head_costs as total_profit";
sql += " from products inner join departments on products.department_name = departments.department_name";
sql += " group by departments.department_id";
conn.query(sql, function(err, res) {
if(err) throw err;
var table = new Table({
head: ["Department ID", "Department Name", "Overhead Costs", "Product Sales", "Total Profit"],
colWidths: [16, 19, 17, 16, 15]
});
for (var i = 0; i < res.length; i++) {
table.push(
[res[i].department_id, res[i].department_name, res[i].over_head_costs, res[i].sales, res[i].total_profit]
);
}
console.log(table.toString());
menu();
});
}
function newDep() {
inquirer.prompt([
{
name: "name",
type: "input",
message: "Enter the new department name:"
},
{
name: "costs",
type: "input",
message: "What are the overhead costs of this department?"
},
{
name: "ID",
type: "input",
message: "Please create a department ID:"
}
]).then(function(ans) {
var sql = "insert into departments set ?";
conn.query(sql,
[
{
department_name: ans.name,
over_head_costs: ans.costs,
department_id: ans.ID
}
], function(err, res) {
if(err) throw err;
conn.query(" select * from departments;", function(err, res) {
var table = new Table({
head: ["Department ID", "Department Name", "Overhead Costs"],
colWidths: [16, 19, 17]
});
for (var i = 0; i < res.length; i++) {
table.push(
[res[i].department_id, res[i].department_name, res[i].over_head_costs]
);
}
console.log(table.toString());
menu();
});
});
});
}