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 22c88f6..5996d49 100644 --- a/src/main/java/com/feesh/domain/main/controller/MainController.java +++ b/src/main/java/com/feesh/domain/main/controller/MainController.java @@ -47,10 +47,11 @@ public PostListResponse getPopularPosts( @GetMapping("/search") public PostSearchResponse search( - @RequestParam String keyword, + @RequestParam(required = false) String keyword, + @RequestParam(required = false) Category category, @PageableDefault Pageable pageable) { Long userId = SecurityUtil.getCurrentUserIdOrNull(); - return mainService.searchPosts(keyword, pageable, userId); + return mainService.searchPosts(keyword, category, 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 0515b2d..41c2b7e 100644 --- a/src/main/java/com/feesh/domain/main/service/MainService.java +++ b/src/main/java/com/feesh/domain/main/service/MainService.java @@ -58,8 +58,8 @@ public List getCategories() { .toList(); } - public PostSearchResponse searchPosts(String keyword, Pageable pageable, Long userId) { - Page posts = postRepository.searchPostSummariesByTitle(keyword, pageable, userId); + public PostSearchResponse searchPosts(String keyword, Category category, Pageable pageable, Long userId) { + Page posts = postRepository.searchPostSummariesByTitle(keyword, category, pageable, userId); return PostSearchResponse.builder() .posts(posts.getContent()) 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 c5231f4..1206047 100644 --- a/src/main/java/com/feesh/domain/post/repository/PostRepository.java +++ b/src/main/java/com/feesh/domain/post/repository/PostRepository.java @@ -81,6 +81,7 @@ Page findPopularPostSummaries( @Param("category") Category category, Pageable pageable, @Param("userId") Long userId); + @Query(value = """ SELECT new com.feesh.domain.main.dto.PostSummaryResponse( p.id, p.title, p.category, p.price, p.content, @@ -94,15 +95,19 @@ CASE WHEN EXISTS ( ) FROM Post p JOIN p.author author - WHERE p.title LIKE CONCAT('%', :keyword, '%') + WHERE (:keyword IS NULL OR p.title LIKE CONCAT('%', :keyword, '%') OR author.nickname LIKE CONCAT('%', :keyword, '%')) + AND (:category IS NULL OR p.category = :category) ORDER BY p.createdAt DESC """, countQuery = """ - SELECT COUNT(p) FROM Post p - WHERE p.title LIKE CONCAT('%', :keyword, '%') - """) + SELECT COUNT(p) FROM Post p + JOIN p.author author + WHERE (:keyword IS NULL OR p.title LIKE CONCAT('%', :keyword, '%') OR author.nickname LIKE CONCAT('%', :keyword, '%')) + AND (:category IS NULL OR p.category = :category) + """) Page searchPostSummariesByTitle( @Param("keyword") String keyword, + @Param("category") Category category, Pageable pageable, @Param("userId") Long userId); }