From 224561f03881b89b262525ac117eefde51c8625f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Wed, 23 Oct 2024 09:49:36 +0200 Subject: [PATCH] PMT-43: Separate Repeated API Logic into ApiTools --- src/main/java/de/hmmh/pmt/ApiController.java | 29 ++++++----------- src/main/java/de/hmmh/pmt/util/ApiTools.java | 34 ++++++++++++++++++++ 2 files changed, 44 insertions(+), 19 deletions(-) create mode 100644 src/main/java/de/hmmh/pmt/util/ApiTools.java diff --git a/src/main/java/de/hmmh/pmt/ApiController.java b/src/main/java/de/hmmh/pmt/ApiController.java index 19d7356..3843178 100644 --- a/src/main/java/de/hmmh/pmt/ApiController.java +++ b/src/main/java/de/hmmh/pmt/ApiController.java @@ -8,6 +8,7 @@ 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.ApiTools; import de.hmmh.pmt.util.Mapper; import de.hmmh.pmt.util.Validator; import jakarta.servlet.http.HttpServletRequest; @@ -79,16 +80,9 @@ public class ApiController implements DefaultApi { return new ResponseEntity<>(HttpStatus.CONFLICT); } - try { - apiClientFactory.getEmployeeApi().findById(body.getAdministratorId()); - } 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); + HttpStatus status = apiTools.checkEmployeeExists(body.getAdministratorId()).status(); + if (status != HttpStatus.OK) { + return new ResponseEntity<>(status); } Project project = mapper.map(body); @@ -109,16 +103,13 @@ public class ApiController implements DefaultApi { } 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); + + ApiTools.CheckEmployeeRecord employeeRecord = apiTools.checkEmployeeExists(body.getEmployeeId()); + if (employeeRecord.status() != HttpStatus.OK) { + return new ResponseEntity<>(employeeRecord.status()); } + EmployeeResponseDTO employee = employeeRecord.employee(); + if (employee.getSkillSet() .stream() diff --git a/src/main/java/de/hmmh/pmt/util/ApiTools.java b/src/main/java/de/hmmh/pmt/util/ApiTools.java new file mode 100644 index 0000000..355a820 --- /dev/null +++ b/src/main/java/de/hmmh/pmt/util/ApiTools.java @@ -0,0 +1,34 @@ +package de.hmmh.pmt.util; + +import de.hmmh.pmt.employee.ApiClientFactory; +import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Component; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestClientException; + +@Component +public class ApiTools { + + @Autowired + private ApiClientFactory apiClientFactory; + + public record CheckEmployeeRecord(EmployeeResponseDTO employee, HttpStatus status) {} + public CheckEmployeeRecord checkEmployeeExists(long id) { + EmployeeResponseDTO employee; + try { + employee =apiClientFactory.getEmployeeApi().findById(id); + } catch (HttpClientErrorException exception) { + return new CheckEmployeeRecord( + null, + exception.getStatusCode().equals(HttpStatus.NOT_FOUND) + ? HttpStatus.NOT_FOUND + : HttpStatus.SERVICE_UNAVAILABLE + ); + } catch (RestClientException exception) { + return new CheckEmployeeRecord(null, HttpStatus.INTERNAL_SERVER_ERROR); + } + return new CheckEmployeeRecord(employee, HttpStatus.OK); + } +}