From f885a37b42a3ac3cf050633860455467da2055e5 Mon Sep 17 00:00:00 2001 From: Katerina881 Date: Tue, 4 Apr 2023 14:07:59 +0400 Subject: [PATCH] lab 4 is done --- front/src/App.js | 2 + front/src/components/Companies.jsx | 71 +------------------ front/src/components/OneCompany.jsx | 62 +++++++++------- front/src/components/commons/Table.jsx | 1 - front/src/models/Employee.js | 3 +- front/src/services/DataService.js | 2 +- .../lab3/controller/CompanyController.java | 5 ++ .../lab3/controller/EmployeeController.java | 21 ++++++ .../springip/lab3/dao/EmployeeRepository.java | 1 + .../lab3/dto/CompanyWithoutEmpDto.java | 21 ++++++ .../springip/lab3/dto/EmployeeDto.java | 7 ++ .../lab3/service/EmployeeService.java | 4 ++ 12 files changed, 102 insertions(+), 98 deletions(-) create mode 100644 src/main/java/com/example/springip/lab3/dto/CompanyWithoutEmpDto.java diff --git a/front/src/App.js b/front/src/App.js index 4609086..05b42ed 100644 --- a/front/src/App.js +++ b/front/src/App.js @@ -7,6 +7,7 @@ import 'bootstrap/dist/css/bootstrap.min.css'; import Companies from './components/Companies.jsx'; import Positions from './components/Positions.jsx'; import OneCompany from './components/OneCompany.jsx'; +import ReportEmployeePosition from './components/ReportEmployeePosition.jsx'; function Router(props) { return useRoutes(props.rootRoute); } @@ -18,6 +19,7 @@ function Router(props) { { path: '/employees', element: , label: 'Сотрудники' }, { path: '/companies', element: , label: 'Компании' }, { path: '/positions', element: , label: 'Должности' }, + { path: '/report', element: , label: 'Сотрудники по должностям' }, { path: '/company/:id', element: }, ]; const links = routes.filter(route => route.hasOwnProperty('label')); diff --git a/front/src/components/Companies.jsx b/front/src/components/Companies.jsx index 71995bf..328eb7c 100644 --- a/front/src/components/Companies.jsx +++ b/front/src/components/Companies.jsx @@ -18,16 +18,10 @@ export default function Companies(props) { const nameCatalog = "Компании"; const url = '/company'; - const urlEmp = '/employee'; - const urlPos = '/position'; - const urlHire = '/company/hire'; - const urlDismiss = '/company/dismiss'; - const requestParamsHire = '?employeeId=empId' const requestParams = '?name=nameData'; const [items, setItems] = useState([]); - const [itemsEmp, setItemsEmp] = useState([]); - const [itemsPos, setItemsPos] = useState([]); + const [data, setData] = useState(new Company()); const [isEditing, setEditing] = useState(false); @@ -40,15 +34,7 @@ export default function Companies(props) { .then(data => setItems(data)); } - function loadItemsEmployees() { - DataService.readAll(urlEmp, (data) => new Employee(data)) - .then(data => setItemsEmp(data)); - } - function loadItemsPositions() { - DataService.readAll(urlPos, (data) => new Position(data)) - .then(data => setItemsPos(data)); - } function handleAdd() { DataService.create(url +requestParams @@ -117,57 +103,7 @@ export default function Companies(props) { Отправить ; - // // логика для найма или увольнения - // const [showModalFormHire, setShow] = useState(false); - // const [modalTitleHire, setmodalTitleHire] = useState("Найм"); - // function showModalForm(e) { - // setmodalTitleHire(e.target.name); - // setShow(true); - // } - // function unshowModalForm() { - // setShow(false); - // } - // const [company, setCompany] = useState(''); - // const [employee, setEmployee] = useState(''); - - // function hire() { - // if (modalTitleHire === "Найм") { - // DataService.update(urlHire + "/" + company + requestParamsHire - // .replace("empId", employee)) - // .then(() => loadItems()); - // } else { - // DataService.update(urlDismiss + "/" + company + requestParamsHire - // .replace("empId", employee)) - // .then(() => loadItems()); - // } - - // } - - // const formHire =
- // - // Компания - // {setCompany(e.target.value)}} required> - // - // { - // items.map((company) => ) - // } - // - // - // - // Сотрудник - // {setEmployee(e.target.value)}} required> - // - // { - // itemsEmp.map((e) => ) - // } - // - // - // - //
; - - + const [showModalForm, setShowChoosing] = useState(false); const formChooseCompany =
@@ -202,9 +138,6 @@ export default function Companies(props) { return
- {/* - - */} { loadCompany(); - loadItemsPositions(); - loadItemsEmployees(); - }); + }, []); function loadCompany() { - DataService.read(url.replace("id", id), (data) => new Company(data)).then((data) => {setCompany(new Company(data))}); + DataService.read(url.replace("id", id), (data) => new Company(data)).then((data) => setCompany(new Company(data))); } function loadItemsEmployees() { DataService.readAll(urlEmp, (data) => new Employee(data)) - .then(data => setItemsEmp(data)); + .then(data => setItemsEmpFree(data)); } function loadItemsPositions() { @@ -53,11 +53,17 @@ export default function OneCompany(props) { .then(data => setItemsPos(data)); } + function loadItemsEmployeesCompany() { + DataService.readAll(urlCompanyEmp.replace("id", company.id), (data) => new Employee(data)) + .then(data => setItemsEmpCompany(data)); + } + // логика для найма или увольнения const [isShowHire, setShowHire] = useState(false); - function showModalFormHire() { + function showModalFormHire() { + loadItemsEmployees(); setShowHire(true); } function unshowModalFormHire() { @@ -66,7 +72,8 @@ export default function OneCompany(props) { const [isShowDismiss, setShowDismiss] = useState(false); - function showModalFormDismiss(e) { + function showModalFormDismiss(e) { + loadItemsEmployeesCompany(); setShowDismiss(true); } function unshowModalFormDismiss() { @@ -74,10 +81,13 @@ export default function OneCompany(props) { } const [employee, setEmployee] = useState(new Employee()); - function employeeChosen(e) { - setEmployee(itemsEmp.filter((emp) => emp.id == e.target.value)[0]); + function employeeChosenFree(e) { + setEmployee(itemsEmpFree.filter((emp) => emp.id == e.target.value)[0]); } + function employeeChosenBusy(e) { + setEmployee(Array.from(company.employees).filter((emp) => emp.id == e.target.value)[0]); + } function formHireSubmit() { DataService.update(urlHire.replace("id",company.id) + requestParamsHire @@ -98,7 +108,9 @@ export default function OneCompany(props) { const [isShowChoosePosition, setShowChoosePosition] = useState(false); - function showModalFormChoosePosition() { + function showModalFormChoosePosition() { + loadItemsPositions(); + loadItemsEmployeesCompany(); setShowChoosePosition(true); } function unshowModalFormChoosePosition() { @@ -130,9 +142,9 @@ export default function OneCompany(props) { } let positionsEmp = employee.positions; - + for (let i = 0; i < positionsEmp.length; i++) { - if (positionsChosen.indexOf(positionsEmp[i]) == -1) { + if (positionsChosen.indexOf(''+positionsEmp[i].id) == -1) { // удаление должности DataService.update(urlDelPos.replace("id",employee.id) + requestParamsPosition .replace("posId", positionsEmp[i].id)) @@ -140,15 +152,14 @@ export default function OneCompany(props) { } } } - const formHire = Сотрудник - + { - itemsEmp.map((e) => ) + itemsEmpFree.map((e) => ) } @@ -161,10 +172,10 @@ export default function OneCompany(props) { const formDismiss = Сотрудник - + { - Array.from(company.employees).map((e) => ) + itemsEmpCompany.map((e) => ) } @@ -172,16 +183,14 @@ export default function OneCompany(props) { Отправить ; - function employeeHasPosition(id) { - return Array.from(employee.positions).filter((pEm) => pEm.id == id)[0] != undefined; - } + const formCheckBoxesPosition =
Сотрудник - + { - Array.from(company.employees).map((e) => ) + itemsEmpCompany.map((e) => ) } @@ -190,8 +199,9 @@ export default function OneCompany(props) {
{ itemsPos.map((p) =>
- {!employeeHasPosition(p) && } - {employeeHasPosition(p) && } + + {/* {!employeeHasPosition(p) && } + {employeeHasPosition(p) && } */} {`${p.name}`}
) } diff --git a/front/src/components/commons/Table.jsx b/front/src/components/commons/Table.jsx index b5a7da6..2be8fc5 100644 --- a/front/src/components/commons/Table.jsx +++ b/front/src/components/commons/Table.jsx @@ -6,7 +6,6 @@ export default function Table(props) { function remove(itemId) { props.onDelete(itemId); } - console.log(props.items); return
diff --git a/front/src/models/Employee.js b/front/src/models/Employee.js index 7cf9e06..2ff191c 100644 --- a/front/src/models/Employee.js +++ b/front/src/models/Employee.js @@ -10,7 +10,8 @@ export default class Employee { this.positionsString = ''; if (this.positions !== '') { this.positions.forEach((p) => {this.positionsString += p.name + " "}); - } + } + this.company = data?.company || ''; } } diff --git a/front/src/services/DataService.js b/front/src/services/DataService.js index 7bf6a9b..01e0093 100644 --- a/front/src/services/DataService.js +++ b/front/src/services/DataService.js @@ -21,7 +21,7 @@ export default class DataService { } static async read(url, transformer) { - const response = await axios.get(this.dataUrlPrefix + url); + const response = await axios.get(this.dataUrlPrefix + url); return transformer(response.data); } diff --git a/src/main/java/com/example/springip/lab3/controller/CompanyController.java b/src/main/java/com/example/springip/lab3/controller/CompanyController.java index 70fcaa4..313cbbe 100644 --- a/src/main/java/com/example/springip/lab3/controller/CompanyController.java +++ b/src/main/java/com/example/springip/lab3/controller/CompanyController.java @@ -25,6 +25,11 @@ public class CompanyController { return new CompanyDto(companyService.findCompany(id)); } + @GetMapping("/{id}/employees") + public List getCompanyEmployees(@PathVariable long id) { + return companyService.findCompany(id).getEmployees().stream().map(EmployeeDto::new).toList(); + } + @GetMapping public List getAllCompanies() { return companyService.findAllCompanies().stream() diff --git a/src/main/java/com/example/springip/lab3/controller/EmployeeController.java b/src/main/java/com/example/springip/lab3/controller/EmployeeController.java index 68ba6fb..528a5c3 100644 --- a/src/main/java/com/example/springip/lab3/controller/EmployeeController.java +++ b/src/main/java/com/example/springip/lab3/controller/EmployeeController.java @@ -1,6 +1,8 @@ package com.example.springip.lab3.controller; +import com.example.springip.lab3.dto.CompanyDto; import com.example.springip.lab3.dto.EmployeeDto; +import com.example.springip.lab3.models.Employee; import com.example.springip.lab3.models.Position; import com.example.springip.lab3.service.EmployeeService; import com.example.springip.lab3.service.PositionService; @@ -70,4 +72,23 @@ public class EmployeeController { return null; return new EmployeeDto(employeeService.deletePosition(id, p)); } + + @GetMapping("/free") + public List getAllFreeEmployees() { + return employeeService.findAllFreeEmployees().stream() + .map(EmployeeDto::new) + .toList(); + } + + @GetMapping("/position") + public List getEmployeesByPosition(@RequestParam Long pos) { + Position p = positionService.findPosition(pos); + if (p == null) { + return null; + } + + return employeeService.getEmployeesByPosition(p).stream() + .map(EmployeeDto::new) + .toList(); + } } diff --git a/src/main/java/com/example/springip/lab3/dao/EmployeeRepository.java b/src/main/java/com/example/springip/lab3/dao/EmployeeRepository.java index 73c7727..157173d 100644 --- a/src/main/java/com/example/springip/lab3/dao/EmployeeRepository.java +++ b/src/main/java/com/example/springip/lab3/dao/EmployeeRepository.java @@ -11,4 +11,5 @@ import java.util.Optional; public interface EmployeeRepository extends JpaRepository { Optional findById(Long id); List findByPositions_Id(Long p_id); + List findByCompanyIsNull(); } diff --git a/src/main/java/com/example/springip/lab3/dto/CompanyWithoutEmpDto.java b/src/main/java/com/example/springip/lab3/dto/CompanyWithoutEmpDto.java new file mode 100644 index 0000000..de28ab9 --- /dev/null +++ b/src/main/java/com/example/springip/lab3/dto/CompanyWithoutEmpDto.java @@ -0,0 +1,21 @@ +package com.example.springip.lab3.dto; + +import com.example.springip.lab3.models.Company; + +public class CompanyWithoutEmpDto { + private final Long Id; + private final String name; + + public CompanyWithoutEmpDto(Company company) { + this.Id = company.getId(); + this.name = company.getName(); + } + + public Long getId() { + return Id; + } + + public String getName() { + return name; + } +} diff --git a/src/main/java/com/example/springip/lab3/dto/EmployeeDto.java b/src/main/java/com/example/springip/lab3/dto/EmployeeDto.java index e632712..db6b217 100644 --- a/src/main/java/com/example/springip/lab3/dto/EmployeeDto.java +++ b/src/main/java/com/example/springip/lab3/dto/EmployeeDto.java @@ -14,12 +14,15 @@ public class EmployeeDto { private final String surname; private final List positions; + private final CompanyWithoutEmpDto company; + public EmployeeDto(Employee employee) { Id = employee.getId(); this.name = employee.getName(); this.phoneNumber = employee.getPhoneNumber(); this.surname = employee.getSurname(); this.positions = employee.getPositions().stream().map(PositionDto::new).toList(); + this.company = employee.getCompany() != null ? new CompanyWithoutEmpDto(employee.getCompany()) : null; } public Long getId() { @@ -41,4 +44,8 @@ public class EmployeeDto { public List getPositions() { return positions; } + + public CompanyWithoutEmpDto getCompany() { + return company; + } } diff --git a/src/main/java/com/example/springip/lab3/service/EmployeeService.java b/src/main/java/com/example/springip/lab3/service/EmployeeService.java index 12dd04c..88ff6bb 100644 --- a/src/main/java/com/example/springip/lab3/service/EmployeeService.java +++ b/src/main/java/com/example/springip/lab3/service/EmployeeService.java @@ -41,6 +41,10 @@ public class EmployeeService { public List findAllEmployees() { return employeeRepository.findAll(); } + @Transactional(readOnly = true) + public List findAllFreeEmployees() { + return employeeRepository.findByCompanyIsNull(); + } @Transactional public Employee updateEmployee(Long id, String surname, String name, String phoneNumber) {