Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/feesh/domain/main/service/MainService.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public List<CategoryResponse> getCategories() {
.toList();
}

public PostSearchResponse searchPosts(String keyword, Pageable pageable, Long userId) {
Page<PostSummaryResponse> posts = postRepository.searchPostSummariesByTitle(keyword, pageable, userId);
public PostSearchResponse searchPosts(String keyword, Category category, Pageable pageable, Long userId) {
Page<PostSummaryResponse> posts = postRepository.searchPostSummariesByTitle(keyword, category, pageable, userId);

return PostSearchResponse.builder()
.posts(posts.getContent())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Page<PostSummaryResponse> 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,
Expand All @@ -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<PostSummaryResponse> searchPostSummariesByTitle(
@Param("keyword") String keyword,
@Param("category") Category category,
Pageable pageable,
@Param("userId") Long userId);
}
Loading