From 9a123d1e76b6882df5a1d5c7046bc5d0b7966566 Mon Sep 17 00:00:00 2001
From: Phan Huy Tran
Date: Wed, 23 Oct 2024 14:11:57 +0200
Subject: [PATCH] Implement feature to acknowledge project time frames when
assigning employees to projects
---
requests/project/createProject.http | 6 +-
.../lf8_starter/project/ProjectService.java | 15 ++
.../action/crud/CreateProjectAction.java | 33 +++-
.../{ => crud}/RemoveProjectAction.java | 2 +-
.../employee/AddEmployeeToProjectAction.java | 20 +-
.../project/CreateProjectActionTest.java | 177 +++++++++++++++++-
6 files changed, 233 insertions(+), 20 deletions(-)
rename src/main/java/de/szut/lf8_starter/project/action/{ => crud}/RemoveProjectAction.java (97%)
diff --git a/requests/project/createProject.http b/requests/project/createProject.http
index 3338a2c..1e044b5 100644
--- a/requests/project/createProject.http
+++ b/requests/project/createProject.http
@@ -6,10 +6,10 @@ Content-Type: application/json
{
"name": "name",
"leading_employee": 1,
- "employees": [315, 312],
+ "employees": [312],
"contractor": 4,
"contractor_name": "Peter File",
"comment": "goal of project",
- "start_date": "01.01.2000",
- "planned_end_date": "01.01.2001"
+ "start_date": "02.01.2020",
+ "planned_end_date": "01.01.2023"
}
\ No newline at end of file
diff --git a/src/main/java/de/szut/lf8_starter/project/ProjectService.java b/src/main/java/de/szut/lf8_starter/project/ProjectService.java
index 17afcea..5a50454 100644
--- a/src/main/java/de/szut/lf8_starter/project/ProjectService.java
+++ b/src/main/java/de/szut/lf8_starter/project/ProjectService.java
@@ -1,5 +1,6 @@
package de.szut.lf8_starter.project;
+import de.szut.lf8_starter.project.dto.project.GetProjectDto;
import org.springframework.stereotype.Service;
import java.util.List;
@@ -32,4 +33,18 @@ public class ProjectService {
public void delete(Long id) {
this.projectRepository.deleteById(id);
}
+
+ public boolean isOverlapping(GetProjectDto getProjectDto, ProjectEntity existingProjectEntity) {
+ return isDateRangeOverlapping(getProjectDto, existingProjectEntity) || isDateEqual(getProjectDto, existingProjectEntity);
+ }
+
+ private boolean isDateRangeOverlapping(GetProjectDto getProjectDto, ProjectEntity existingProjectEntity) {
+ return getProjectDto.getStartDate().isBefore(existingProjectEntity.getPlannedEndDate())
+ && getProjectDto.getPlannedEndDate().isAfter(existingProjectEntity.getStartDate());
+ }
+
+ private boolean isDateEqual(GetProjectDto getProjectDto, ProjectEntity existingProjectEntity) {
+ return getProjectDto.getStartDate().isEqual(existingProjectEntity.getStartDate())
+ || getProjectDto.getPlannedEndDate().isEqual(existingProjectEntity.getPlannedEndDate());
+ }
}
diff --git a/src/main/java/de/szut/lf8_starter/project/action/crud/CreateProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/crud/CreateProjectAction.java
index 1725ba8..6b0c3b5 100644
--- a/src/main/java/de/szut/lf8_starter/project/action/crud/CreateProjectAction.java
+++ b/src/main/java/de/szut/lf8_starter/project/action/crud/CreateProjectAction.java
@@ -1,5 +1,6 @@
package de.szut.lf8_starter.project.action.crud;
+import de.szut.lf8_starter.employee.EmployeeService;
import de.szut.lf8_starter.project.ProjectEntity;
import de.szut.lf8_starter.project.ProjectService;
import de.szut.lf8_starter.project.dto.project.CreateProjectDto;
@@ -12,15 +13,18 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping(value = "/projects")
public class CreateProjectAction {
+ private final EmployeeService employeeService;
private final ProjectService projectService;
private final ProjectMapper projectMapper;
- public CreateProjectAction(ProjectService projectService, ProjectMapper mappingService) {
+ public CreateProjectAction(EmployeeService employeeService, ProjectService projectService, ProjectMapper mappingService) {
+ this.employeeService = employeeService;
this.projectService = projectService;
this.projectMapper = mappingService;
}
@@ -29,14 +33,31 @@ public class CreateProjectAction {
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "created project", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = GetProjectDto.class))}),
@ApiResponse(responseCode = "400", description = "invalid JSON posted", content = @Content),
- @ApiResponse(responseCode = "401", description = "not authorized", content = @Content)})
+ @ApiResponse(responseCode = "401", description = "not authorized", content = @Content),
+ @ApiResponse(responseCode = "409", description = "Project dates conflict", content = @Content)
+ })
@PostMapping
@ResponseStatus(code = HttpStatus.CREATED)
- public GetProjectDto create(@RequestBody @Valid CreateProjectDto createProjectDto) {
- ProjectEntity projectEntity = this.projectMapper.mapCreateDtoToEntity(createProjectDto);
+ public ResponseEntity