-
Notifications
You must be signed in to change notification settings - Fork 3
/
bamazonSupervisor.js
145 lines (134 loc) · 4.7 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// SETUP
// =====================================================================================
var mysql = require('mysql');
var inquirer = require('inquirer');
var chalk = require('chalk');
var Table = require('cli-table');
var connection = mysql.createConnection({
host: '',
user: 'root',
password: 'root',
database: 'bamazon'
});
connection.connect((err) => {
if (err) throw err;
console.log('Connection successful');
resetData();
// display all items from database once mysql connection has been established
displayMenu();
});
// GLOBAL VARIABLES
// =====================================================================================
var deptToDelete = [];
// FUNCTIONS
// =====================================================================================
var resetData = function() {
deptToDelete = [];
};
var displayMenu = function() {
inquirer.prompt({
name: 'action',
type: 'rawlist',
message: 'Choose an action:',
choices: [
'View Departments',
'View Product Sales by Department',
'Create New Department',
'Delete A Department'
]
}).then((answer) => {
switch (answer.action) {
case 'View Departments':
viewDepartments();
break;
case 'View Products Sales by Department':
viewDepartmentSales();
break;
case 'Create New Department':
createDepartment();
break;
case 'Delete A Department':
deleteDepartment();
break;
}
});
};
var viewDepartments = function() {
connection.query('SELECT * FROM departments', (err, res) => {
var listTable = new Table({
head: ['Dept ID', 'Dept Name', 'Overhead'],
colWidths: [10, 25, 12]
});
for (var i = 0; i < res.length; i++) {
listTable.push([res[i].department_id, res[i].department_name, `$${res[i].over_head_costs}`])
// console.log(chalk.blue.bold(`\n\tDept ID: ${res[i].department_id}\n\tDept Name: ${res[i].department_name}\n\tOverhead Costs: $${res[i].over_head_costs}\n`));
}
console.log(`\n\n${listTable.toString()}\n\n`);
connection.end();
});
};
var viewDepartmentSales = function() {
connection.query(`SELECT * FROM products`, (err, res) => {
for (var i = 0; i < res.length; i++) {
console.log(chalk.blue.bold(`\n\tItem ID: ${res[i].item_id}\n\tProduct Name: ${res[i].product_name}\n\tPrice: $${res[i].price}\n`));
}
connection.end();
});
};
var createDepartment = function() {
inquirer.prompt([
{
name: 'name',
type: 'input',
message: 'Enter the department name:'
},
{
name: 'overhead',
type: 'input',
message: 'Enter the overhead costs for this department:',
validate: (value) => {
if (!isNaN(value) && value > 0) {
return true;
} else {
console.log(chalk.red(' => Oops, please enter a number greater than 0'));
return false;
}
}
},
]).then((answers) => {
connection.query('INSERT INTO departments SET ?', {
department_name: answers.name,
over_head_costs: answers.overhead
}, (err, res) => {
if (err) throw err;
console.log(chalk.blue.bold('\n\tDepartment successfully added!\n'));
connection.end();
});
});
};
var deleteDepartment = function() {
inquirer.prompt({
name: 'deptID',
type: 'input',
message: 'Enter the ID of the department you\'d like to remove:'
}).then((answer) => {
connection.query('SELECT * FROM departments WHERE ?', { department_id: answer.deptID }, (err, res) => {
inquirer.prompt({
name: 'confirm',
type: 'confirm',
message: `You would like to delete` + chalk.blue.bold(` '${res[0].department_name}'. `) + `Is this correct?`
}).then((answer) => {
if (answer.confirm) {
deptToDelete.push(res);
connection.query('DELETE FROM departments WHERE ?', { department_id: deptToDelete[0][0].department_id }, (err, res) => {
if (err) throw err;
console.log(chalk.blue.bold('\n\tDepartment successfully deleted!\n'));
connection.end();
});
} else {
deleteDepartment();
}
});
});
});
};