From 8b0cb91e53906ebc83221a00bd0a9682a2d51f73 Mon Sep 17 00:00:00 2001 From: Rajbir Singh Date: Mon, 21 Oct 2024 10:07:58 +0200 Subject: [PATCH] PMT-4 Refactor addEmployee --- src/main/java/de/hmmh/pmt/ApiController.java | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/main/java/de/hmmh/pmt/ApiController.java b/src/main/java/de/hmmh/pmt/ApiController.java index f2a736f..b201393 100644 --- a/src/main/java/de/hmmh/pmt/ApiController.java +++ b/src/main/java/de/hmmh/pmt/ApiController.java @@ -100,43 +100,36 @@ public class ApiController implements DefaultApi { public ResponseEntity addEmployee(Long id, AddEmployeeDTO body) { Optional optionalProject = projectRepository.findById(id); - - if (projectRepository.findById(id).isEmpty()) { + if (optionalProject.isEmpty()) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } Project project = optionalProject.get(); EmployeeResponseDTO employee; try { - employee = apiClientFactory.getEmployeeApi().findById(body.getEmployeeId()); + 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); } - boolean hasQualification = employee.getSkillSet() + if (employee.getSkillSet() .stream() - .anyMatch(qualification -> qualification.getId().equals(body.getQualificationId())); - - if (!hasQualification) { + .anyMatch(qualification -> qualification.getId().equals(body.getQualificationId()))) { return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY); } long start = project.getStart().toEpochSecond(null); long plannedEnd = project.getPlannedEnd().toEpochSecond(null); - List allocations = allocationRepository.findAllocationsByEmployeeId(body.getEmployeeId()); - - boolean hasOverlap = allocations.stream() + 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); - }); - - if (hasOverlap) { + })) { return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY); }