Skip to content

Commit

Permalink
fix: Encontrando Posts e Projetos pelo Titulo/Nome
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacAndra committed Sep 29, 2024
1 parent 5bf5d4a commit ccee7b4
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ public ResponseEntity<AuthorWithPostsDTO> 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",
Expand All @@ -99,9 +99,9 @@ public ResponseEntity<AuthorWithPostsDTO> getPostsByAuthor(@PathVariable Long au
@ApiResponse(description = "Internal Error", responseCode = "500", content = @Content),
}
)
public ResponseEntity<PostDTO> findPostById(@PathVariable Long id){
PostDTO postsById = postService.findById(id);
return ResponseEntity.ok(postsById);
public ResponseEntity<PostDTO> findPostByTitle(@PathVariable String title){
PostDTO postsByTitle = postService.findByTitle(title);
return ResponseEntity.ok(postsByTitle);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ public ResponseEntity<List<ProjectDto>> 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",
Expand All @@ -77,10 +77,10 @@ public ResponseEntity<List<ProjectDto>> getAllProjects(){
@ApiResponse(description = "Internal Error", responseCode = "500", content = @Content),
}
)
public ResponseEntity<ProjectDto> getProjectById(@PathVariable Long id) {
ProjectDto projectById = projectService.getProjectById(id);
public ResponseEntity<ProjectDto> getProjectByName(@PathVariable String name) {
ProjectDto projectByName = projectService.getProjectByName(name);

return ResponseEntity.ok(projectById);
return ResponseEntity.ok(projectByName);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface PostRepository extends JpaRepository<Post, Long> {
List<Post> findByIsActiveTrue();
List<Post> findPostsByAuthorId(Long authorId);

Post findByTitle(String title);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface ProjectRepository extends JpaRepository<Project, Long> {
Project findByName(String name);
}
7 changes: 5 additions & 2 deletions src/main/java/com/isaacandrade/blog/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/isaacandrade/blog/service/ProjectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ public List<ProjectDto> 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);
}
Expand Down

0 comments on commit ccee7b4

Please sign in to comment.