From 954ad8237ca8b06d597ac66b3671f7b0d4d53445 Mon Sep 17 00:00:00 2001 From: Rajbir Singh Date: Mon, 21 Oct 2024 13:07:03 +0200 Subject: [PATCH] PMT-4: Implement Endpoint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ole Kück --- src/main/java/de/hmmh/pmt/ApiController.java | 59 ++++++++++++++++++-- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/hmmh/pmt/ApiController.java b/src/main/java/de/hmmh/pmt/ApiController.java index 51799b5..36761f9 100644 --- a/src/main/java/de/hmmh/pmt/ApiController.java +++ b/src/main/java/de/hmmh/pmt/ApiController.java @@ -1,13 +1,13 @@ package de.hmmh.pmt; import com.fasterxml.jackson.databind.ObjectMapper; +import de.hmmh.pmt.db.Allocation; +import de.hmmh.pmt.db.AllocationRepository; import de.hmmh.pmt.db.Project; import de.hmmh.pmt.db.ProjectRepository; -import de.hmmh.pmt.dtos.CreateProjectDTO; -import de.hmmh.pmt.dtos.CreatedProjectDTO; -import de.hmmh.pmt.dtos.GetAllProjectsDTO; -import de.hmmh.pmt.dtos.ProjectInfo; +import de.hmmh.pmt.dtos.*; import de.hmmh.pmt.employee.ApiClientFactory; +import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO; import de.hmmh.pmt.oas.DefaultApi; import de.hmmh.pmt.util.Mapper; import jakarta.servlet.http.HttpServletRequest; @@ -19,6 +19,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestClientException; +import java.time.ZoneOffset; +import java.util.List; import java.util.Optional; @Controller @@ -30,6 +32,8 @@ public class ApiController implements DefaultApi { private ApiClientFactory apiClientFactory; @Autowired private ProjectRepository projectRepository; + @Autowired + AllocationRepository allocationRepository; @Override public Optional getObjectMapper() { @@ -92,4 +96,51 @@ public class ApiController implements DefaultApi { CreatedProjectDTO response = mapper.map(project); return new ResponseEntity<>(response, HttpStatus.CREATED); } + + @Override + public ResponseEntity addEmployee(Long id, AddEmployeeDTO body) { + Optional optionalProject = projectRepository.findById(id); + if (optionalProject.isEmpty()) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + Project project = optionalProject.get(); + + EmployeeResponseDTO employee; + try { + employee = apiClientFactory.getEmployeeApi().findById(body.getEmployeeId()); + } catch (HttpClientErrorException exception) { + return new ResponseEntity<>(exception.getStatusCode().equals(HttpStatus.NOT_FOUND) + ? HttpStatus.NOT_FOUND + : HttpStatus.SERVICE_UNAVAILABLE); + } catch (RestClientException exception) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + + if (employee.getSkillSet() + .stream() + .noneMatch(qualification -> qualification.getId().equals(body.getQualificationId()))) { + return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY); + } + + long start = project.getStart().toEpochSecond(ZoneOffset.UTC); + long plannedEnd = project.getPlannedEnd().toEpochSecond(ZoneOffset.UTC); + List allocations = allocationRepository.findAllocationsByEmployeeId(body.getEmployeeId()); + if (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); + })) { + return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY); + } + + Allocation allocation = new Allocation(); + allocation.setEmployeeId(employee.getId()); + allocation.setRole(body.getQualificationId()); + allocation.setProject(project); + allocationRepository.save(allocation); + + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } }