From 9692f1f17920221ed521b4a545af362be50d17b4 Mon Sep 17 00:00:00 2001 From: Rajbir Singh Date: Tue, 22 Oct 2024 15:10:13 +0200 Subject: [PATCH] PMT-26 get all Employees from a specific Project --- src/main/java/de/hmmh/pmt/ApiController.java | 30 +++++++++++++++++-- .../de/hmmh/pmt/db/AllocationRepository.java | 1 + 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/hmmh/pmt/ApiController.java b/src/main/java/de/hmmh/pmt/ApiController.java index 44205b5..b8da582 100644 --- a/src/main/java/de/hmmh/pmt/ApiController.java +++ b/src/main/java/de/hmmh/pmt/ApiController.java @@ -20,8 +20,8 @@ import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestClientException; import java.time.ZoneOffset; -import java.util.List; -import java.util.Optional; +import java.util.*; +import java.util.stream.Collectors; @Controller @RequestMapping("${openapi.projectManagement.base-path:/api/v1}") @@ -143,4 +143,30 @@ public class ApiController implements DefaultApi { return new ResponseEntity<>(HttpStatus.NO_CONTENT); } + + @Override + public ResponseEntity> getAListOfAllEmployeesFromASpecificProject(Long id) { + if (!projectRepository.existsById(id)) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + List allocationsByProject = allocationRepository.findAllByProjectId(id); + if (allocationsByProject.isEmpty()) { + return new ResponseEntity<>(Collections.emptyList(), HttpStatus.OK); + } + + Set employeeIds = allocationsByProject.stream() + .map(Allocation::getEmployeeId) + .collect(Collectors.toSet()); + + List employees = apiClientFactory.getEmployeeApi().findAll1().stream() + .filter(employeeResponseDTO -> employeeIds.contains(employeeResponseDTO.getId())) + .toList(); + + // Was wenn, die Liste der Allocation und die Liste der EmployeeResponseDTO nicht die gleiche Anzahl haben. + // Das EmployeeResponseDTO wird vom EmployeeService gestellt, sollte man die Daten auf eigenes DTO mappen. + + return new ResponseEntity<>(employees, HttpStatus.OK); + + } } diff --git a/src/main/java/de/hmmh/pmt/db/AllocationRepository.java b/src/main/java/de/hmmh/pmt/db/AllocationRepository.java index dcbc9d3..f10e765 100644 --- a/src/main/java/de/hmmh/pmt/db/AllocationRepository.java +++ b/src/main/java/de/hmmh/pmt/db/AllocationRepository.java @@ -7,4 +7,5 @@ import java.util.List; public interface AllocationRepository extends JpaRepository { List findAllByEmployeeId(Long employeeId); + List findAllByProjectId(Long projectId); }