PMT-4 add Employee to a Project
Some checks failed
Quality Check / Validate OAS (push) Successful in 32s
Quality Check / Linting (push) Failing after 55s
Quality Check / Static Analysis (push) Failing after 1m1s
Quality Check / Testing (push) Successful in 1m7s
Quality Check / Validate OAS (pull_request) Successful in 32s
Quality Check / Linting (pull_request) Failing after 47s
Quality Check / Static Analysis (pull_request) Failing after 1m10s
Quality Check / Testing (pull_request) Successful in 1m12s

This commit is contained in:
Rajbir Singh 2024-10-18 17:21:51 +02:00
parent e1fc61dfdd
commit 193a7c5953
2 changed files with 57 additions and 7 deletions

View file

@ -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<Void> addEmployee(Long id, AddEmployeeDTO body) {
Optional<Project> optionalProject = projectRepository.findById(id);
if (optionalProject.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
try {
EmployeeResponseDTO employee = apiClientFactory.getEmployeeApi().findById(body.getEmployeeId());
List<QualificationGetDTO> 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<QualificationGetDTO> qualifications, Long neededQualificationId) {
for (QualificationGetDTO qualification : qualifications) {
if (qualification.getId().equals(neededQualificationId)) {
return true;
}
}
return false;
}
}

View file

@ -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<Allocation, AllocationId> {
@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);
}