Частичная реализация

This commit is contained in:
Katerina881 2023-03-21 11:34:22 +04:00
parent 47f4a4eea7
commit 19473c157c
14 changed files with 333 additions and 62 deletions

View File

@ -0,0 +1,46 @@
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.service.CompanyService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/company")
public class CompanyController {
private final CompanyService companyService;
public CompanyController(CompanyService companyService) {
this.companyService = companyService;
}
@GetMapping("/{id}")
public CompanyDto getCompany(@PathVariable long id) {
return new CompanyDto(companyService.findCompany(id));
}
@GetMapping
public List<CompanyDto> getAllCompanies() {
return companyService.findAllCompanies().stream()
.map(CompanyDto::new)
.toList();
}
@PostMapping
public CompanyDto create(@RequestParam("name") String name) {
return new CompanyDto(companyService.addCompany(name));
}
@PutMapping("/{id}")
public CompanyDto update(@PathVariable Long id,
@RequestParam("name") String name) {
return new CompanyDto(companyService.updateCompany(id, name));
}
@DeleteMapping("/{id}")
public CompanyDto delete(@PathVariable Long id) {
return new CompanyDto(companyService.deleteCompany(id));
}
}

View File

@ -0,0 +1,52 @@
package com.example.springip.lab3.controller;
import com.example.springip.lab3.dto.EmployeeDto;
import com.example.springip.lab3.service.EmployeeService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/employee")
public class EmployeeController {
private final EmployeeService employeeService;
public EmployeeController(EmployeeService employeeService) {
this.employeeService = employeeService;
}
@GetMapping("/{id}")
public EmployeeDto getEmployee(@PathVariable long id) {
return new EmployeeDto(employeeService.findEmployee(id));
}
@GetMapping
public List<EmployeeDto> getAllEmployees() {
return employeeService.findAllEmployees().stream()
.map(EmployeeDto::new)
.toList();
}
@PostMapping
public EmployeeDto createEmployee(@RequestParam("name") String name,
@RequestParam("surname") String surname,
@RequestParam("phoneNumber") String phoneNumber) {
return new EmployeeDto(employeeService.addEmployee(
surname,
name,
phoneNumber));
}
@PutMapping("/{id}")
public EmployeeDto updateEmployee(@PathVariable Long id,
@RequestParam("name") String name,
@RequestParam("surname") String surname,
@RequestParam("phoneNumber") String phoneNumber) {
return new EmployeeDto(employeeService.updateEmployee(id, surname, name, phoneNumber));
}
@DeleteMapping("/{id}")
public EmployeeDto deleteEmployee(@PathVariable Long id) {
return new EmployeeDto(employeeService.deleteEmployee(id));
}
}

View File

@ -0,0 +1,46 @@
package com.example.springip.lab3.controller;
import com.example.springip.lab3.dto.CompanyDto;
import com.example.springip.lab3.dto.PositionDto;
import com.example.springip.lab3.service.PositionService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/position")
public class PositionController {
private final PositionService positionService;
public PositionController(PositionService positionService) {
this.positionService = positionService;
}
@GetMapping("/{id}")
public PositionDto getCompany(@PathVariable long id) {
return new PositionDto(positionService.findPosition(id));
}
@GetMapping
public List<PositionDto> getAllCompanies() {
return positionService.findAllPositions().stream()
.map(PositionDto::new)
.toList();
}
@PostMapping
public PositionDto create(@RequestParam("name") String name) {
return new PositionDto(positionService.addPosition(name));
}
@PutMapping("/{id}")
public PositionDto update(@PathVariable Long id,
@RequestParam("name") String name) {
return new PositionDto(positionService.updatePosition(id, name));
}
@DeleteMapping("/{id}")
public PositionDto delete(@PathVariable Long id) {
return new PositionDto(positionService.deletePosition(id));
}
}

View File

@ -0,0 +1,10 @@
package com.example.springip.lab3.dao;
import com.example.springip.lab3.models.Company;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface CompanyRepository extends JpaRepository<Company, Long> {
Optional<Company> findById(Long id);
}

