Лабораторная 3

This commit is contained in:
prodigygirl 2023-03-16 17:15:56 +04:00
parent 5343479239
commit 755388d16b
12 changed files with 806 additions and 2 deletions

View File

@ -14,6 +14,10 @@ repositories {
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.h2database:h2:2.1.210'
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
} }

View File

@ -0,0 +1,83 @@
package com.example.springip.lab3.models;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long Id;
@Column(unique = true)
private String name;
@OneToMany(mappedBy = "company", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Employee> employees = new ArrayList<>();
public Company(String name, List<Employee> employees) {
this.name = name;
this.employees = employees;
}
public Company(String name) {
this.name = name;
}
public Company() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addNewEmployee(Employee employee) {
employees.add(employee);
employee.setCompany(this);
}
public void deleteEmployee(Employee employee) {
employees.remove(employee);
employee.deleteCompany();
}
public Long getId() {
return Id;
}
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Company company = (Company) o;
return Objects.equals(Id, company.Id);
}
@Override
public int hashCode() {
return Objects.hash(Id);
}
@Override
public String toString() {
return "Company{" +
"Id=" + Id +
", name='" + name + '\'' +
", employees=" + employees +
'}';
}
}

View File

@ -0,0 +1,109 @@
package com.example.springip.lab3.models;
import jakarta.persistence.*;
import java.util.*;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String phoneNumber;
private String name;
private String surname;
@ManyToOne(fetch = FetchType.EAGER)
private Company company;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<Position> positions = new HashSet<>();
public Employee(String phoneNumber, String name, String surname) {
this.phoneNumber = phoneNumber;
this.name = name;
this.surname = surname;
}
public Employee() {
}
public Long getId() {
return id;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public List<Position> getPositions() {
return positions.stream().toList();
}
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
if (!company.getEmployees().contains(this)) {
company.addNewEmployee(this);
}
}
public void deleteCompany() {
if (company.getEmployees().contains(this)) {
company.deleteEmployee(this);
}
this.company = null;
}
public void addNewPosition(Position p) {
positions.add(p);
if (!p.getEmployees().contains(this)) {
p.addNewEmployee(this);
}
}
public void removePosition(Position p) {
positions.remove(p);
if (p.getEmployees().contains(this)) {
p.deleteEmployee(this);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee e = (Employee) o;
return Objects.equals(id, e.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}

View File

@ -0,0 +1,73 @@
package com.example.springip.lab3.models;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
public class Position {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long Id;
private String name;
//mappedBy - атрибут, указывающий, что классом-владельцем отношений является другой класс
@ManyToMany(mappedBy = "positions", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Employee> employees = new ArrayList<>();
public Position() {
}
public Position(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return Id;
}
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
public void addNewEmployee(Employee e) {
employees.add(e);
if (!e.getPositions().contains(this)) {
e.addNewPosition(this);
}
}
public void deleteEmployee(Employee e) {
employees.remove(e);
if (e.getPositions().contains(this)) {
e.removePosition(this);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Position student = (Position) o;
return Objects.equals(Id, student.Id);
}
@Override
public int hashCode() {
return Objects.hash(Id);
}
}

View File

@ -0,0 +1,91 @@
package com.example.springip.lab3.service;
import com.example.springip.lab3.models.Company;
import com.example.springip.lab3.models.Employee;
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;
import java.util.List;
@Service
public class CompanyService {
@PersistenceContext
private EntityManager em;
@Transactional
public Company addCompany(String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Student name is null or empty");
}
final Company company = new Company(name);
em.persist(company);
return company;
}
@Transactional(readOnly = true)
public Company findCompany(Long id) {
final Company company = em.find(Company.class, id);
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();
}
@Transactional
public Company updateCompany(Long id, String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Company name is null or empty");
}
final Company currentCompany = findCompany(id);
currentCompany.setName(name);
return em.merge(currentCompany);
}
@Transactional
public Company deleteCompany(Long id) {
final Company currentCompany = findCompany(id);
em.remove(currentCompany);
return currentCompany;
}
@Transactional
public void deleteAllCompanies() {
em.createQuery("delete from Company").executeUpdate();
}
@Transactional
public void addNewEmployee(Long id, Employee employee) {
Company currentCompany = findCompany(id);
currentCompany.addNewEmployee(employee);
em.merge(currentCompany);
}
@Transactional
public void deleteEmployee(Long id, Employee employee) {
Company currentCompany = findCompany(id);
currentCompany.deleteEmployee(employee);
em.merge(employee);
}
}

View File

@ -0,0 +1,100 @@
package com.example.springip.lab3.service;
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;
import java.util.List;
@Service
public class EmployeeService {
@PersistenceContext
private EntityManager em;
@Transactional
public Employee addEmployee(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");
}
final Employee employee = new Employee(surname, name, phoneNumber);
em.persist(employee);
return employee;
}
@Transactional
public Employee findEmployee(Long id) {
final Employee employee = em.find(Employee.class, id);
if (employee == null) {
throw new EntityNotFoundException(String.format("Employee with id [%s] is not found", id));
}
return employee;
}
@Transactional(readOnly = true)
public List<Employee> findAllEmployees() {
return em.createQuery("select e from Employee e", Employee.class)
.getResultList();
}
@Transactional
public Employee updateCompany(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");
}
final Employee currentEmployee = findEmployee(id);
currentEmployee.setName(name);
currentEmployee.setSurname(surname);
currentEmployee.setPhoneNumber(phoneNumber);
return em.merge(currentEmployee);
}
@Transactional
public Employee deleteEmployee(Long id) {
final Employee employee = findEmployee(id);
em.remove(employee);
return employee;
}
@Transactional
public void deleteAllEmployees() {
em.createQuery("delete from Employee").executeUpdate();
}
@Transactional
public void addCompany(Long id, Company c) {
final Employee employee = findEmployee(id);
employee.setCompany(c);
em.merge(employee);
}
@Transactional
public void deleteCompany(Long id) {
final Employee employee = findEmployee(id);
employee.deleteCompany();
em.merge(employee);
}
@Transactional
public void addPosition(Long id, Position p) {
Employee e = findEmployee(id);
e.addNewPosition(p);
System.out.println(e.getPositions().size());
em.merge(e);
}
@Transactional
public void deletePosition(Long id, Position p) {
Employee e = findEmployee(id);
e.removePosition(p);
System.out.println("Количество должностей после удаления: " + e.getPositions().size());
em.merge(e);
em.flush();
}
}

