feat: Implement get project by ID Route (!25)
All checks were successful
Build / Build and analyze (push) Successful in 1m25s
Quality Check / Tests (push) Successful in 42s
Quality Check / Checkstyle Main (push) Successful in 37s
Release / Release (push) Successful in 39s
Quality Check / Tests (pull_request) Successful in 59s
Quality Check / Checkstyle Main (pull_request) Successful in 43s
gitea-sonarqube-bot OK
Build PR / Build and analyze (pull_request) Successful in 1m52s

Co-authored-by: Phan Huy Tran <p.tran@neusta.de>
Reviewed-on: #25
Reviewed-by: Jan Gleytenhoover <krisellp9@gmail.com>
Co-authored-by: Phan Huy Tran <ptran@noreply.localhost>
Co-committed-by: Phan Huy Tran <ptran@noreply.localhost>
This commit is contained in:
Phan Huy Tran 2024-10-02 07:04:08 +00:00 committed by Jan Gleytenhoover
parent d047bce4ec
commit c317b226fe
6 changed files with 126 additions and 10 deletions

3
requests/getProject.http Normal file

@ -0,0 +1,3 @@
### GET request to example server
GET http://localhost:8080/projects/2
Authorization: Bearer {{auth_token}}

@ -1,8 +1,10 @@
package de.szut.lf8_starter.project;
import de.szut.lf8_starter.exceptionHandling.ResourceNotFoundException;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class ProjectService {
@ -19,4 +21,14 @@ public class ProjectService {
public List<ProjectEntity> readAll() {
return this.projectRepository.findAll();
}
public ProjectEntity findById(Long id) {
Optional<ProjectEntity> articleEntity = projectRepository.findById(id);
if (articleEntity.isEmpty()) {
throw new ResourceNotFoundException("Project with id " + id + " not found");
}
return articleEntity.get();
}
}

@ -1,5 +1,8 @@
package de.szut.lf8_starter.project;
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.CreateProjectDto;
import de.szut.lf8_starter.project.dto.GetProjectDto;
import io.swagger.v3.oas.annotations.Operation;

@ -1,32 +1,36 @@
package de.szut.lf8_starter.project;
package de.szut.lf8_starter.project.action;
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.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(value = "projects")
public class ProjectController {
public class GetAllProjectsAction {
private final ProjectService service;
private final ProjectMapper projectMapper;
public ProjectController(ProjectService service, ProjectMapper projectMapper) {
public GetAllProjectsAction(ProjectService service, ProjectMapper projectMapper) {
this.service = service;
this.projectMapper = projectMapper;
}
@Operation(summary = "returns all projects")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "all projects",
content = {@Content(mediaType = "application/json",
schema = @Schema(implementation = GetProjectDto.class))}),
@ApiResponse(responseCode = "401", description = "not authorized",
content = @Content)})
@ApiResponse(responseCode = "200", description = "all projects", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = GetProjectDto.class))
}),
@ApiResponse(responseCode = "401", description = "not authorized", content = @Content)})
@GetMapping
public List<GetProjectDto> findAll() {
return this.service

@ -0,0 +1,40 @@
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.*;
@RestController
@RequestMapping(value = "projects")
public class GetProjectAction {
private final ProjectService projectService;
private final ProjectMapper projectMapper;
public GetProjectAction(ProjectService projectService, ProjectMapper projectMapper) {
this.projectService = projectService;
this.projectMapper = projectMapper;
}
@Operation(summary = "Find project by ID")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Project found", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = GetProjectDto.class))
}),
@ApiResponse(responseCode = "404", description = "Project not found", content = @Content)
})
@GetMapping("/{id}")
public ResponseEntity<GetProjectDto> findArticleById(@PathVariable Long id) {
ProjectEntity project = this.projectService.findById(id);
return new ResponseEntity<>(this.projectMapper.mapToGetDto(project), HttpStatus.OK);
}
}

@ -0,0 +1,54 @@
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.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)
class FindProjectActionTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ProjectRepository projectRepository;
@Test
void createProjectTest() 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(get("/projects/1"))
.andExpect(status().isOk())
.andExpect(jsonPath("id").value(1))
.andExpect(jsonPath("comment").value("comment"))
.andExpect(jsonPath("contractor").value(1))
.andExpect(jsonPath("contractor_name").value("contractorName"))
.andExpect(jsonPath("end_date").value("01.01.2024"))
.andExpect(jsonPath("leading_employee").value(1))
.andExpect(jsonPath("name").value("name"))
.andExpect(jsonPath("start_date").value("01.01.2021"))
.andExpect(jsonPath("employees").isArray())
.andExpect(jsonPath("employees", hasSize(3)));
}
}