View File

@ -0,0 +1,14 @@
package com.example.springip.lab3.dao;
import com.example.springip.lab3.models.Employee;
import com.example.springip.lab3.models.Position;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
import java.util.Optional;
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
Optional<Employee> findById(Long id);
List<Employee> findByPositions_Id(Long p_id);
}

View File

@ -0,0 +1,10 @@
package com.example.springip.lab3.dao;
import com.example.springip.lab3.models.Position;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface PositionRepository extends JpaRepository<Position, Long> {
Optional<Position> findById(Long id);
}

View File

@ -0,0 +1,31 @@
package com.example.springip.lab3.dto;
import com.example.springip.lab3.models.Company;
import com.example.springip.lab3.models.Employee;
import java.util.ArrayList;
import java.util.List;
public class CompanyDto {
private final Long Id;
private final String name;
private final List<EmployeeDto> employees;
public CompanyDto(Company company) {
Id = company.getId();
this.name = company.getName();
this.employees = company.getEmployees().stream().map(EmployeeDto::new).toList();
}
public Long getId() {
return Id;
}
public String getName() {
return name;
}
public List<EmployeeDto> getEmployees() {
return employees;
}
}

View File

@ -0,0 +1,44 @@
package com.example.springip.lab3.dto;
import com.example.springip.lab3.models.Company;
import com.example.springip.lab3.models.Employee;
import com.example.springip.lab3.models.Position;
import java.util.List;
import java.util.Set;
public class EmployeeDto {
private final Long Id;
private final String name;
private final String phoneNumber;
private final String surname;
private final List<PositionDto> positions;
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();
}
public Long getId() {
return Id;
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getSurname() {
return surname;
}
public List<PositionDto> getPositions() {
return positions;
}
}

View File

@ -0,0 +1,21 @@
package com.example.springip.lab3.dto;
import com.example.springip.lab3.models.Position;
public class PositionDto {
private final Long Id;
private final String name;
public PositionDto(Position position) {
Id = position.getId();
this.name = position.getName();
}
public Long getId() {
return Id;
}
public String getName() {
return name;
}
}

View File

@ -1,5 +1,7 @@
package com.example.springip.lab3.service;
import com.example.springip.lab3.dao.CompanyRepository;
import com.example.springip.lab3.dao.EmployeeRepository;
import com.example.springip.lab3.models.Company;
import com.example.springip.lab3.models.Employee;
import jakarta.persistence.EntityManager;
@ -13,8 +15,12 @@ import java.util.List;
@Service
public class CompanyService {
@PersistenceContext
private EntityManager em;
private CompanyRepository companyRepository;
private EmployeeRepository employeeRepository;
public CompanyService(CompanyRepository companyRepository, EmployeeRepository employeeRepository) {
this.companyRepository = companyRepository;
this.employeeRepository = employeeRepository;
}
@Transactional
public Company addCompany(String name) {
@ -22,34 +28,22 @@ public class CompanyService {
throw new IllegalArgumentException("Student name is null or empty");
}
final Company company = new Company(name);
em.persist(company);
companyRepository.save(company);
return company;
}
@Transactional(readOnly = true)
public Company findCompany(Long id) {
final Company company = em.find(Company.class, id);
final Company company = companyRepository.findById(id).orElse(null);
if (company == null) {
throw new EntityNotFoundException(String.format("Company with id [%s] is not found", id));
}
return company;
}
@Transactional(readOnly = true)
public Company findCompany(String name) {
Company company = em.createQuery("select c from Company c WHERE c.name = :companyName", Company.class)
.setParameter("companyName",name)
.getSingleResult();
if (company == null) {
throw new EntityNotFoundException(String.format("Company with name [%s] is not found", name));
}
return company;
}
@Transactional(readOnly = true)
public List<Company> findAllCompanies() {
return em.createQuery("select c from Company c", Company.class)
.getResultList();
return companyRepository.findAll();
}
@Transactional
@ -60,32 +54,33 @@ public class CompanyService {
final Company currentCompany = findCompany(id);
currentCompany.setName(name);
return em.merge(currentCompany);
return companyRepository.save(currentCompany);
}
@Transactional
public Company deleteCompany(Long id) {
final Company currentCompany = findCompany(id);
em.remove(currentCompany);
companyRepository.delete(currentCompany);
return currentCompany;
}
@Transactional
public void deleteAllCompanies() {
em.createQuery("delete from Company").executeUpdate();
companyRepository.deleteAll();
}
@Transactional
public void addNewEmployee(Long id, Employee employee) {
Company currentCompany = findCompany(id);
currentCompany.addNewEmployee(employee);
em.merge(currentCompany);
companyRepository.save(currentCompany);
}
@Transactional
public void deleteEmployee(Long id, Employee employee) {
Company currentCompany = findCompany(id);
currentCompany.deleteEmployee(employee);
em.merge(employee);
//em.merge(employee); !!!
employeeRepository.save(employee);
}
}

