From b2ef8ce2abe610ca7263e82fc14bc8fec12ec23d Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 09:43:10 +0000 Subject: [PATCH] feat(project): Delete project (SCRUM-35) (!29) Reviewed-on: https://git.kjan.de/jank/LF8/pulls/29 Reviewed-by: Phan Huy Tran Co-authored-by: Jan Klattenhoff Co-committed-by: Jan Klattenhoff --- .../lf8_starter/project/ProjectService.java | 4 ++ .../project/action/RemoveProjectAction.java | 40 ++++++++++++++++ .../project/DeleteProjectActionTest.java | 47 +++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java create mode 100644 src/test/java/de/szut/lf8_starter/integration/project/DeleteProjectActionTest.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..d14dbee --- /dev/null +++ b/src/main/java/de/szut/lf8_starter/project/action/RemoveProjectAction.java @@ -0,0 +1,40 @@ +package de.szut.lf8_starter.project.action; + +import de.szut.lf8_starter.project.ProjectEntity; +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.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; + + public RemoveProjectAction(ProjectService projectService) { + this.projectService = projectService; + } + + @Operation(summary = "Remove project by ID") + @ApiResponses(value = { + @ApiResponse(responseCode = "204", 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<>(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 new file mode 100644 index 0000000..93285ab --- /dev/null +++ b/src/test/java/de/szut/lf8_starter/integration/project/DeleteProjectActionTest.java @@ -0,0 +1,47 @@ +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.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.status; + +@SpringBootTest +@AutoConfigureMockMvc(addFilters = false) +class RemoveProjectActionTest { + @Autowired + private MockMvc mockMvc; + @Autowired + private ProjectRepository projectRepository; + + @Test + void deleteProjectTest() throws Exception { + var project = new ProjectEntity(); + 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/" + project.getId())) + .andExpect(status().isNoContent()); + } + + @Test + void deleteProjectShouldReturnNotFoundResponseWhenProjectIsNotFound() throws Exception { + this.mockMvc.perform(get("/projects/2987")).andExpect(status().isNotFound()); + } +}