From b558120b70203ee0c918989b3630d94191858fe8 Mon Sep 17 00:00:00 2001 From: Rajbir Singh Date: Sat, 19 Oct 2024 23:54:38 +0200 Subject: [PATCH] PMT-4 Refactor addEmployee and AllocationRepository --- src/main/java/de/hmmh/pmt/ApiController.java | 64 +++++++++++-------- .../de/hmmh/pmt/db/AllocationRepository.java | 9 +-- 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/main/java/de/hmmh/pmt/ApiController.java b/src/main/java/de/hmmh/pmt/ApiController.java index 4d0ca78..f2a736f 100644 --- a/src/main/java/de/hmmh/pmt/ApiController.java +++ b/src/main/java/de/hmmh/pmt/ApiController.java @@ -8,7 +8,6 @@ import de.hmmh.pmt.db.ProjectRepository; import de.hmmh.pmt.dtos.*; import de.hmmh.pmt.employee.ApiClientFactory; import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO; -import de.hmmh.pmt.employee.dtos.QualificationGetDTO; import de.hmmh.pmt.oas.DefaultApi; import de.hmmh.pmt.util.Mapper; import jakarta.servlet.http.HttpServletRequest; @@ -101,41 +100,52 @@ public class ApiController implements DefaultApi { public ResponseEntity addEmployee(Long id, AddEmployeeDTO body) { Optional optionalProject = projectRepository.findById(id); - if (optionalProject.isEmpty()) { + + if (projectRepository.findById(id).isEmpty()) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } + Project project = optionalProject.get(); + + EmployeeResponseDTO employee; try { - EmployeeResponseDTO employee = apiClientFactory.getEmployeeApi().findById(body.getEmployeeId()); - List qualifications = employee.getSkillSet(); - if (!hasQualification(qualifications, body.getQualificationId())) { - return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE); - } - - Project project = optionalProject.get(); - if (allocationRepository.hasOverlappingAllocation(employee.getId(), project.getStart(), project.getPlannedEnd())) { - return new ResponseEntity<>(HttpStatus.CONFLICT); - } - Allocation allocation = new Allocation(); - allocation.setEmployeeId(employee.getId()); - allocation.setRole(body.getQualificationId()); - allocation.setProjectId(project.getId()); - allocationRepository.save(allocation); - - return new ResponseEntity<>(HttpStatus.NO_CONTENT); - + employee = apiClientFactory.getEmployeeApi().findById(body.getEmployeeId()); } catch (HttpClientErrorException exception) { return new ResponseEntity<>(exception.getStatusCode().equals(HttpStatus.NOT_FOUND) ? HttpStatus.NOT_FOUND : HttpStatus.INTERNAL_SERVER_ERROR); } - } - private boolean hasQualification(List qualifications, Long neededQualificationId) { - for (QualificationGetDTO qualification : qualifications) { - if (qualification.getId().equals(neededQualificationId)) { - return true; - } + boolean hasQualification = employee.getSkillSet() + .stream() + .anyMatch(qualification -> qualification.getId().equals(body.getQualificationId())); + + if (!hasQualification) { + return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY); } - return false; + + long start = project.getStart().toEpochSecond(null); + long plannedEnd = project.getPlannedEnd().toEpochSecond(null); + + List allocations = allocationRepository.findAllocationsByEmployeeId(body.getEmployeeId()); + + boolean hasOverlap = allocations.stream() + .map(Allocation::getProject) + .anyMatch(allocatedProject -> { + long allocatedStart = allocatedProject.getStart().toEpochSecond(null); + long allocatedPlannedEnd = allocatedProject.getPlannedEnd().toEpochSecond(null); + return Math.max(start, allocatedStart) <= Math.min(plannedEnd, allocatedPlannedEnd); + }); + + if (hasOverlap) { + return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY); + } + + Allocation allocation = new Allocation(); + allocation.setEmployeeId(employee.getId()); + allocation.setRole(body.getQualificationId()); + allocation.setProjectId(project.getId()); + allocationRepository.save(allocation); + + return new ResponseEntity<>(HttpStatus.NO_CONTENT); } } \ No newline at end of file diff --git a/src/main/java/de/hmmh/pmt/db/AllocationRepository.java b/src/main/java/de/hmmh/pmt/db/AllocationRepository.java index 8321dcd..5d4bc07 100644 --- a/src/main/java/de/hmmh/pmt/db/AllocationRepository.java +++ b/src/main/java/de/hmmh/pmt/db/AllocationRepository.java @@ -1,15 +1,10 @@ package de.hmmh.pmt.db; -import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; -import java.time.LocalDateTime; +import java.util.List; public interface AllocationRepository extends CrudRepository { - @Query("SELECT COUNT(a) > 0 FROM Allocation a " + - "WHERE a.employeeId = :employeeId " + - "AND a.project.start <= :endDate " + - "AND a.project.plannedEnd >= :startDate") - boolean hasOverlappingAllocation(Long employeeId, LocalDateTime startDate, LocalDateTime endDate); + List findAllocationsByEmployeeId(Long employeeId); }