View File

@ -0,0 +1,69 @@
package com.example.springip.lab3.service;
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;
import java.util.List;
@Service
public class PositionService {
@PersistenceContext
private EntityManager em;
@Transactional
public Position addPosition(String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Employee's data is null or empty");
}
final Position position = new Position(name);
em.persist(position);
return position;
}
@Transactional
public Position findPosition(Long id) {
final Position position = em.find(Position.class, id);
if (position == null) {
throw new EntityNotFoundException(String.format("Position with id [%s] is not found", id));
}
return position;
}
@Transactional(readOnly = true)
public List<Position> findAllPositions() {
return em.createQuery("select p from Position p", Position.class)
.getResultList();
}
@Transactional
public Position updatePosition(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");
}
final Position currentPosition = findPosition(id);
currentPosition.setName(name);
return em.merge(currentPosition);
}
@Transactional
public Position deletePosition(Long id) {
final Position p = findPosition(id);
em.remove(p);
return p;
}
@Transactional
public void deleteAllPositions() {
em.createQuery("delete from Position").executeUpdate();
}
}

View File

@ -1 +1,11 @@
spring.main.banner-mode=off
#server.port=8080
spring.datasource.url=jdbc:h2:file:./data
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
spring.h2.console.enabled=true
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false

View File

@ -0,0 +1,117 @@
package com.example.springip;
import com.example.springip.lab3.models.Company;
import com.example.springip.lab3.models.Employee;
import com.example.springip.lab3.service.CompanyService;
import com.example.springip.lab3.service.EmployeeService;
import jakarta.persistence.EntityNotFoundException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class CompanyServiceTests {
@Autowired
private CompanyService companyService;
@Autowired
private EmployeeService employeeService;
@Test
public void testDeleteAllCompanies() {
companyService.deleteAllCompanies();
int n = 3;
for (int i = 0; i < n; i++) {
String name = "Company" + i;
companyService.addCompany(name);
}
companyService.deleteAllCompanies();
Assertions.assertEquals(companyService.findAllCompanies().size(), 0);
}
@Test
public void testDeleteCompany() {
companyService.deleteAllCompanies();
String name = "Company1";
Company c = companyService.addCompany(name);
companyService.deleteCompany(c.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> {companyService.findCompany(c.getId());});
}
@Test
public void testAddCompany() {
companyService.deleteAllCompanies();
String name = "Company1";
Company c = companyService.addCompany(name);
Assertions.assertNotNull(companyService.findCompany(c.getId()));
companyService.deleteAllCompanies();
}
@Test
public void testUpdateCompany() {
companyService.deleteAllCompanies();
String name = "Company1";
companyService.addCompany(name);
String name2 = "Company2";
companyService.updateCompany(companyService.findCompany(name).getId(), name2);
Assertions.assertNotNull(companyService.findCompany(name2));
companyService.deleteAllCompanies();
}
@Test
public void testFindAllCompanies() {
companyService.deleteAllCompanies();
int n = 3;
for (int i = 0; i < n; i++) {
String name = "Company" + i;
companyService.addCompany(name);
}
Assertions.assertEquals(companyService.findAllCompanies().size(), n);
companyService.deleteAllCompanies();
}
@Test
public void testAddEmployee() {
employeeService.deleteAllEmployees();
companyService.deleteAllCompanies();
final String name = "Company";
Company c = companyService.addCompany(name);
Employee newEmployee = employeeService.addEmployee("cha", "chacha", "111");
companyService.addNewEmployee(c.getId(), newEmployee);
Assertions.assertTrue(companyService.findCompany(c.getId()).getEmployees().contains(newEmployee));
employeeService.deleteAllEmployees();
companyService.deleteAllCompanies();
}
@Test
public void testDeleteEmployee() {
employeeService.deleteAllEmployees();
companyService.deleteAllCompanies();
final String name = "Company";
Company c = companyService.addCompany(name);
Employee newEmployee = employeeService.addEmployee("cha", "chacha", "111");
companyService.addNewEmployee(c.getId(), newEmployee);
Assertions.assertTrue(companyService.findCompany(c.getId()).getEmployees().contains(newEmployee));
companyService.deleteEmployee(c.getId(), newEmployee);
Company cFromDB = companyService.findCompany(c.getId());
Assertions.assertFalse(companyService.findCompany(c.getId()).getEmployees().contains(newEmployee));
employeeService.deleteAllEmployees();
companyService.deleteAllCompanies();
}
}

