From 967946bee2a734f6bde05aac832ea14f99b54355 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20K=C3=BCck?= Date: Tue, 22 Oct 2024 14:36:24 +0200 Subject: [PATCH] PMT-27: implement removeEmployeeFromProject method --- src/main/java/de/hmmh/pmt/ApiController.java | 31 +++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/hmmh/pmt/ApiController.java b/src/main/java/de/hmmh/pmt/ApiController.java index 44205b5..e4e8ac1 100644 --- a/src/main/java/de/hmmh/pmt/ApiController.java +++ b/src/main/java/de/hmmh/pmt/ApiController.java @@ -1,12 +1,11 @@ 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.db.*; import de.hmmh.pmt.dtos.*; import de.hmmh.pmt.employee.ApiClientFactory; +import de.hmmh.pmt.employee.api.EmployeeControllerApi; import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO; import de.hmmh.pmt.oas.DefaultApi; import de.hmmh.pmt.util.Mapper; @@ -143,4 +142,28 @@ public class ApiController implements DefaultApi { return new ResponseEntity<>(HttpStatus.NO_CONTENT); } + + @Override + public ResponseEntity removeEmployeeFromProject(Long id, Long employeeId){ + if (!projectRepository.existsById(id)) { + return ResponseEntity.notFound().build(); + } + + EmployeeResponseDTO employee; + try { + employee = apiClientFactory.getEmployeeApi().findById(employeeId); + } 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); + } + + Allocation allocation = allocationRepository.findById(id); + if (allocation.getEmployeeId().equals(employeeId)) { + allocationRepository.delete(allocation); + } + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } }