PMT-4: Mitarbeiter zu einem projekt hinzufügen #14
1 changed files with 55 additions and 4 deletions
|
@ -1,13 +1,13 @@
|
||||||
package de.hmmh.pmt;
|
package de.hmmh.pmt;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
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.Project;
|
||||||
import de.hmmh.pmt.db.ProjectRepository;
|
import de.hmmh.pmt.db.ProjectRepository;
|
||||||
import de.hmmh.pmt.dtos.CreateProjectDTO;
|
import de.hmmh.pmt.dtos.*;
|
||||||
import de.hmmh.pmt.dtos.CreatedProjectDTO;
|
|
||||||
import de.hmmh.pmt.dtos.GetAllProjectsDTO;
|
|
||||||
import de.hmmh.pmt.dtos.ProjectInfo;
|
|
||||||
import de.hmmh.pmt.employee.ApiClientFactory;
|
import de.hmmh.pmt.employee.ApiClientFactory;
|
||||||
|
import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO;
|
||||||
import de.hmmh.pmt.oas.DefaultApi;
|
import de.hmmh.pmt.oas.DefaultApi;
|
||||||
import de.hmmh.pmt.util.Mapper;
|
import de.hmmh.pmt.util.Mapper;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
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.HttpClientErrorException;
|
||||||
import org.springframework.web.client.RestClientException;
|
import org.springframework.web.client.RestClientException;
|
||||||
|
|
||||||
|
import java.time.ZoneOffset;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
|
@ -30,6 +32,8 @@ public class ApiController implements DefaultApi {
|
||||||
private ApiClientFactory apiClientFactory;
|
private ApiClientFactory apiClientFactory;
|
||||||
@Autowired
|
@Autowired
|
||||||
private ProjectRepository projectRepository;
|
private ProjectRepository projectRepository;
|
||||||
|
@Autowired
|
||||||
|
AllocationRepository allocationRepository;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<ObjectMapper> getObjectMapper() {
|
public Optional<ObjectMapper> getObjectMapper() {
|
||||||
|
@ -92,4 +96,51 @@ public class ApiController implements DefaultApi {
|
||||||
CreatedProjectDTO response = mapper.map(project);
|
CreatedProjectDTO response = mapper.map(project);
|
||||||
return new ResponseEntity<>(response, HttpStatus.CREATED);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue