From 8602c6cf94e86f0d24832bbcacc78872fd4d2c6f Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 11:02:18 +0200 Subject: [PATCH 1/8] feat(project): add remove project functionality with API endpoint --- .../lf8_starter/project/ProjectService.java | 4 ++ .../project/action/RemoveProjectAction.java | 45 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java 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 917607f..31efbec 100644 --- a/src/main/java/de/szut/lf8_starter/project/ProjectService.java +++ b/src/main/java/de/szut/lf8_starter/project/ProjectService.java @@ -30,4 +30,8 @@ public class ProjectService { return project; } + + public void delete(Long id) { + this.projectRepository.deleteById(id); + } } diff --git a/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java new file mode 100644 index 0000000..660856f --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java @@ -0,0 +1,45 @@ +package de.szut.lf8_starter.project.action; + +import de.szut.lf8_starter.project.ProjectEntity; +import de.szut.lf8_starter.project.ProjectMapper; +import de.szut.lf8_starter.project.ProjectService; +import de.szut.lf8_starter.project.dto.GetProjectDto; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.Optional; + +@RestController +@RequestMapping(value = "projects") +public class RemoveProjectAction { + private final ProjectService projectService; + private final ProjectMapper projectMapper; + + public RemoveProjectAction(ProjectService projectService, ProjectMapper projectMapper) { + this.projectService = projectService; + this.projectMapper = projectMapper; + } + + @Operation(summary = "Remove project by ID") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "Project deleted", content = {}), + @ApiResponse(responseCode = "404", description = "Project not found", content = @Content) + }) + @DeleteMapping("/{id}") + public ResponseEntity findArticleById(@PathVariable Long id) { + Optional project = this.projectService.findById(id); + + if (project.isEmpty()) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + this.projectService.delete(id); + return new ResponseEntity<>(this.projectMapper.mapToGetDto(project.get()), HttpStatus.OK); + } +} -- 2.45.2 From 87f4d05a4382428f483acea5096882eaca914d5a Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 11:06:23 +0200 Subject: [PATCH 2/8] refactor(RemoveProjectAction): simplify project deletion response --- .../szut/lf8_starter/project/action/RemoveProjectAction.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java index 660856f..4d93a28 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java @@ -6,7 +6,6 @@ import de.szut.lf8_starter.project.ProjectService; import de.szut.lf8_starter.project.dto.GetProjectDto; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; -import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import org.springframework.http.HttpStatus; @@ -39,7 +38,7 @@ public class RemoveProjectAction { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - this.projectService.delete(id); - return new ResponseEntity<>(this.projectMapper.mapToGetDto(project.get()), HttpStatus.OK); + this.projectService.delete(project.get().getId()); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); } } -- 2.45.2 From 730c144c22c66137c94953950b6bf78dcca6ebe7 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 11:12:26 +0200 Subject: [PATCH 3/8] test(DeleteProjectAction): add integration tests for project deletion --- .../project/DeleteProjectActionTest.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/test/java/de/szut/lf8_starter/integration/project/DeleteProjectActionTest.java diff --git a/src/test/java/de/szut/lf8_starter/integration/project/DeleteProjectActionTest.java b/src/test/java/de/szut/lf8_starter/integration/project/DeleteProjectActionTest.java new file mode 100644 index 0000000..1e006d9 --- /dev/null +++ b/src/test/java/de/szut/lf8_starter/integration/project/DeleteProjectActionTest.java @@ -0,0 +1,50 @@ +package de.szut.lf8_starter.integration.project; + +import de.szut.lf8_starter.project.ProjectEntity; +import de.szut.lf8_starter.project.ProjectRepository; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.web.servlet.MockMvc; + +import java.time.LocalDate; +import java.util.List; + +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc(addFilters = false) +public class DeleteProjectActionTest { + @Autowired + private MockMvc mockMvc; + @Autowired + private ProjectRepository projectRepository; + + @Test + void deleteProjectTest() throws Exception { + var project = new ProjectEntity(); + project.setId(1); + project.setComment("comment"); + project.setContractor(1); + project.setContractorName("contractorName"); + project.setEndDate(LocalDate.of(2024, 1, 1)); + project.setLeadingEmployee(1); + project.setName("name"); + project.setStartDate(LocalDate.of(2021, 1, 1)); + project.setEmployees(List.of(1L, 2L, 3L)); + this.projectRepository.save(project); + + this.mockMvc.perform(delete("/projects/1")) + .andExpect(status().isNoContent()); + } + + @Test + void deleteProjectShouldReturnNotFoundResponseWhenProjectIsNotFound() throws Exception { + this.mockMvc.perform(get("/projects/2")).andExpect(status().isNotFound()); + } +} -- 2.45.2 From 4abce554e5ad4b96487d32f6076979a7c2449797 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 11:17:09 +0200 Subject: [PATCH 4/8] test(DeleteProjectActionTest): update test project IDs for accuracy --- .../integration/project/DeleteProjectActionTest.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/test/java/de/szut/lf8_starter/integration/project/DeleteProjectActionTest.java b/src/test/java/de/szut/lf8_starter/integration/project/DeleteProjectActionTest.java index 1e006d9..070fbe9 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/DeleteProjectActionTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/DeleteProjectActionTest.java @@ -11,10 +11,8 @@ import org.springframework.test.web.servlet.MockMvc; import java.time.LocalDate; import java.util.List; -import static org.hamcrest.collection.IsCollectionWithSize.hasSize; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @SpringBootTest @@ -28,7 +26,7 @@ public class DeleteProjectActionTest { @Test void deleteProjectTest() throws Exception { var project = new ProjectEntity(); - project.setId(1); + project.setId(20); project.setComment("comment"); project.setContractor(1); project.setContractorName("contractorName"); @@ -39,12 +37,12 @@ public class DeleteProjectActionTest { project.setEmployees(List.of(1L, 2L, 3L)); this.projectRepository.save(project); - this.mockMvc.perform(delete("/projects/1")) + this.mockMvc.perform(delete("/projects/20")) .andExpect(status().isNoContent()); } @Test void deleteProjectShouldReturnNotFoundResponseWhenProjectIsNotFound() throws Exception { - this.mockMvc.perform(get("/projects/2")).andExpect(status().isNotFound()); + this.mockMvc.perform(get("/projects/2987")).andExpect(status().isNotFound()); } } -- 2.45.2 From 4c44b843ae11836b63d86014dee971a5fa394d7d Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 11:27:25 +0200 Subject: [PATCH 5/8] refactor: simplify project deletion logic in action class --- .../szut/lf8_starter/project/action/RemoveProjectAction.java | 3 +-- .../integration/project/DeleteProjectActionTest.java | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java index 4d93a28..767c22d 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java @@ -33,12 +33,11 @@ public class RemoveProjectAction { @DeleteMapping("/{id}") public ResponseEntity findArticleById(@PathVariable Long id) { Optional project = this.projectService.findById(id); - if (project.isEmpty()) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - this.projectService.delete(project.get().getId()); + this.projectService.delete(id); return new ResponseEntity<>(HttpStatus.NO_CONTENT); } } diff --git a/src/test/java/de/szut/lf8_starter/integration/project/DeleteProjectActionTest.java b/src/test/java/de/szut/lf8_starter/integration/project/DeleteProjectActionTest.java index 070fbe9..6d9ee5b 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/DeleteProjectActionTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/DeleteProjectActionTest.java @@ -26,7 +26,6 @@ public class DeleteProjectActionTest { @Test void deleteProjectTest() throws Exception { var project = new ProjectEntity(); - project.setId(20); project.setComment("comment"); project.setContractor(1); project.setContractorName("contractorName"); @@ -37,7 +36,7 @@ public class DeleteProjectActionTest { project.setEmployees(List.of(1L, 2L, 3L)); this.projectRepository.save(project); - this.mockMvc.perform(delete("/projects/20")) + this.mockMvc.perform(delete("/projects/" + project.getId())) .andExpect(status().isNoContent()); } -- 2.45.2 From 5e3ee4bf2356638068adb934e593e276502a9542 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 11:31:15 +0200 Subject: [PATCH 6/8] refactor: simplify RemoveProjectAction constructor --- .../szut/lf8_starter/project/action/RemoveProjectAction.java | 4 +--- .../integration/project/DeleteProjectActionTest.java | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java index 767c22d..4128191 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java @@ -18,11 +18,9 @@ import java.util.Optional; @RequestMapping(value = "projects") public class RemoveProjectAction { private final ProjectService projectService; - private final ProjectMapper projectMapper; - public RemoveProjectAction(ProjectService projectService, ProjectMapper projectMapper) { + public RemoveProjectAction(ProjectService projectService) { this.projectService = projectService; - this.projectMapper = projectMapper; } @Operation(summary = "Remove project by ID") diff --git a/src/test/java/de/szut/lf8_starter/integration/project/DeleteProjectActionTest.java b/src/test/java/de/szut/lf8_starter/integration/project/DeleteProjectActionTest.java index 6d9ee5b..93285ab 100644 --- a/src/test/java/de/szut/lf8_starter/integration/project/DeleteProjectActionTest.java +++ b/src/test/java/de/szut/lf8_starter/integration/project/DeleteProjectActionTest.java @@ -17,7 +17,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @SpringBootTest @AutoConfigureMockMvc(addFilters = false) -public class DeleteProjectActionTest { +class RemoveProjectActionTest { @Autowired private MockMvc mockMvc; @Autowired -- 2.45.2 From 94fad947dab471026e67537480b8695900365860 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 11:34:03 +0200 Subject: [PATCH 7/8] refactor: remove unused import in RemoveProjectAction.java --- .../de/szut/lf8_starter/project/action/RemoveProjectAction.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java index 4128191..bacc931 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java @@ -1,7 +1,6 @@ package de.szut.lf8_starter.project.action; import de.szut.lf8_starter.project.ProjectEntity; -import de.szut.lf8_starter.project.ProjectMapper; import de.szut.lf8_starter.project.ProjectService; import de.szut.lf8_starter.project.dto.GetProjectDto; import io.swagger.v3.oas.annotations.Operation; -- 2.45.2 From 48b836e203898572f077d424f3d81bcbd93b019d Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 11:40:32 +0200 Subject: [PATCH 8/8] fix(RemoveProjectAction): update response code to 204 --- .../de/szut/lf8_starter/project/action/RemoveProjectAction.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java b/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java index bacc931..d14dbee 100644 --- a/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java +++ b/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java @@ -24,7 +24,7 @@ public class RemoveProjectAction { @Operation(summary = "Remove project by ID") @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "Project deleted", content = {}), + @ApiResponse(responseCode = "204", description = "Project deleted", content = {}), @ApiResponse(responseCode = "404", description = "Project not found", content = @Content) }) @DeleteMapping("/{id}") -- 2.45.2