Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] 데이터베이스 업데이트하는 로직 쿼리 사용하도록 리팩토링 #120

Merged
merged 63 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
b91a85f
infra: 빌드 테스트 yml 작성
DrRivaski Aug 4, 2024
ffd6afb
Merge branch 'refs/heads/develop' into feature/33
DrRivaski Aug 5, 2024
4c6ae62
infra: DB 정보 추가
DrRivaski Aug 5, 2024
ad7d92e
infra: ssh-agent 버전 변경
DrRivaski Aug 5, 2024
14d1887
infra: known_hosts 추가
DrRivaski Aug 5, 2024
31f31a8
infra: db port 변경
DrRivaski Aug 5, 2024
da5c7dc
infra: database test 설정 변경
DrRivaski Aug 5, 2024
05054ab
infra: DB 환경변수 설정 및 application.yml 생성
DrRivaski Aug 5, 2024
372f84c
infra: application.yml 동적 생성 스크립트 수정
DrRivaski Aug 5, 2024
57b7bd1
infra: 레디스 설정 추가
DrRivaski Aug 6, 2024
efb92c9
infra: redis test 추가
DrRivaski Aug 6, 2024
babd1a8
infra: redis 버전 변경
DrRivaski Aug 6, 2024
be93408
infra: redis cli 설치
DrRivaski Aug 6, 2024
212a34c
infra: application.yml 위치 및 내용 확인
DrRivaski Aug 6, 2024
db65184
infra: Github Actions 환경변수에 REDIS_HOST, REDIS_PORT 추가
DrRivaski Aug 6, 2024
4658edd
infra: 환경변수 확인 추가
DrRivaski Aug 6, 2024
bcfb880
Merge remote-tracking branch 'upstream/develop' into develop
DrRivaski Aug 6, 2024
1706d79
feat: draw_rank column 이름 수정
DrRivaski Aug 6, 2024
8d5e4c8
Merge remote-tracking branch 'upstream/develop' into develop
DrRivaski Aug 6, 2024
55edd9c
infra: pull_request시 Github Actions 동작하도록 수정
DrRivaski Aug 6, 2024
f1ebddf
Merge remote-tracking branch 'upstream/develop' into develop
DrRivaski Aug 6, 2024
89b5c0f
cicd test (#54)
hyeokson Aug 6, 2024
409fcd1
[Feat] 기대평 기능 구현 (#57)
hyeokson Aug 8, 2024
d42d605
cicd test (#58)
hyeokson Aug 9, 2024
72ee18b
Merge remote-tracking branch 'upstream/develop' into develop
DrRivaski Aug 9, 2024
3122173
Merge remote-tracking branch 'upstream/develop' into develop
DrRivaski Aug 9, 2024
0e2f44d
[Feat] 성공, 실패 상태 코드 수정하기 (#65)
hyeokson Aug 10, 2024
d27eb9d
[Feat]Admin Api 구현 (#67)
hyeokson Aug 10, 2024
9faa347
infra: 레디스 설정 추가
DrRivaski Aug 6, 2024
6fbc9c8
[Feat] 기대평 기능 구현 (#57)
hyeokson Aug 8, 2024
a63f49a
[Refactor] 공유 url 조회하는 api 수정 (#70)
DrRivaski Aug 11, 2024
402432f
[Refactor] 공유 url 조회하는 api 수정 (#70)
DrRivaski Aug 11, 2024
a88eed7
[Feature] static text를 관리하는 enum 구현 (#71)
hyeokson Aug 11, 2024
cf8557f
[Refactor] MainPageController 클래스명 변경 (#72)
hyeokson Aug 11, 2024
f26769f
[Feature] 메인 페이지 GET API 구현하기 (#73)
hyeokson Aug 11, 2024
298efef
[Feature] 어드민 회원가입 기능 구현 (#75)
hyeokson Aug 11, 2024
315b0a6
[Feature] 특정 url에 대해 인가 검사 하지 않도록 구현 (#76)
hyeokson Aug 11, 2024
8675b99
[Refactor] 공유 url 조회하는 api 수정 (#70)
DrRivaski Aug 11, 2024
7c2c6e6
[Refactor] 사용자 생성 시 추첨 참여 정보, 공유 링크 정보, 공유 정보 생성 (#78)
DrRivaski Aug 12, 2024
9c59a7d
[Refactor] 공유 url 조회하는 api 수정 (#70)
DrRivaski Aug 11, 2024
193b559
[Refactor] 사용자 생성 시 추첨 참여 정보, 공유 링크 정보, 공유 정보 생성 (#78)
DrRivaski Aug 12, 2024
21e3305
[Refactor] MainPageCarResponseDto 필드 변경 (#79)
hyeokson Aug 12, 2024
d870037
Merge remote-tracking branch 'upstream/develop' into develop
DrRivaski Aug 12, 2024
d9d5e5e
Merge branch 'develop' of https://github.com/softeerbootcamp4th/Team2…
DrRivaski Aug 12, 2024
f3f7d76
Merge remote-tracking branch 'upstream/develop' into develop
DrRivaski Aug 12, 2024
77d7037
Merge remote-tracking branch 'upstream/develop' into develop
DrRivaski Aug 12, 2024
3503af4
Merge remote-tracking branch 'upstream/develop' into develop
DrRivaski Aug 13, 2024
28b28be
Merge remote-tracking branch 'upstream/develop' into develop
DrRivaski Aug 13, 2024
1ae74fe
Merge remote-tracking branch 'upstream/develop' into develop
DrRivaski Aug 14, 2024
8c56c1c
Merge remote-tracking branch 'upstream/develop' into develop
DrRivaski Aug 14, 2024
291b5ab
Merge remote-tracking branch 'upstream/develop' into develop
DrRivaski Aug 14, 2024
2730180
Merge remote-tracking branch 'upstream/develop' into develop
DrRivaski Aug 14, 2024
eb472b7
Merge remote-tracking branch 'upstream/develop' into develop
DrRivaski Aug 14, 2024
f08f9ec
Merge remote-tracking branch 'upstream/develop' into develop
DrRivaski Aug 15, 2024
e6165b5
Merge remote-tracking branch 'upstream/develop' into develop
DrRivaski Aug 15, 2024
2c6712a
Merge remote-tracking branch 'upstream/develop' into develop
DrRivaski Aug 15, 2024
2c67299
Merge remote-tracking branch 'upstream/develop' into develop
DrRivaski Aug 15, 2024
542538c
Merge remote-tracking branch 'upstream/develop' into develop
DrRivaski Aug 15, 2024
30eb41e
feat: 쿼리를 통해 당첨 횟수, 낙첨 횟수 업데이트하는 메서드 추가
DrRivaski Aug 16, 2024
4594edc
feat: 쿼리를 통해 남은 추첨 횟수 업데이트하는 메서드 추가
DrRivaski Aug 16, 2024
e9764f0
feat: 쿼리를 통해 당첨 횟수, 낙첨 횟수, 남은 추첨 횟수 업데이트하도록 수정
DrRivaski Aug 16, 2024
c328a77
chore: 사용하지 않는 메서드 삭제
DrRivaski Aug 16, 2024
9cca38c
chore: 사용하지 않는 변수 삭제
DrRivaski Aug 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<DrawParticipationInfo, Integer> {
Optional<DrawParticipationInfo> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,19 @@ public ResponseDto<DrawModalResponseDto> participateDrawEvent(Integer userId) {
ShareInfo shareInfo = shareInfoRepository.findShareInfoByUserId(userId)
.orElseThrow(() -> new ShareInfoException(ErrorStatus._NOT_FOUND));

int invitedNum = shareInfo.getInvitedNum();
int remainDrawCount = shareInfo.getRemainDrawCount();

// 만약 남은 참여 기회가 0이라면
if (remainDrawCount == 0) {
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 반환
}

Expand All @@ -144,7 +140,7 @@ public ResponseDto<DrawModalResponseDto> participateDrawEvent(Integer userId) {
drawUtil.performDraw();

if (drawUtil.isDrawWin()) { // 당첨자일 경우
decreaseRemainDrawCount(userId, invitedNum, remainDrawCount); // 횟수 1회 차감
shareInfoRepository.decreaseRemainDrawCount(userId); // 횟수 1회 차감

ranking = drawUtil.getRanking();
int winnerNum;
Expand All @@ -159,18 +155,18 @@ public ResponseDto<DrawModalResponseDto> 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 반환
}
}
Expand Down Expand Up @@ -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;
Expand All @@ -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과 같은 당첨 내역 모달 응답
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -15,4 +16,9 @@ public interface ShareInfoRepository extends JpaRepository<ShareInfo, Integer> {
@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);
}
Loading