diff --git a/src/main/java/com/dnd/snappy/domain/mission/repository/MissionRepository.java b/src/main/java/com/dnd/snappy/domain/mission/repository/MissionRepository.java index 6fb15ed..a877dee 100644 --- a/src/main/java/com/dnd/snappy/domain/mission/repository/MissionRepository.java +++ b/src/main/java/com/dnd/snappy/domain/mission/repository/MissionRepository.java @@ -1,14 +1,29 @@ package com.dnd.snappy.domain.mission.repository; +import com.dnd.snappy.domain.mission.dto.response.LeaderMeetingMissionDetailResponseDto; import com.dnd.snappy.domain.mission.entity.Mission; 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.data.repository.query.Param; public interface MissionRepository extends JpaRepository { Optional findByIdAndMeetingId(Long id, Long meetingId); List findAllByMeetingId(Long meetingId); -} + + @Query(""" + SELECT new com.dnd.snappy.domain.mission.dto.response.LeaderMeetingMissionDetailResponseDto(m.id, m.content, + CASE WHEN mp.id IS NOT NULL THEN TRUE ELSE FALSE END) + FROM Mission m + LEFT JOIN MissionParticipant mp ON mp.mission.id = m.id + JOIN Participant p ON p.meeting.id = m.meeting.id + WHERE p.id = :participantId AND p.role = 'LEADER' AND m.meeting.id = :meetingId + """) + List findLeaderMeetingMissions(@Param("meetingId") Long meetingId, @Param("participantId") Long participantId); + +} \ No newline at end of file diff --git a/src/main/java/com/dnd/snappy/domain/mission/service/MissionDetailService.java b/src/main/java/com/dnd/snappy/domain/mission/service/MissionDetailService.java index 6177239..c62fe9f 100644 --- a/src/main/java/com/dnd/snappy/domain/mission/service/MissionDetailService.java +++ b/src/main/java/com/dnd/snappy/domain/mission/service/MissionDetailService.java @@ -7,7 +7,6 @@ import com.dnd.snappy.domain.mission.dto.response.LeaderMeetingMissionDetailResponseDto; import com.dnd.snappy.domain.mission.dto.response.MeetingMissionDetailResponseDto; import com.dnd.snappy.domain.mission.entity.Mission; -import com.dnd.snappy.domain.mission.repository.MissionParticipantRepository; import com.dnd.snappy.domain.mission.repository.MissionRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -23,7 +22,6 @@ public class MissionDetailService { private final MissionRepository missionRepository; private final MeetingRepository meetingRepository; private final MissionValidationService missionValidationService; - private final MissionParticipantRepository missionParticipantRepository; @Transactional(readOnly = true) public List findMeetingMissions(Long meetingId, Long participantId) { @@ -40,23 +38,7 @@ public List findLeaderMeetingMissions(Lon findByMeetingIdOrThrow(meetingId); missionValidationService.validateIsLeader(participantId, meetingId); - List missions = missionRepository.findAllByMeetingId(meetingId); - - return missions.stream() - .map(mission -> { - boolean hasParticipants = hasParticipants(mission.getId()); - return new LeaderMeetingMissionDetailResponseDto( - mission.getId(), - mission.getContent(), - hasParticipants - ); - }) - .collect(Collectors.toList()); - } - - @Transactional - public boolean hasParticipants(Long missionId) { - return missionParticipantRepository.existsByMissionId(missionId); + return missionRepository.findLeaderMeetingMissions(meetingId, participantId); } private Meeting findByMeetingIdOrThrow(Long meetingId) { diff --git a/src/test/java/com/dnd/snappy/domain/mission/service/MissionDetailServiceTest.java b/src/test/java/com/dnd/snappy/domain/mission/service/MissionDetailServiceTest.java index f265480..bd48bd5 100644 --- a/src/test/java/com/dnd/snappy/domain/mission/service/MissionDetailServiceTest.java +++ b/src/test/java/com/dnd/snappy/domain/mission/service/MissionDetailServiceTest.java @@ -13,8 +13,6 @@ import com.dnd.snappy.domain.mission.dto.response.MeetingMissionDetailResponseDto; import com.dnd.snappy.domain.mission.entity.Mission; import com.dnd.snappy.domain.mission.exception.MissionErrorCode; - -import com.dnd.snappy.domain.mission.repository.MissionParticipantRepository; import com.dnd.snappy.domain.mission.repository.MissionRepository; import java.util.List; @@ -43,9 +41,6 @@ class MissionDetailServiceTest { @Mock private MissionValidationService missionValidationService; - @Mock - private MissionParticipantRepository missionParticipantRepository; - @DisplayName("미션 목록을 조회한다.") @Test void findMeetingMissions() { @@ -92,8 +87,14 @@ void findLeaderMeetingMissions() { given(meetingRepository.findById(meetingId)).willReturn(Optional.of(meeting)); doNothing().when(missionValidationService).validateIsLeader(participantId, meetingId); - given(missionRepository.findAllByMeetingId(meetingId)).willReturn(missions); - given(missionParticipantRepository.existsByMissionId(anyLong())).willReturn(false); + + given(missionRepository.findLeaderMeetingMissions(meetingId, participantId)).willReturn( + List.of( + new LeaderMeetingMissionDetailResponseDto(1L, "미션 내용 1", false), + new LeaderMeetingMissionDetailResponseDto(2L, "미션 내용 2", false), + new LeaderMeetingMissionDetailResponseDto(3L, "미션 내용 3", false) + ) + ); // when List result = missionDetailService.findLeaderMeetingMissions(meetingId, participantId);