add service and dto
This commit is contained in:
parent
a48605b616
commit
50b50ebe0d
@ -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;
|
||||
}
|
||||
}
|
@ -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; }
|
||||
}
|
@ -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; }
|
||||
}
|
@ -91,7 +91,7 @@ public class RequestForCooperationController {
|
||||
typeSotr = TypeOfRequestEnum.TRACKING;
|
||||
break;
|
||||
}
|
||||
EmployeeService employeeService = new EmployeeService();
|
||||
EmployeeService employeeService = new EmployeeService(employeeRepository, appointmentRepository);
|
||||
String result = "";
|
||||
for (var i : List) {
|
||||
var Worker = employeeService.findWorker(i.getWorker().getId());
|
||||
|
@ -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; }
|
||||
}
|
@ -15,6 +15,10 @@ public class Appointment {
|
||||
{
|
||||
return this.namePost;
|
||||
}
|
||||
public void setNamePost(String name)
|
||||
{
|
||||
this.namePost = name;
|
||||
}
|
||||
|
||||
public Appointment() { }
|
||||
|
||||
|
@ -3,6 +3,7 @@ package com.example.demo.speaker.model;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@Entity
|
||||
public class Employee {
|
||||
@ -15,7 +16,7 @@ public class Employee {
|
||||
@OneToOne(fetch = FetchType.LAZY)
|
||||
private Appointment appointment;
|
||||
|
||||
public Employee() {}
|
||||
public Employee(String fio, Optional<Appointment> appointment) {}
|
||||
public Employee(String fio, Appointment appointment) {
|
||||
this.fio = fio;
|
||||
this.appointment = appointment;
|
||||
|
@ -55,6 +55,8 @@ public class RequestForCooperation {
|
||||
{
|
||||
return this.sotr;
|
||||
}
|
||||
|
||||
public String getComment() { return this.comment; }
|
||||
public void setWorker(Employee sotr)
|
||||
{
|
||||
this.sotr = sotr;
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
@ -1,53 +1,56 @@
|
||||
package com.example.demo.speaker.service;
|
||||
|
||||
import com.example.demo.speaker.controller.AppointmentDTO;
|
||||
import com.example.demo.speaker.model.Appointment;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import com.example.demo.speaker.repository.IAppointmentRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class AppointmentService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
private final IAppointmentRepository appointmentRepository;
|
||||
|
||||
public AppointmentService(IAppointmentRepository appointmentRepository) {
|
||||
this.appointmentRepository = appointmentRepository;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Appointment addPost(String name) {
|
||||
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)
|
||||
public Appointment findPost(Long id) {
|
||||
final Appointment appointment = em.find(Appointment.class, id);
|
||||
if (appointment == null) {
|
||||
throw new EntityNotFoundException(String.format("Type with id [%s] is not found", id));
|
||||
}
|
||||
public Appointment addAppointment(AppointmentDTO appointmentDTO) {
|
||||
Appointment appointment = new Appointment(appointmentDTO.getName());
|
||||
appointmentRepository.save(appointment);
|
||||
return appointment;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Appointment> findAllPosts() {
|
||||
return em.createQuery("select s from Appointment s", Appointment.class)
|
||||
.getResultList();
|
||||
public Appointment findAppointment(Long id) {
|
||||
final Optional<Appointment> appointment = appointmentRepository.findById(id);
|
||||
return appointment.orElseThrow(()->new AppointmentNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Appointment> findAllAppointments() {
|
||||
return appointmentRepository.findAll();
|
||||
}
|
||||
@Transactional
|
||||
public Appointment deletePost(Long id) {
|
||||
final Appointment currentAppointment = findPost(id);
|
||||
em.remove(currentAppointment);
|
||||
public Appointment updateAppointment(Long id, AppointmentDTO appointmentDTO) {
|
||||
final Appointment currentAppointment = findAppointment(id);
|
||||
currentAppointment.setNamePost(appointmentDTO.getName());
|
||||
appointmentRepository.save(currentAppointment);
|
||||
return currentAppointment;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllvs() {
|
||||
em.createQuery("delete from Appointment").executeUpdate();
|
||||
public Appointment deleteAppointment(Long id) {
|
||||
final Appointment currentAppointment = findAppointment(id);
|
||||
appointmentRepository.delete(currentAppointment);
|
||||
return currentAppointment;
|
||||
}
|
||||
@Transactional
|
||||
public void deleteAllAppointments() {
|
||||
appointmentRepository.deleteAll();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
@ -1,55 +1,56 @@
|
||||
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.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.PersistenceContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class EmployeeService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
private final IEmployeeRepository employeeRepository;
|
||||
private final IAppointmentRepository appointmentRepository;
|
||||
|
||||
public EmployeeService(IEmployeeRepository employeeRepository, IAppointmentRepository appointmentRepository) {
|
||||
this.employeeRepository = employeeRepository;
|
||||
this.appointmentRepository = appointmentRepository;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Employee addWorker(String name, Appointment appointment) {
|
||||
if (!StringUtils.hasText(name) || appointment == null) {
|
||||
throw new IllegalArgumentException("Name of worker is null or empty");
|
||||
}
|
||||
final Employee employee = new Employee(name, appointment);
|
||||
em.persist(employee);
|
||||
return employee;
|
||||
public Employee addWorker(EmployeeDTO employee) {
|
||||
Optional<Appointment> appointment = appointmentRepository.findById(employee.getAppointment().getId());
|
||||
Employee employeeNew = new Employee(employee.getFio(), appointment);
|
||||
employeeRepository.save(employeeNew);
|
||||
return employeeNew;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Employee findWorker(Long id) {
|
||||
final Employee employee = em.find(Employee.class, id);
|
||||
if (employee == null) {
|
||||
throw new EntityNotFoundException(String.format("Report with id [%s] is not found", id));
|
||||
}
|
||||
return employee;
|
||||
final Optional<Employee> employee = employeeRepository.findById(id);
|
||||
return employee.orElseThrow(()->new EmployeeNotFoundException(id);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Employee> findAllWorkers() {
|
||||
return em.createQuery("select s from Employee s", Employee.class)
|
||||
.getResultList();
|
||||
return employeeRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Employee updateReportWorker(Long id, String Name, Appointment appointment) {
|
||||
if (!StringUtils.hasText(Name.toString()) || appointment == null) {
|
||||
throw new IllegalArgumentException("Name is null or empty");
|
||||
}
|
||||
public Employee updateReportWorker(Long id, EmployeeDTO employeeDTO) {
|
||||
final Employee currentEmployee = findWorker(id);
|
||||
currentEmployee.setFio(Name);
|
||||
currentEmployee.setPost(appointment);
|
||||
return em.merge(currentEmployee);
|
||||
currentEmployee.setFio(currentEmployee.getFio());
|
||||
currentEmployee.setPost(currentEmployee.getPost());
|
||||
employeeRepository.save(currentEmployee);
|
||||
return currentEmployee;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@ -57,27 +58,21 @@ public class EmployeeService {
|
||||
if (appointment == null) {
|
||||
throw new IllegalArgumentException("Post is null or empty");
|
||||
}
|
||||
final List<Employee> currentEmployees = findAllWorkers();
|
||||
List<Employee> list = new ArrayList<Employee>();
|
||||
for (var l: currentEmployees) {
|
||||
if (l.getPost().getNamePost() == appointment.getNamePost())
|
||||
{
|
||||
list.add(l);
|
||||
}
|
||||
}
|
||||
List<Employee> list = employeeRepository.getEmployeeByAppointment(appointment);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Employee deleteWorker(Long id) {
|
||||
final Employee currentEmployee = findWorker(id);
|
||||
em.remove(currentEmployee);
|
||||
employeeRepository.delete(currentEmployee);
|
||||
return currentEmployee;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllWorkers() {
|
||||
em.createQuery("delete from Employee").executeUpdate();
|
||||
employeeRepository.deleteAll();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,73 +1,65 @@
|
||||
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.TypeOfRequestEnum;
|
||||
import com.example.demo.speaker.model.Employee;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import com.example.demo.speaker.repository.IEmployeeRepository;
|
||||
import com.example.demo.speaker.repository.IRequestForCooperationRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class RequestForCooperationService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
private final IRequestForCooperationRepository requestForCooperationRepository;
|
||||
private final EmployeeService employeeService;
|
||||
|
||||
public RequestForCooperationService(IRequestForCooperationRepository requestForCooperationRepository, EmployeeService employeeRepository) {
|
||||
this.requestForCooperationRepository = requestForCooperationRepository;
|
||||
this.employeeService = employeeRepository;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public RequestForCooperation addRequest(Company name, Date createDate, TypeOfRequestEnum type, String text) {
|
||||
if (name == null || ! StringUtils.hasText(createDate.toString())
|
||||
|| type == null) {
|
||||
throw new IllegalArgumentException("Some field of report is null or empty");
|
||||
}
|
||||
final RequestForCooperation report = new RequestForCooperation(name, createDate, type, text);
|
||||
em.persist(report);
|
||||
name.setList(report);
|
||||
return report;
|
||||
public RequestForCooperation addRequest(RequestForCooperationDTO requestDTO) {
|
||||
final RequestForCooperation request = new RequestForCooperation(requestDTO.getNameCompany(),
|
||||
requestDTO.getCreateDate(),
|
||||
requestDTO.getType(),
|
||||
requestDTO.getComment());
|
||||
requestForCooperationRepository.save(request);
|
||||
return request;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public RequestForCooperation findRequest(Long id) {
|
||||
final RequestForCooperation report = em.find(RequestForCooperation.class, id);
|
||||
if (report == null) {
|
||||
throw new EntityNotFoundException(String.format("Report with id [%s] is not found", id));
|
||||
}
|
||||
return report;
|
||||
final Optional<RequestForCooperation> report = requestForCooperationRepository.findById(id);
|
||||
return report.orElseThrow(() -> new RequestNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<RequestForCooperation> findAllRequests() {
|
||||
return em.createQuery("select s from RequestForCooperation s", RequestForCooperation.class)
|
||||
.getResultList();
|
||||
return requestForCooperationRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public RequestForCooperation updateRequest(Long id, Date approveDate, Boolean isActive, Employee employee) {
|
||||
if (!StringUtils.hasText(approveDate.toString()) || !StringUtils.hasText(isActive.toString())) {
|
||||
throw new IllegalArgumentException("Some field is null or empty");
|
||||
}
|
||||
final RequestForCooperation currentReport = findRequest(id);
|
||||
currentReport.setApproveDate(approveDate);
|
||||
currentReport.setActive(isActive);
|
||||
currentReport.setUser(employee);
|
||||
return em.merge(currentReport);
|
||||
public RequestForCooperation updateRequest(RequestForCooperationDTO requestDTO) {
|
||||
final RequestForCooperation currentReport = findRequest(requestDTO.getId());
|
||||
currentReport.setApproveDate(requestDTO.getApproveDate());
|
||||
currentReport.setActive(requestDTO.getActive());
|
||||
currentReport.setUser(employeeService.findWorker(requestDTO.getSotr().getId()));
|
||||
requestForCooperationRepository.save(currentReport);
|
||||
return currentReport;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public RequestForCooperation deleteRequest(Long id) {
|
||||
final RequestForCooperation currentReport = findRequest(id);
|
||||
em.remove(currentReport);
|
||||
requestForCooperationRepository.delete(currentReport);
|
||||
return currentReport;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllRequests() {
|
||||
em.createQuery("delete from RequestForCooperation").executeUpdate();
|
||||
requestForCooperationRepository.deleteAll();
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user