View File

@ -0,0 +1,92 @@
package com.example.springip;
import com.example.springip.lab3.models.Company;
import com.example.springip.lab3.models.Employee;
import com.example.springip.lab3.service.CompanyService;
import com.example.springip.lab3.service.EmployeeService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class EmployeeServiceTests {
@Autowired
private EmployeeService employeeService;
@Autowired
private CompanyService companyService;
@Test
public void testAddEmployee() {
employeeService.deleteAllEmployees();
employeeService.addEmployee("name", "surname", "111");
Assertions.assertEquals(1, employeeService.findAllEmployees().size());
employeeService.deleteAllEmployees();
}
@Test
public void testUpdateEmployee() {
employeeService.deleteAllEmployees();
final String newName = "name";
Employee e = employeeService.addEmployee("surname", "name", "111");
employeeService.updateCompany(e.getId(),e.getSurname(), newName, e.getPhoneNumber());
Assertions.assertEquals(newName, employeeService.findEmployee(e.getId()).getName());
employeeService.deleteAllEmployees();
}
@Test
public void testDeleteEmployee() {
employeeService.deleteAllEmployees();
Employee e = employeeService.addEmployee("name", "surname", "111");
employeeService.deleteEmployee(e.getId());
Assertions.assertEquals(0, employeeService.findAllEmployees().size());
employeeService.deleteAllEmployees();
}
@Test
public void testFindAllEmployee() {
employeeService.deleteAllEmployees();
int n = 3;
for (int i = 0; i < n; i++) {
employeeService.addEmployee("name", "surname", "111");
}
Assertions.assertEquals(n, employeeService.findAllEmployees().size());
employeeService.deleteAllEmployees();
}
@Test
public void testAddCompany() {
employeeService.deleteAllEmployees();
companyService.deleteAllCompanies();
Employee e = employeeService.addEmployee("name", "surname", "111");
Company c = companyService.addCompany("Comp");
companyService.addNewEmployee(c.getId(), e);
Assertions.assertEquals(c, employeeService.findEmployee(e.getId()).getCompany());
employeeService.deleteAllEmployees();
companyService.deleteAllCompanies();
}
@Test
public void testDeleteFromCompany() {
employeeService.deleteAllEmployees();
companyService.deleteAllCompanies();
Employee e = employeeService.addEmployee("name", "surname", "111");
Company c = companyService.addCompany("Comp");
employeeService.addCompany(e.getId(), c);
Assertions.assertEquals(c, employeeService.findEmployee(e.getId()).getCompany());
employeeService.deleteCompany(e.getId());
Assertions.assertNull(employeeService.findEmployee(e.getId()).getCompany());
employeeService.deleteAllEmployees();
companyService.deleteAllCompanies();
}
}

View File

@ -0,0 +1,56 @@
package com.example.springip;
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;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Service;
@SpringBootTest
public class PositionServiceTests {
@Autowired
private PositionService positionService;
@Autowired
private EmployeeService employeeService;
@Test
public void testAddEmployee() {
employeeService.deleteAllEmployees();
positionService.deleteAllPositions();
Position p = positionService.addPosition("Position");
Employee e = employeeService.addEmployee("1", "2", "33");
employeeService.addPosition(e.getId(), p);
Assertions.assertTrue(employeeService.findEmployee(e.getId()).getPositions().contains(p));
Assertions.assertTrue(positionService.findPosition(p.getId()).getEmployees().contains(e));
employeeService.deleteAllEmployees();
positionService.deleteAllPositions();
}
@Test
public void testDeleteEmployee() {
employeeService.deleteAllEmployees();
positionService.deleteAllPositions();
Position p = positionService.addPosition("Position");
Employee e = employeeService.addEmployee("1", "2", "33");
employeeService.addPosition(e.getId(), p);
employeeService.deletePosition(e.getId(), p);
Assertions.assertFalse(employeeService.findEmployee(e.getId()).getPositions().contains(p));
Assertions.assertFalse(positionService.findPosition(p.getId()).getEmployees().contains(e));
employeeService.deleteAllEmployees();
positionService.deleteAllPositions();
}
}

View File

@ -1,6 +1,6 @@
package com.example.springip; package com.example.springip;
import com.example.springip.service.OperationService; import com.example.springip.lab2.service.OperationService;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;