Skip to content

Commit

Permalink
Merge pull request #47 from KNU-HAEDAL/issue/43
Browse files Browse the repository at this point in the history
[Refactor]: 경험치 비정규화 로직 Usecase로 분리
  • Loading branch information
bayy1216 authored Jul 21, 2024
2 parents 84263be + a2d7ad6 commit 5c7cf1c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@Getter
@AllArgsConstructor(staticName = "of")
public class AddExpToChallengeGroupEvent {
public class AddUserExpByVerificationEvent {
private Long userId;
private Integer acquiredExp;
private Long challengeGroupId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.haedal.zzansuni.domain.challengegroup.userchallenge;

import lombok.RequiredArgsConstructor;
import org.haedal.zzansuni.domain.challengegroup.ChallengeGroupReader;
import org.haedal.zzansuni.domain.challengegroup.userexp.ChallengeGroupUserExp;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@RequiredArgsConstructor
public class AddUserExpByVerificationUseCase {
private final ChallengeGroupReader challengeGroupReader;

@Transactional
public void invoke(AddUserExpByVerificationEvent event) {
Long challengeGroupId = event.getChallengeGroupId();
Long userId = event.getUserId();

ChallengeGroupUserExp challengeGroupUserExp = challengeGroupReader
.findByChallengeGroupIdAndUserId(challengeGroupId, userId).orElseThrow();
challengeGroupUserExp.addExp(event.getAcquiredExp());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static UserChallenge create(Challenge challenge, User user) {
* 3. 챌린지를 완료한 경우 챌린지 완료로 변경, 경험치 추가
* 4. 챌린지그룹-경험치 추가 이벤트를 반환
*/
public AddExpToChallengeGroupEvent addChallengeVerification(ChallengeCommand.VerificationCreate command) {
public AddUserExpByVerificationEvent addChallengeVerification(ChallengeCommand.VerificationCreate command) {
ChallengeVerification challengeVerification = ChallengeVerification.create(command, this);
this.challengeVerifications.add(challengeVerification);

Expand All @@ -81,7 +81,7 @@ public AddExpToChallengeGroupEvent addChallengeVerification(ChallengeCommand.Ver
acquiredExp += challenge.getSuccessExp();
this.completeChallengeStatus();
}
return AddExpToChallengeGroupEvent
return AddUserExpByVerificationEvent
.of(user.getId(), acquiredExp, challenge.getChallengeGroupId());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.haedal.zzansuni.domain.challengegroup.ChallengeGroupReader;
import org.haedal.zzansuni.domain.challengegroup.userexp.ChallengeGroupUserExp;
import org.haedal.zzansuni.domain.challengegroup.userexp.ChallengeGroupUserExpStore;
import org.haedal.zzansuni.domain.challengegroup.challenge.Challenge;
Expand All @@ -29,8 +28,8 @@ public class UserChallengeService {
private final ChallengeReviewReader challengeReviewReader;
private final UserReader userReader;
private final ChallengeReader challengeReader;
private final ChallengeGroupReader challengeGroupReader;
private final ChallengeGroupUserExpStore challengeGroupUserExpStore;
private final AddUserExpByVerificationUseCase addUserExpByVerificationUseCase;
/**
* 챌린지 참여하기 <br>
* 1. 유저와 챌린지 정보를 받아서 UserChallenge 테이블에 데이터 추가
Expand Down Expand Up @@ -77,14 +76,10 @@ public ChallengeModel.ChallengeVerificationResult verification(
throw new IllegalArgumentException("해당 챌린지에 참여한 유저가 아닙니다.");
}


AddExpToChallengeGroupEvent addExpToChallengeGroupEvent = userChallenge.addChallengeVerification(command);
AddUserExpByVerificationEvent event = userChallenge.addChallengeVerification(command);

// 챌린지 경험치 획득 로직
Long challengeGroupId = userChallenge.getChallenge().getChallengeGroupId();
ChallengeGroupUserExp challengeGroupUserExp = challengeGroupReader
.findByChallengeGroupIdAndUserId(challengeGroupId, userId).orElseThrow();
challengeGroupUserExp.addExp(addExpToChallengeGroupEvent.getAcquiredExp());
addUserExpByVerificationUseCase.invoke(event);

// 챌린지 RequiredCount 가져오기 위해 챌린지 정보 가져온다
Challenge challenge = userChallenge.getChallenge();
Expand Down

0 comments on commit 5c7cf1c

Please sign in to comment.