Merge remote-tracking branch 'origin/lab4' into lab4
This commit is contained in:
commit
acb147b0a3
@ -2,7 +2,9 @@ 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.service.CompanyService;
|
||||
import com.example.springip.lab3.service.EmployeeService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
@ -11,9 +13,11 @@ import java.util.List;
|
||||
@RequestMapping("/company")
|
||||
public class CompanyController {
|
||||
private final CompanyService companyService;
|
||||
private final EmployeeService employeeService;
|
||||
|
||||
public CompanyController(CompanyService companyService) {
|
||||
public CompanyController(CompanyService companyService, EmployeeService employeeService) {
|
||||
this.companyService = companyService;
|
||||
this.employeeService = employeeService;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@ -43,4 +47,17 @@ public class CompanyController {
|
||||
public CompanyDto delete(@PathVariable Long id) {
|
||||
return new CompanyDto(companyService.deleteCompany(id));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}/hire")
|
||||
public EmployeeDto hire(@PathVariable Long id, @RequestParam Long employeeId) {
|
||||
Employee e = employeeService.findEmployee(employeeId);
|
||||
return new EmployeeDto(companyService.addNewEmployee(id, e));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}/dismiss")
|
||||
public EmployeeDto dismiss(@PathVariable Long id, @RequestParam Long employeeId) {
|
||||
Employee e = employeeService.findEmployee(employeeId);
|
||||
return new EmployeeDto(companyService.deleteEmployee(id, e));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
package com.example.springip.lab3.controller;
|
||||
|
||||
import com.example.springip.lab3.dto.EmployeeDto;
|
||||
import com.example.springip.lab3.models.Position;
|
||||
import com.example.springip.lab3.service.EmployeeService;
|
||||
import com.example.springip.lab3.service.PositionService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
@ -10,9 +12,11 @@ import java.util.List;
|
||||
@RequestMapping("/employee")
|
||||
public class EmployeeController {
|
||||
private final EmployeeService employeeService;
|
||||
private final PositionService positionService;
|
||||
|
||||
public EmployeeController(EmployeeService employeeService) {
|
||||
public EmployeeController(EmployeeService employeeService, PositionService positionService) {
|
||||
this.employeeService = employeeService;
|
||||
this.positionService = positionService;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@ -49,4 +53,21 @@ public class EmployeeController {
|
||||
public EmployeeDto deleteEmployee(@PathVariable Long id) {
|
||||
return new EmployeeDto(employeeService.deleteEmployee(id));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}/addPos")
|
||||
public EmployeeDto addPosition(@PathVariable Long id,
|
||||
@RequestParam("position") Long position) {
|
||||
Position p = positionService.findPosition(position);
|
||||
if (p == null)
|
||||
return null;
|
||||
return new EmployeeDto(employeeService.addPosition(id, p));
|
||||
}
|
||||
@PutMapping("/{id}/delPos")
|
||||
public EmployeeDto delPosition(@PathVariable Long id,
|
||||
@RequestParam("position") Long position) {
|
||||
Position p = positionService.findPosition(position);
|
||||
if (p == null)
|
||||
return null;
|
||||
return new EmployeeDto(employeeService.deletePosition(id, p));
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,9 @@
|
||||
package com.example.springip.lab3.models;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonRootName;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
|
||||
@Entity
|
||||
public class Company {
|
||||
@ -14,9 +13,9 @@ public class Company {
|
||||
@Column(unique = true)
|
||||
private String name;
|
||||
@OneToMany(mappedBy = "company", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
private List<Employee> employees = new ArrayList<>();
|
||||
private Set<Employee> employees = new HashSet<>();
|
||||
|
||||
public Company(String name, List<Employee> employees) {
|
||||
public Company(String name, Set<Employee> employees) {
|
||||
this.name = name;
|
||||
this.employees = employees;
|
||||
}
|
||||
@ -51,11 +50,11 @@ public class Company {
|
||||
return Id;
|
||||
}
|
||||
|
||||
public List<Employee> getEmployees() {
|
||||
public Set<Employee> getEmployees() {
|
||||
return employees;
|
||||
}
|
||||
|
||||
public void setEmployees(List<Employee> employees) {
|
||||
public void setEmployees(Set<Employee> employees) {
|
||||
this.employees = employees;
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.example.springip.lab3.models;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.*;
|
||||
@ -20,7 +21,7 @@ public class Employee {
|
||||
private Set<Position> positions = new HashSet<>();
|
||||
|
||||
|
||||
public Employee(String phoneNumber, String name, String surname) {
|
||||
public Employee(String surname, String name, String phoneNumber) {
|
||||
this.phoneNumber = phoneNumber;
|
||||
this.name = name;
|
||||
this.surname = surname;
|
||||
@ -77,6 +78,7 @@ public class Employee {
|
||||
company.deleteEmployee(this);
|
||||
}
|
||||
this.company = null;
|
||||
this.positions.clear();
|
||||
}
|
||||
|
||||
public void addNewPosition(Position p) {
|
||||
|
@ -70,17 +70,17 @@ public class CompanyService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void addNewEmployee(Long id, Employee employee) {
|
||||
public Employee addNewEmployee(Long id, Employee employee) {
|
||||
Company currentCompany = findCompany(id);
|
||||
currentCompany.addNewEmployee(employee);
|
||||
companyRepository.save(currentCompany);
|
||||
return employee;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteEmployee(Long id, Employee employee) {
|
||||
public Employee deleteEmployee(Long id, Employee employee) {
|
||||
Company currentCompany = findCompany(id);
|
||||
currentCompany.deleteEmployee(employee);
|
||||
//em.merge(employee); !!!
|
||||
employeeRepository.save(employee);
|
||||
return employeeRepository.save(employee);
|
||||
}
|
||||
}
|
||||
|
@ -82,18 +82,18 @@ public class EmployeeService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void addPosition(Long id, Position p) {
|
||||
public Employee addPosition(Long id, Position p) {
|
||||
Employee e = findEmployee(id);
|
||||
e.addNewPosition(p);
|
||||
System.out.println(e.getPositions().size());
|
||||
employeeRepository.save(e);
|
||||
return employeeRepository.save(e);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deletePosition(Long id, Position p) {
|
||||
public Employee deletePosition(Long id, Position p) {
|
||||
Employee e = findEmployee(id);
|
||||
e.removePosition(p);
|
||||
employeeRepository.save(e);
|
||||
return employeeRepository.save(e);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
|
Loading…
Reference in New Issue
Block a user