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