我正在尝试创建一个命令行界面,提示用户输入或从数据库中检索数据。
到目前为止,我编写的唯一方法是获取用户创建新数据库行所需的信息。但是,当调用其中一个查询器提示时,以下提示的其余部分也会执行,即使不在同一方法中也是如此。
class Query {
constructor () {
this.options = ['View All Departments', 'View All Roles', 'View All Employees', 'Add Department', 'Add Role', 'Add Employee', 'Update Employee Role']
};
optionsMenu() {
inquirer.prompt({
type: 'list',
message: 'What would you like to do next?',
choices: this.options,
name: 'userOption'
})
.then((res) => {
switch(res.userOption) {
case 'View All Departments':
this.viewDept();
case 'View All Roles':
this.viewRoles();
case 'View All Employees':
this.viewEmp();
case 'Add Department':
this.addDept();
case 'Add Role':
this.addRole();
case 'Add Employee':
this.addEmp();
case 'Update Employee Role':
this.updateEmp();
}
})
}
viewDept() {}
viewRoles() {}
viewEmp() {}
async addDept() {
const inquiry = await inquirer.prompt({
type: 'input',
message: 'What is the name of the new department?',
name: 'department'
})
const newDept = new Department(inquiry.department);
newDept.insertDept().then(() => {
this.optionsMenu();
});
}
async addRole() {
//Query DB for available departments to tie to the role being added
const departments = await mysql.db.promise().query('SELECT * FROM department').then((results) => {return results[0]});
//Map departments into an array for the inquirer question
const departmentChoice = departments.map(x => x.name)
//Query DB for roles
const roleQuery = await mysql.db.promise().query('SELECT * FROM role').then((results) => {return results[0]});
//Map roles into an array for the inquirer validation
const roles = roleQuery.map(x => x.title.toLowerCase());
//Inquirer prompts for role information from User
const inquiry = await inquirer.prompt([
{
type: 'input',
message: 'What is the name of the role you would like to add?',
name: 'role',
validate: (input) => {
//Check input against existing roles to make sure it is unique
let lowercase = input.toLowerCase
if (roles.includes(lowercase)) {
return 'Role already exists'
} else {
return true;
}
}
},
{
type: 'list',
message: 'Which department does the role belong to?',
choices: departmentChoice,
name: 'department'
},
{
type: 'number',
message: 'What is the salary of the new role?',
name: 'salary'
}
])
//Loop through deparments array to find matching department id
for(const i = 0; i < departments.length; i++) {
if (Object.values(departments[i]).includes(inquiry.department)) {
const newRole = new Role(inquiry.role, departments[i].id, inquiry.salary)
newRole.insertRole().then(() => {this.optionsMenu()})
}
}
}
async addEmp() {
//Query DB for roles
const roleQuery = await mysql.db.promise().query('SELECT * FROM role').then((results) => {return results[0]});
//Map roles into an array for the inquirer question
const roleChoice = roleQuery.map(x => x.title);
//Query DB for employees
const employeeQuery = await mysql.db.promise().query('SELECT * FROM employee').then((results) => {return results[0]});
//Map employees into an array for the inquirer question
const employeeChoice = employeeQuery.map(x => x.first_name + " " + x.last_name);
//Add 'None' as an option for employees
employeeChoice.push('None');
const inquiry = await inquirer.prompt([
{
type: 'input',
message: "What is the employee's first name?",
name: 'firstName',
validate: (input) => {
if (!input) {
return 'First name cannot be blank.'
} else {
return true;
}
}
},
{
type: 'input',
message: "What is the employee's last name?",
name: 'lastName',
validate: (input) => {
if (!input) {
return 'Last name cannot be blank.'
} else {
return true;
}
}
},
{
type: 'list',
message: "Which role will the employee take?",
choices: roleChoice,
name: 'role'
},
{
type: 'list',
message: "Enter the employee's manager:",
choices: employeeChoice,
name: 'manager'
}
])
}
updateEmp() {}
}