Skip to content

Commit

Permalink
Merge pull request #798 from woowacourse-teams/refactor/#797
Browse files Browse the repository at this point in the history
쿼리 추가 발생 문제 해결
  • Loading branch information
ay-eonii authored Nov 25, 2024
2 parents 0a37cd5 + ae7ebf6 commit e77f98a
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package mouda.backend.darakbang.infrastructure;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import mouda.backend.darakbang.domain.Darakbang;
Expand All @@ -15,4 +17,11 @@ public interface DarakbangRepository extends JpaRepository<Darakbang, Long> {
boolean existsByCode(String code);

Optional<Darakbang> findByCode(String code);

@Query("""
SELECT d
FROM Darakbang d
WHERE d.id IN :darakbangIds
""")
List<Darakbang> findAllByIds(List<Long> darakbangIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import lombok.RequiredArgsConstructor;
import mouda.backend.darakbang.domain.Darakbang;
import mouda.backend.darakbang.infrastructure.DarakbangRepository;
import mouda.backend.darakbangmember.domain.DarakBangMemberRole;
import mouda.backend.darakbangmember.domain.DarakbangMember;
import mouda.backend.darakbangmember.domain.DarakbangMembers;
Expand All @@ -21,6 +22,7 @@
public class DarakbangMemberFinder {

private final DarakbangMemberRepository darakbangMemberRepository;
private final DarakbangRepository darakbangRepository;

public DarakbangMember find(Darakbang darakbang, Member member) {
return darakbangMemberRepository.findByDarakbangIdAndMemberId(darakbang.getId(), member.getId())
Expand All @@ -29,10 +31,8 @@ public DarakbangMember find(Darakbang darakbang, Member member) {
}

public List<Darakbang> findAllByMember(Member member) {
return darakbangMemberRepository.findAllByMemberId(member.getId())
.stream()
.map(DarakbangMember::getDarakbang)
.toList();
List<Long> darakbangIds = darakbangMemberRepository.findAllDarakbangIdsByMemberId(member.getId());
return darakbangRepository.findAllByIds(darakbangIds);
}

public DarakbangMembers findAllDarakbangMembers(Long darakbangId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import mouda.backend.darakbangmember.domain.DarakbangMember;

@Repository
public interface DarakbangMemberRepository extends JpaRepository<DarakbangMember, Long> {

List<DarakbangMember> findAllByMemberId(long memberId);
@Query("""
SELECT dm.darakbang.id
FROM DarakbangMember dm
WHERE dm.memberId = :memberId
""")
List<Long> findAllDarakbangIdsByMemberId(long memberId);

Optional<DarakbangMember> findByDarakbangIdAndMemberId(Long darakbangId, Long id);

Expand Down
3 changes: 2 additions & 1 deletion backend/src/main/java/mouda/backend/moim/domain/Moim.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import mouda.backend.moim.exception.MoimErrorMessage;
Expand All @@ -25,6 +26,7 @@
@Table(name = "moim")
@Getter
@NoArgsConstructor
@EqualsAndHashCode
public class Moim {

private static final int TITLE_MAX_LENGTH = 30;
Expand Down Expand Up @@ -215,7 +217,6 @@ public boolean isCompleted() {
return moimStatus == MoimStatus.COMPLETED;
}


public boolean isMoiming() {
return moimStatus == MoimStatus.MOIMING;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mouda.backend.moim.domain;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;

@Getter
@EqualsAndHashCode
@AllArgsConstructor
public class MoimCurrentPeople {
private final long moimId;
private final long currentPeople;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public class MoimOverview {
private final int currentPeople;
private final boolean isZzimed;

public MoimOverview(Moim moim, int currentPeople, boolean isZzimed) {
public MoimOverview(Moim moim, long currentPeople, boolean isZzimed) {
this.moim = moim;
this.currentPeople = currentPeople;
this.currentPeople = (int)currentPeople;
this.isZzimed = isZzimed;
}
}
5 changes: 3 additions & 2 deletions backend/src/main/java/mouda/backend/moim/domain/Zzim.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mouda.backend.moim.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
Expand All @@ -22,11 +23,11 @@ public class Zzim {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false)
private Moim moim;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false)
private DarakbangMember darakbangMember;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import mouda.backend.moim.infrastructure.dto.ChamyoCountResponse;
import mouda.backend.moim.presentation.response.moim.MoimFindAllResponse;
import mouda.backend.moim.presentation.response.moim.MoimFindAllResponses;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

Expand All @@ -17,15 +16,14 @@
import mouda.backend.moim.domain.Chamyo;
import mouda.backend.moim.domain.FilterType;
import mouda.backend.moim.domain.Moim;
import mouda.backend.moim.domain.MoimCurrentPeople;
import mouda.backend.moim.domain.MoimOverview;
import mouda.backend.moim.exception.MoimErrorMessage;
import mouda.backend.moim.exception.MoimException;
import mouda.backend.moim.infrastructure.ChamyoRepository;
import mouda.backend.moim.infrastructure.MoimRepository;
import mouda.backend.moim.infrastructure.ZzimRepository;

import static java.util.stream.Collectors.toMap;

@Component
@RequiredArgsConstructor
public class MoimFinder {
Expand All @@ -34,6 +32,9 @@ public class MoimFinder {
private final ChamyoRepository chamyoRepository;
private final ZzimFinder zzimFinder;
private final ZzimRepository zzimRepository;
private final ChatRoomRepository chatRoomRepository;
private final ChatRoomFinder chatRoomFinder;
private final ChamyoFinder chamyoFinder;

public Moim read(long moimId, long currentDarakbangId) {
return moimRepository.findByIdAndDarakbangId(moimId, currentDarakbangId)
Expand All @@ -42,29 +43,25 @@ public Moim read(long moimId, long currentDarakbangId) {

public List<MoimOverview> readAll(long darakbangId, DarakbangMember darakbangMember) {
List<Moim> moims = moimRepository.findAllByDarakbangIdOrderByIdDesc(darakbangId);
List<Long> moimIds = moims.stream()
.map(Moim::getId)
.toList();
Map<Long, Integer> counts = chamyoRepository.countByMoimIds(moimIds)
.stream()
.collect(toMap(ChamyoCountResponse::getMoimId, ChamyoCountResponse::getCount));
List<Long> zzims = zzimRepository.findZzimedMoimByMoimIdsAndDarakbangMemberId(moimIds, darakbangMember.getId());
List<MoimCurrentPeople> moimCurrentPeople = chamyoRepository.findAllByMoims(moims);
Set<Long> zzimedMoimIds = zzimRepository.findMoimIdsByDarakbangMemberId(darakbangMember.getId());

return moims.stream()
.map(moim -> {
int currentPeople = counts.getOrDefault(moim.getId(), 0);
boolean isZzimed = zzims.contains(moim.getId());
return new MoimOverview(moim, currentPeople, isZzimed);
})
.toList();
return createMoimOverview(moims, moimCurrentPeople, zzimedMoimIds);
}

public List<MoimOverview> readAllMyMoim(DarakbangMember darakbangMember, FilterType filterType) {
return chamyoRepository.findAllByDarakbangMemberIdOrderByIdDesc(darakbangMember.getId()).stream()
.map(Chamyo::getMoim)
List<MoimCurrentPeople> moimCurrentPeople = chamyoRepository.findAllByDarakbangMemberId(
darakbangMember.getId());
Set<Long> moimIds = moimCurrentPeople.stream()
.map(MoimCurrentPeople::getMoimId)
.collect(Collectors.toSet());
List<Moim> moims = moimRepository.findAllByIds(moimIds)
.stream()
.filter(getFilter(filterType))
.map(moim -> createMoimOverview(moim, darakbangMember))
.toList();
Set<Long> zzimedMoimIds = zzimRepository.findMoimIdsByDarakbangMemberId(darakbangMember.getId());

return createMoimOverview(moims, moimCurrentPeople, zzimedMoimIds);
}

private Predicate<Moim> getFilter(FilterType filterType) {
Expand All @@ -78,16 +75,26 @@ private Predicate<Moim> getFilter(FilterType filterType) {
}

public List<MoimOverview> readAllZzimedMoim(DarakbangMember darakbangMember) {
return zzimRepository.findAllByDarakbangMemberIdOrderByIdDesc(darakbangMember.getId()).stream()
.map(zzim -> createMoimOverview(zzim.getMoim(), darakbangMember))
.toList();
Set<Long> zzimMoimIds = zzimRepository.findMoimIdsByDarakbangMemberId(darakbangMember.getId());
List<Moim> moims = moimRepository.findAllByIds(zzimMoimIds);
List<MoimCurrentPeople> moimCurrentPeople = chamyoRepository.findAllByMoims(moims);

return createMoimOverview(moims, moimCurrentPeople, zzimMoimIds);
}

private MoimOverview createMoimOverview(Moim moim, DarakbangMember darakbangMember) {
int currentPeople = countCurrentPeople(moim);
boolean isZzimed = zzimFinder.isMoimZzimedByMember(moim.getId(), darakbangMember);
private List<MoimOverview> createMoimOverview(List<Moim> moims, List<MoimCurrentPeople> moimCurrentPeople,
Set<Long> zzimedMoimIds) {
Map<Long, Long> currentPeople = moimCurrentPeople.stream()
.collect(Collectors.toMap(MoimCurrentPeople::getMoimId, MoimCurrentPeople::getCurrentPeople));

return new MoimOverview(moim, currentPeople, isZzimed);
return moims.stream()
.filter(moim -> currentPeople.get(moim.getId()) != null)
.map(moim -> new MoimOverview(
moim,
currentPeople.get(moim.getId()),
zzimedMoimIds.contains(moim.getId())
))
.toList();
}

public int countCurrentPeople(Moim moim) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package mouda.backend.moim.infrastructure;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

import mouda.backend.moim.infrastructure.dto.ChamyoCountResponse;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import mouda.backend.moim.domain.Chamyo;
import mouda.backend.moim.domain.Moim;
import mouda.backend.moim.domain.MoimCurrentPeople;

public interface ChamyoRepository extends JpaRepository<Chamyo, Long> {

Expand Down Expand Up @@ -39,7 +38,29 @@ public interface ChamyoRepository extends JpaRepository<Chamyo, Long> {
@Query("SELECT c FROM Chamyo c WHERE c.moim.id = :moimId AND c.moimRole = 'MOIMER'")
Optional<Chamyo> findMoimerByMoimId(@Param("moimId") Long moimId);

@Query("SELECT new mouda.backend.moim.infrastructure.dto.ChamyoCountResponse(c.moim.id, COUNT(c)) FROM Chamyo c WHERE c.moim.id IN :moimIds GROUP BY c.moim.id")
List<ChamyoCountResponse> countByMoimIds(@Param("moimIds") List<Long> moimIds);

@Query("""
SELECT new mouda.backend.moim.domain.MoimCurrentPeople(
c.moim.id,
(SELECT COUNT(c2)
FROM Chamyo c2
WHERE c2.moim = c.moim)
)
FROM Chamyo c
WHERE c.darakbangMember.id = :darakbangMemberId
GROUP BY c.moim
ORDER BY c.moim.id DESC
""")
List<MoimCurrentPeople> findAllByDarakbangMemberId(long darakbangMemberId);

@Query("""
SELECT new mouda.backend.moim.domain.MoimCurrentPeople(
c.moim.id,
COUNT(c)
)
FROM Chamyo c
WHERE c.moim IN :moims
GROUP BY c.moim
ORDER BY c.moim.id DESC
""")
List<MoimCurrentPeople> findAllByMoims(List<Moim> moims);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import mouda.backend.darakbang.domain.Darakbang;
import mouda.backend.moim.domain.Moim;
import mouda.backend.moim.domain.MoimStatus;

Expand All @@ -33,4 +33,11 @@ public interface MoimRepository extends JpaRepository<Moim, Long> {

boolean existsByIdAndDarakbangId(Long moimId, Long darakbangId);

@Query("""
SELECT m
FROM Moim m
WHERE m.id IN :moimIds
ORDER BY m.id DESC
""")
List<Moim> findAllByIds(Set<Long> moimIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import mouda.backend.moim.domain.Zzim;
import org.springframework.data.jpa.repository.Query;

public interface ZzimRepository extends JpaRepository<Zzim, Long> {

Expand All @@ -16,6 +17,12 @@ public interface ZzimRepository extends JpaRepository<Zzim, Long> {

List<Zzim> findAllByDarakbangMemberIdOrderByIdDesc(Long darakbangMemberId);

@Query("SELECT z.moim.id FROM Zzim z WHERE z.darakbangMember.id = :darakbangMemberId AND z.moim.id IN :moimIds")
List<Long> findZzimedMoimByMoimIdsAndDarakbangMemberId(List<Long> moimIds, Long darakbangMemberId);
@Query("""
SELECT
z.moim.id
FROM Zzim z
WHERE z.darakbangMember.id = :darakbangMemberId
ORDER BY z.id DESC
""")
Set<Long> findMoimIdsByDarakbangMemberId(long darakbangMemberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ void read_notExist() {
@Test
void readAll() {
Moim coffeeMoim = moimRepository.save(MoimFixture.getCoffeeMoim(darakbang.getId()));
chamyoRepository.save(new Chamyo(coffeeMoim, darakbangAnna, MoimRole.MOIMER));
Moim soccerMoim = moimRepository.save(MoimFixture.getSoccerMoim(darakbang.getId()));
chamyoRepository.save(new Chamyo(soccerMoim, darakbangAnna, MoimRole.MOIMER));

zzimRepository.save(Zzim.builder().moim(coffeeMoim).darakbangMember(darakbangHogee).build());

Expand Down Expand Up @@ -130,12 +132,13 @@ void readAllMyMoim_upcoming() {
}
}


@DisplayName("내가 찜한 모임을 조회한다.")
@Test
void readAllZzimedMoim() {
Moim coffeeMoim = moimRepository.save(MoimFixture.getCoffeeMoim(darakbang.getId()));
chamyoRepository.save(new Chamyo(coffeeMoim, darakbangAnna, MoimRole.MOIMER));
Moim soccerMoim = moimRepository.save(MoimFixture.getSoccerMoim(darakbang.getId()));
chamyoRepository.save(new Chamyo(soccerMoim, darakbangAnna, MoimRole.MOIMER));

zzimRepository.save(Zzim.builder()
.moim(coffeeMoim)
Expand Down
Loading

0 comments on commit e77f98a

Please sign in to comment.