From ccee7b44d74e2458c1e6aaa1fe1a6d19c02abc0a Mon Sep 17 00:00:00 2001 From: IsaacAndra Date: Sun, 29 Sep 2024 18:36:40 -0300 Subject: [PATCH] fix: Encontrando Posts e Projetos pelo Titulo/Nome --- .../isaacandrade/blog/controller/PostController.java | 10 +++++----- .../blog/controller/ProjectController.java | 10 +++++----- .../isaacandrade/blog/domain/post/PostRepository.java | 3 +++ .../blog/domain/projects/ProjectRepository.java | 3 +++ .../com/isaacandrade/blog/service/PostService.java | 7 +++++-- .../com/isaacandrade/blog/service/ProjectService.java | 10 ++++++---- 6 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/isaacandrade/blog/controller/PostController.java b/src/main/java/com/isaacandrade/blog/controller/PostController.java index 8d353fd..15417e5 100644 --- a/src/main/java/com/isaacandrade/blog/controller/PostController.java +++ b/src/main/java/com/isaacandrade/blog/controller/PostController.java @@ -83,10 +83,10 @@ public ResponseEntity getPostsByAuthor(@PathVariable Long au @GetMapping( - value = "/{id}", + value = "/{title}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_YML} ) - @Operation(summary = "Encontre o Post pelo Id", description = "Encontre o Post pelo Id", + @Operation(summary = "Encontre o Post pelo Titulo", description = "Encontre o Post pelo Titulo", tags = {"Posts"}, responses = { @ApiResponse(description = "Success", responseCode = "200", @@ -99,9 +99,9 @@ public ResponseEntity getPostsByAuthor(@PathVariable Long au @ApiResponse(description = "Internal Error", responseCode = "500", content = @Content), } ) - public ResponseEntity findPostById(@PathVariable Long id){ - PostDTO postsById = postService.findById(id); - return ResponseEntity.ok(postsById); + public ResponseEntity findPostByTitle(@PathVariable String title){ + PostDTO postsByTitle = postService.findByTitle(title); + return ResponseEntity.ok(postsByTitle); } diff --git a/src/main/java/com/isaacandrade/blog/controller/ProjectController.java b/src/main/java/com/isaacandrade/blog/controller/ProjectController.java index b5a5652..79cae1f 100644 --- a/src/main/java/com/isaacandrade/blog/controller/ProjectController.java +++ b/src/main/java/com/isaacandrade/blog/controller/ProjectController.java @@ -61,10 +61,10 @@ public ResponseEntity> getAllProjects(){ @GetMapping( - value = "/{id}", + value = "/{name}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_YML} ) - @Operation(summary = "Encontre o Projeto pelo Id", description = "Encontre o Usuário pelo Id", + @Operation(summary = "Encontre o Projeto pelo nome", description = "Encontre o Usuário pelo nome", tags = {"Projetos"}, responses = { @ApiResponse(description = "Success", responseCode = "200", @@ -77,10 +77,10 @@ public ResponseEntity> getAllProjects(){ @ApiResponse(description = "Internal Error", responseCode = "500", content = @Content), } ) - public ResponseEntity getProjectById(@PathVariable Long id) { - ProjectDto projectById = projectService.getProjectById(id); + public ResponseEntity getProjectByName(@PathVariable String name) { + ProjectDto projectByName = projectService.getProjectByName(name); - return ResponseEntity.ok(projectById); + return ResponseEntity.ok(projectByName); } diff --git a/src/main/java/com/isaacandrade/blog/domain/post/PostRepository.java b/src/main/java/com/isaacandrade/blog/domain/post/PostRepository.java index 8db9bcb..dfc5996 100644 --- a/src/main/java/com/isaacandrade/blog/domain/post/PostRepository.java +++ b/src/main/java/com/isaacandrade/blog/domain/post/PostRepository.java @@ -3,8 +3,11 @@ import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; +import java.util.Optional; public interface PostRepository extends JpaRepository { List findByIsActiveTrue(); List findPostsByAuthorId(Long authorId); + + Post findByTitle(String title); } diff --git a/src/main/java/com/isaacandrade/blog/domain/projects/ProjectRepository.java b/src/main/java/com/isaacandrade/blog/domain/projects/ProjectRepository.java index 68cbcf5..17becb4 100644 --- a/src/main/java/com/isaacandrade/blog/domain/projects/ProjectRepository.java +++ b/src/main/java/com/isaacandrade/blog/domain/projects/ProjectRepository.java @@ -2,5 +2,8 @@ import org.springframework.data.jpa.repository.JpaRepository; +import java.util.Optional; + public interface ProjectRepository extends JpaRepository { + Project findByName(String name); } diff --git a/src/main/java/com/isaacandrade/blog/service/PostService.java b/src/main/java/com/isaacandrade/blog/service/PostService.java index 649984b..ba73901 100644 --- a/src/main/java/com/isaacandrade/blog/service/PostService.java +++ b/src/main/java/com/isaacandrade/blog/service/PostService.java @@ -47,8 +47,11 @@ public AuthorWithPostsDTO findPostsWithAuthorId(Long authorId){ return new AuthorWithPostsDTO(author, postDTOs); } - public PostDTO findById(Long id){ - Post posts = postRepository.findById(id).orElseThrow(() -> new PostNotFoundException("Post With Id " + id + " Was Not Found")); + public PostDTO findByTitle(String title){ + Post posts = postRepository.findByTitle(title); + if (posts.getTitle().isEmpty()) { + throw new PostNotFoundException("Post With Title" + title + "Was Not Found"); + } return mapToPostDTO(posts); } diff --git a/src/main/java/com/isaacandrade/blog/service/ProjectService.java b/src/main/java/com/isaacandrade/blog/service/ProjectService.java index 5d2e36b..bb6d043 100644 --- a/src/main/java/com/isaacandrade/blog/service/ProjectService.java +++ b/src/main/java/com/isaacandrade/blog/service/ProjectService.java @@ -35,10 +35,12 @@ public List getAllProjects() { } // Metodo para mostrar um projeto pelo id - public ProjectDto getProjectById(Long id) { - Project project = projectRepository.findById(id).orElseThrow( - () -> new ProjectNotFoundException("Project With id " + id + " Was Not Found!") - ); + public ProjectDto getProjectByName(String name) { + Project project = projectRepository.findByName(name); + + if (project.getName().isEmpty()) { + throw new ProjectNotFoundException("Project with name " + name + " Was Not Found!"); + } return mapToProjectDTO(project); }