feat: Implement get project by ID Route #25

Merged
jank merged 5 commits from feature/find-projec into main 2024-10-02 07:04:09 +00:00
5 changed files with 62 additions and 15 deletions
Showing only changes of commit 25ed7e8759 - Show all commits

@ -1,8 +1,10 @@
package de.szut.lf8_starter.project; package de.szut.lf8_starter.project;
import de.szut.lf8_starter.exceptionHandling.ResourceNotFoundException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
import java.util.Optional;
@Service @Service
public class ProjectService { public class ProjectService {
@ -19,4 +21,14 @@ public class ProjectService {
public List<ProjectEntity> readAll() { public List<ProjectEntity> readAll() {
return this.projectRepository.findAll(); 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.CreateProjectDto;
import de.szut.lf8_starter.project.dto.GetProjectDto; import de.szut.lf8_starter.project.dto.GetProjectDto;
import io.swagger.v3.oas.annotations.Operation; 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 de.szut.lf8_starter.project.dto.GetProjectDto;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses; 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; import java.util.List;
@RestController @RestController
@RequestMapping(value = "projects") @RequestMapping(value = "projects")
public class ProjectController { public class GetAllProjectsAction {
private final ProjectService service; private final ProjectService service;
private final ProjectMapper projectMapper; private final ProjectMapper projectMapper;
public ProjectController(ProjectService service, ProjectMapper projectMapper) {
public GetAllProjectsAction(ProjectService service, ProjectMapper projectMapper) {
this.service = service; this.service = service;
this.projectMapper = projectMapper; this.projectMapper = projectMapper;
} }
@Operation(summary = "returns all projects") @Operation(summary = "returns all projects")
@ApiResponses(value = { @ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "all projects", @ApiResponse(responseCode = "200", description = "all projects", content = {
content = {@Content(mediaType = "application/json", @Content(mediaType = "application/json", schema = @Schema(implementation = GetProjectDto.class))
schema = @Schema(implementation = GetProjectDto.class))}), }),
@ApiResponse(responseCode = "401", description = "not authorized", @ApiResponse(responseCode = "401", description = "not authorized", content = @Content)})
content = @Content)})
@GetMapping @GetMapping
public List<GetProjectDto> findAll() { public List<GetProjectDto> findAll() {
return this.service return this.service

@ -0,0 +1,28 @@
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 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;
}
@GetMapping("/{id}")
public ResponseEntity<GetProjectDto> findArticleById(@PathVariable Long id) {
ProjectEntity project = this.projectService.findById(id);
return new ResponseEntity<>(this.projectMapper.mapToGetDto(project), HttpStatus.OK);
}
}