Compare commits
5 commits
022c187b27
...
c2a93e5a75
Author | SHA1 | Date | |
---|---|---|---|
c2a93e5a75 | |||
95ff26473a | |||
21040c69b6 | |||
516a088817 | |||
6de7c650ea |
8 changed files with 213 additions and 56 deletions
56
api/pmt.yml
56
api/pmt.yml
|
@ -46,6 +46,28 @@ components:
|
|||
plannedEnd:
|
||||
type: string
|
||||
format: date-time
|
||||
UpdateProjectDTO:
|
||||
type: object
|
||||
properties:
|
||||
name:
|
||||
type: string
|
||||
goal:
|
||||
type: string
|
||||
customerId:
|
||||
type: integer
|
||||
format: int64
|
||||
administratorId:
|
||||
type: integer
|
||||
format: int64
|
||||
start:
|
||||
type: string
|
||||
format: date-time
|
||||
plannedEnd:
|
||||
type: string
|
||||
format: date-time
|
||||
end:
|
||||
type: string
|
||||
format: date-time
|
||||
CreatedProjectDTO:
|
||||
type: object
|
||||
properties:
|
||||
|
@ -153,8 +175,7 @@ paths:
|
|||
$ref: "#/components/responses/InternalError"
|
||||
503:
|
||||
$ref: "#/components/responses/ServiceUnavailable"
|
||||
|
||||
|
||||
|
||||
/project/{id}:
|
||||
post:
|
||||
operationId: "addEmployee"
|
||||
|
@ -186,7 +207,36 @@ paths:
|
|||
$ref: "#/components/responses/InternalError"
|
||||
503:
|
||||
$ref: "#/components/responses/ServiceUnavailable"
|
||||
|
||||
put:
|
||||
operationId: "updateProject"
|
||||
description: "Updates a project"
|
||||
parameters:
|
||||
- in: path
|
||||
name: id
|
||||
schema:
|
||||
type: integer
|
||||
format: int64
|
||||
required: true
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/UpdateProjectDTO"
|
||||
responses:
|
||||
204:
|
||||
description: "Project updated successfully"
|
||||
401:
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
404:
|
||||
$ref: "#/components/responses/NotFound"
|
||||
409:
|
||||
$ref: "#/components/responses/Conflict"
|
||||
422:
|
||||
$ref: "#/components/responses/UnprocessableContent"
|
||||
500:
|
||||
$ref: "#/components/responses/InternalError"
|
||||
503:
|
||||
$ref: "#/components/responses/ServiceUnavailable"
|
||||
delete:
|
||||
operationId: "deleteProject"
|
||||
description: "Delete a specific Project"
|
||||
|
|
|
@ -9,18 +9,16 @@ 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.ApiTools;
|
||||
import de.hmmh.pmt.util.Mapper;
|
||||
import de.hmmh.pmt.util.Validator;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
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
|
||||
|
@ -31,6 +29,10 @@ public class ApiController implements DefaultApi {
|
|||
@Autowired
|
||||
private ApiClientFactory apiClientFactory;
|
||||
@Autowired
|
||||
private Validator validator;
|
||||
@Autowired
|
||||
private ApiTools apiTools;
|
||||
@Autowired
|
||||
private ProjectRepository projectRepository;
|
||||
@Autowired
|
||||
AllocationRepository allocationRepository;
|
||||
|
@ -69,26 +71,61 @@ public class ApiController implements DefaultApi {
|
|||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<Void> updateProject(Long id, UpdateProjectDTO body) {
|
||||
Optional<Project> optionalProject = projectRepository.findById(id);
|
||||
if (optionalProject.isEmpty()) {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
Project project = optionalProject.get();
|
||||
|
||||
if (project.getName().equals(body.getName()) && projectRepository.existsByName(body.getName())) {
|
||||
return new ResponseEntity<>(HttpStatus.CONFLICT);
|
||||
}
|
||||
|
||||
if (project.getAdministratorId().equals(body.getAdministratorId())) {
|
||||
HttpStatus status = apiTools.checkEmployeeExists(body.getAdministratorId()).status();
|
||||
if (status != HttpStatus.OK) {
|
||||
return new ResponseEntity<>(status);
|
||||
}
|
||||
}
|
||||
|
||||
Project finalProject = mapper.map(project, body);
|
||||
if (!validator.isValidProject(project)) {
|
||||
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
if (validator.areAllocationTimeRangesOverlapping(
|
||||
project.getStart(),
|
||||
project.getPlannedEnd(),
|
||||
allocationRepository
|
||||
.findAllByProjectId(finalProject.getId())
|
||||
.stream()
|
||||
.map(Allocation::getEmployeeId)
|
||||
.flatMap(employeeId -> allocationRepository.findAllByEmployeeId(employeeId).stream())
|
||||
.filter(employeeAllocation -> employeeAllocation.getProjectId().equals(finalProject.getId()))
|
||||
.toList()
|
||||
)){
|
||||
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
projectRepository.save(project);
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<CreatedProjectDTO> createProject(CreateProjectDTO body) {
|
||||
if (projectRepository.existsByName(body.getName())) {
|
||||
return new ResponseEntity<>(HttpStatus.CONFLICT);
|
||||
}
|
||||
|
||||
try {
|
||||
apiClientFactory.getEmployeeApi().findById(body.getAdministratorId());
|
||||
} 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);
|
||||
HttpStatus status = apiTools.checkEmployeeExists(body.getAdministratorId()).status();
|
||||
if (status != HttpStatus.OK) {
|
||||
return new ResponseEntity<>(status);
|
||||
}
|
||||
|
||||
Project project = mapper.map(body);
|
||||
if (!project.isValid()) {
|
||||
if (!validator.isValidProject(project)) {
|
||||
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
projectRepository.save(project);
|
||||
|
@ -105,16 +142,13 @@ public class ApiController implements DefaultApi {
|
|||
}
|
||||
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);
|
||||
|
||||
ApiTools.CheckEmployeeRecord employeeRecord = apiTools.checkEmployeeExists(body.getEmployeeId());
|
||||
if (employeeRecord.status() != HttpStatus.OK) {
|
||||
return new ResponseEntity<>(employeeRecord.status());
|
||||
}
|
||||
EmployeeResponseDTO employee = employeeRecord.employee();
|
||||
|
||||
|
||||
if (employee.getSkillSet()
|
||||
.stream()
|
||||
|
@ -122,16 +156,11 @@ public class ApiController implements DefaultApi {
|
|||
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
long start = project.getStart().toEpochSecond(ZoneOffset.UTC);
|
||||
long plannedEnd = project.getPlannedEnd().toEpochSecond(ZoneOffset.UTC);
|
||||
List<Allocation> allocations = allocationRepository.findAllByEmployeeId(body.getEmployeeId());
|
||||
if (allocations.stream()
|
||||
.map(Allocation::getProject)
|
||||
.anyMatch(allocatedProject -> {
|
||||
long allocatedStart = allocatedProject.getStart().toEpochSecond(ZoneOffset.UTC);
|
||||
long allocatedPlannedEnd = allocatedProject.getPlannedEnd().toEpochSecond(ZoneOffset.UTC);
|
||||
return Math.max(start, allocatedStart) <= Math.min(plannedEnd, allocatedPlannedEnd);
|
||||
})) {
|
||||
if (validator.areAllocationTimeRangesOverlapping(
|
||||
project.getStart(),
|
||||
project.getPlannedEnd(),
|
||||
allocationRepository.findAllByEmployeeId(body.getEmployeeId())
|
||||
)) {
|
||||
return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
|
|
|
@ -7,4 +7,5 @@ import java.util.List;
|
|||
public interface AllocationRepository extends JpaRepository<Allocation, AllocationId> {
|
||||
|
||||
List<Allocation> findAllByEmployeeId(Long employeeId);
|
||||
List<Allocation> findAllByProjectId(Long projectId);
|
||||
}
|
||||
|
|
|
@ -47,16 +47,4 @@ public class Project {
|
|||
private LocalDateTime plannedEnd;
|
||||
|
||||
private LocalDateTime realEnd; // Cant be named just "end" because it's and SQL Keyword
|
||||
|
||||
|
||||
public boolean isValid() {
|
||||
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
Set<ConstraintViolation<Project>> violations = validator.validate(this);
|
||||
|
||||
return violations.isEmpty() &&
|
||||
plannedEnd.isAfter(start) &&
|
||||
(realEnd == null || realEnd.isAfter(start));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
34
src/main/java/de/hmmh/pmt/util/ApiTools.java
Normal file
34
src/main/java/de/hmmh/pmt/util/ApiTools.java
Normal file
|
@ -0,0 +1,34 @@
|
|||
package de.hmmh.pmt.util;
|
||||
|
||||
import de.hmmh.pmt.employee.ApiClientFactory;
|
||||
import de.hmmh.pmt.employee.dtos.EmployeeResponseDTO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
import org.springframework.web.client.RestClientException;
|
||||
|
||||
@Component
|
||||
public class ApiTools {
|
||||
|
||||
@Autowired
|
||||
private ApiClientFactory apiClientFactory;
|
||||
|
||||
public record CheckEmployeeRecord(EmployeeResponseDTO employee, HttpStatus status) {}
|
||||
public CheckEmployeeRecord checkEmployeeExists(long id) {
|
||||
EmployeeResponseDTO employee;
|
||||
try {
|
||||
employee =apiClientFactory.getEmployeeApi().findById(id);
|
||||
} catch (HttpClientErrorException exception) {
|
||||
return new CheckEmployeeRecord(
|
||||
null,
|
||||
exception.getStatusCode().equals(HttpStatus.NOT_FOUND)
|
||||
? HttpStatus.NOT_FOUND
|
||||
: HttpStatus.SERVICE_UNAVAILABLE
|
||||
);
|
||||
} catch (RestClientException exception) {
|
||||
return new CheckEmployeeRecord(null, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
return new CheckEmployeeRecord(employee, HttpStatus.OK);
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@ package de.hmmh.pmt.util;
|
|||
import de.hmmh.pmt.db.Project;
|
||||
import de.hmmh.pmt.dtos.CreateProjectDTO;
|
||||
import de.hmmh.pmt.dtos.CreatedProjectDTO;
|
||||
import de.hmmh.pmt.dtos.UpdateProjectDTO;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
|
@ -29,4 +30,15 @@ public class Mapper {
|
|||
dto.setPlannedEnd(project.getPlannedEnd());
|
||||
return dto;
|
||||
}
|
||||
|
||||
public Project map(Project project, UpdateProjectDTO dto) {
|
||||
project.setName(dto.getName());
|
||||
project.setGoal(dto.getGoal());
|
||||
project.setCustomerId(dto.getCustomerId());
|
||||
project.setAdministratorId(dto.getAdministratorId());
|
||||
project.setStart(dto.getStart());
|
||||
project.setPlannedEnd(dto.getPlannedEnd());
|
||||
project.setRealEnd(dto.getEnd());
|
||||
return project;
|
||||
}
|
||||
}
|
||||
|
|
48
src/main/java/de/hmmh/pmt/util/Validator.java
Normal file
48
src/main/java/de/hmmh/pmt/util/Validator.java
Normal file
|
@ -0,0 +1,48 @@
|
|||
package de.hmmh.pmt.util;
|
||||
|
||||
import de.hmmh.pmt.db.Allocation;
|
||||
import de.hmmh.pmt.db.Project;
|
||||
import jakarta.validation.ConstraintViolation;
|
||||
import jakarta.validation.Validation;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Component
|
||||
public class Validator {
|
||||
|
||||
public boolean isValidProject(Project project) {
|
||||
jakarta.validation.Validator validator = Validation
|
||||
.buildDefaultValidatorFactory()
|
||||
.getValidator();
|
||||
Set<ConstraintViolation<Project>> violations = validator.validate(project);
|
||||
|
||||
LocalDateTime start = project.getStart();
|
||||
LocalDateTime plannedEnd = project.getPlannedEnd();
|
||||
LocalDateTime realEnd = project.getRealEnd();
|
||||
|
||||
return violations.isEmpty() &&
|
||||
plannedEnd.isAfter(start) &&
|
||||
(realEnd == null || realEnd.isAfter(start));
|
||||
}
|
||||
|
||||
public boolean areAllocationTimeRangesOverlapping(
|
||||
LocalDateTime start,
|
||||
LocalDateTime plannedEnd,
|
||||
List<Allocation> allocations
|
||||
){
|
||||
long startUnix = start.toEpochSecond(ZoneOffset.UTC);
|
||||
long plannedEndUnix = plannedEnd.toEpochSecond(ZoneOffset.UTC);
|
||||
return allocations.stream()
|
||||
.map(Allocation::getProject)
|
||||
.anyMatch(allocatedProject -> {
|
||||
long allocatedStart = allocatedProject.getStart().toEpochSecond(ZoneOffset.UTC);
|
||||
long allocatedPlannedEnd = allocatedProject.getPlannedEnd().toEpochSecond(ZoneOffset.UTC);
|
||||
return Math.max(startUnix, allocatedStart) <= Math.min(plannedEndUnix, allocatedPlannedEnd);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
|
@ -1,12 +1,7 @@
|
|||
<FindBugsFilter xmlns="https://raw.githubusercontent.com/spotbugs/spotbugs/4.8.6/spotbugs/etc/findbugsfilter.xsd">
|
||||
<Match>
|
||||
<!-- We Want This Exposure of Resources the Way it is for our usage -->
|
||||
<Class name="de.hmmh.pmt.employee.ApiClientFactory"/>
|
||||
<Bug code="M,V,EI"/>
|
||||
</Match>
|
||||
<Match>
|
||||
<Class name="de.hmmh.pmt.db.Allocation"/>
|
||||
<Bug code="M,V,EI,EI2"/>
|
||||
<Source name="~.*"/>
|
||||
<Bug code="EI,EI2"/> <!-- We don't care about these codes -->
|
||||
</Match>
|
||||
<Match>
|
||||
<!--Ignore Auto Generated Code -->
|
||||
|
|
Loading…
Reference in a new issue