-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* [feat] RandomUtis getPositiveNumber 메서드 생성 1~max까지의 값을 반환하는 메서드 생성 * [feat] 1등 추첨 시 에러 처리를 위한 에러 코드 추가 * [feat] 1등 추첨 응답을 위한 response 클래스 생성 * [feat] 1등 추첨 응답을 위한 service 클래스 생성 날짜 및 회원 정보 조회 검사 후 추첨 진행 날짜가 유효하지 않을 시 추첨 불가능 * [docs] TopPrizeWinnerSerivce jacoco 문서 추가 * [feat] 1등 추첨 시 검사를 위한 에러코드 추가 * [feat] 1등 추첨 api 추가 * [docs] javadoc 문서 미비한 부분 수정 * [feat] originalUrl 반환하는 응답 클래스 * [feat] 변환된 응답 타입을 이용한 서비스 로직 수정 * [feat] 변환된 응답 타입을 이용한 컨트롤러 로직 수정
- Loading branch information
Showing
14 changed files
with
172 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/softeer/team_pineapple_be/domain/admin/response/TopPrizeWinnerResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package softeer.team_pineapple_be.domain.admin.response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
/** | ||
* 1등 응답을 위한 response클래스 | ||
*/ | ||
@Getter | ||
@AllArgsConstructor | ||
public class TopPrizeWinnerResponse { | ||
private String phoneNumber; | ||
} |
86 changes: 86 additions & 0 deletions
86
src/main/java/softeer/team_pineapple_be/domain/admin/service/TopPrizeWinnerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package softeer.team_pineapple_be.domain.admin.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import softeer.team_pineapple_be.domain.admin.domain.EventDayInfo; | ||
import softeer.team_pineapple_be.domain.admin.exception.AdminErrorCode; | ||
import softeer.team_pineapple_be.domain.admin.repisotory.EventDayInfoRepository; | ||
import softeer.team_pineapple_be.domain.admin.response.TopPrizeWinnerResponse; | ||
import softeer.team_pineapple_be.domain.draw.domain.DrawHistory; | ||
import softeer.team_pineapple_be.domain.draw.domain.DrawRewardInfo; | ||
import softeer.team_pineapple_be.domain.draw.exception.DrawErrorCode; | ||
import softeer.team_pineapple_be.domain.draw.repository.DrawHistoryRepository; | ||
import softeer.team_pineapple_be.domain.draw.repository.DrawRewardInfoRepository; | ||
import softeer.team_pineapple_be.global.common.utils.RandomUtils; | ||
import softeer.team_pineapple_be.global.exception.RestApiException; | ||
|
||
import java.time.LocalDate; | ||
|
||
/** | ||
* 1등 추첨을 위한 클래스 | ||
*/ | ||
@Service | ||
@RequiredArgsConstructor | ||
public class TopPrizeWinnerService { | ||
|
||
private final EventDayInfoRepository eventDayInfoRepository; | ||
private final DrawHistoryRepository drawHistoryRepository; | ||
private final DrawRewardInfoRepository drawRewardInfoRepository; | ||
|
||
private static final int SCHEDULE_LENGTH = 14; | ||
private static final byte TOP_PRIZE_REWARD_ID = 1; | ||
|
||
/** | ||
* 1등을 추첨하는 메서드 | ||
* @return 1등 당첨된 유저에 대한 핸드폰 번호를 담고있는 객체 | ||
*/ | ||
@Transactional | ||
public TopPrizeWinnerResponse getTopPrizeWinner() { | ||
validateDrawDate(); | ||
|
||
long totalCount = drawHistoryRepository.count(); | ||
Long topPrizeIndex = RandomUtils.getPositiveNumber(totalCount); | ||
|
||
DrawHistory drawHistory = findDrawHistoryById(topPrizeIndex); | ||
DrawRewardInfo drawRewardInfo = findDrawRewardInfoById(TOP_PRIZE_REWARD_ID); | ||
|
||
processReward(drawRewardInfo); | ||
saveTopPrizeDrawHistory(drawHistory); | ||
|
||
return new TopPrizeWinnerResponse(drawHistory.getPhoneNumber()); | ||
} | ||
|
||
private void validateDrawDate() { | ||
EventDayInfo eventDayInfo = eventDayInfoRepository.findById(SCHEDULE_LENGTH) | ||
.orElseThrow(() -> new RestApiException(AdminErrorCode.NOT_EVENT_DAY)); | ||
|
||
LocalDate drawDate = LocalDate.now(); | ||
if (!drawDate.isAfter(eventDayInfo.getEventDate())) { | ||
throw new RestApiException(AdminErrorCode.CAN_NOT_DRAW_TOP_PRIZE_WINNER); | ||
} | ||
} | ||
|
||
private DrawHistory findDrawHistoryById(Long id) { | ||
return drawHistoryRepository.findById(id) | ||
.orElseThrow(() -> new RestApiException(DrawErrorCode.NOT_VALID_WINNER)); | ||
} | ||
|
||
private DrawRewardInfo findDrawRewardInfoById(byte id) { | ||
return drawRewardInfoRepository.findById(id) | ||
.orElseThrow(() -> new RestApiException(DrawErrorCode.NO_PRIZE)); | ||
} | ||
|
||
private void processReward(DrawRewardInfo drawRewardInfo) { | ||
if (drawRewardInfo.getStock() <= 0) { | ||
throw new RestApiException(DrawErrorCode.NO_PRIZE); | ||
} | ||
drawRewardInfo.decreaseStock(); | ||
drawRewardInfoRepository.save(drawRewardInfo); | ||
} | ||
|
||
private void saveTopPrizeDrawHistory(DrawHistory drawHistory) { | ||
DrawHistory topPrizeDrawHistory = new DrawHistory(TOP_PRIZE_REWARD_ID, drawHistory.getPhoneNumber()); | ||
drawHistoryRepository.save(topPrizeDrawHistory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...java/softeer/team_pineapple_be/global/shortenurl/response/ShortenOriginalUrlResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package softeer.team_pineapple_be.global.shortenurl.response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
/** | ||
* original url을 응답하는 클래스 | ||
*/ | ||
@Getter | ||
@AllArgsConstructor | ||
public class ShortenOriginalUrlResponse { | ||
private String originalUrl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters