diff --git a/src/main/java/com/softeer/backend/fo_domain/draw/controller/DrawController.java b/src/main/java/com/softeer/backend/fo_domain/draw/controller/DrawController.java index 7ff94ea9..aca3a723 100644 --- a/src/main/java/com/softeer/backend/fo_domain/draw/controller/DrawController.java +++ b/src/main/java/com/softeer/backend/fo_domain/draw/controller/DrawController.java @@ -21,11 +21,11 @@ public class DrawController { @GetMapping("/event/draw") public ResponseDto getDrawMainPageInfo(@AuthInfo Integer userId) { - return drawService.getDrawMainPageInfo(userId); + return ResponseDto.onSuccess(drawService.getDrawMainPageInfo(userId)); } @PostMapping("/event/draw") - public ResponseEntity participateDrawEvent(@AuthInfo Integer userId) { + public ResponseEntity participateDrawEvent() { HttpHeaders headers = new HttpHeaders(); headers.add("Location", "/event/draw-result"); return new ResponseEntity<>(headers, HttpStatus.FOUND); // HTTP 302 Found 응답 @@ -33,11 +33,11 @@ public ResponseEntity participateDrawEvent(@AuthInfo Integer userId) { @GetMapping("/event/draw-result") public ResponseDto getDrawResult(@AuthInfo Integer userId) { - return drawService.participateDrawEvent(userId); + return ResponseDto.onSuccess(drawService.participateDrawEvent(userId)); } @GetMapping("/event/draw/history") public ResponseDto getDrawHistory(@AuthInfo Integer userId) { - return drawService.getDrawHistory(userId); + return ResponseDto.onSuccess(drawService.getDrawHistory(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 b4dac54e..f9614d91 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 @@ -1,28 +1,20 @@ package com.softeer.backend.fo_domain.draw.service; import com.softeer.backend.fo_domain.draw.domain.DrawParticipationInfo; -import com.softeer.backend.fo_domain.draw.dto.main.DrawMainFullAttendResponseDto; import com.softeer.backend.fo_domain.draw.dto.main.DrawMainResponseDto; -import com.softeer.backend.fo_domain.draw.dto.participate.DrawLoseModalResponseDto; import com.softeer.backend.fo_domain.draw.dto.participate.DrawModalResponseDto; -import com.softeer.backend.fo_domain.draw.dto.participate.DrawWinModalResponseDto; -import com.softeer.backend.fo_domain.draw.dto.result.DrawHistoryLoserResponseDto; import com.softeer.backend.fo_domain.draw.dto.result.DrawHistoryResponseDto; -import com.softeer.backend.fo_domain.draw.dto.result.DrawHistoryWinnerResponseDto; import com.softeer.backend.fo_domain.draw.exception.DrawException; import com.softeer.backend.fo_domain.draw.repository.DrawParticipationInfoRepository; +import com.softeer.backend.fo_domain.draw.util.DrawResponseGenerateUtil; import com.softeer.backend.fo_domain.draw.util.DrawUtil; import com.softeer.backend.fo_domain.share.domain.ShareInfo; import com.softeer.backend.fo_domain.share.exception.ShareInfoException; -import com.softeer.backend.fo_domain.share.exception.ShareUrlInfoException; import com.softeer.backend.fo_domain.share.repository.ShareInfoRepository; -import com.softeer.backend.fo_domain.share.repository.ShareUrlInfoRepository; import com.softeer.backend.global.annotation.EventLock; import com.softeer.backend.global.common.code.status.ErrorStatus; import com.softeer.backend.global.common.constant.RedisKeyPrefix; -import com.softeer.backend.global.common.response.ResponseDto; -import com.softeer.backend.global.staticresources.util.StaticResourcesUtil; -import com.softeer.backend.global.util.EventLockRedisUtil; +import com.softeer.backend.global.util.DrawRedisUtil; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -33,10 +25,9 @@ public class DrawService { private final DrawParticipationInfoRepository drawParticipationInfoRepository; private final ShareInfoRepository shareInfoRepository; - private final ShareUrlInfoRepository shareUrlInfoRepository; - private final EventLockRedisUtil eventLockRedisUtil; - private final StaticResourcesUtil staticResourcesUtil; + private final DrawRedisUtil drawRedisUtil; private final DrawUtil drawUtil; + private final DrawResponseGenerateUtil drawResponseGenerateUtil; private final DrawSettingManager drawSettingManager; /** @@ -44,7 +35,7 @@ public class DrawService { * 1-1. 만약 7일 연속 참여했다면 상품 정보 응답 * 1-2. 만약 7일 미만 참여라면 일반 정보 응답 */ - public ResponseDto getDrawMainPageInfo(Integer userId) { + public DrawMainResponseDto getDrawMainPageInfo(Integer userId) { // 참여 정보 (연속참여일수) 조회 DrawParticipationInfo drawParticipationInfo = drawParticipationInfoRepository.findDrawParticipationInfoByUserId(userId) .orElseThrow(() -> new DrawException(ErrorStatus._NOT_FOUND)); @@ -59,71 +50,37 @@ public ResponseDto getDrawMainPageInfo(Integer userId) { if (drawParticipationCount == 7) { // 7일 연속 출석자라면 - return ResponseDto.onSuccess(responseMainFullAttend(invitedNum, remainDrawCount, drawParticipationCount)); + return drawResponseGenerateUtil.generateMainFullAttendResponse(invitedNum, remainDrawCount, drawParticipationCount); } else { // 연속 출석자가 아니라면 - return ResponseDto.onSuccess(responseMainNotAttend(invitedNum, remainDrawCount, drawParticipationCount)); + return drawResponseGenerateUtil.generateMainNotAttendResponse(invitedNum, remainDrawCount, drawParticipationCount); } } - /** - * 7일 연속 출석 시 상품 정보 모달 만들어서 반환하는 메서드 - * - * @param invitedNum 초대한 사람 수 - * @param remainDrawCount 남은 추첨 기회 - * @param drawParticipationCount 연속 출석 일수 - * @return 7일 연속 출석 상품 모달 - */ - private DrawMainFullAttendResponseDto responseMainFullAttend(int invitedNum, int remainDrawCount, int drawParticipationCount) { - return DrawMainFullAttendResponseDto.builder() - .invitedNum(invitedNum) - .remainDrawCount(remainDrawCount) - .drawParticipationCount(drawParticipationCount) - .fullAttendModal(drawUtil.generateFullAttendModal()) - .build(); - } - - /** - * 7일 미만 출석 시 모달 만들어서 반환하는 메서드 - * - * @param invitedNum 초대한 사람 수 - * @param remainDrawCount 남은 추첨 기회 - * @param drawParticipationCount 연속 출석 일수 - * @return 7일 미만 출석 상품 모달 - */ - private DrawMainResponseDto responseMainNotAttend(int invitedNum, int remainDrawCount, int drawParticipationCount) { - return DrawMainResponseDto.builder() - .invitedNum(invitedNum) - .remainDrawCount(remainDrawCount) - .drawParticipationCount(drawParticipationCount) - .build(); - } - /** * 추첨 이벤트 당첨 로직 작성 * * @param userId 사용자 아이디 * @return 추첨 결과에 따른 응답 반환 */ - public ResponseDto participateDrawEvent(Integer userId) { + public DrawModalResponseDto participateDrawEvent(Integer userId) { // 복권 기회 조회 ShareInfo shareInfo = shareInfoRepository.findShareInfoByUserId(userId) .orElseThrow(() -> new ShareInfoException(ErrorStatus._NOT_FOUND)); - int remainDrawCount = shareInfo.getRemainDrawCount(); - // 만약 남은 참여 기회가 0이라면 - if (remainDrawCount == 0) { - return ResponseDto.onSuccess(responseLoseModal(userId)); + if (shareInfo.getRemainDrawCount() == 0) { + return drawResponseGenerateUtil.generateDrawLoserResponse(userId); } + increaseDrawParticipationCount(); // 추첨 이벤트 참여자수 증가 + shareInfoRepository.decreaseRemainDrawCount(userId); // 횟수 1회 차감 + // 만약 당첨 목록에 존재한다면 이미 오늘은 한 번 당첨됐다는 뜻이므로 LoseModal 반환 int ranking = getRankingIfWinner(userId); // 당첨 목록에 존재한다면 랭킹 반환 if (ranking != 0) { - shareInfoRepository.decreaseRemainDrawCount(userId); // 횟수 1회 차감 - increaseDrawParticipationCount(); // 추첨 이벤트 참여자수 증가 drawParticipationInfoRepository.increaseLoseCount(userId); // 낙첨 횟수 증가 - return ResponseDto.onSuccess(responseLoseModal(userId)); // LoseModal 반환 + return drawResponseGenerateUtil.generateDrawLoserResponse(userId); // LoseModal 반환 } // 당첨자 수 조회 @@ -140,8 +97,6 @@ public ResponseDto participateDrawEvent(Integer userId) { drawUtil.performDraw(); if (drawUtil.isDrawWin()) { // 당첨자일 경우 - shareInfoRepository.decreaseRemainDrawCount(userId); // 횟수 1회 차감 - ranking = drawUtil.getRanking(); int winnerNum; if (ranking == 1) { @@ -154,61 +109,28 @@ public ResponseDto participateDrawEvent(Integer userId) { if (isWinner(userId, ranking, winnerNum)) { // 레디스에 추첨 티켓이 남았다면, 레디스 당첨 목록에 추가 // 추첨 티켓이 다 팔리지 않았다면 - increaseDrawParticipationCount(); // 추첨 이벤트 참여자수 증가 drawParticipationInfoRepository.increaseWinCount(userId); // 당첨 횟수 증가 - return ResponseDto.onSuccess(responseWinModal()); // WinModal 반환 + return drawResponseGenerateUtil.generateDrawWinnerResponse(ranking); // WinModal 반환 } else { // 추첨 티켓이 다 팔렸다면 로직상 당첨자라도 실패 반환 - increaseDrawParticipationCount(); // 추첨 이벤트 참여자수 증가 drawParticipationInfoRepository.increaseLoseCount(userId); // 낙첨 횟수 증가 - return ResponseDto.onSuccess(responseLoseModal(userId)); // LoseModal 반환 + return drawResponseGenerateUtil.generateDrawLoserResponse(userId); // LoseModal 반환 } } else { // 낙첨자일 경우 - shareInfoRepository.decreaseRemainDrawCount(userId); // 횟수 1회 차감 - increaseDrawParticipationCount(); // 추첨 이벤트 참여자수 증가 drawParticipationInfoRepository.increaseLoseCount(userId); // 낙첨 횟수 증가 - return ResponseDto.onSuccess(responseLoseModal(userId)); // LoseModal 반환 + return drawResponseGenerateUtil.generateDrawLoserResponse(userId); // LoseModal 반환 } } - /** - * 낙첨자 응답 만들어서 반환 - * - * @param userId 를 이용하여 공유 url 조회 - * @return 낙첨자 응답 - */ - private DrawLoseModalResponseDto responseLoseModal(Integer userId) { - String shareUrl = getShareUrl(userId); - - return DrawLoseModalResponseDto.builder() - .isDrawWin(false) - .images(drawUtil.generateLoseImages()) - .shareUrl(shareUrl) - .build(); - } - - /** - * 당첨자 응답 만들어서 반환 - * - * @return 당첨자 응답 - */ - private DrawWinModalResponseDto responseWinModal() { - return DrawWinModalResponseDto.builder() - .isDrawWin(true) - .images(drawUtil.generateWinImages()) - .winModal(drawUtil.generateWinModal()) - .build(); - } - @EventLock(key = "DRAW_WINNER_#{#ranking}") private boolean isWinner(Integer userId, int ranking, int winnerNum) { String drawWinnerKey = RedisKeyPrefix.DRAW_WINNER_LIST_PREFIX.getPrefix() + ranking; - Set drawWinnerSet = eventLockRedisUtil.getAllDataAsSet(drawWinnerKey); + Set drawWinnerSet = drawRedisUtil.getAllDataAsSet(drawWinnerKey); // 레디스에서 해당 랭킹에 자리가 있는지 확인 if (drawWinnerSet.size() < winnerNum) { // 자리가 있다면 당첨 성공. 당첨자 리스트에 추가 - eventLockRedisUtil.addValueToSet(drawWinnerKey, userId); + drawRedisUtil.setIntegerValueToSet(drawWinnerKey, userId); return true; } else { // 이미 자리가 가득 차서 당첨 실패 @@ -218,7 +140,7 @@ private boolean isWinner(Integer userId, int ranking, int winnerNum) { @EventLock(key = "DRAW_PARTICIPATION_COUNT") private void increaseDrawParticipationCount() { - eventLockRedisUtil.incrementData(RedisKeyPrefix.DRAW_PARTICIPANT_COUNT_PREFIX.getPrefix()); + drawRedisUtil.incrementIntegerValue(RedisKeyPrefix.DRAW_PARTICIPANT_COUNT_PREFIX.getPrefix()); } /** @@ -229,24 +151,16 @@ private void increaseDrawParticipationCount() { * @param userId 사용자 아이디 * @return 당첨 내역에 따른 응답 */ - public ResponseDto getDrawHistory(Integer userId) { + public DrawHistoryResponseDto getDrawHistory(Integer userId) { int ranking = getRankingIfWinner(userId); if (ranking != 0) { // 당첨자라면 - drawUtil.setRanking(ranking); - return ResponseDto.onSuccess(DrawHistoryWinnerResponseDto.builder() - .isDrawWin(true) - .winModal(drawUtil.generateWinModalForHistory()) - .build()); + return drawResponseGenerateUtil.generateDrawHistoryWinnerResponse(ranking); } // 당첨자가 아니라면 - String shareUrl = getShareUrl(userId); - return ResponseDto.onSuccess(DrawHistoryLoserResponseDto.builder() - .isDrawWin(false) - .shareUrl(shareUrl) - .build()); + return drawResponseGenerateUtil.generateDrawHistoryLoserResponse(userId); } /** @@ -255,25 +169,14 @@ public ResponseDto getDrawHistory(Integer userId) { * @param userId 사용자 아이디 */ private int getRankingIfWinner(int userId) { - String drawTempKey; + String drawWinnerKey; for (int ranking = 1; ranking < 4; ranking++) { - drawTempKey = RedisKeyPrefix.DRAW_WINNER_LIST_PREFIX.getPrefix() + ranking; - Set drawTempSet = eventLockRedisUtil.getAllDataAsSet(drawTempKey); + drawWinnerKey = RedisKeyPrefix.DRAW_WINNER_LIST_PREFIX.getPrefix() + ranking; + Set drawTempSet = drawRedisUtil.getAllDataAsSet(drawWinnerKey); if (drawTempSet.contains(userId)) { return ranking; } } return 0; } - - /** - * 공유 url 조회 - * - * @param userId 사용자 아이디 - * @return 공유 url - */ - private String getShareUrl(Integer userId) { - return staticResourcesUtil.getData("BASE_URL") + shareUrlInfoRepository.findShareUrlByUserId(userId) - .orElseThrow(() -> new ShareUrlInfoException(ErrorStatus._NOT_FOUND)); - } } diff --git a/src/main/java/com/softeer/backend/fo_domain/draw/util/DrawModalGenerateUtil.java b/src/main/java/com/softeer/backend/fo_domain/draw/util/DrawModalGenerateUtil.java new file mode 100644 index 00000000..c59da10d --- /dev/null +++ b/src/main/java/com/softeer/backend/fo_domain/draw/util/DrawModalGenerateUtil.java @@ -0,0 +1,114 @@ +package com.softeer.backend.fo_domain.draw.util; + +import com.softeer.backend.fo_domain.draw.dto.main.DrawMainFullAttendResponseDto; +import com.softeer.backend.fo_domain.draw.dto.participate.DrawWinModalResponseDto; +import com.softeer.backend.fo_domain.draw.dto.result.DrawHistoryWinnerResponseDto; +import com.softeer.backend.global.staticresources.util.StaticResourcesUtil; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class DrawModalGenerateUtil { + private final StaticResourcesUtil staticResourcesUtil; + + /** + * 7일 연속 출석자 상품 정보 반환 메서드 + * + * @return FullAttendModal 반환 + */ + public DrawMainFullAttendResponseDto.FullAttendModal generateFullAttendModal() { + return DrawMainFullAttendResponseDto.FullAttendModal.builder() + .title(staticResourcesUtil.getData("FULL_ATTEND_MODAL_TITLE")) + .subtitle(staticResourcesUtil.getData("FULL_ATTEND_MODAL_SUBTITLE")) + .image(staticResourcesUtil.getData("attendance_reward_image")) + .description(staticResourcesUtil.getData("FULL_ATTEND_MODAL_DESCRIPTION")) + .build(); + } + + /** + * @return 등수에 따른 WinModal을 반환 + */ + public DrawWinModalResponseDto.WinModal generateWinModal(int ranking) { + if (ranking == 1) { + return generateFirstWinModal(); + } else if (ranking == 2) { + return generateSecondWinModal(); + } else { + return generateThirdWinModal(); + } + } + + /** + * @return 1등 WinModal 반환 + */ + private DrawWinModalResponseDto.WinModal generateFirstWinModal() { + return DrawWinModalResponseDto.WinModal.builder() + .title(staticResourcesUtil.getData("DRAW_WINNER_MODAL_TITLE")) + .subtitle(staticResourcesUtil.getData("DRAW_FIRST_WINNER_MODAL_SUBTITLE")) + .img(staticResourcesUtil.getData("draw_reward_image_1")) + .description(staticResourcesUtil.getData("DRAW_WINNER_MODAL_DESCRIPTION")) + .build(); + } + + /** + * @return 2등 WinModal 반환 + */ + private DrawWinModalResponseDto.WinModal generateSecondWinModal() { + return DrawWinModalResponseDto.WinModal.builder() + .title(staticResourcesUtil.getData("DRAW_WINNER_MODAL_TITLE")) + .subtitle(staticResourcesUtil.getData("DRAW_SECOND_WINNER_MODAL_SUBTITLE")) + .img(staticResourcesUtil.getData("draw_reward_image_2")) + .description(staticResourcesUtil.getData("DRAW_WINNER_MODAL_DESCRIPTION")) + .build(); + } + + /** + * @return 3등 WinModal 반환 + */ + private DrawWinModalResponseDto.WinModal generateThirdWinModal() { + return DrawWinModalResponseDto.WinModal.builder() + .title(staticResourcesUtil.getData("DRAW_WINNER_MODAL_TITLE")) + .subtitle(staticResourcesUtil.getData("DRAW_THIRD_WINNER_MODAL_SUBTITLE")) + .img(staticResourcesUtil.getData("draw_reward_image_3")) + .description(staticResourcesUtil.getData("DRAW_WINNER_MODAL_DESCRIPTION")) + .build(); + } + + public DrawHistoryWinnerResponseDto.WinModal generateWinModalForHistory(int ranking) { + if (ranking == 1) { + return generateFirstWinModalForHistory(); + } else if (ranking == 2) { + return generateSecondWinModalForHistory(); + } else { + return generateThirdWinModalForHistory(); + } + } + + private DrawHistoryWinnerResponseDto.WinModal generateFirstWinModalForHistory() { + return DrawHistoryWinnerResponseDto.WinModal.builder() + .title(staticResourcesUtil.getData("DRAW_WINNER_MODAL_TITLE")) + .subtitle(staticResourcesUtil.getData("DRAW_FIRST_WINNER_MODAL_SUBTITLE")) + .img(staticResourcesUtil.getData("draw_reward_image_1")) + .description(staticResourcesUtil.getData("DRAW_WINNER_MODAL_DESCRIPTION")) + .build(); + } + + private DrawHistoryWinnerResponseDto.WinModal generateSecondWinModalForHistory() { + return DrawHistoryWinnerResponseDto.WinModal.builder() + .title(staticResourcesUtil.getData("DRAW_WINNER_MODAL_TITLE")) + .subtitle(staticResourcesUtil.getData("DRAW_SECOND_WINNER_MODAL_SUBTITLE")) + .img(staticResourcesUtil.getData("draw_reward_image_2")) + .description(staticResourcesUtil.getData("DRAW_WINNER_MODAL_DESCRIPTION")) + .build(); + } + + private DrawHistoryWinnerResponseDto.WinModal generateThirdWinModalForHistory() { + return DrawHistoryWinnerResponseDto.WinModal.builder() + .title(staticResourcesUtil.getData("DRAW_WINNER_MODAL_TITLE")) + .subtitle(staticResourcesUtil.getData("DRAW_THIRD_WINNER_MODAL_SUBTITLE")) + .img(staticResourcesUtil.getData("draw_reward_image_3")) + .description(staticResourcesUtil.getData("DRAW_WINNER_MODAL_DESCRIPTION")) + .build(); + } +} diff --git a/src/main/java/com/softeer/backend/fo_domain/draw/util/DrawResponseGenerateUtil.java b/src/main/java/com/softeer/backend/fo_domain/draw/util/DrawResponseGenerateUtil.java new file mode 100644 index 00000000..23bbb2ad --- /dev/null +++ b/src/main/java/com/softeer/backend/fo_domain/draw/util/DrawResponseGenerateUtil.java @@ -0,0 +1,118 @@ +package com.softeer.backend.fo_domain.draw.util; + +import com.softeer.backend.fo_domain.draw.dto.main.DrawMainFullAttendResponseDto; +import com.softeer.backend.fo_domain.draw.dto.main.DrawMainResponseDto; +import com.softeer.backend.fo_domain.draw.dto.participate.DrawLoseModalResponseDto; +import com.softeer.backend.fo_domain.draw.dto.participate.DrawWinModalResponseDto; +import com.softeer.backend.fo_domain.draw.dto.result.DrawHistoryLoserResponseDto; +import com.softeer.backend.fo_domain.draw.dto.result.DrawHistoryWinnerResponseDto; +import com.softeer.backend.fo_domain.share.exception.ShareUrlInfoException; +import com.softeer.backend.fo_domain.share.repository.ShareUrlInfoRepository; +import com.softeer.backend.global.common.code.status.ErrorStatus; +import com.softeer.backend.global.staticresources.util.StaticResourcesUtil; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class DrawResponseGenerateUtil { + private final ShareUrlInfoRepository shareUrlInfoRepository; + private final DrawUtil drawUtil; + private final DrawModalGenerateUtil drawModalGenerateUtil; + private final StaticResourcesUtil staticResourcesUtil; + + /** + * 7일 연속 출석 시 상품 정보 모달 만들어서 반환하는 메서드 + * + * @param invitedNum 초대한 사람 수 + * @param remainDrawCount 남은 추첨 기회 + * @param drawParticipationCount 연속 출석 일수 + * @return 7일 연속 출석 상품 모달 + */ + public DrawMainFullAttendResponseDto generateMainFullAttendResponse(int invitedNum, int remainDrawCount, int drawParticipationCount) { + return DrawMainFullAttendResponseDto.builder() + .invitedNum(invitedNum) + .remainDrawCount(remainDrawCount) + .drawParticipationCount(drawParticipationCount) + .fullAttendModal(drawModalGenerateUtil.generateFullAttendModal()) + .build(); + } + + /** + * 7일 미만 출석 시 모달 만들어서 반환하는 메서드 + * + * @param invitedNum 초대한 사람 수 + * @param remainDrawCount 남은 추첨 기회 + * @param drawParticipationCount 연속 출석 일수 + * @return 7일 미만 출석 상품 모달 + */ + public DrawMainResponseDto generateMainNotAttendResponse(int invitedNum, int remainDrawCount, int drawParticipationCount) { + return DrawMainResponseDto.builder() + .invitedNum(invitedNum) + .remainDrawCount(remainDrawCount) + .drawParticipationCount(drawParticipationCount) + .build(); + } + + /** + * 낙첨자 응답 만들어서 반환 + * + * @param userId 를 이용하여 공유 url 조회 + * @return 낙첨자 응답 + */ + public DrawLoseModalResponseDto generateDrawLoserResponse(Integer userId) { + return DrawLoseModalResponseDto.builder() + .isDrawWin(false) + .images(drawUtil.generateLoseImages()) + .shareUrl(getShareUrl(userId)) + .build(); + } + + /** + * 당첨자 응답 만들어서 반환 + * + * @return 당첨자 응답 + */ + public DrawWinModalResponseDto generateDrawWinnerResponse(int ranking) { + return DrawWinModalResponseDto.builder() + .isDrawWin(true) + .images(drawUtil.generateWinImages()) + .winModal(drawModalGenerateUtil.generateWinModal(ranking)) + .build(); + } + + /** + * 당첨내역이 있는 경우 당첨 내역 응답 만들어서 반환 + * @param ranking 등수 + * @return 당첨 내역 응답 + */ + public DrawHistoryWinnerResponseDto generateDrawHistoryWinnerResponse(int ranking) { + return DrawHistoryWinnerResponseDto.builder() + .isDrawWin(true) + .winModal(drawModalGenerateUtil.generateWinModalForHistory(ranking)) + .build(); + } + + /** + * 당첨내역이 없는 경우 낙첨 응답 만들어서 반환 + * @param userId 사용자 아이디 + * @return 낙첨 내역 응답 + */ + public DrawHistoryLoserResponseDto generateDrawHistoryLoserResponse(Integer userId) { + return DrawHistoryLoserResponseDto.builder() + .isDrawWin(false) + .shareUrl(getShareUrl(userId)) + .build(); + } + + /** + * 공유 url 조회 + * + * @param userId 사용자 아이디 + * @return 공유 url + */ + private String getShareUrl(Integer userId) { + return staticResourcesUtil.getData("BASE_URL") + shareUrlInfoRepository.findShareUrlByUserId(userId) + .orElseThrow(() -> new ShareUrlInfoException(ErrorStatus._NOT_FOUND)); + } +} diff --git a/src/main/java/com/softeer/backend/fo_domain/draw/util/DrawUtil.java b/src/main/java/com/softeer/backend/fo_domain/draw/util/DrawUtil.java index cdb15cd9..0ecc204c 100644 --- a/src/main/java/com/softeer/backend/fo_domain/draw/util/DrawUtil.java +++ b/src/main/java/com/softeer/backend/fo_domain/draw/util/DrawUtil.java @@ -1,8 +1,5 @@ package com.softeer.backend.fo_domain.draw.util; -import com.softeer.backend.fo_domain.draw.dto.main.DrawMainFullAttendResponseDto; -import com.softeer.backend.fo_domain.draw.dto.participate.DrawWinModalResponseDto; -import com.softeer.backend.fo_domain.draw.dto.result.DrawHistoryWinnerResponseDto; import com.softeer.backend.global.staticresources.util.StaticResourcesUtil; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -102,104 +99,4 @@ private String getImageUrl(int direction) { } return directionImage; } - - /** - * @return 등수에 따른 WinModal을 반환 - */ - public DrawWinModalResponseDto.WinModal generateWinModal() { - if (ranking == 1) { - return generateFirstWinModal(); - } else if (ranking == 2) { - return generateSecondWinModal(); - } else { - return generateThirdWinModal(); - } - } - - /** - * @return 1등 WinModal 반환 - */ - private DrawWinModalResponseDto.WinModal generateFirstWinModal() { - return DrawWinModalResponseDto.WinModal.builder() - .title(staticResourcesUtil.getData("DRAW_WINNER_MODAL_TITLE")) - .subtitle(staticResourcesUtil.getData("DRAW_FIRST_WINNER_MODAL_SUBTITLE")) - .img(staticResourcesUtil.getData("draw_reward_image_1")) - .description(staticResourcesUtil.getData("DRAW_WINNER_MODAL_DESCRIPTION")) - .build(); - } - - /** - * @return 2등 WinModal 반환 - */ - private DrawWinModalResponseDto.WinModal generateSecondWinModal() { - return DrawWinModalResponseDto.WinModal.builder() - .title(staticResourcesUtil.getData("DRAW_WINNER_MODAL_TITLE")) - .subtitle(staticResourcesUtil.getData("DRAW_SECOND_WINNER_MODAL_SUBTITLE")) - .img(staticResourcesUtil.getData("draw_reward_image_2")) - .description(staticResourcesUtil.getData("DRAW_WINNER_MODAL_DESCRIPTION")) - .build(); - } - - /** - * @return 3등 WinModal 반환 - */ - private DrawWinModalResponseDto.WinModal generateThirdWinModal() { - return DrawWinModalResponseDto.WinModal.builder() - .title(staticResourcesUtil.getData("DRAW_WINNER_MODAL_TITLE")) - .subtitle(staticResourcesUtil.getData("DRAW_THIRD_WINNER_MODAL_SUBTITLE")) - .img(staticResourcesUtil.getData("draw_reward_image_3")) - .description(staticResourcesUtil.getData("DRAW_WINNER_MODAL_DESCRIPTION")) - .build(); - } - - /** - * 7일 연속 출석자 상품 정보 반환 메서드 - * - * @return FullAttendModal 반환 - */ - public DrawMainFullAttendResponseDto.FullAttendModal generateFullAttendModal() { - return DrawMainFullAttendResponseDto.FullAttendModal.builder() - .title(staticResourcesUtil.getData("FULL_ATTEND_MODAL_TITLE")) - .subtitle(staticResourcesUtil.getData("FULL_ATTEND_MODAL_SUBTITLE")) - .image(staticResourcesUtil.getData("attendance_reward_image")) - .description(staticResourcesUtil.getData("FULL_ATTEND_MODAL_DESCRIPTION")) - .build(); - } - - public DrawHistoryWinnerResponseDto.WinModal generateWinModalForHistory() { - if (ranking == 1) { - return generateFirstWinModalForHistory(); - } else if (ranking == 2) { - return generateSecondWinModalForHistory(); - } else { - return generateThirdWinModalForHistory(); - } - } - - private DrawHistoryWinnerResponseDto.WinModal generateFirstWinModalForHistory() { - return DrawHistoryWinnerResponseDto.WinModal.builder() - .title(staticResourcesUtil.getData("DRAW_WINNER_MODAL_TITLE")) - .subtitle(staticResourcesUtil.getData("DRAW_FIRST_WINNER_MODAL_SUBTITLE")) - .img(staticResourcesUtil.getData("draw_reward_image_1")) - .description(staticResourcesUtil.getData("DRAW_WINNER_MODAL_DESCRIPTION")) - .build(); - } - - private DrawHistoryWinnerResponseDto.WinModal generateSecondWinModalForHistory() { - return DrawHistoryWinnerResponseDto.WinModal.builder() - .title(staticResourcesUtil.getData("DRAW_WINNER_MODAL_TITLE")) - .subtitle(staticResourcesUtil.getData("DRAW_SECOND_WINNER_MODAL_SUBTITLE")) - .img(staticResourcesUtil.getData("draw_reward_image_2")) - .description(staticResourcesUtil.getData("DRAW_WINNER_MODAL_DESCRIPTION")) - .build(); - } - - private DrawHistoryWinnerResponseDto.WinModal generateThirdWinModalForHistory() { - return DrawHistoryWinnerResponseDto.WinModal.builder() - .title(staticResourcesUtil.getData("DRAW_WINNER_MODAL_TITLE")) - .subtitle(staticResourcesUtil.getData("DRAW_THIRD_WINNER_MODAL_SUBTITLE")) - .img(staticResourcesUtil.getData("draw_reward_image_3")) - .description(staticResourcesUtil.getData("DRAW_WINNER_MODAL_DESCRIPTION")) - .build(); - } } diff --git a/src/main/java/com/softeer/backend/fo_domain/share/controller/ShareController.java b/src/main/java/com/softeer/backend/fo_domain/share/controller/ShareController.java index 6b24c343..88b27ec9 100644 --- a/src/main/java/com/softeer/backend/fo_domain/share/controller/ShareController.java +++ b/src/main/java/com/softeer/backend/fo_domain/share/controller/ShareController.java @@ -22,7 +22,7 @@ public class ShareController { @GetMapping("/share-shorten-url") public ResponseDto getShortenShareUrl(@Parameter(hidden = true) @AuthInfo Integer userId) { - return shareUrlInfoService.getShortenShareUrl(userId); + return ResponseDto.onSuccess(shareUrlInfoService.getShortenShareUrl(userId)); } @GetMapping("/share/{shareUrl}") diff --git a/src/main/java/com/softeer/backend/fo_domain/share/service/ShareUrlInfoService.java b/src/main/java/com/softeer/backend/fo_domain/share/service/ShareUrlInfoService.java index 63681a9f..9a466cbe 100644 --- a/src/main/java/com/softeer/backend/fo_domain/share/service/ShareUrlInfoService.java +++ b/src/main/java/com/softeer/backend/fo_domain/share/service/ShareUrlInfoService.java @@ -4,7 +4,6 @@ import com.softeer.backend.fo_domain.share.exception.ShareInfoException; import com.softeer.backend.fo_domain.share.repository.ShareUrlInfoRepository; import com.softeer.backend.global.common.code.status.ErrorStatus; -import com.softeer.backend.global.common.response.ResponseDto; import com.softeer.backend.global.staticresources.util.StaticResourcesUtil; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -15,12 +14,12 @@ public class ShareUrlInfoService { private final ShareUrlInfoRepository shareUrlInfoRepository; private final StaticResourcesUtil staticResourcesUtil; - public ResponseDto getShortenShareUrl(Integer userId) { + public ShareUrlInfoResponseDto getShortenShareUrl(Integer userId) { if (userId == null) { // 로그인하지 않은 사용자 - return ResponseDto.onSuccess(ShareUrlInfoResponseDto.builder() + return ShareUrlInfoResponseDto.builder() .shareUrl(staticResourcesUtil.getData("NON_USER_SHARE_URL")) - .build()); + .build(); } else { // 로그인한 사용자 String shareUrl = shareUrlInfoRepository.findShareUrlByUserId(userId).orElseThrow( @@ -28,9 +27,9 @@ public ResponseDto getShortenShareUrl(Integer userId) { ); // DB에 이미 생성된 단축 url 반환 - return ResponseDto.onSuccess(ShareUrlInfoResponseDto.builder() + return ShareUrlInfoResponseDto.builder() .shareUrl(staticResourcesUtil.getData("BASE_URL") + shareUrl) - .build()); + .build(); } } } diff --git a/src/main/java/com/softeer/backend/fo_domain/user/service/LoginService.java b/src/main/java/com/softeer/backend/fo_domain/user/service/LoginService.java index 55ad294a..34d2365a 100644 --- a/src/main/java/com/softeer/backend/fo_domain/user/service/LoginService.java +++ b/src/main/java/com/softeer/backend/fo_domain/user/service/LoginService.java @@ -137,7 +137,7 @@ private void createDrawParticipationInfo(Integer userId) { .userId(userId) .drawWinningCount(0) .drawLosingCount(0) - .drawParticipationCount(0) + .drawParticipationCount(1) .build(); drawParticipationInfoRepository.save(drawParticipationInfo); diff --git a/src/main/java/com/softeer/backend/global/util/DrawRedisUtil.java b/src/main/java/com/softeer/backend/global/util/DrawRedisUtil.java new file mode 100644 index 00000000..8b0fe382 --- /dev/null +++ b/src/main/java/com/softeer/backend/global/util/DrawRedisUtil.java @@ -0,0 +1,44 @@ +package com.softeer.backend.global.util; + +import lombok.RequiredArgsConstructor; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; + +import java.util.Set; + +/** + * 추첨 이벤트에서 사용할 레디스 + */ +@Component +@RequiredArgsConstructor +public class DrawRedisUtil { + private final RedisTemplate integerRedisTemplate; + + // 추첨 당첨자 목록: DRAW_WINNER_LIST_{ranking}, Set + // 추첨 참여자 수: DRAW_PARTICIPANT_COUNT, Integer + + // 추첨 참여자 수 증가 + public void incrementIntegerValue(String key) { + integerRedisTemplate.opsForValue().increment(key); + } + + // ranking의 추첨 당첨자 목록 반환 + public Set getAllDataAsSet(String key) { + return integerRedisTemplate.opsForSet().members(key); + } + + // ranking의 당첨자 목록 업데이트 + public void setIntegerValueToSet(String key, Integer userId) { + integerRedisTemplate.opsForSet().add(key, userId); + } + + // ranking의 Set 값 모두 삭제 + public void deleteAllSetData(String key) { + integerRedisTemplate.delete(key); + } + + // 참여자 수 삭제 + public void deleteIntegerValue(String key) { + integerRedisTemplate.delete(key); + } +}