Skip to content

Commit

Permalink
Merge pull request #67 from dnd-side-project/Refactor/#41
Browse files Browse the repository at this point in the history
refactor: 리더의 모임 미션 조회 쿼리 리팩토링 (#41)
  • Loading branch information
chaewon-io authored Aug 23, 2024
2 parents 437a443 + a83e58d commit 51461c0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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<Mission, Long> {

Optional<Mission> findByIdAndMeetingId(Long id, Long meetingId);

List<Mission> 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<LeaderMeetingMissionDetailResponseDto> findLeaderMeetingMissions(@Param("meetingId") Long meetingId, @Param("participantId") Long participantId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<MeetingMissionDetailResponseDto> findMeetingMissions(Long meetingId, Long participantId) {
Expand All @@ -40,23 +38,7 @@ public List<LeaderMeetingMissionDetailResponseDto> findLeaderMeetingMissions(Lon
findByMeetingIdOrThrow(meetingId);
missionValidationService.validateIsLeader(participantId, meetingId);

List<Mission> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,9 +41,6 @@ class MissionDetailServiceTest {
@Mock
private MissionValidationService missionValidationService;

@Mock
private MissionParticipantRepository missionParticipantRepository;

@DisplayName("미션 목록을 조회한다.")
@Test
void findMeetingMissions() {
Expand Down Expand Up @@ -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<LeaderMeetingMissionDetailResponseDto> result = missionDetailService.findLeaderMeetingMissions(meetingId, participantId);
Expand Down

0 comments on commit 51461c0

Please sign in to comment.