-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamazonSupervisor.js
100 lines (78 loc) · 2.78 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
var mysql = require("mysql");
var inquirer = require("inquirer");
var Table = require("cli-table");
var connection = mysql.createConnection({
host: "localhost",
// no port set = default port 3306
user: "root",
password: "",
database: "bamazon"
});
// test connection
connection.connect(function(error){
if (error) throw error;
// console.log("connected to " + connection.threadId);
});
function start() {
inquirer.prompt([
{
name: "menuItem",
type: "list",
message: "Please select what you would like to do.",
choices: ["View Product Sales By Department", "Create New Department"]
}
]).then(function(answer){
var menuOption = answer.menuItem;
switch (menuOption) {
case "View Product Sales By Department":
sales();
break;
case "Create New Department":
create();
break;
}
});
};
start();
function sales() {
connection.query("SELECT departments.*, SUM(products.product_sales) AS total_product_sales, (SUM(products.product_sales) - over_head_costs) AS total_profit FROM `departments` JOIN products ON departments.department_id=products.department_id GROUP BY departments.department_id", function(error, response){
var table = new Table({
head: ['department_id', 'department_name', 'over_head_costs', 'product_sales', 'total_profit']
, colWidths: [14, 20, 14, 14, 14]
});
response.forEach(function(response){
var id = response.department_id;
var name = response.department_name;
var costs = response.over_head_costs;
var sales = response.total_product_sales;
var total = response.total_profit;
table.push(
[id, name, costs, sales, total]
);
});
console.log(table.toString());
connection.end();
})
}
function create() {
inquirer.prompt([
{
name: "newDepartmentName",
type: "input",
message: "Please enter the name of your new department."
},
{
name: "newOverheadCosts",
type: "input",
message: "Please enter the overhead costs for this department."
}
]).then(function(answer){
var department = answer.newDepartmentName;
var costs = answer.newOverheadCosts;
connection.query("INSERT INTO departments(department_name, over_head_costs) VALUES ('" + department + "'," + costs + ")", function(error, response){
if (error) throw error;
console.log("You've added the department named " + department + " to your records, with the overhead costs of " + costs + ".");
})
connection.end();
});
}