diff --git a/backend/src/main/java/com/happy/friendogly/pet/repository/PetRepository.java b/backend/src/main/java/com/happy/friendogly/pet/repository/PetRepository.java index e477b9815..9bfe22b23 100644 --- a/backend/src/main/java/com/happy/friendogly/pet/repository/PetRepository.java +++ b/backend/src/main/java/com/happy/friendogly/pet/repository/PetRepository.java @@ -35,4 +35,12 @@ default Pet getById(Long id) { boolean existsByMemberId(Long memberId); void deleteAllByMemberId(Long memberId); + + @Query(""" + SELECT p + FROM Pet p + WHERE p.member.id + IN :memberIds + """) + List findAllByMemberIds(List memberIds); } diff --git a/backend/src/main/java/com/happy/friendogly/playground/service/PlaygroundQueryService.java b/backend/src/main/java/com/happy/friendogly/playground/service/PlaygroundQueryService.java index 5c1edbac6..384d0b448 100644 --- a/backend/src/main/java/com/happy/friendogly/playground/service/PlaygroundQueryService.java +++ b/backend/src/main/java/com/happy/friendogly/playground/service/PlaygroundQueryService.java @@ -13,9 +13,12 @@ import com.happy.friendogly.playground.repository.PlaygroundMemberRepository; import com.happy.friendogly.playground.repository.PlaygroundRepository; import java.util.ArrayList; +import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -40,19 +43,24 @@ public PlaygroundQueryService( } public FindPlaygroundDetailResponse findDetail(Long callMemberId, Long playgroundId) { - Playground playground = playgroundRepository.getById(playgroundId); List playgroundMembers = playgroundMemberRepository.findAllByPlaygroundId(playgroundId); int totalPetCount = 0; int arrivedPetCount = 0; - List playgroundPetDetails = new ArrayList<>(); + List memberIds = playgroundMembers.stream() + .map(playgroundMember -> playgroundMember.getMember().getId()) + .toList(); + + Map> memberPets = petRepository.findAllByMemberIds(memberIds).stream() + .collect(Collectors.groupingBy(pet -> pet.getMember().getId())); + List playgroundPetDetails = new ArrayList<>(); for (PlaygroundMember playgroundMember : playgroundMembers) { Member member = playgroundMember.getMember(); boolean isMyPet = member.getId().equals(callMemberId); - List pets = petRepository.findByMemberId(member.getId()); + List pets = memberPets.getOrDefault(member.getId(), Collections.emptyList()); totalPetCount += pets.size(); arrivedPetCount += getArrivedPetCount(playgroundMember, pets); @@ -70,7 +78,7 @@ public FindPlaygroundDetailResponse findDetail(Long callMemberId, Long playgroun .anyMatch(playgroundMember -> playgroundMember.equalsMemberId(callMemberId)); return new FindPlaygroundDetailResponse( - playground.getId(), + playgroundId, totalPetCount, arrivedPetCount, isParticipating, diff --git a/backend/src/test/java/com/happy/friendogly/pet/repository/PetRepositoryTest.java b/backend/src/test/java/com/happy/friendogly/pet/repository/PetRepositoryTest.java new file mode 100644 index 000000000..7d1eb6ada --- /dev/null +++ b/backend/src/test/java/com/happy/friendogly/pet/repository/PetRepositoryTest.java @@ -0,0 +1,64 @@ +package com.happy.friendogly.pet.repository; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +import com.happy.friendogly.member.domain.Member; +import com.happy.friendogly.member.repository.MemberRepository; +import com.happy.friendogly.pet.domain.Gender; +import com.happy.friendogly.pet.domain.Pet; +import com.happy.friendogly.pet.domain.SizeType; +import java.time.LocalDate; +import java.util.List; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; + +@DataJpaTest +class PetRepositoryTest { + + @Autowired + MemberRepository memberRepository; + @Autowired + PetRepository petRepository; + + @DisplayName("다수의 멤버 ID로 멤버들의 펫을 조회할 수 있다.") + @Disabled + @Test + void findAllByMemberIdIn() { + // given + Member savedMember1 = memberRepository.save(new Member("member1", "tag1", "imageUrl1")); + Member savedMember2 = memberRepository.save(new Member("member2", "tag2", "imageUrl2")); + petRepository.save( + new Pet( + savedMember1, + "name1", + "description1", + LocalDate.now(), + SizeType.LARGE, + Gender.FEMALE, + "imageUrl1" + ) + ); + petRepository.save( + new Pet( + savedMember2, + "name2", + "description2", + LocalDate.now(), + SizeType.LARGE, + Gender.FEMALE, + "imageUrl2" + ) + ); + + // when + List pets = petRepository.findAllByMemberIds( + List.of(savedMember1.getId(), savedMember2.getId())); + + // then + assertThat(pets).hasSize(2); + } +} \ No newline at end of file