lab 4 is done

This commit is contained in:
Katerina881 2023-04-04 14:07:59 +04:00
parent acb147b0a3
commit f885a37b42
12 changed files with 102 additions and 98 deletions

View File

@ -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: <Employees />, label: 'Сотрудники' },
{ path: '/companies', element: <Companies />, label: 'Компании' },
{ path: '/positions', element: <Positions />, label: 'Должности' },
{ path: '/report', element: <ReportEmployeePosition />, label: 'Сотрудники по должностям' },
{ path: '/company/:id', element: <OneCompany />},
];
const links = routes.filter(route => route.hasOwnProperty('label'));

View File

@ -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,56 +103,6 @@ export default function Companies(props) {
Отправить
</Button>
</Form>;
// // логика для найма или увольнения
// 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 = <Form onSubmit={hire}>
// <Form.Group className="mb-3" controlId="name">
// <Form.Label>Компания</Form.Label>
// <Form.Select name="name_select" type="input" onChange={(e) => {setCompany(e.target.value)}} required>
// <option selected disabled>Выберите компанию</option>
// {
// items.map((company) => <option key={`company_${company.id}`} value={`${company.id}`}>{`${company.name}`}</option>)
// }
// </Form.Select>
// </Form.Group>
// <Form.Group className="mb-3" controlId="employee">
// <Form.Label>Сотрудник</Form.Label>
// <Form.Select name="employee_select" type="input" onClick={loadItemsEmployees} onChange={(e) => {setEmployee(e.target.value)}} required>
// <option selected disabled>Выберите сотрудника</option>
// {
// itemsEmp.map((e) => <option key={`emp_${e.id}`} value={`${e.id}`}>{`${e.surname} ${e.name}`}</option>)
// }
// </Form.Select>
// </Form.Group>
// <Button variant="primary" type="submit">
// Отправить
// </Button>
// </Form>;
const [showModalForm, setShowChoosing] = useState(false);
@ -202,9 +138,6 @@ export default function Companies(props) {
return <div className="container-lg pt-5 min-vh-100">
<Button onClick={showModalFormChoosing} variant="info">Перейти к компании</Button>
{/* <Button name="Найм" onClick={showModalForm} variant="success">Нанять сотрудника</Button>
<Button name='Увольнение' onClick={showModalForm} variant="danger">Уволить сотрудника</Button>
<ModalForm show={showModalFormHire} onClose={unshowModalForm} modalTitle={modalTitleHire} form={formHire}></ModalForm> */}
<ModalForm show={showModalForm} onClose={unshowModalFormChoosing} modalTitle={"Выбор компаниии"} form={formChooseCompany}></ModalForm>
<Catalog name={nameCatalog}
headers={headers}

View File

@ -13,7 +13,8 @@ export default function OneCompany(props) {
const { id } = useParams();
const url = '/company/id';
const urlEmp = '/employee';
const urlCompanyEmp = '/company/id/employees';
const urlEmp = '/employee/free';
const urlPos = '/position';
const urlHire = '/company/id/hire';
const urlDismiss = '/company/id/dismiss';
@ -23,7 +24,8 @@ export default function OneCompany(props) {
const requestParamsHire = '?employeeId=empId';
const [company, setCompany] = useState(new Company());
const [itemsEmp, setItemsEmp] = useState([]);
const [itemsEmpFree, setItemsEmpFree] = useState([]);
const [itemsEmpCompany, setItemsEmpCompany] = useState([]);
const [itemsPos, setItemsPos] = useState([]);
const headersEmp = [
@ -35,17 +37,15 @@ export default function OneCompany(props) {
useEffect(() => {
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() {
loadItemsEmployees();
setShowHire(true);
}
function unshowModalFormHire() {
@ -67,6 +73,7 @@ export default function OneCompany(props) {
const [isShowDismiss, setShowDismiss] = useState(false);
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
@ -99,6 +109,8 @@ export default function OneCompany(props) {
const [isShowChoosePosition, setShowChoosePosition] = useState(false);
function showModalFormChoosePosition() {
loadItemsPositions();
loadItemsEmployeesCompany();
setShowChoosePosition(true);
}
function unshowModalFormChoosePosition() {
@ -132,7 +144,7 @@ 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))
@ -141,14 +153,13 @@ export default function OneCompany(props) {
}
}
const formHire = <Form onSubmit={formHireSubmit}>
<Form.Group className="mb-3" controlId="employee">
<Form.Label>Сотрудник</Form.Label>
<Form.Select name="employee_select" type="input" onChange={employeeChosen} required>
<Form.Select name="employee_select" type="input" onChange={employeeChosenFree} required>
<option selected disabled>Выберите сотрудника</option>
{
itemsEmp.map((e) => <option key={`emp_${e.id}`} value={`${e.id}`}>{`${e.surname} ${e.name}`}</option>)
itemsEmpFree.map((e) => <option key={`emp_${e.id}`} value={`${e.id}`}>{`${e.surname} ${e.name}`}</option>)
}
</Form.Select>
</Form.Group>
@ -161,10 +172,10 @@ export default function OneCompany(props) {
const formDismiss = <Form onSubmit={formDismissSubmit}>
<Form.Group className="mb-3" controlId="employee">
<Form.Label>Сотрудник</Form.Label>
<Form.Select name="employee_select" type="input" onChange={employeeChosen} required>
<Form.Select name="employee_select" type="input" onChange={employeeChosenBusy} required>
<option selected disabled>Выберите сотрудника</option>
{
Array.from(company.employees).map((e) => <option key={`emp_${e.id}`} value={`${e.id}`}>{`${e.surname} ${e.name}`}</option>)
itemsEmpCompany.map((e) => <option key={`emp_${e.id}`} value={`${e.id}`}>{`${e.surname} ${e.name}`}</option>)
}
</Form.Select>
</Form.Group>
@ -172,16 +183,14 @@ export default function OneCompany(props) {
Отправить
</Button>
</Form>;
function employeeHasPosition(id) {
return Array.from(employee.positions).filter((pEm) => pEm.id == id)[0] != undefined;
}
const formCheckBoxesPosition = <Form onSubmit={formChoosePositionsSubmit}>
<Form.Group className="mb-3" controlId="employee">
<Form.Label>Сотрудник</Form.Label>
<Form.Select name="employee_select" type="input" onChange={employeeChosen} required>
<Form.Select name="employee_select" type="input" onChange={employeeChosenBusy} required>
<option selected disabled>Выберите сотрудника</option>
{
Array.from(company.employees).map((e) => <option key={`emp_${e.id}`} value={`${e.id}`}>{`${e.surname} ${e.name}`}</option>)
itemsEmpCompany.map((e) => <option key={`emp_${e.id}`} value={`${e.id}`}>{`${e.surname} ${e.name}`}</option>)
}
</Form.Select>
</Form.Group>
@ -190,8 +199,9 @@ export default function OneCompany(props) {
<div className={`${styles.prokrutka}`}>
{
itemsPos.map((p) => <div style={{width: `150 px`}}>
{!employeeHasPosition(p) && <input type="checkbox" key={`${p.id}`} value={`${p.id}`} onChange={checkBoxChanged}/>}
{employeeHasPosition(p) && <input type="checkbox" key={`${p.id}`} value={`${p.id}`} checked onChange={checkBoxChanged}/>}
<input type="checkbox" key={`${p.id}`} value={`${p.id}`} onChange={checkBoxChanged}/>
{/* {!employeeHasPosition(p) && <input type="checkbox" key={`${p.id}`} value={`${p.id}`} onChange={checkBoxChanged}/>}
{employeeHasPosition(p) && <input type="checkbox" key={`${p.id}`} value={`${p.id}`} checked onChange={checkBoxChanged}/>} */}
{`${p.name}`}
</div>)
}

View File

@ -6,7 +6,6 @@ export default function Table(props) {
function remove(itemId) {
props.onDelete(itemId);
}
console.log(props.items);
return <div >
<table className={`table table-hover`}>
<thead>

View File

@ -11,6 +11,7 @@ export default class Employee {
if (this.positions !== '') {
this.positions.forEach((p) => {this.positionsString += p.name + " "});
}
this.company = data?.company || '';
}
}

View File

@ -25,6 +25,11 @@ public class CompanyController {
return new CompanyDto(companyService.findCompany(id));
}
@GetMapping("/{id}/employees")
public List<EmployeeDto> getCompanyEmployees(@PathVariable long id) {
return companyService.findCompany(id).getEmployees().stream().map(EmployeeDto::new).toList();
}
@GetMapping
public List<CompanyDto> getAllCompanies() {
return companyService.findAllCompanies().stream()

View File

@ -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<EmployeeDto> getAllFreeEmployees() {
return employeeService.findAllFreeEmployees().stream()
.map(EmployeeDto::new)
.toList();
}
@GetMapping("/position")
public List<EmployeeDto> getEmployeesByPosition(@RequestParam Long pos) {
Position p = positionService.findPosition(pos);
if (p == null) {
return null;
}
return employeeService.getEmployeesByPosition(p).stream()
.map(EmployeeDto::new)
.toList();
}
}

View File

@ -11,4 +11,5 @@ import java.util.Optional;
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
Optional<Employee> findById(Long id);
List<Employee> findByPositions_Id(Long p_id);
List<Employee> findByCompanyIsNull();
}

View File

@ -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;
}
}

View File

@ -14,12 +14,15 @@ public class EmployeeDto {
private final String surname;
private final List<PositionDto> 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<PositionDto> getPositions() {
return positions;
}
public CompanyWithoutEmpDto getCompany() {
return company;
}
}

View File

@ -41,6 +41,10 @@ public class EmployeeService {
public List<Employee> findAllEmployees() {
return employeeRepository.findAll();
}
@Transactional(readOnly = true)
public List<Employee> findAllFreeEmployees() {
return employeeRepository.findByCompanyIsNull();
}
@Transactional
public Employee updateEmployee(Long id, String surname, String name, String phoneNumber) {