findByTitle (query methods)

Spring Query Methods

Controller

@GetMapping("/titlesearch")
public ResponseEntity<List<PostDTO>> searchByTitle(@RequestParam("text") String text) {
    List<PostDTO> posts = postService.searchByTitle(text);
    if (posts.isEmpty()) {
        return ResponseEntity.notFound().build();
    }
    return ResponseEntity.ok(posts);
}

Service

public List<PostDTO> searchByTitle(String text) {
    List<Post> posts = postRepository.findByTitleContaingIgnoreCase(text);

    return posts.stream().map(PostDTO::new).toList();
}

Repository

@Repository
public interface PostRepository extends MongoRepository<Post, String> {
    List<Post> findByTitleContaingIgnoreCase(String text);
}

Atualizado