View File

@ -1,11 +1,10 @@
package com.example.springip.lab3.service;
import com.example.springip.lab3.dao.EmployeeRepository;
import com.example.springip.lab3.models.Company;
import com.example.springip.lab3.models.Employee;
import com.example.springip.lab3.models.Position;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
@ -14,8 +13,10 @@ import java.util.List;
@Service
public class EmployeeService {
@PersistenceContext
private EntityManager em;
private EmployeeRepository employeeRepository;
public EmployeeService(EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
}
@Transactional
public Employee addEmployee(String surname, String name, String phoneNumber) {
@ -23,13 +24,13 @@ public class EmployeeService {
throw new IllegalArgumentException("Employee's data is null or empty");
}
final Employee employee = new Employee(surname, name, phoneNumber);
em.persist(employee);
employeeRepository.save(employee);
return employee;
}
@Transactional
public Employee findEmployee(Long id) {
final Employee employee = em.find(Employee.class, id);
final Employee employee = employeeRepository.findById(id).orElse(null);
if (employee == null) {
throw new EntityNotFoundException(String.format("Employee with id [%s] is not found", id));
}
@ -38,12 +39,11 @@ public class EmployeeService {
@Transactional(readOnly = true)
public List<Employee> findAllEmployees() {
return em.createQuery("select e from Employee e", Employee.class)
.getResultList();
return employeeRepository.findAll();
}
@Transactional
public Employee updateCompany(Long id, String surname, String name, String phoneNumber) {
public Employee updateEmployee(Long id, String surname, String name, String phoneNumber) {
if (!StringUtils.hasText(name) ||!StringUtils.hasText(surname) || !StringUtils.hasText(phoneNumber)) {
throw new IllegalArgumentException("Employee's data is null or empty");
}
@ -52,33 +52,33 @@ public class EmployeeService {
currentEmployee.setSurname(surname);
currentEmployee.setPhoneNumber(phoneNumber);
return em.merge(currentEmployee);
return employeeRepository.save(currentEmployee);
}
@Transactional
public Employee deleteEmployee(Long id) {
final Employee employee = findEmployee(id);
em.remove(employee);
employeeRepository.delete(employee);
return employee;
}
@Transactional
public void deleteAllEmployees() {
em.createQuery("delete from Employee").executeUpdate();
employeeRepository.deleteAll();
}
@Transactional
public void addCompany(Long id, Company c) {
final Employee employee = findEmployee(id);
employee.setCompany(c);
em.merge(employee);
employeeRepository.save(employee);
}
@Transactional
public void deleteCompany(Long id) {
final Employee employee = findEmployee(id);
employee.deleteCompany();
em.merge(employee);
employeeRepository.save(employee);
}
@Transactional
@ -86,27 +86,23 @@ public class EmployeeService {
Employee e = findEmployee(id);
e.addNewPosition(p);
System.out.println(e.getPositions().size());
em.merge(e);
employeeRepository.save(e);
}
@Transactional
public void deletePosition(Long id, Position p) {
Employee e = findEmployee(id);
e.removePosition(p);
em.merge(e);
em.flush();
employeeRepository.save(e);
}
@Transactional
public List<Employee> getEmployeesByPosition(Position p) {
List<Employee> employees = em.createQuery("select e from Employee e INNER JOIN e.positions p WHERE p.id=:posit",
Employee.class)
.setParameter("posit", p.getId())
.getResultList();
for (Employee e : employees) {
System.out.println(e);
}
// List<Employee> employees = em.createQuery("select e from Employee e INNER JOIN e.positions p WHERE p.id=:posit",
// Employee.class)
// .setParameter("posit", p.getId())
// .getResultList();
List<Employee> employees = employeeRepository.findByPositions_Id(p.getId());
return employees;
}
}

View File

@ -1,5 +1,6 @@
package com.example.springip.lab3.service;
import com.example.springip.lab3.dao.PositionRepository;
import com.example.springip.lab3.models.Company;
import com.example.springip.lab3.models.Employee;
import com.example.springip.lab3.models.Position;
@ -14,8 +15,12 @@ import java.util.List;
@Service
public class PositionService {
@PersistenceContext
private EntityManager em;
private PositionRepository positionRepository;
public PositionService(PositionRepository positionRepository) {
this.positionRepository = positionRepository;
}
@Transactional
public Position addPosition(String name) {
@ -23,13 +28,13 @@ public class PositionService {
throw new IllegalArgumentException("Employee's data is null or empty");
}
final Position position = new Position(name);
em.persist(position);
positionRepository.save(position);
return position;
}
@Transactional
public Position findPosition(Long id) {
final Position position = em.find(Position.class, id);
final Position position = positionRepository.findById(id).orElse(null);
if (position == null) {
throw new EntityNotFoundException(String.format("Position with id [%s] is not found", id));
}
@ -38,31 +43,30 @@ public class PositionService {
@Transactional(readOnly = true)
public List<Position> findAllPositions() {
return em.createQuery("select p from Position p", Position.class)
.getResultList();
return positionRepository.findAll();
}
@Transactional
public Position updatePosition(Long id, String surname, String name, String phoneNumber) {
if (!StringUtils.hasText(name) ||!StringUtils.hasText(surname) || !StringUtils.hasText(phoneNumber)) {
public Position updatePosition(Long id, String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Employee's data is null or empty");
}
final Position currentPosition = findPosition(id);
currentPosition.setName(name);
return em.merge(currentPosition);
return positionRepository.save(currentPosition);
}
@Transactional
public Position deletePosition(Long id) {
final Position p = findPosition(id);
em.remove(p);
positionRepository.delete(p);
return p;
}
@Transactional
public void deleteAllPositions() {
em.createQuery("delete from Position").executeUpdate();
positionRepository.deleteAll();
}

View File

@ -10,6 +10,8 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.NoSuchElementException;
@SpringBootTest
public class CompanyServiceTests {
@Autowired
@ -53,10 +55,10 @@ public class CompanyServiceTests {
public void testUpdateCompany() {
companyService.deleteAllCompanies();
String name = "Company1";
companyService.addCompany(name);
Company c = companyService.addCompany(name);
String name2 = "Company2";
companyService.updateCompany(companyService.findCompany(name).getId(), name2);
Assertions.assertNotNull(companyService.findCompany(name2));
companyService.updateCompany(c.getId(), name2);
Assertions.assertNotNull(companyService.findCompany(c.getId()));
companyService.deleteAllCompanies();
}

View File

@ -37,7 +37,7 @@ public class EmployeeServiceTests {
final String newName = "name";
Employee e = employeeService.addEmployee("surname", "name", "111");
employeeService.updateCompany(e.getId(),e.getSurname(), newName, e.getPhoneNumber());
employeeService.updateEmployee(e.getId(),e.getSurname(), newName, e.getPhoneNumber());
Assertions.assertEquals(newName, employeeService.findEmployee(e.getId()).getName());
employeeService.deleteAllEmployees();
}