Skip to content

Commit

Permalink
refactor: 한번에 모든 모임에 대한 참여와 찜 정보를 가져오도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingyum-Kim committed Oct 31, 2024
1 parent c880e87 commit a64437e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package mouda.backend.moim.implement.finder;

import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

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 @@ -20,6 +24,8 @@
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 @@ -28,19 +34,29 @@ 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)
.orElseThrow(() -> new MoimException(HttpStatus.NOT_FOUND, MoimErrorMessage.NOT_FOUND));
}

public List<MoimOverview> readAll(long darakbangId, DarakbangMember darakbangMember) {
return moimRepository.findAllByDarakbangIdOrderByIdDesc(darakbangId).stream()
.map(moim -> createMoimOverview(moim, darakbangMember))
.toList();
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());

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();
}

public List<MoimOverview> readAllMyMoim(DarakbangMember darakbangMember, FilterType filterType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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;
Expand Down Expand Up @@ -36,4 +38,8 @@ 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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.data.jpa.repository.JpaRepository;

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

public interface ZzimRepository extends JpaRepository<Zzim, Long> {

Expand All @@ -14,4 +15,7 @@ public interface ZzimRepository extends JpaRepository<Zzim, Long> {
Optional<Zzim> findByMoimIdAndDarakbangMemberId(Long moimId, Long darakbangMemberId);

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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package mouda.backend.moim.infrastructure.dto;

import lombok.Getter;

@Getter
public class ChamyoCountResponse {

private final Long moimId;
private final Integer count;

public ChamyoCountResponse(Long moimId, Long count) {
this.moimId = moimId;
this.count = count.intValue();
}
}

0 comments on commit a64437e

Please sign in to comment.