PMT-4: Implement Endpoint

Co-authored-by: Ole Kück <ole.kueck@hmmh.de>
This commit is contained in:
Rajbir Singh 2024-10-21 13:07:03 +02:00 committed by Dominik Säume
parent d5a0483293
commit 954ad8237c
Signed by: SZUT-Dominik
GPG key ID: 67D15BB250B41E7C

View file

@ -1,13 +1,13 @@
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.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.oas.DefaultApi;
import de.hmmh.pmt.util.Mapper;
import jakarta.servlet.http.HttpServletRequest;
@ -19,6 +19,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClientException;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Optional;
@Controller
@ -30,6 +32,8 @@ public class ApiController implements DefaultApi {
private ApiClientFactory apiClientFactory;
@Autowired
private ProjectRepository projectRepository;
@Autowired
AllocationRepository allocationRepository;
@Override
public Optional<ObjectMapper> getObjectMapper() {
@ -92,4 +96,51 @@ public class ApiController implements DefaultApi {
CreatedProjectDTO response = mapper.map(project);
return new ResponseEntity<>(response, HttpStatus.CREATED);
}
@Override
public ResponseEntity<Void> addEmployee(Long id, AddEmployeeDTO body) {
Optional<Project> optionalProject = projectRepository.findById(id);
if (optionalProject.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
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);
}
if (employee.getSkillSet()
.stream()
.noneMatch(qualification -> qualification.getId().equals(body.getQualificationId()))) {
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
}
long start = project.getStart().toEpochSecond(ZoneOffset.UTC);
long plannedEnd = project.getPlannedEnd().toEpochSecond(ZoneOffset.UTC);
List<Allocation> allocations = allocationRepository.findAllocationsByEmployeeId(body.getEmployeeId());
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);
})) {
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
}
Allocation allocation = new Allocation();
allocation.setEmployeeId(employee.getId());
allocation.setRole(body.getQualificationId());
allocation.setProject(project);
allocationRepository.save(allocation);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}