From 3d920f89f609e981fdb5c25c4ad118750daf01c3 Mon Sep 17 00:00:00 2001 From: chaen-ing Date: Sun, 3 May 2026 18:05:38 +0900 Subject: [PATCH] =?UTF-8?q?[SCRUM-447]=20refactor=20:=20=EA=B2=80=EC=83=89?= =?UTF-8?q?=20=EB=A1=9C=EC=A7=81=20like=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CommunityPostRepository.java | 4 ++ .../community/service/CommunityService.java | 53 ++++++++++--------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/kkinikong/be/community/repository/communityPost/CommunityPostRepository.java b/src/main/java/com/kkinikong/be/community/repository/communityPost/CommunityPostRepository.java index 765b9fe..0511227 100644 --- a/src/main/java/com/kkinikong/be/community/repository/communityPost/CommunityPostRepository.java +++ b/src/main/java/com/kkinikong/be/community/repository/communityPost/CommunityPostRepository.java @@ -37,6 +37,10 @@ ORDER BY COUNT(cpl.id) DESC, cp.viewCount DESC @EntityGraph(attributePaths = {"communityPostImageList"}) Page findAll(Pageable pageable); + @EntityGraph(attributePaths = {"communityPostImageList"}) + Page findByTitleContainingOrContentContaining( + String titleKeyword, String contentKeyword, Pageable pageable); + Page findAllByUserIdOrderByCreatedDateDesc(Long userId, Pageable pageable); @Modifying(clearAutomatically = true) diff --git a/src/main/java/com/kkinikong/be/community/service/CommunityService.java b/src/main/java/com/kkinikong/be/community/service/CommunityService.java index c1f8fd6..96f1e02 100644 --- a/src/main/java/com/kkinikong/be/community/service/CommunityService.java +++ b/src/main/java/com/kkinikong/be/community/service/CommunityService.java @@ -5,13 +5,11 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.Optional; import java.util.stream.Collectors; import org.springframework.cache.annotation.Cacheable; import org.springframework.context.ApplicationEventPublisher; import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; @@ -258,30 +256,35 @@ public Page searchCommunityPost( } // Elasticsearch에서 검색어로 커뮤니티 게시글 페이징해서 가져옴 - Pageable pageable = PageRequest.of(page, size); - List communitySearchResponses = - openSearchService.searchCommunityPost(keyword, page, size); + // Pageable pageable = PageRequest.of(page, size); + // List communitySearchResponses = + // openSearchService.searchCommunityPost(keyword, page, size); + // + // if (communitySearchResponses.isEmpty()) { + // return new PageImpl<>(List.of(), pageable, 0); + // } + // + // Map postMap = + // communityPostRepository.findByIdIn(communitySearchResponses).stream() + // .collect(Collectors.toMap(CommunityPost::getId, post -> post)); + // + // // 순서 유지: 검색 결과에 있는 ID 순서대로 매핑 + // List results = + // communitySearchResponses.stream() + // .map( + // id -> + // Optional.ofNullable(postMap.get(id)) + // .map(CommunityPostListResponse::from) + // .orElse(null)) + // .filter(Objects::nonNull) + // .toList(); + // + // return new PageImpl<>(results, pageable, communitySearchResponses.size()); - if (communitySearchResponses.isEmpty()) { - return new PageImpl<>(List.of(), pageable, 0); - } - - Map postMap = - communityPostRepository.findByIdIn(communitySearchResponses).stream() - .collect(Collectors.toMap(CommunityPost::getId, post -> post)); - - // 순서 유지: 검색 결과에 있는 ID 순서대로 매핑 - List results = - communitySearchResponses.stream() - .map( - id -> - Optional.ofNullable(postMap.get(id)) - .map(CommunityPostListResponse::from) - .orElse(null)) - .filter(Objects::nonNull) - .toList(); - - return new PageImpl<>(results, pageable, communitySearchResponses.size()); + Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdDate")); + return communityPostRepository + .findByTitleContainingOrContentContaining(keyword, keyword, pageable) + .map(CommunityPostListResponse::from); } public List getRecentSearchKeywords(Long userId) {