diff --git a/src/main/java/de/hmmh/pmt/ApiController.java b/src/main/java/de/hmmh/pmt/ApiController.java index c20b9f4..4d0ca78 100644 --- a/src/main/java/de/hmmh/pmt/ApiController.java +++ b/src/main/java/de/hmmh/pmt/ApiController.java @@ -1,15 +1,14 @@ 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.employee.ApiClientFactory; 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.employee.dtos.QualificationGetDTO; import de.hmmh.pmt.oas.DefaultApi; import de.hmmh.pmt.util.Mapper; import jakarta.servlet.http.HttpServletRequest; @@ -21,6 +20,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestClientException; +import java.util.List; import java.util.Optional; @Controller @@ -96,4 +96,46 @@ 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); + } + try { + EmployeeResponseDTO employee = apiClientFactory.getEmployeeApi().findById(body.getEmployeeId()); + List qualifications = employee.getSkillSet(); + if (!hasQualification(qualifications, body.getQualificationId())) { + return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE); + } + + Project project = optionalProject.get(); + if (allocationRepository.hasOverlappingAllocation(employee.getId(), project.getStart(), project.getPlannedEnd())) { + return new ResponseEntity<>(HttpStatus.CONFLICT); + } + Allocation allocation = new Allocation(); + allocation.setEmployeeId(employee.getId()); + allocation.setRole(body.getQualificationId()); + allocation.setProjectId(project.getId()); + allocationRepository.save(allocation); + + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + + } catch (HttpClientErrorException exception) { + return new ResponseEntity<>(exception.getStatusCode().equals(HttpStatus.NOT_FOUND) + ? HttpStatus.NOT_FOUND + : HttpStatus.INTERNAL_SERVER_ERROR); + } + } + + private boolean hasQualification(List qualifications, Long neededQualificationId) { + for (QualificationGetDTO qualification : qualifications) { + if (qualification.getId().equals(neededQualificationId)) { + return true; + } + } + return false; + } +} \ No newline at end of file diff --git a/src/main/java/de/hmmh/pmt/db/AllocationRepository.java b/src/main/java/de/hmmh/pmt/db/AllocationRepository.java index 387b20f..8321dcd 100644 --- a/src/main/java/de/hmmh/pmt/db/AllocationRepository.java +++ b/src/main/java/de/hmmh/pmt/db/AllocationRepository.java @@ -1,7 +1,15 @@ package de.hmmh.pmt.db; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; +import java.time.LocalDateTime; + public interface AllocationRepository extends CrudRepository { - + + @Query("SELECT COUNT(a) > 0 FROM Allocation a " + + "WHERE a.employeeId = :employeeId " + + "AND a.project.start <= :endDate " + + "AND a.project.plannedEnd >= :startDate") + boolean hasOverlappingAllocation(Long employeeId, LocalDateTime startDate, LocalDateTime endDate); }