diff --git a/src/main/java/com/feesh/domain/main/controller/MainController.java b/src/main/java/com/feesh/domain/main/controller/MainController.java index 1a10867..22c88f6 100644 --- a/src/main/java/com/feesh/domain/main/controller/MainController.java +++ b/src/main/java/com/feesh/domain/main/controller/MainController.java @@ -49,7 +49,8 @@ public PostListResponse getPopularPosts( public PostSearchResponse search( @RequestParam String keyword, @PageableDefault Pageable pageable) { - return mainService.searchPosts(keyword, pageable); + Long userId = SecurityUtil.getCurrentUserIdOrNull(); + return mainService.searchPosts(keyword, pageable, userId); } @GetMapping("/categories") diff --git a/src/main/java/com/feesh/domain/main/service/MainService.java b/src/main/java/com/feesh/domain/main/service/MainService.java index 5531c4a..0515b2d 100644 --- a/src/main/java/com/feesh/domain/main/service/MainService.java +++ b/src/main/java/com/feesh/domain/main/service/MainService.java @@ -58,33 +58,11 @@ public List getCategories() { .toList(); } - public PostSearchResponse searchPosts(String keyword, Pageable pageable) { - Page posts = postRepository.findByTitleContaining(keyword, pageable); - - List summaries = posts.getContent().stream() - .map(post -> PostSummaryResponse.builder() - .id(post.getId()) - .title(post.getTitle()) - .build()) - .toList(); + public PostSearchResponse searchPosts(String keyword, Pageable pageable, Long userId) { + Page posts = postRepository.searchPostSummariesByTitle(keyword, pageable, userId); return PostSearchResponse.builder() - .posts(summaries) - .totalPages(posts.getTotalPages()) - .totalElements(posts.getTotalElements()) - .build(); - } - - private PostListResponse toPostListResponse(Page posts) { - List summaries = posts.getContent().stream() - .map(post -> PostSummaryResponse.builder() - .id(post.getId()) - .title(post.getTitle()) - .build()) - .toList(); - - return PostListResponse.builder() - .posts(summaries) + .posts(posts.getContent()) .totalPages(posts.getTotalPages()) .totalElements(posts.getTotalElements()) .build(); diff --git a/src/main/java/com/feesh/domain/post/repository/PostRepository.java b/src/main/java/com/feesh/domain/post/repository/PostRepository.java index e302b73..c5231f4 100644 --- a/src/main/java/com/feesh/domain/post/repository/PostRepository.java +++ b/src/main/java/com/feesh/domain/post/repository/PostRepository.java @@ -11,12 +11,6 @@ public interface PostRepository extends JpaRepository { - Page findAllByOrderByCreatedAtDesc(Pageable pageable); - - Page findAllByOrderByLikeCountDesc(Pageable pageable); - - Page findByTitleContaining(String keyword, Pageable pageable); - Page findByAuthorId(Long authorId, Pageable pageable); void deleteAllByAuthor_Id(Long authorId); @@ -87,4 +81,28 @@ Page findPopularPostSummaries( @Param("category") Category category, Pageable pageable, @Param("userId") Long userId); -} \ No newline at end of file + @Query(value = """ + SELECT new com.feesh.domain.main.dto.PostSummaryResponse( + p.id, p.title, p.category, p.price, p.content, + author.nickname, author.profileImageUrl, p.likeCount, + (SELECT COUNT(c) FROM Comment c WHERE c.post = p AND c.isDeleted = false), + p.createdAt, + CASE WHEN EXISTS ( + SELECT 1 FROM PostLike pl WHERE pl.post = p AND pl.user.id = :userId + ) THEN true ELSE false END, + p.viewCount + ) + FROM Post p + JOIN p.author author + WHERE p.title LIKE CONCAT('%', :keyword, '%') + ORDER BY p.createdAt DESC + """, + countQuery = """ + SELECT COUNT(p) FROM Post p + WHERE p.title LIKE CONCAT('%', :keyword, '%') + """) + Page searchPostSummariesByTitle( + @Param("keyword") String keyword, + Pageable pageable, + @Param("userId") Long userId); +}