diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/userchallenge/AddExpToChallengeGroupEvent.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/userchallenge/AddUserExpByVerificationEvent.java similarity index 85% rename from zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/userchallenge/AddExpToChallengeGroupEvent.java rename to zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/userchallenge/AddUserExpByVerificationEvent.java index ea486c6..28d92a5 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/userchallenge/AddExpToChallengeGroupEvent.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/userchallenge/AddUserExpByVerificationEvent.java @@ -5,7 +5,7 @@ @Getter @AllArgsConstructor(staticName = "of") -public class AddExpToChallengeGroupEvent { +public class AddUserExpByVerificationEvent { private Long userId; private Integer acquiredExp; private Long challengeGroupId; diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/userchallenge/AddUserExpByVerificationUseCase.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/userchallenge/AddUserExpByVerificationUseCase.java new file mode 100644 index 0000000..24e39f0 --- /dev/null +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/userchallenge/AddUserExpByVerificationUseCase.java @@ -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()); + } +} diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/userchallenge/UserChallenge.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/userchallenge/UserChallenge.java index a777b2a..09991ea 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/userchallenge/UserChallenge.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/userchallenge/UserChallenge.java @@ -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); @@ -81,7 +81,7 @@ public AddExpToChallengeGroupEvent addChallengeVerification(ChallengeCommand.Ver acquiredExp += challenge.getSuccessExp(); this.completeChallengeStatus(); } - return AddExpToChallengeGroupEvent + return AddUserExpByVerificationEvent .of(user.getId(), acquiredExp, challenge.getChallengeGroupId()); } diff --git a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/userchallenge/UserChallengeService.java b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/userchallenge/UserChallengeService.java index 594808b..7636a0f 100644 --- a/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/userchallenge/UserChallengeService.java +++ b/zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/challengegroup/userchallenge/UserChallengeService.java @@ -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; @@ -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; /** * 챌린지 참여하기
* 1. 유저와 챌린지 정보를 받아서 UserChallenge 테이블에 데이터 추가 @@ -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();