diff --git a/src/main/java/com/ssafy/ssafsound/domain/lunch/repository/LunchPollRepository.java b/src/main/java/com/ssafy/ssafsound/domain/lunch/repository/LunchPollRepository.java index e4444351b..e45979391 100644 --- a/src/main/java/com/ssafy/ssafsound/domain/lunch/repository/LunchPollRepository.java +++ b/src/main/java/com/ssafy/ssafsound/domain/lunch/repository/LunchPollRepository.java @@ -3,8 +3,8 @@ import com.ssafy.ssafsound.domain.lunch.domain.Lunch; import com.ssafy.ssafsound.domain.lunch.domain.LunchPoll; import com.ssafy.ssafsound.domain.member.domain.Member; +import com.ssafy.ssafsound.domain.meta.domain.MetaData; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; @@ -14,7 +14,23 @@ @Repository public interface LunchPollRepository extends JpaRepository { - LunchPoll findByMemberAndPolledAt(Member member, LocalDate polledAt); +// LunchPoll findByMemberAndPolledAt(Member member, LocalDate polledAt); Optional findByMemberAndLunch(Member member, Lunch lunch); + + @Query( "SELECT" + + " lp " + + "FROM " + + " lunch_poll lp " + + "INNER JOIN " + + " lunch l " + + "ON" + + " lp.lunch = l " + + "AND" + + " lp.member = :member " + + "AND " + + " l.campus = :campus " + + "AND " + + " lp.polledAt = :polledAt") + LunchPoll findByMemberAndCampusAndPolledAt(Member member, MetaData campus, LocalDate polledAt); } diff --git a/src/main/java/com/ssafy/ssafsound/domain/lunch/service/LunchPollService.java b/src/main/java/com/ssafy/ssafsound/domain/lunch/service/LunchPollService.java index 30c6e8c05..746488fe5 100644 --- a/src/main/java/com/ssafy/ssafsound/domain/lunch/service/LunchPollService.java +++ b/src/main/java/com/ssafy/ssafsound/domain/lunch/service/LunchPollService.java @@ -48,8 +48,8 @@ public PostLunchPollResDto saveLunchPoll(Long memberId, Long lunchId) { throw new LunchException(LunchErrorInfo.INVALID_DATE); } - // 3. 오늘 투표한 점심 투표 엔티티 조회 - LunchPoll lunchPoll = lunchPollRepository.findByMemberAndPolledAt(member, currentTime); + // 3. 같은 캠퍼스에 오늘 투표한 점심 투표 엔티티 조회 + LunchPoll lunchPoll = lunchPollRepository.findByMemberAndCampusAndPolledAt(member, lunch.getCampus(), currentTime); // 4-1. 이미 오늘 투표한 경우 if (lunchPoll != null) { diff --git a/src/main/java/com/ssafy/ssafsound/domain/lunch/service/LunchService.java b/src/main/java/com/ssafy/ssafsound/domain/lunch/service/LunchService.java index a8d36acee..5c622e1aa 100644 --- a/src/main/java/com/ssafy/ssafsound/domain/lunch/service/LunchService.java +++ b/src/main/java/com/ssafy/ssafsound/domain/lunch/service/LunchService.java @@ -75,7 +75,7 @@ public GetLunchListResDto findLunches(Long memberId, GetLunchListReqDto getLunch Member member = memberRepository.findById(memberId) .orElseThrow(() -> new MemberException(MemberErrorInfo.MEMBER_NOT_FOUND_BY_ID)); - LunchPoll lunchPoll = lunchPollRepository.findByMemberAndPolledAt(member, currentTime); + LunchPoll lunchPoll = lunchPollRepository.findByMemberAndCampusAndPolledAt(member, campus, currentTime); Integer polledAt = lunchPoll != null ? lunches.indexOf(lunchPoll.getLunch()) : -1; // 인증 유저 응답 diff --git a/src/test/java/com/ssafy/ssafsound/domain/lunch/service/LunchPollServiceTest.java b/src/test/java/com/ssafy/ssafsound/domain/lunch/service/LunchPollServiceTest.java index e1700a279..985f1e497 100644 --- a/src/test/java/com/ssafy/ssafsound/domain/lunch/service/LunchPollServiceTest.java +++ b/src/test/java/com/ssafy/ssafsound/domain/lunch/service/LunchPollServiceTest.java @@ -11,6 +11,8 @@ import com.ssafy.ssafsound.domain.member.exception.MemberErrorInfo; import com.ssafy.ssafsound.domain.member.exception.MemberException; import com.ssafy.ssafsound.domain.member.repository.MemberRepository; +import com.ssafy.ssafsound.domain.meta.domain.Campus; +import com.ssafy.ssafsound.domain.meta.domain.MetaData; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -50,6 +52,7 @@ class LunchPollServiceTest { @InjectMocks private LunchPollService lunchPollService; + private MetaData SEOUL; private Lunch lunch1; private Lunch lunch2; private Lunch lunch3; @@ -66,18 +69,23 @@ class LunchPollServiceTest { @BeforeEach void setUp() { + SEOUL = new MetaData(Campus.SEOUL); + lunch1 = Lunch.builder() .id(1L) + .campus(SEOUL) .createdAt(today) .build(); lunch2 = Lunch.builder() .id(2L) + .campus(SEOUL) .createdAt(today) .build(); lunch3 = Lunch.builder() .id(3L) + .campus(SEOUL) .createdAt(today.plusDays(1)) .build(); @@ -118,8 +126,8 @@ void Given_MemberIdAndLunchId_When_SaveLunchPoll_Then_Succeed(Long memberId, Lon Integer expectedLunch2PollCount) { // given - lenient().when(lunchPollRepository.findByMemberAndPolledAt(member1, today)).thenReturn(lunchPoll); - lenient().when(lunchPollRepository.findByMemberAndPolledAt(member2, today)).thenReturn(null); + lenient().when(lunchPollRepository.findByMemberAndCampusAndPolledAt(member1, SEOUL, today)).thenReturn(lunchPoll); + lenient().when(lunchPollRepository.findByMemberAndCampusAndPolledAt(member2, SEOUL, today)).thenReturn(null); // when PostLunchPollResDto postLunchPollResDto = lunchPollService.saveLunchPoll(memberId, lunchId); @@ -167,13 +175,13 @@ void Given_AlreadyPolledLunchId_When_SaveLunchPoll_Then_ThrowException() { // given Long memberId = 1L; Long lunchId = 2L; - given(lunchPollRepository.findByMemberAndPolledAt(member1, today)).willReturn(lunchPoll); + given(lunchPollRepository.findByMemberAndCampusAndPolledAt(member1, SEOUL, today)).willReturn(lunchPoll); // when, then LunchException exception = assertThrows(LunchException.class, () -> lunchPollService.saveLunchPoll(memberId, lunchId)); assertEquals(LunchErrorInfo.DUPLICATE_LUNCH_POLL, exception.getInfo()); - verify(lunchPollRepository).findByMemberAndPolledAt(member1, today); + verify(lunchPollRepository).findByMemberAndCampusAndPolledAt(member1, SEOUL, today); } @Test diff --git a/src/test/java/com/ssafy/ssafsound/domain/lunch/service/LunchServiceTest.java b/src/test/java/com/ssafy/ssafsound/domain/lunch/service/LunchServiceTest.java index db7438916..3f029408a 100644 --- a/src/test/java/com/ssafy/ssafsound/domain/lunch/service/LunchServiceTest.java +++ b/src/test/java/com/ssafy/ssafsound/domain/lunch/service/LunchServiceTest.java @@ -5,8 +5,6 @@ import com.ssafy.ssafsound.domain.lunch.dto.GetLunchListElementResDto; import com.ssafy.ssafsound.domain.lunch.dto.GetLunchListReqDto; import com.ssafy.ssafsound.domain.lunch.dto.GetLunchListResDto; -import com.ssafy.ssafsound.domain.lunch.exception.LunchErrorInfo; -import com.ssafy.ssafsound.domain.lunch.exception.LunchException; import com.ssafy.ssafsound.domain.lunch.repository.LunchPollRepository; import com.ssafy.ssafsound.domain.lunch.repository.LunchRepository; import com.ssafy.ssafsound.domain.member.domain.Member; @@ -36,8 +34,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertAll; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.lenient; @@ -112,9 +108,9 @@ void setUp() { lenient().when(memberRepository.findById(1L)).thenReturn(Optional.of(member1)); lenient().when(memberRepository.findById(2L)).thenReturn(Optional.of(member2)); - lenient().when(lunchPollRepository.findByMemberAndPolledAt(member1, today)) + lenient().when(lunchPollRepository.findByMemberAndCampusAndPolledAt(member1, testCampus, today)) .thenReturn(lunchPoll); - lenient().when(lunchPollRepository.findByMemberAndPolledAt(member2, today)) + lenient().when(lunchPollRepository.findByMemberAndCampusAndPolledAt(member2, testCampus, today)) .thenReturn(null); } @@ -132,7 +128,7 @@ void Given_DateAndCampus_When_FindLunches_Then_Succeed( given(clock.getZone()).willReturn(ZoneId.of("Asia/Seoul")); given(metaDataConsumer.getMetaData(MetaDataType.CAMPUS.name(), inputCampus)) - .willReturn(new MetaData(Campus.SEOUL)); + .willReturn(testCampus); given(lunchRepository.findAllByCampusAndDate( metaDataConsumer.getMetaData(MetaDataType.CAMPUS.name(), inputCampus), inputDate))