Skip to content

Commit

Permalink
added querryies and features for application commands: Add Department…
Browse files Browse the repository at this point in the history
… \ Add job Role
  • Loading branch information
Zach-EE committed Sep 7, 2021
1 parent ab7fa81 commit 417b8c6
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 3 deletions.
99 changes: 98 additions & 1 deletion src/lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ const start = () => {
console.log(commandList.commandList[3]);
addEmployee();
break;

case commandList.commandList[4]:
console.log(commandList.commandList[4]);
addDepartment();
break;

case commandList.commandList[5]:
console.log(commandList.commandList[5]);
addRole();
break;
}
}

Expand Down Expand Up @@ -90,6 +100,7 @@ const viewAllEmployees = async () => {
console.log(err);
}
}

//* Add to DB Functions:
const addEmployee = async () => {
try {
Expand Down Expand Up @@ -157,7 +168,7 @@ const addEmployee = async () => {
},
(err) => {
if (err) throw err;
console.log(`The new employee ${answer.empFirstName} ${answer.empLastName} was added successfully!`);
console.log(`Success: Employee ${answer.empFirstName} ${answer.empLastName} was added to DB`);
start();
});
});
Expand All @@ -173,4 +184,90 @@ const addEmployee = async () => {
}
}

const addDepartment = async () => {
try {
const promptUser = () => {
return inquirer
.prompt([
{
name: "deptName",
type: "input",
message: "Please Enter New Department Name...",
}
])
.then((answer) => {
connection.query(
queries.addDepartment,
{
name: answer.deptName
},
(err) => {
if (err) throw err;
console.log(`Success: ${answer.deptName} department added to db...\n`);
start();
});
});
}
await promptUser();
} catch (err) {
console.log(err);
}
}

const addRole = async () => {
try {
const promptUser = () => {
return inquirer
.prompt([
{
name: "roleTitle",
type: "input",
message: "Enter Role Title...",
},
{
name: "roleSalary",
type: "input",
message: "Enter Salary for Role (ex. $80000)...",
},
{
name: "roleDeptId",
type: "rawlist",
choices: function () {
const choiceArray = [];
depts.forEach((dept) => {
const deptObj = {
name: dept.department_name,
value: dept.id
}
choiceArray.push(deptObj)
})
return choiceArray;
},
message: "What Department is Role in?..."

}
])
.then((answer) => {
connection.query(
queries.addRole,
{
title: answer.roleTitle,
salary: answer.roleSalary,
department_id: answer.roleDeptId
},
(err) => {
if (err) throw err;
console.log(`Success: New Role ${answer.role} added to ${answer.roleDeptId}`);
start();
}
)
})
}
const depts = await getDepartment();
await promptUser();
} catch (err) {
console.log(err);
}
}

module.exports ={start};
3 changes: 1 addition & 2 deletions src/lib/commandList.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ const commandList = [
'Add Employee',
'Add Department',
'Add job Role',
'Test',
'Test',
'Update Employee Job Role',
'Quit',
];

Expand Down
6 changes: 6 additions & 0 deletions src/lib/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ const queries = {

addEmployee: fs.readFileSync(path.join(__dirname, '../sql/addEmployee.sql')).toString(),


addDepartment: fs.readFileSync(path.join(__dirname, '../sql/addDepartment.sql')).toString(),

addRole: fs.readFileSync(path.join(__dirname, '../sql/addRole.sql')).toString(),


}

module.exports = queries;
1 change: 1 addition & 0 deletions src/sql/addDepartment.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO department SET ?
1 change: 1 addition & 0 deletions src/sql/addRole.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO role SET ?

0 comments on commit 417b8c6

Please sign in to comment.