From 022c187b270e2a5ea086ea9c1e8a6b547a60ece8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Wed, 23 Oct 2024 09:50:29 +0200 Subject: [PATCH] PMT-43: Implement Endpoint --- src/main/java/de/hmmh/pmt/ApiController.java | 42 +++++++++++++++++++ .../de/hmmh/pmt/db/AllocationRepository.java | 1 + src/main/java/de/hmmh/pmt/util/Mapper.java | 12 ++++++ 3 files changed, 55 insertions(+) diff --git a/src/main/java/de/hmmh/pmt/ApiController.java b/src/main/java/de/hmmh/pmt/ApiController.java index 1412d48..577b292 100644 --- a/src/main/java/de/hmmh/pmt/ApiController.java +++ b/src/main/java/de/hmmh/pmt/ApiController.java @@ -71,6 +71,48 @@ public class ApiController implements DefaultApi { return ResponseEntity.ok(response); } + @Override + public ResponseEntity updateProject(Long id, UpdateProjectDTO body) { + Optional 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 createProject(CreateProjectDTO body) { if (projectRepository.existsByName(body.getName())) { diff --git a/src/main/java/de/hmmh/pmt/db/AllocationRepository.java b/src/main/java/de/hmmh/pmt/db/AllocationRepository.java index dcbc9d3..f10e765 100644 --- a/src/main/java/de/hmmh/pmt/db/AllocationRepository.java +++ b/src/main/java/de/hmmh/pmt/db/AllocationRepository.java @@ -7,4 +7,5 @@ import java.util.List; public interface AllocationRepository extends JpaRepository { List findAllByEmployeeId(Long employeeId); + List findAllByProjectId(Long projectId); } diff --git a/src/main/java/de/hmmh/pmt/util/Mapper.java b/src/main/java/de/hmmh/pmt/util/Mapper.java index d5d7591..c57ae46 100644 --- a/src/main/java/de/hmmh/pmt/util/Mapper.java +++ b/src/main/java/de/hmmh/pmt/util/Mapper.java @@ -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; + } }