diff --git a/src/main/java/com/softeer/backend/fo_domain/draw/repository/DrawParticipationInfoRepository.java b/src/main/java/com/softeer/backend/fo_domain/draw/repository/DrawParticipationInfoRepository.java index 80fd7cc0..736e7b40 100644 --- a/src/main/java/com/softeer/backend/fo_domain/draw/repository/DrawParticipationInfoRepository.java +++ b/src/main/java/com/softeer/backend/fo_domain/draw/repository/DrawParticipationInfoRepository.java @@ -2,11 +2,24 @@ import com.softeer.backend.fo_domain.draw.domain.DrawParticipationInfo; 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; +import org.springframework.transaction.annotation.Transactional; import java.util.Optional; @Repository public interface DrawParticipationInfoRepository extends JpaRepository { Optional findDrawParticipationInfoByUserId(Integer userId); + + @Modifying + @Transactional + @Query("UPDATE DrawParticipationInfo d SET d.drawWinningCount = d.drawWinningCount + 1 WHERE d.userId = :userId") + void increaseWinCount(Integer userId); + + @Modifying + @Transactional + @Query("UPDATE DrawParticipationInfo d SET d.drawLosingCount = d.drawLosingCount + 1 WHERE d.userId = :userId") + void increaseLoseCount(Integer userId); } diff --git a/src/main/java/com/softeer/backend/fo_domain/draw/service/DrawService.java b/src/main/java/com/softeer/backend/fo_domain/draw/service/DrawService.java index 70be4c02..b4dac54e 100644 --- a/src/main/java/com/softeer/backend/fo_domain/draw/service/DrawService.java +++ b/src/main/java/com/softeer/backend/fo_domain/draw/service/DrawService.java @@ -110,7 +110,6 @@ public ResponseDto participateDrawEvent(Integer userId) { ShareInfo shareInfo = shareInfoRepository.findShareInfoByUserId(userId) .orElseThrow(() -> new ShareInfoException(ErrorStatus._NOT_FOUND)); - int invitedNum = shareInfo.getInvitedNum(); int remainDrawCount = shareInfo.getRemainDrawCount(); // 만약 남은 참여 기회가 0이라면 @@ -118,15 +117,12 @@ public ResponseDto participateDrawEvent(Integer userId) { return ResponseDto.onSuccess(responseLoseModal(userId)); } - DrawParticipationInfo drawParticipationInfo = drawParticipationInfoRepository.findDrawParticipationInfoByUserId(userId) - .orElseThrow(() -> new DrawException(ErrorStatus._NOT_FOUND)); - // 만약 당첨 목록에 존재한다면 이미 오늘은 한 번 당첨됐다는 뜻이므로 LoseModal 반환 int ranking = getRankingIfWinner(userId); // 당첨 목록에 존재한다면 랭킹 반환 if (ranking != 0) { - decreaseRemainDrawCount(userId, invitedNum, remainDrawCount); // 횟수 1회 차감 + shareInfoRepository.decreaseRemainDrawCount(userId); // 횟수 1회 차감 increaseDrawParticipationCount(); // 추첨 이벤트 참여자수 증가 - increaseLoseCount(drawParticipationInfo); // 낙첨 횟수 증가 + drawParticipationInfoRepository.increaseLoseCount(userId); // 낙첨 횟수 증가 return ResponseDto.onSuccess(responseLoseModal(userId)); // LoseModal 반환 } @@ -144,7 +140,7 @@ public ResponseDto participateDrawEvent(Integer userId) { drawUtil.performDraw(); if (drawUtil.isDrawWin()) { // 당첨자일 경우 - decreaseRemainDrawCount(userId, invitedNum, remainDrawCount); // 횟수 1회 차감 + shareInfoRepository.decreaseRemainDrawCount(userId); // 횟수 1회 차감 ranking = drawUtil.getRanking(); int winnerNum; @@ -159,18 +155,18 @@ public ResponseDto participateDrawEvent(Integer userId) { if (isWinner(userId, ranking, winnerNum)) { // 레디스에 추첨 티켓이 남았다면, 레디스 당첨 목록에 추가 // 추첨 티켓이 다 팔리지 않았다면 increaseDrawParticipationCount(); // 추첨 이벤트 참여자수 증가 - increaseWinCount(drawParticipationInfo); // 당첨 횟수 증가 + drawParticipationInfoRepository.increaseWinCount(userId); // 당첨 횟수 증가 return ResponseDto.onSuccess(responseWinModal()); // WinModal 반환 } else { // 추첨 티켓이 다 팔렸다면 로직상 당첨자라도 실패 반환 increaseDrawParticipationCount(); // 추첨 이벤트 참여자수 증가 - increaseLoseCount(drawParticipationInfo); // 낙첨 횟수 증가 + drawParticipationInfoRepository.increaseLoseCount(userId); // 낙첨 횟수 증가 return ResponseDto.onSuccess(responseLoseModal(userId)); // LoseModal 반환 } } else { // 낙첨자일 경우 - decreaseRemainDrawCount(userId, invitedNum, remainDrawCount); // 횟수 1회 차감 + shareInfoRepository.decreaseRemainDrawCount(userId); // 횟수 1회 차감 increaseDrawParticipationCount(); // 추첨 이벤트 참여자수 증가 - increaseLoseCount(drawParticipationInfo); // 낙첨 횟수 증가 + drawParticipationInfoRepository.increaseLoseCount(userId); // 낙첨 횟수 증가 return ResponseDto.onSuccess(responseLoseModal(userId)); // LoseModal 반환 } } @@ -204,34 +200,6 @@ private DrawWinModalResponseDto responseWinModal() { .build(); } - /** - * 당첨된 경우 당첨 횟수 증가 - * - * @param drawParticipationInfo 추첨 참여 정보 - */ - private void increaseWinCount(DrawParticipationInfo drawParticipationInfo) { - drawParticipationInfoRepository.save(DrawParticipationInfo.builder() - .userId(drawParticipationInfo.getUserId()) - .drawWinningCount(drawParticipationInfo.getDrawWinningCount() + 1) - .drawLosingCount(drawParticipationInfo.getDrawLosingCount()) - .drawParticipationCount(drawParticipationInfo.getDrawParticipationCount()) - .build()); - } - - /** - * 당첨되지 않은 경우 낙첨 횟수 증가 - * - * @param drawParticipationInfo 추첨 참여 정보 - */ - private void increaseLoseCount(DrawParticipationInfo drawParticipationInfo) { - drawParticipationInfoRepository.save(DrawParticipationInfo.builder() - .userId(drawParticipationInfo.getUserId()) - .drawWinningCount(drawParticipationInfo.getDrawWinningCount()) - .drawLosingCount(drawParticipationInfo.getDrawLosingCount() + 1) - .drawParticipationCount(drawParticipationInfo.getDrawParticipationCount()) - .build()); - } - @EventLock(key = "DRAW_WINNER_#{#ranking}") private boolean isWinner(Integer userId, int ranking, int winnerNum) { String drawWinnerKey = RedisKeyPrefix.DRAW_WINNER_LIST_PREFIX.getPrefix() + ranking; @@ -253,25 +221,6 @@ private void increaseDrawParticipationCount() { eventLockRedisUtil.incrementData(RedisKeyPrefix.DRAW_PARTICIPANT_COUNT_PREFIX.getPrefix()); } - /** - * 참여 횟수 1회 차감 - * - * @param userId 그대로 저장 - * @param invitedNum 그대로 저장 - * @param remainDrawCount 1회 차감 후 저장 - */ - private void decreaseRemainDrawCount(Integer userId, int invitedNum, int remainDrawCount) { - // 횟수 1회 차감 - int newRemainDrawCount = remainDrawCount - 1; - ShareInfo shareInfo = ShareInfo.builder() - .userId(userId) - .invitedNum(invitedNum) - .remainDrawCount(newRemainDrawCount) - .build(); - - shareInfoRepository.save(shareInfo); - } - /** * 당첨 내역 조회하는 메서드 * 1. 당첨자라면 WinModal과 같은 당첨 내역 모달 응답 diff --git a/src/main/java/com/softeer/backend/fo_domain/share/repository/ShareInfoRepository.java b/src/main/java/com/softeer/backend/fo_domain/share/repository/ShareInfoRepository.java index ca99b8ac..31d62e8b 100644 --- a/src/main/java/com/softeer/backend/fo_domain/share/repository/ShareInfoRepository.java +++ b/src/main/java/com/softeer/backend/fo_domain/share/repository/ShareInfoRepository.java @@ -5,6 +5,7 @@ import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; import java.util.Optional; @@ -15,4 +16,9 @@ public interface ShareInfoRepository extends JpaRepository { @Modifying @Query("UPDATE ShareInfo s SET s.remainDrawCount = s.remainDrawCount + 1 WHERE s.userId = :userId") void increaseRemainDrawCount(Integer userId); + + @Modifying + @Transactional + @Query("UPDATE ShareInfo s SET s.remainDrawCount = s.remainDrawCount - 1 WHERE s.userId = :userId") + void decreaseRemainDrawCount(Integer userId); }