Skip to content

Commit

Permalink
fix : 점심 투표 정책 변경
Browse files Browse the repository at this point in the history
* fix : 점심 투표 정책 변경

- 다른 캠퍼스여도 중복 일자 투표 불가능했던 점 수정

* fix : 정책 수정에 의한 테스트코드 수정

* fix : join 조건 수정
  • Loading branch information
moonn6pence authored Nov 10, 2023
1 parent 20355ef commit ddb4903
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -14,7 +14,23 @@
@Repository
public interface LunchPollRepository extends JpaRepository<LunchPoll, Long> {

LunchPoll findByMemberAndPolledAt(Member member, LocalDate polledAt);
// LunchPoll findByMemberAndPolledAt(Member member, LocalDate polledAt);

Optional<LunchPoll> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

// 인증 유저 응답
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,6 +52,7 @@ class LunchPollServiceTest {
@InjectMocks
private LunchPollService lunchPollService;

private MetaData SEOUL;
private Lunch lunch1;
private Lunch lunch2;
private Lunch lunch3;
Expand All @@ -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();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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))
Expand Down

0 comments on commit ddb4903

Please sign in to comment.