add service and dto

This commit is contained in:
DozorovaA.A 2023-05-09 17:22:39 +04:00
parent a48605b616
commit 50b50ebe0d
15 changed files with 271 additions and 103 deletions

View File

@ -0,0 +1,21 @@
package com.example.demo.speaker.controller;
import com.example.demo.speaker.model.Appointment;
public class AppointmentDTO {
private long id;
private String name;
public AppointmentDTO(Appointment appointment)
{
this.id = appointment.getId();
this.name = appointment.getNamePost();
}
public long getId() {
return id;
}
public String getName() {
return name;
}
}

View File

@ -0,0 +1,46 @@
package com.example.demo.speaker.controller;
import com.example.demo.speaker.model.Company;
import com.example.demo.speaker.model.RequestForCooperation;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.OneToMany;
import java.util.ArrayList;
import java.util.List;
public class CompanyDTO {
private long id;
private String nameCompany;
private String legalAdressCompany;
private String adressCompany;
private String contactEmail;
private List<RequestForCooperationDTO> requests = new ArrayList<>();
public CompanyDTO(Company company)
{
this.id = company.getId();
this.nameCompany = company.getNameCompany();
this.legalAdressCompany = company.getLegalAdressCompany();
this.adressCompany = company.getAdressCompany();
this.contactEmail = company.getContactEmail();
this.requests = company.getList() == null ? null : company.getList()
.stream()
.map(RequestForCooperationDTO::new)
.toList();
}
public long getId() {
return id;
}
public String getAdressCompany() { return adressCompany; }
public String getNameCompany() { return nameCompany; }
public String getContactEmail() { return contactEmail; }
public String getLegalAdressCompany() { return legalAdressCompany; }
public List<RequestForCooperationDTO> getRequests() { return requests; }
}

View File

@ -0,0 +1,26 @@
package com.example.demo.speaker.controller;
import com.example.demo.speaker.model.Appointment;
import com.example.demo.speaker.model.Employee;
import jakarta.persistence.Column;
import jakarta.persistence.FetchType;
import jakarta.persistence.OneToOne;
public class EmployeeDTO {
private Long id;
private String fio;
private AppointmentDTO appointment;
public EmployeeDTO(Employee employee)
{
this.id = employee.getId();
this.fio = employee.getFio();
this.appointment = new AppointmentDTO(employee.getPost());
}
public Long getId() { return id; }
public AppointmentDTO getAppointment() { return appointment; }
public String getFio() { return fio; }
}

View File

