From 4bdda28536553ac3689f528841a326b33c4617f9 Mon Sep 17 00:00:00 2001 From: Jeong Jiho Date: Wed, 23 Oct 2024 11:50:40 +0900 Subject: [PATCH] =?UTF-8?q?[BE]=20fix:=20=EC=B0=B8=EC=97=AC=20=EA=B0=80?= =?UTF-8?q?=EB=8A=A5=20=EB=AA=A8=EC=9E=84=20=EC=A1=B0=ED=9A=8C=20=EC=8B=9C?= =?UTF-8?q?=20=EB=AC=B4=ED=95=9C=20=EB=A3=A8=ED=94=84=20=ED=98=84=EC=83=81?= =?UTF-8?q?=20=EC=88=98=EC=A0=95=20(#695)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../club/service/ClubQueryService.java | 12 ++--- .../club/service/ClubQueryServiceTest.java | 47 +++++++++++++++++++ 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/backend/src/main/java/com/happy/friendogly/club/service/ClubQueryService.java b/backend/src/main/java/com/happy/friendogly/club/service/ClubQueryService.java index 1003f32ec..5f28845d8 100644 --- a/backend/src/main/java/com/happy/friendogly/club/service/ClubQueryService.java +++ b/backend/src/main/java/com/happy/friendogly/club/service/ClubQueryService.java @@ -60,8 +60,8 @@ public FindClubPageByFilterResponse findByFilter(Long memberId, FindClubByFilter addFilteredResults(request, filteredClubs, result); while (result.size() < request.pageSize() && clubSlice.hasNext()) { - lastFoundCreatedAt = updateLastFoundCreatedAt(lastFoundCreatedAt, result); - lastFoundId = updateLastFoundId(lastFoundId, result); + lastFoundCreatedAt = updateLastFoundCreatedAt(clubSlice, result); + lastFoundId = updateLastFoundId(clubSlice, result); clubSlice = fetchClubSlice(request, lastFoundCreatedAt, lastFoundId); filteredClubs = filterClubs(clubSlice, filterCondition, member, pets); @@ -118,16 +118,16 @@ private void addFilteredResults(FindClubByFilterRequest request, List filt } } - private LocalDateTime updateLastFoundCreatedAt(LocalDateTime lastFoundCreatedAt, List result) { + private LocalDateTime updateLastFoundCreatedAt(Slice clubSlice, List result) { if (result.isEmpty()) { - return lastFoundCreatedAt; + return clubSlice.getContent().get(clubSlice.getSize() - 1).getCreatedAt(); } return result.get(result.size() - 1).getCreatedAt(); } - private Long updateLastFoundId(Long lastFoundId, List result) { + private Long updateLastFoundId(Slice clubSlice, List result) { if (result.isEmpty()) { - return lastFoundId; + return clubSlice.getContent().get(clubSlice.getSize() - 1).getId(); } return result.get(result.size() - 1).getId(); } diff --git a/backend/src/test/java/com/happy/friendogly/club/service/ClubQueryServiceTest.java b/backend/src/test/java/com/happy/friendogly/club/service/ClubQueryServiceTest.java index c99c556d2..f1992f3a7 100644 --- a/backend/src/test/java/com/happy/friendogly/club/service/ClubQueryServiceTest.java +++ b/backend/src/test/java/com/happy/friendogly/club/service/ClubQueryServiceTest.java @@ -17,9 +17,13 @@ import com.happy.friendogly.pet.domain.Pet; import com.happy.friendogly.pet.domain.SizeType; import jakarta.transaction.Transactional; +import java.time.LocalDate; import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -139,6 +143,49 @@ void findSearching_Nothing() { assertThat(responses.content().isEmpty()).isTrue(); } + @DisplayName("첫 페이지에 필터링 조건에 맞는 모임이 존재하지 않는 경우에도 다음 페이지를 읽어온다.") + @Test + void findSearching_NoResultOnFirstPage_MoveToNextPage() { + List clubs = new ArrayList<>(); + clubs.add(createSavedClub( // member가 참여 불가능한 모임 + savedMember, + List.of(savedPet), + Set.of(Gender.FEMALE), + Set.of(SizeType.SMALL) + )); + clubs.add(createSavedClub( // member가 참여 불가능한 모임 + savedMember, + List.of(savedPet), + Set.of(Gender.FEMALE), + Set.of(SizeType.SMALL) + )); + clubs.add(createSavedClub( // member가 참여 가능한 모임 + savedMember, + List.of(savedPet), + Set.of(Gender.values()), + Set.of(SizeType.values()) + )); + + Member member = memberRepository.save(new Member("member", "sdfdfef3", "imageUrl")); + Pet pet = petRepository.save( + new Pet(member, "뚱땡이", "수컷 대형견", LocalDate.now().minusDays(1L), SizeType.LARGE, Gender.MALE, "imageUrl")); + + FindClubByFilterRequest request = new FindClubByFilterRequest( + FilterCondition.ABLE_TO_JOIN.name(), + province, + city, + village, + Arrays.stream(Gender.values()).map(gender -> gender.name()).collect(Collectors.toSet()), + Arrays.stream(SizeType.values()).map(sizeType -> sizeType.name()).collect(Collectors.toSet()), + 20, + LocalDateTime.of(9999, 12, 31, 23, 59, 59), + 999999999L + ); + + FindClubPageByFilterResponse responses = clubQueryService.findByFilter(member.getId(), request); + assertThat(responses.content()).hasSize(1); + } + @DisplayName("페이지 사이즈보다 더 많은 모임이 존재하는 경우, 한번에 페이지 사이즈 만큼의 모임이 조회된다.") @Test void findSearching_MoreThanPageSize() {