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);
|
||||||
SZUT-Rajbir marked this conversation as resolved
Outdated
|
|||||||
|
}
|
||||||
SZUT-Rajbir marked this conversation as resolved
Outdated
SZUT-Dominik
commented
Doppelte Nutzung von Doppelte Nutzung von `findById`, was einen 2ten Datenbank Query auslöst, hast doch schon den Optional
|
|||||||
|
Project project = optionalProject.get();
|
||||||
SZUT-Rajbir marked this conversation as resolved
Outdated
SZUT-Dominik
commented
Newline um eine Logische Trennung zu machen Newline um eine Logische Trennung zu machen
|
|||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
SZUT-Rajbir marked this conversation as resolved
Outdated
SZUT-Dominik
commented
Der Happy Path sollte möglichsts zu letzt kommen, du hast viel zu viel im try catch Der Happy Path sollte möglichsts zu letzt kommen, du hast viel zu viel im try catch
|
|||||||
|
if (employee.getSkillSet()
|
||||||
SZUT-Rajbir marked this conversation as resolved
Outdated
SZUT-Dominik
commented
unnötige zwischen Variable, tu es doch direkt in das if unnötige zwischen Variable, tu es doch direkt in das if
|
|||||||
|
.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);
|
||||||
SZUT-Rajbir marked this conversation as resolved
Outdated
SZUT-Dominik
commented
Direkt in das If tun Direkt in das If tun
|
|||||||
|
long allocatedPlannedEnd = allocatedProject.getPlannedEnd().toEpochSecond(null);
|
||||||
|
return Math.max(start, allocatedStart) <= Math.min(plannedEnd, allocatedPlannedEnd);
|
||||||
SZUT-Rajbir marked this conversation as resolved
Outdated
SZUT-Dominik
commented
Einmalige methode zum enscheiden von etwas hat hier nix zu suchen, gehört inline Einmalige methode zum enscheiden von etwas hat hier nix zu suchen, gehört inline
|
|||||||
|
})) {
|
||||||
|
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
du kannst einfach direkt
repository.existsById(id)
nutzen wenn du nichtmer auf as Projekt zugreifen möchtest danach, anosnten bitte direkt nach dem If check aus dem Optional rausziehen in eine neue Variable