@ -91,7 +91,7 @@ public class RequestForCooperationController {
typeSotr = TypeOfRequestEnum.TRACKING; typeSotr = TypeOfRequestEnum.TRACKING;
break; break;
} }
EmployeeService employeeService = new EmployeeService(); EmployeeService employeeService = new EmployeeService(employeeRepository, appointmentRepository);
String result = ""; String result = "";
for (var i : List) { for (var i : List) {
var Worker = employeeService.findWorker(i.getWorker().getId()); var Worker = employeeService.findWorker(i.getWorker().getId());

View File

@ -0,0 +1,50 @@
package com.example.demo.speaker.controller;
import com.example.demo.speaker.model.Company;
import com.example.demo.speaker.model.Employee;
import com.example.demo.speaker.model.RequestForCooperation;
import com.example.demo.speaker.model.TypeOfRequestEnum;
import jakarta.persistence.Column;
import jakarta.persistence.FetchType;
import jakarta.persistence.OneToOne;
import java.util.Date;
public class RequestForCooperationDTO {
private Long id;
private Company nameCompany;
private String comment;
private Date createDate;
private Date approveDate;
private Boolean isActive;
private EmployeeDTO sotr;
private TypeOfRequestEnum type;
public RequestForCooperationDTO(RequestForCooperation request)
{
this.id = request.getId();
this.nameCompany = request.getName();
this.comment = request.getComment();
this.approveDate = request.getApproveDate();
this.sotr = new EmployeeDTO(request.getWorker());
this.createDate = request.getCreateDate();
this.isActive = request.getActive();
this.type = request.getType();
}
public long getId() { return this.id; }
public String getComment() { return comment; }
public Boolean getActive() { return isActive; }
public Company getNameCompany() { return nameCompany; }
public Date getApproveDate() { return approveDate; }
public Date getCreateDate() { return createDate; }
public EmployeeDTO getSotr() { return sotr; }
public TypeOfRequestEnum getType() {return type; }
}

View File

@ -15,6 +15,10 @@ public class Appointment {
{ {
return this.namePost; return this.namePost;
} }
public void setNamePost(String name)
{
this.namePost = name;
}
public Appointment() { } public Appointment() { }

View File

@ -3,6 +3,7 @@ package com.example.demo.speaker.model;
import jakarta.persistence.*; import jakarta.persistence.*;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
@Entity @Entity
public class Employee { public class Employee {
@ -15,7 +16,7 @@ public class Employee {
@OneToOne(fetch = FetchType.LAZY) @OneToOne(fetch = FetchType.LAZY)
private Appointment appointment; private Appointment appointment;
public Employee() {} public Employee(String fio, Optional<Appointment> appointment) {}
public Employee(String fio, Appointment appointment) { public Employee(String fio, Appointment appointment) {
this.fio = fio; this.fio = fio;
this.appointment = appointment; this.appointment = appointment;

View File

@ -55,6 +55,8 @@ public class RequestForCooperation {
{ {
return this.sotr; return this.sotr;
} }
public String getComment() { return this.comment; }
public void setWorker(Employee sotr) public void setWorker(Employee sotr)
{ {
this.sotr = sotr; this.sotr = sotr;

View File

@ -0,0 +1,7 @@
package com.example.demo.speaker.service;
public class AppointmentNotFoundException extends RuntimeException{
public AppointmentNotFoundException(Long id){
super(String.format("Appointment with id [%s] is not found", id));
}
}

View File

@ -1,53 +1,56 @@
package com.example.demo.speaker.service; package com.example.demo.speaker.service;
import com.example.demo.speaker.controller.AppointmentDTO;
import com.example.demo.speaker.model.Appointment; import com.example.demo.speaker.model.Appointment;
import jakarta.persistence.EntityManager; import com.example.demo.speaker.repository.IAppointmentRepository;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List; import java.util.List;
import java.util.Optional;
@Service @Service
public class AppointmentService { public class AppointmentService {
@PersistenceContext private final IAppointmentRepository appointmentRepository;
private EntityManager em;
@Transactional public AppointmentService(IAppointmentRepository appointmentRepository) {
public Appointment addPost(String name) { this.appointmentRepository = appointmentRepository;
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Name of Type is null or empty");
}
final Appointment type = new Appointment(name);
em.persist(type);
return type;
} }
@Transactional(readOnly = true) @Transactional
public Appointment findPost(Long id) { public Appointment addAppointment(AppointmentDTO appointmentDTO) {
final Appointment appointment = em.find(Appointment.class, id); Appointment appointment = new Appointment(appointmentDTO.getName());
if (appointment == null) { appointmentRepository.save(appointment);
throw new EntityNotFoundException(String.format("Type with id [%s] is not found", id));
}
return appointment; return appointment;
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<Appointment> findAllPosts() { public Appointment findAppointment(Long id) {
return em.createQuery("select s from Appointment s", Appointment.class) final Optional<Appointment> appointment = appointmentRepository.findById(id);
.getResultList(); return appointment.orElseThrow(()->new AppointmentNotFoundException(id));
} }
@Transactional(readOnly = true)
public List<Appointment> findAllAppointments() {
return appointmentRepository.findAll();
}
@Transactional @Transactional
public Appointment deletePost(Long id) { public Appointment updateAppointment(Long id, AppointmentDTO appointmentDTO) {
final Appointment currentAppointment = findPost(id); final Appointment currentAppointment = findAppointment(id);
em.remove(currentAppointment); currentAppointment.setNamePost(appointmentDTO.getName());
appointmentRepository.save(currentAppointment);
return currentAppointment; return currentAppointment;
} }
@Transactional @Transactional
public void deleteAllvs() { public Appointment deleteAppointment(Long id) {
em.createQuery("delete from Appointment").executeUpdate(); final Appointment currentAppointment = findAppointment(id);
appointmentRepository.delete(currentAppointment);
return currentAppointment;
} }
@Transactional
public void deleteAllAppointments() {
appointmentRepository.deleteAll();
}
} }

View File

@ -0,0 +1,7 @@
package com.example.demo.speaker.service;
public class CompanyNotFoundException extends RuntimeException{
public CompanyNotFoundException(Long id){
super(String.format("Company with id [%s] is not found", id));
}
}

View File

@ -0,0 +1,7 @@
package com.example.demo.speaker.service;
public class EmployeeNotFoundException extends RuntimeException{
public EmployeeNotFoundException(Long id){
super(String.format("Employee with id [%s] is not found", id));
}
}

View File

@ -1,55 +1,56 @@
package com.example.demo.speaker.service; package com.example.demo.speaker.service;
import com.example.demo.speaker.controller.AppointmentDTO;
import com.example.demo.speaker.controller.EmployeeDTO;
import com.example.demo.speaker.model.Appointment; import com.example.demo.speaker.model.Appointment;
import com.example.demo.speaker.model.Employee; import com.example.demo.speaker.model.Employee;
import jakarta.persistence.EntityManager; import com.example.demo.speaker.repository.IAppointmentRepository;
import com.example.demo.speaker.repository.IEmployeeRepository;
import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
@Service @Service
public class EmployeeService { public class EmployeeService {
@PersistenceContext private final IEmployeeRepository employeeRepository;
private EntityManager em; private final IAppointmentRepository appointmentRepository;
public EmployeeService(IEmployeeRepository employeeRepository, IAppointmentRepository appointmentRepository) {
this.employeeRepository = employeeRepository;
this.appointmentRepository = appointmentRepository;
}
@Transactional @Transactional
public Employee addWorker(String name, Appointment appointment) { public Employee addWorker(EmployeeDTO employee) {
if (!StringUtils.hasText(name) || appointment == null) { Optional<Appointment> appointment = appointmentRepository.findById(employee.getAppointment().getId());
throw new IllegalArgumentException("Name of worker is null or empty"); Employee employeeNew = new Employee(employee.getFio(), appointment);
} employeeRepository.save(employeeNew);
final Employee employee = new Employee(name, appointment); return employeeNew;
em.persist(employee);
return employee;
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Employee findWorker(Long id) { public Employee findWorker(Long id) {
final Employee employee = em.find(Employee.class, id); final Optional<Employee> employee = employeeRepository.findById(id);
if (employee == null) { return employee.orElseThrow(()->new EmployeeNotFoundException(id);
throw new EntityNotFoundException(String.format("Report with id [%s] is not found", id));
}
return employee;
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<Employee> findAllWorkers() { public List<Employee> findAllWorkers() {
return em.createQuery("select s from Employee s", Employee.class) return employeeRepository.findAll();
.getResultList();
} }
@Transactional @Transactional
public Employee updateReportWorker(Long id, String Name, Appointment appointment) { public Employee updateReportWorker(Long id, EmployeeDTO employeeDTO) {
if (!StringUtils.hasText(Name.toString()) || appointment == null) {
throw new IllegalArgumentException("Name is null or empty");
}
final Employee currentEmployee = findWorker(id); final Employee currentEmployee = findWorker(id);
currentEmployee.setFio(Name); currentEmployee.setFio(currentEmployee.getFio());
currentEmployee.setPost(appointment); currentEmployee.setPost(currentEmployee.getPost());
return em.merge(currentEmployee); employeeRepository.save(currentEmployee);
return currentEmployee;
} }
@Transactional @Transactional
@ -57,27 +58,21 @@ public class EmployeeService {
if (appointment == null) { if (appointment == null) {
throw new IllegalArgumentException("Post is null or empty"); throw new IllegalArgumentException("Post is null or empty");
} }
final List<Employee> currentEmployees = findAllWorkers(); List<Employee> list = employeeRepository.getEmployeeByAppointment(appointment);
List<Employee> list = new ArrayList<Employee>();
for (var l: currentEmployees) {
if (l.getPost().getNamePost() == appointment.getNamePost())
{
list.add(l);
}
}
return list; return list;
} }
@Transactional @Transactional
public Employee deleteWorker(Long id) { public Employee deleteWorker(Long id) {
final Employee currentEmployee = findWorker(id); final Employee currentEmployee = findWorker(id);
em.remove(currentEmployee); employeeRepository.delete(currentEmployee);
return currentEmployee; return currentEmployee;
} }
@Transactional @Transactional
public void deleteAllWorkers() { public void deleteAllWorkers() {
em.createQuery("delete from Employee").executeUpdate(); employeeRepository.deleteAll();
} }
} }

View File

@ -1,73 +1,65 @@
package com.example.demo.speaker.service; package com.example.demo.speaker.service;
import com.example.demo.speaker.model.Company; import com.example.demo.speaker.controller.RequestForCooperationDTO;
import com.example.demo.speaker.model.RequestForCooperation; import com.example.demo.speaker.model.RequestForCooperation;
import com.example.demo.speaker.model.TypeOfRequestEnum; import com.example.demo.speaker.repository.IEmployeeRepository;
import com.example.demo.speaker.model.Employee; import com.example.demo.speaker.repository.IRequestForCooperationRepository;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.stream.Stream; import java.util.Optional;
@Service @Service
public class RequestForCooperationService { public class RequestForCooperationService {
@PersistenceContext private final IRequestForCooperationRepository requestForCooperationRepository;
private EntityManager em; private final EmployeeService employeeService;
public RequestForCooperationService(IRequestForCooperationRepository requestForCooperationRepository, EmployeeService employeeRepository) {
this.requestForCooperationRepository = requestForCooperationRepository;
this.employeeService = employeeRepository;
}
@Transactional @Transactional
public RequestForCooperation addRequest(Company name, Date createDate, TypeOfRequestEnum type, String text) { public RequestForCooperation addRequest(RequestForCooperationDTO requestDTO) {
if (name == null || ! StringUtils.hasText(createDate.toString()) final RequestForCooperation request = new RequestForCooperation(requestDTO.getNameCompany(),
|| type == null) { requestDTO.getCreateDate(),
throw new IllegalArgumentException("Some field of report is null or empty"); requestDTO.getType(),
} requestDTO.getComment());
final RequestForCooperation report = new RequestForCooperation(name, createDate, type, text); requestForCooperationRepository.save(request);
em.persist(report); return request;
name.setList(report);
return report;
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public RequestForCooperation findRequest(Long id) { public RequestForCooperation findRequest(Long id) {
final RequestForCooperation report = em.find(RequestForCooperation.class, id); final Optional<RequestForCooperation> report = requestForCooperationRepository.findById(id);
if (report == null) { return report.orElseThrow(() -> new RequestNotFoundException(id));
throw new EntityNotFoundException(String.format("Report with id [%s] is not found", id));
}
return report;
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<RequestForCooperation> findAllRequests() { public List<RequestForCooperation> findAllRequests() {
return em.createQuery("select s from RequestForCooperation s", RequestForCooperation.class) return requestForCooperationRepository.findAll();
.getResultList();
} }
@Transactional @Transactional
public RequestForCooperation updateRequest(Long id, Date approveDate, Boolean isActive, Employee employee) { public RequestForCooperation updateRequest(RequestForCooperationDTO requestDTO) {
if (!StringUtils.hasText(approveDate.toString()) || !StringUtils.hasText(isActive.toString())) { final RequestForCooperation currentReport = findRequest(requestDTO.getId());
throw new IllegalArgumentException("Some field is null or empty"); currentReport.setApproveDate(requestDTO.getApproveDate());
} currentReport.setActive(requestDTO.getActive());
final RequestForCooperation currentReport = findRequest(id); currentReport.setUser(employeeService.findWorker(requestDTO.getSotr().getId()));
currentReport.setApproveDate(approveDate); requestForCooperationRepository.save(currentReport);
currentReport.setActive(isActive); return currentReport;
currentReport.setUser(employee);
return em.merge(currentReport);
} }
@Transactional @Transactional
public RequestForCooperation deleteRequest(Long id) { public RequestForCooperation deleteRequest(Long id) {
final RequestForCooperation currentReport = findRequest(id); final RequestForCooperation currentReport = findRequest(id);
em.remove(currentReport); requestForCooperationRepository.delete(currentReport);
return currentReport; return currentReport;
} }
@Transactional @Transactional
public void deleteAllRequests() { public void deleteAllRequests() {
em.createQuery("delete from RequestForCooperation").executeUpdate(); requestForCooperationRepository.deleteAll();
} }
} }

View File

@ -0,0 +1,7 @@
package com.example.demo.speaker.service;
public class RequestNotFoundException extends RuntimeException{
public RequestNotFoundException(Long id){
super(String.format("Request with id [%s] is not found", id));
}
}