Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] refactor: 놀이터 상세조회 쿼리 반복 호출 개선 #791

Merged
merged 2 commits into from
Jan 22, 2025
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 @@ -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<Pet> findAllByMemberIds(List<Long> memberIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -40,19 +43,24 @@ public PlaygroundQueryService(
}

public FindPlaygroundDetailResponse findDetail(Long callMemberId, Long playgroundId) {
Playground playground = playgroundRepository.getById(playgroundId);
List<PlaygroundMember> playgroundMembers = playgroundMemberRepository.findAllByPlaygroundId(playgroundId);

int totalPetCount = 0;
int arrivedPetCount = 0;

List<PlaygroundPetDetail> playgroundPetDetails = new ArrayList<>();
List<Long> memberIds = playgroundMembers.stream()
.map(playgroundMember -> playgroundMember.getMember().getId())
.toList();

Map<Long, List<Pet>> memberPets = petRepository.findAllByMemberIds(memberIds).stream()
.collect(Collectors.groupingBy(pet -> pet.getMember().getId()));

List<PlaygroundPetDetail> playgroundPetDetails = new ArrayList<>();
for (PlaygroundMember playgroundMember : playgroundMembers) {
Member member = playgroundMember.getMember();
boolean isMyPet = member.getId().equals(callMemberId);

List<Pet> pets = petRepository.findByMemberId(member.getId());
List<Pet> pets = memberPets.getOrDefault(member.getId(), Collections.emptyList());
totalPetCount += pets.size();
arrivedPetCount += getArrivedPetCount(playgroundMember, pets);

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Pet> pets = petRepository.findAllByMemberIds(
List.of(savedMember1.getId(), savedMember2.getId()));

// then
assertThat(pets).hasSize(2);
}
}
Loading