diff --git a/src/main/java/com/genius/gitget/challenge/certification/controller/CertificationController.java b/src/main/java/com/genius/gitget/challenge/certification/controller/CertificationController.java index 81330437..d276dd1d 100644 --- a/src/main/java/com/genius/gitget/challenge/certification/controller/CertificationController.java +++ b/src/main/java/com/genius/gitget/challenge/certification/controller/CertificationController.java @@ -14,7 +14,7 @@ import com.genius.gitget.challenge.instance.service.InstanceProvider; import com.genius.gitget.challenge.myChallenge.dto.ActivatedResponse; import com.genius.gitget.challenge.participant.domain.Participant; -import com.genius.gitget.challenge.participant.service.ParticipantProvider; +import com.genius.gitget.challenge.participant.service.ParticipantService; import com.genius.gitget.challenge.user.domain.User; import com.genius.gitget.challenge.user.service.UserService; import com.genius.gitget.global.security.domain.UserPrincipal; @@ -45,7 +45,7 @@ public class CertificationController { private final UserService userService; private final CertificationService certificationService; private final InstanceProvider instanceProvider; - private final ParticipantProvider participantProvider; + private final ParticipantService participantService; @GetMapping("/{instanceId}") @@ -92,7 +92,7 @@ public ResponseEntity> getWeekCertification( @PathVariable Long instanceId ) { LocalDate kstDate = DateUtil.convertToKST(LocalDateTime.now()); - Participant participant = participantProvider.findByJoinInfo(userPrincipal.getUser().getId(), instanceId); + Participant participant = participantService.findByJoinInfo(userPrincipal.getUser().getId(), instanceId); WeekResponse weekResponse = certificationService.getMyWeekCertifications(participant.getId(), kstDate); return ResponseEntity.ok().body( @@ -123,7 +123,7 @@ public ResponseEntity> getTotalCertifications( ) { LocalDate kstDate = DateUtil.convertToKST(LocalDateTime.now()); User user = userService.findUserById(userId); - Participant participant = participantProvider.findByJoinInfo(user.getId(), instanceId); + Participant participant = participantService.findByJoinInfo(user.getId(), instanceId); TotalResponse totalResponse = certificationService.getTotalCertification( participant.getId(), kstDate); @@ -140,7 +140,7 @@ public ResponseEntity> getCertification LocalDate kstDate = DateUtil.convertToKST(LocalDateTime.now()); Instance instance = instanceProvider.findById(instanceId); - Participant participant = participantProvider.findByJoinInfo( + Participant participant = participantService.findByJoinInfo( userPrincipal.getUser().getId(), instanceId); diff --git a/src/main/java/com/genius/gitget/challenge/certification/domain/Certification.java b/src/main/java/com/genius/gitget/challenge/certification/domain/Certification.java index a184182a..c60e9dab 100644 --- a/src/main/java/com/genius/gitget/challenge/certification/domain/Certification.java +++ b/src/main/java/com/genius/gitget/challenge/certification/domain/Certification.java @@ -1,5 +1,6 @@ package com.genius.gitget.challenge.certification.domain; +import static com.genius.gitget.challenge.certification.domain.CertificateStatus.NOT_YET; import static com.genius.gitget.challenge.certification.domain.CertificateStatus.PASSED; import com.genius.gitget.challenge.participant.domain.Participant; @@ -64,6 +65,15 @@ public static Certification createPassed(LocalDate certificatedAt) { .build(); } + public static Certification createDummy(LocalDate certificatedAt) { + return Certification.builder() + .currentAttempt(0) + .certificationStatus(NOT_YET) + .certificatedAt(certificatedAt) + .certificationLinks(null) + .build(); + } + //=== 비지니스 로직 ===// public void update(LocalDate certificatedAt, CertificateStatus status, String certificationLinks) { this.certificatedAt = certificatedAt; diff --git a/src/main/java/com/genius/gitget/challenge/certification/service/CertificationProvider.java b/src/main/java/com/genius/gitget/challenge/certification/service/CertificationProvider.java index 34ec34ad..b9e40b0b 100644 --- a/src/main/java/com/genius/gitget/challenge/certification/service/CertificationProvider.java +++ b/src/main/java/com/genius/gitget/challenge/certification/service/CertificationProvider.java @@ -7,6 +7,7 @@ import com.genius.gitget.challenge.certification.domain.Certification; import com.genius.gitget.challenge.certification.repository.CertificationRepository; import com.genius.gitget.challenge.certification.util.DateUtil; +import com.genius.gitget.challenge.instance.domain.Instance; import com.genius.gitget.challenge.participant.domain.Participant; import java.time.LocalDate; import java.util.List; @@ -50,6 +51,12 @@ targetDate, getCertificateStatus(pullRequests), getPrLinks(pullRequests) return certification; } + @Transactional + public Certification findOrGetDummy(LocalDate targetDate, Long participantId) { + return findByDate(targetDate, participantId) + .orElse(Certification.createDummy(targetDate)); + } + @Transactional public Certification createCertification(Participant participant, LocalDate targetDate, @@ -83,4 +90,12 @@ private CertificateStatus getCertificateStatus(List pullRequests) { } return CERTIFICATED; } + + public double getAchievementRate(Instance instance, Long participantId, LocalDate targetDate) { + int totalAttempt = instance.getTotalAttempt(); + int successCount = countByStatus(participantId, CERTIFICATED, targetDate); + + double successPercent = (double) successCount / (double) totalAttempt * 100; + return Math.round(successPercent * 100 / 100.0); + } } diff --git a/src/main/java/com/genius/gitget/challenge/certification/service/CertificationService.java b/src/main/java/com/genius/gitget/challenge/certification/service/CertificationService.java index d0e6cb40..b3c1453b 100644 --- a/src/main/java/com/genius/gitget/challenge/certification/service/CertificationService.java +++ b/src/main/java/com/genius/gitget/challenge/certification/service/CertificationService.java @@ -17,7 +17,7 @@ import com.genius.gitget.challenge.instance.service.InstanceProvider; import com.genius.gitget.challenge.myChallenge.dto.ActivatedResponse; import com.genius.gitget.challenge.participant.domain.Participant; -import com.genius.gitget.challenge.participant.service.ParticipantProvider; +import com.genius.gitget.challenge.participant.service.ParticipantService; import com.genius.gitget.challenge.user.domain.User; import com.genius.gitget.challenge.user.dto.UserProfileInfo; import com.genius.gitget.challenge.user.service.UserService; @@ -49,18 +49,18 @@ public class CertificationService { private final FilesService filesService; private final GithubProvider githubProvider; private final CertificationProvider certificationProvider; - private final ParticipantProvider participantProvider; + private final ParticipantService participantService; private final InstanceProvider instanceProvider; public WeekResponse getMyWeekCertifications(Long participantId, LocalDate currentDate) { - Participant participant = participantProvider.findById(participantId); + Participant participant = participantService.findById(participantId); return getWeekResponse(participant, currentDate); } public Slice getOthersWeekCertifications(Long userId, Long instanceId, LocalDate currentDate, Pageable pageable) { - Slice participants = participantProvider.findAllByInstanceId(userId, instanceId, pageable); + Slice participants = participantService.findAllByInstanceId(userId, instanceId, pageable); return participants.map( participant -> getWeekResponse(participant, currentDate) ); @@ -107,7 +107,7 @@ private List getWeekCertifications(List ce } public TotalResponse getTotalCertification(Long participantId, LocalDate currentDate) { - Instance instance = participantProvider.getInstanceById(participantId); + Instance instance = participantService.getInstanceById(participantId); LocalDate startDate = instance.getStartedDate().toLocalDate(); int totalAttempts = instance.getTotalAttempt(); @@ -156,7 +156,7 @@ private List getTotalCertifications(List c @Transactional public ActivatedResponse passCertification(Long userId, CertificationRequest certificationRequest) { Instance instance = instanceProvider.findById(certificationRequest.instanceId()); - Participant participant = participantProvider.findByJoinInfo(userId, instance.getId()); + Participant participant = participantService.findByJoinInfo(userId, instance.getId()); LocalDate targetDate = certificationRequest.targetDate(); Optional optionalCertification = certificationProvider.findByDate(targetDate, @@ -179,7 +179,7 @@ public ActivatedResponse passCertification(Long userId, CertificationRequest cer FileResponse fileResponse = filesService.convertToFileResponse(instance.getFiles()); //TODO: pass 했기 때문에 pass item이 필요없어 numOfPassItem을 0으로 전달하는 것 같음. but, 가독성이 떨어지기 때문에 수정 필요 - return ActivatedResponse.create(instance, certification.getCertificationStatus(), + return ActivatedResponse.of(instance, certification.getCertificationStatus(), 0, participant.getRepositoryName(), fileResponse); } @@ -193,7 +193,7 @@ private void validatePassCondition(Optional optional) { public CertificationResponse updateCertification(User user, CertificationRequest certificationRequest) { GitHub gitHub = githubProvider.getGithubConnection(user); Instance instance = instanceProvider.findById(certificationRequest.instanceId()); - Participant participant = participantProvider.findByJoinInfo(user.getId(), instance.getId()); + Participant participant = participantService.findByJoinInfo(user.getId(), instance.getId()); String repositoryName = participant.getRepositoryName(); LocalDate targetDate = certificationRequest.targetDate(); diff --git a/src/main/java/com/genius/gitget/challenge/instance/service/InstanceDetailService.java b/src/main/java/com/genius/gitget/challenge/instance/service/InstanceDetailService.java index 5d331418..a03b3878 100644 --- a/src/main/java/com/genius/gitget/challenge/instance/service/InstanceDetailService.java +++ b/src/main/java/com/genius/gitget/challenge/instance/service/InstanceDetailService.java @@ -14,7 +14,7 @@ import com.genius.gitget.challenge.likes.repository.LikesRepository; import com.genius.gitget.challenge.participant.domain.JoinStatus; import com.genius.gitget.challenge.participant.domain.Participant; -import com.genius.gitget.challenge.participant.service.ParticipantProvider; +import com.genius.gitget.challenge.participant.service.ParticipantService; import com.genius.gitget.challenge.user.domain.User; import com.genius.gitget.challenge.user.service.UserService; import com.genius.gitget.global.file.dto.FileResponse; @@ -36,7 +36,7 @@ public class InstanceDetailService { private final UserService userService; private final FilesService filesService; private final InstanceProvider instanceProvider; - private final ParticipantProvider participantProvider; + private final ParticipantService participantService; private final GithubProvider githubProvider; private final LikesRepository likesRepository; @@ -46,7 +46,7 @@ public InstanceResponse getInstanceDetailInformation(User user, Long instanceId) FileResponse fileResponse = filesService.convertToFileResponse(instance.getFiles()); LikesInfo likesInfo = getLikesInfo(user.getId(), instance); - if (participantProvider.hasJoinedParticipant(user.getId(), instanceId)) { + if (participantService.hasJoinedParticipant(user.getId(), instanceId)) { return InstanceResponse.createByEntity(instance, likesInfo, JoinStatus.YES, fileResponse); } @@ -77,7 +77,7 @@ public JoinResponse joinNewChallenge(User user, JoinRequest joinRequest) { instance.updateParticipantCount(1); Participant participant = Participant.createDefaultParticipant(repository); participant.setUserAndInstance(persistUser, instance); - return JoinResponse.createJoinResponse(participantProvider.save(participant)); + return JoinResponse.createJoinResponse(participantService.save(participant)); } private void validateJoinDate(Instance instance, LocalDate todayDate) { @@ -90,7 +90,7 @@ private void validateJoinDate(Instance instance, LocalDate todayDate) { } private void validateInstanceCondition(User user, Instance instance) { - boolean isParticipated = participantProvider.hasJoinedParticipant(user.getId(), instance.getId()); + boolean isParticipated = participantService.hasJoinedParticipant(user.getId(), instance.getId()); if ((instance.getProgress() == Progress.PREACTIVITY) && !isParticipated) { return; } @@ -106,7 +106,7 @@ private void validateGithub(User user, String repository) { @Transactional public JoinResponse quitChallenge(User user, Long instanceId) { Instance instance = instanceProvider.findById(instanceId); - Participant participant = participantProvider.findByJoinInfo(user.getId(), instanceId); + Participant participant = participantService.findByJoinInfo(user.getId(), instanceId); if (instance.getProgress() == Progress.DONE) { throw new BusinessException(CAN_NOT_QUIT_INSTANCE); @@ -114,7 +114,7 @@ public JoinResponse quitChallenge(User user, Long instanceId) { if (instance.getProgress() == Progress.PREACTIVITY) { instance.updateParticipantCount(-1); - participantProvider.delete(participant); + participantService.delete(participant); return JoinResponse.createQuitResponse(); } diff --git a/src/main/java/com/genius/gitget/challenge/myChallenge/controller/MyChallengeController.java b/src/main/java/com/genius/gitget/challenge/myChallenge/controller/MyChallengeController.java index ef3a2d72..22f17812 100644 --- a/src/main/java/com/genius/gitget/challenge/myChallenge/controller/MyChallengeController.java +++ b/src/main/java/com/genius/gitget/challenge/myChallenge/controller/MyChallengeController.java @@ -7,7 +7,7 @@ import com.genius.gitget.challenge.myChallenge.dto.DoneResponse; import com.genius.gitget.challenge.myChallenge.dto.PreActivityResponse; import com.genius.gitget.challenge.myChallenge.dto.RewardRequest; -import com.genius.gitget.challenge.myChallenge.service.MyChallengeService; +import com.genius.gitget.challenge.myChallenge.facade.MyChallengeFacade; import com.genius.gitget.global.security.domain.UserPrincipal; import com.genius.gitget.global.util.response.dto.ListResponse; import com.genius.gitget.global.util.response.dto.SingleResponse; @@ -28,14 +28,13 @@ @RequiredArgsConstructor @CrossOrigin public class MyChallengeController { - private final MyChallengeService myChallengeService; - + private final MyChallengeFacade myChallengeFacade; @GetMapping("/my/pre-activity") public ResponseEntity> getPreActivityChallenges( @AuthenticationPrincipal UserPrincipal userPrincipal ) { - List preActivityInstances = myChallengeService.getPreActivityInstances( + List preActivityInstances = myChallengeFacade.getPreActivityInstances( userPrincipal.getUser(), DateUtil.convertToKST(LocalDateTime.now())); @@ -49,7 +48,7 @@ public ResponseEntity> getPreActivityChallenge public ResponseEntity> getActivatedChallenges( @AuthenticationPrincipal UserPrincipal userPrincipal ) { - List activatedInstances = myChallengeService.getActivatedInstances( + List activatedInstances = myChallengeFacade.getActivatedInstances( userPrincipal.getUser(), DateUtil.convertToKST(LocalDateTime.now())); @@ -62,7 +61,7 @@ public ResponseEntity> getActivatedChallenges( public ResponseEntity> getDoneChallenges( @AuthenticationPrincipal UserPrincipal userPrincipal ) { - List doneInstances = myChallengeService.getDoneInstances( + List doneInstances = myChallengeFacade.getDoneInstances( userPrincipal.getUser(), DateUtil.convertToKST(LocalDateTime.now())); @@ -77,8 +76,8 @@ public ResponseEntity> getRewards( @PathVariable Long instanceId ) { LocalDate kstDate = DateUtil.convertToKST(LocalDateTime.now()); - RewardRequest rewardRequest = new RewardRequest(userPrincipal.getUser(), instanceId, kstDate); - DoneResponse doneResponse = myChallengeService.getRewards(rewardRequest, false); + RewardRequest rewardRequest = new RewardRequest(userPrincipal.getUser().getId(), instanceId, kstDate); + DoneResponse doneResponse = myChallengeFacade.getRewards(rewardRequest); return ResponseEntity.ok().body( new SingleResponse<>(SUCCESS.getStatus(), SUCCESS.getMessage(), doneResponse) diff --git a/src/main/java/com/genius/gitget/challenge/myChallenge/dto/ActivatedResponse.java b/src/main/java/com/genius/gitget/challenge/myChallenge/dto/ActivatedResponse.java index e1c01068..5ee50427 100644 --- a/src/main/java/com/genius/gitget/challenge/myChallenge/dto/ActivatedResponse.java +++ b/src/main/java/com/genius/gitget/challenge/myChallenge/dto/ActivatedResponse.java @@ -34,8 +34,8 @@ public ActivatedResponse(Long instanceId, String title, int pointPerPerson, Stri this.fileResponse = fileResponse; } - public static ActivatedResponse create(Instance instance, CertificateStatus certificateStatus, - int numOfPassItem, String repository, FileResponse fileResponse) { + public static ActivatedResponse of(Instance instance, CertificateStatus certificateStatus, + int numOfPassItem, String repository, FileResponse fileResponse) { boolean canUseItem = checkItemCondition(certificateStatus, numOfPassItem); return ActivatedResponse.builder() diff --git a/src/main/java/com/genius/gitget/challenge/myChallenge/dto/PreActivityResponse.java b/src/main/java/com/genius/gitget/challenge/myChallenge/dto/PreActivityResponse.java index a00f16c3..9ed51312 100644 --- a/src/main/java/com/genius/gitget/challenge/myChallenge/dto/PreActivityResponse.java +++ b/src/main/java/com/genius/gitget/challenge/myChallenge/dto/PreActivityResponse.java @@ -1,5 +1,6 @@ package com.genius.gitget.challenge.myChallenge.dto; +import com.genius.gitget.challenge.instance.domain.Instance; import com.genius.gitget.global.file.dto.FileResponse; import lombok.Builder; @@ -12,4 +13,15 @@ public record PreActivityResponse( int remainDays, FileResponse fileResponse ) { + + public static PreActivityResponse of(Instance instance, int remainDays, FileResponse fileResponse) { + return PreActivityResponse.builder() + .instanceId(instance.getId()) + .title(instance.getTitle()) + .participantCount(instance.getParticipantCount()) + .pointPerPerson(instance.getPointPerPerson()) + .remainDays(remainDays) + .fileResponse(fileResponse) + .build(); + } } diff --git a/src/main/java/com/genius/gitget/challenge/myChallenge/dto/RewardRequest.java b/src/main/java/com/genius/gitget/challenge/myChallenge/dto/RewardRequest.java index 5c13009e..d2f08b8c 100644 --- a/src/main/java/com/genius/gitget/challenge/myChallenge/dto/RewardRequest.java +++ b/src/main/java/com/genius/gitget/challenge/myChallenge/dto/RewardRequest.java @@ -1,15 +1,14 @@ package com.genius.gitget.challenge.myChallenge.dto; -import com.genius.gitget.challenge.user.domain.User; import java.time.LocalDate; public record RewardRequest( - User user, + Long userId, Long instanceId, LocalDate targetDate ) { - public static RewardRequest of(User user, Long instanceId, LocalDate targetDate) { - return new RewardRequest(user, instanceId, targetDate); + public static RewardRequest of(Long userId, Long instanceId, LocalDate targetDate) { + return new RewardRequest(userId, instanceId, targetDate); } } diff --git a/src/main/java/com/genius/gitget/challenge/myChallenge/facade/MyChallengeFacade.java b/src/main/java/com/genius/gitget/challenge/myChallenge/facade/MyChallengeFacade.java new file mode 100644 index 00000000..135153e9 --- /dev/null +++ b/src/main/java/com/genius/gitget/challenge/myChallenge/facade/MyChallengeFacade.java @@ -0,0 +1,19 @@ +package com.genius.gitget.challenge.myChallenge.facade; + +import com.genius.gitget.challenge.myChallenge.dto.ActivatedResponse; +import com.genius.gitget.challenge.myChallenge.dto.DoneResponse; +import com.genius.gitget.challenge.myChallenge.dto.PreActivityResponse; +import com.genius.gitget.challenge.myChallenge.dto.RewardRequest; +import com.genius.gitget.challenge.user.domain.User; +import java.time.LocalDate; +import java.util.List; + +public interface MyChallengeFacade { + List getPreActivityInstances(User user, LocalDate targetDate); + + List getActivatedInstances(User user, LocalDate targetDate); + + List getDoneInstances(User user, LocalDate targetDate); + + DoneResponse getRewards(RewardRequest rewardRequest); +} diff --git a/src/main/java/com/genius/gitget/challenge/myChallenge/service/MyChallengeService.java b/src/main/java/com/genius/gitget/challenge/myChallenge/facade/MyChallengeFacadeService.java similarity index 58% rename from src/main/java/com/genius/gitget/challenge/myChallenge/service/MyChallengeService.java rename to src/main/java/com/genius/gitget/challenge/myChallenge/facade/MyChallengeFacadeService.java index 325ff858..97b41591 100644 --- a/src/main/java/com/genius/gitget/challenge/myChallenge/service/MyChallengeService.java +++ b/src/main/java/com/genius/gitget/challenge/myChallenge/facade/MyChallengeFacadeService.java @@ -1,13 +1,9 @@ -package com.genius.gitget.challenge.myChallenge.service; +package com.genius.gitget.challenge.myChallenge.facade; -import static com.genius.gitget.challenge.certification.domain.CertificateStatus.CERTIFICATED; -import static com.genius.gitget.challenge.participant.domain.JoinResult.SUCCESS; import static com.genius.gitget.challenge.participant.domain.RewardStatus.NO; -import static com.genius.gitget.challenge.participant.domain.RewardStatus.YES; import static com.genius.gitget.store.item.domain.ItemCategory.CERTIFICATION_PASSER; import static com.genius.gitget.store.item.domain.ItemCategory.POINT_MULTIPLIER; -import com.genius.gitget.challenge.certification.domain.CertificateStatus; import com.genius.gitget.challenge.certification.domain.Certification; import com.genius.gitget.challenge.certification.service.CertificationProvider; import com.genius.gitget.challenge.certification.util.DateUtil; @@ -18,13 +14,10 @@ import com.genius.gitget.challenge.myChallenge.dto.PreActivityResponse; import com.genius.gitget.challenge.myChallenge.dto.RewardRequest; import com.genius.gitget.challenge.participant.domain.Participant; -import com.genius.gitget.challenge.participant.service.ParticipantProvider; +import com.genius.gitget.challenge.participant.service.ParticipantService; import com.genius.gitget.challenge.user.domain.User; -import com.genius.gitget.challenge.user.service.UserService; import com.genius.gitget.global.file.dto.FileResponse; import com.genius.gitget.global.file.service.FilesService; -import com.genius.gitget.global.util.exception.BusinessException; -import com.genius.gitget.global.util.exception.ErrorCode; import com.genius.gitget.store.item.domain.Item; import com.genius.gitget.store.item.service.ItemService; import com.genius.gitget.store.item.service.OrdersService; @@ -38,45 +31,64 @@ @Service @Transactional(readOnly = true) @RequiredArgsConstructor -public class MyChallengeService { - private final UserService userService; +public class MyChallengeFacadeService implements MyChallengeFacade { private final FilesService filesService; - private final ParticipantProvider participantProvider; + private final ParticipantService participantService; private final CertificationProvider certificationProvider; private final ItemService itemService; private final OrdersService ordersService; + @Override public List getPreActivityInstances(User user, LocalDate targetDate) { List preActivity = new ArrayList<>(); - List participants = participantProvider.findJoinedByProgress(user.getId(), Progress.PREACTIVITY); + List participants = participantService.findJoinedByProgress(user.getId(), Progress.PREACTIVITY); for (Participant participant : participants) { Instance instance = participant.getInstance(); FileResponse fileResponse = filesService.convertToFileResponse(instance.getFiles()); + int remainDays = DateUtil.getRemainDaysToStart(participant.getStartedDate(), targetDate); - PreActivityResponse preActivityResponse = PreActivityResponse.builder() - .instanceId(instance.getId()) - .title(instance.getTitle()) - .participantCount(instance.getParticipantCount()) - .pointPerPerson(instance.getPointPerPerson()) - .remainDays(DateUtil.getRemainDaysToStart(participant.getStartedDate(), targetDate)) - .fileResponse(fileResponse) - .build(); + PreActivityResponse preActivityResponse = PreActivityResponse.of(instance, remainDays, fileResponse); preActivity.add(preActivityResponse); } return preActivity; } + @Override + public List getActivatedInstances(User user, LocalDate targetDate) { + List activated = new ArrayList<>(); + List participants = participantService.findJoinedByProgress(user.getId(), Progress.ACTIVITY); + + for (Participant participant : participants) { + Instance instance = participant.getInstance(); + FileResponse fileResponse = filesService.convertToFileResponse(instance.getFiles()); + Certification certification = certificationProvider.findOrGetDummy(targetDate, participant.getId()); + + Item item = itemService.findAllByCategory(CERTIFICATION_PASSER).get(0); + int numOfPassItem = ordersService.countNumOfItem(user, item.getId()); + + ActivatedResponse activatedResponse = ActivatedResponse.of( + instance, certification.getCertificationStatus(), + numOfPassItem, participant.getRepositoryName(), fileResponse + ); + activatedResponse.setItemId(item.getId()); + activated.add(activatedResponse); + } + return activated; + } + + @Override public List getDoneInstances(User user, LocalDate targetDate) { List done = new ArrayList<>(); - List participants = participantProvider.findDoneInstances(user.getId()); + List participants = participantService.findDoneInstances(user.getId()); for (Participant participant : participants) { Instance instance = participant.getInstance(); FileResponse fileResponse = filesService.convertToFileResponse(instance.getFiles()); - double achievementRate = getAchievementRate(instance, participant.getId(), targetDate); + double achievementRate = certificationProvider.getAchievementRate(instance, participant.getId(), + targetDate); // 포인트를 아직 수령하지 않았을 때 if (participant.getRewardStatus() == NO) { @@ -98,76 +110,22 @@ public List getDoneInstances(User user, LocalDate targetDate) { return done; } - private double getAchievementRate(Instance instance, Long participantId, LocalDate targetDate) { - int totalAttempt = instance.getTotalAttempt(); - int successCount = certificationProvider.countByStatus(participantId, CERTIFICATED, - targetDate); - - double successPercent = (double) successCount / (double) totalAttempt * 100; - return Math.round(successPercent * 100 / 100.0); - } - - public List getActivatedInstances(User user, LocalDate targetDate) { - List activated = new ArrayList<>(); - List participants = participantProvider.findJoinedByProgress(user.getId(), Progress.ACTIVITY); - - for (Participant participant : participants) { - Instance instance = participant.getInstance(); - FileResponse fileResponse = filesService.convertToFileResponse(instance.getFiles()); - Certification certification = certificationProvider.findByDate(targetDate, participant.getId()) - .orElse(getDummyCertification()); - - //TODO: 로직 수정 필요 - Item item = itemService.findAllByCategory(CERTIFICATION_PASSER).get(0); - int numOfPassItem = ordersService.countNumOfItem(user, item.getId()); - - ActivatedResponse activatedResponse = ActivatedResponse.create( - instance, certification.getCertificationStatus(), - numOfPassItem, participant.getRepositoryName(), fileResponse - ); - activatedResponse.setItemId(item.getId()); - activated.add(activatedResponse); - } - return activated; - } - - private Certification getDummyCertification() { - return Certification.builder() - .currentAttempt(0) - .certificationStatus(CertificateStatus.NOT_YET) - .certificatedAt(null) - .certificationLinks(null) - .build(); - } - + @Override @Transactional - public DoneResponse getRewards(RewardRequest rewardRequest, boolean useItem) { - User user = userService.findUserById(rewardRequest.user().getId()); - Participant participant = participantProvider.findByJoinInfo(user.getId(), rewardRequest.instanceId()); + public DoneResponse getRewards(RewardRequest rewardRequest) { + Participant participant = participantService.findByJoinInfo( + rewardRequest.userId(), rewardRequest.instanceId() + ); Instance instance = participant.getInstance(); FileResponse fileResponse = filesService.convertToFileResponse(instance.getFiles()); - validRewardCondition(participant); - int rewardPoints = instance.getPointPerPerson(); - if (useItem) { - rewardPoints *= 2; - } + participantService.getRewards(participant, rewardPoints); - user.updatePoints((long) rewardPoints); - double achievementRate = getAchievementRate(instance, participant.getId(), rewardRequest.targetDate()); + double achievementRate = certificationProvider.getAchievementRate(instance, participant.getId(), + rewardRequest.targetDate()); - participant.getRewards(rewardPoints); return DoneResponse.createRewarded(instance, participant, achievementRate, fileResponse); } - - private void validRewardCondition(Participant participant) { - if (participant.getJoinResult() != SUCCESS) { - throw new BusinessException(ErrorCode.CAN_NOT_GET_REWARDS); - } - if (participant.getRewardStatus() == YES) { - throw new BusinessException(ErrorCode.ALREADY_REWARDED); - } - } } \ No newline at end of file diff --git a/src/main/java/com/genius/gitget/challenge/participant/domain/Participant.java b/src/main/java/com/genius/gitget/challenge/participant/domain/Participant.java index 02504576..09cfba9f 100644 --- a/src/main/java/com/genius/gitget/challenge/participant/domain/Participant.java +++ b/src/main/java/com/genius/gitget/challenge/participant/domain/Participant.java @@ -1,8 +1,13 @@ package com.genius.gitget.challenge.participant.domain; +import static com.genius.gitget.challenge.participant.domain.JoinResult.SUCCESS; +import static com.genius.gitget.challenge.participant.domain.RewardStatus.YES; + import com.genius.gitget.challenge.certification.domain.Certification; import com.genius.gitget.challenge.instance.domain.Instance; import com.genius.gitget.challenge.user.domain.User; +import com.genius.gitget.global.util.exception.BusinessException; +import com.genius.gitget.global.util.exception.ErrorCode; import jakarta.persistence.CascadeType; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -108,6 +113,15 @@ public LocalDate getStartedDate() { return this.getInstance().getStartedDate().toLocalDate(); } + public void validateRewardCondition() { + if (this.getJoinResult() != SUCCESS) { + throw new BusinessException(ErrorCode.CAN_NOT_GET_REWARDS); + } + if (this.getRewardStatus() == YES) { + throw new BusinessException(ErrorCode.ALREADY_REWARDED); + } + } + /*== 연관관계 편의 메서드 ==*/ public void setUserAndInstance(User user, Instance instance) { diff --git a/src/main/java/com/genius/gitget/challenge/participant/service/ParticipantProvider.java b/src/main/java/com/genius/gitget/challenge/participant/service/ParticipantService.java similarity index 90% rename from src/main/java/com/genius/gitget/challenge/participant/service/ParticipantProvider.java rename to src/main/java/com/genius/gitget/challenge/participant/service/ParticipantService.java index a8241f21..e33c23fe 100644 --- a/src/main/java/com/genius/gitget/challenge/participant/service/ParticipantProvider.java +++ b/src/main/java/com/genius/gitget/challenge/participant/service/ParticipantService.java @@ -7,6 +7,7 @@ import com.genius.gitget.challenge.participant.domain.JoinStatus; import com.genius.gitget.challenge.participant.domain.Participant; import com.genius.gitget.challenge.participant.repository.ParticipantRepository; +import com.genius.gitget.challenge.user.domain.User; import com.genius.gitget.global.util.exception.BusinessException; import java.util.ArrayList; import java.util.List; @@ -22,7 +23,7 @@ @Service @Transactional(readOnly = true) @RequiredArgsConstructor -public class ParticipantProvider { +public class ParticipantService { private final ParticipantRepository participantRepository; @Transactional @@ -81,4 +82,13 @@ public boolean hasJoinedParticipant(Long userId, Long instanceId) { .map(participant -> participant.getJoinStatus() != JoinStatus.NO) .orElse(false); } + + @Transactional + public void getRewards(Participant participant, int rewardPoints) { + User user = participant.getUser(); + + participant.validateRewardCondition(); + user.updatePoints((long) rewardPoints); + participant.getRewards(rewardPoints); + } } diff --git a/src/main/java/com/genius/gitget/store/item/facade/StoreFacadeService.java b/src/main/java/com/genius/gitget/store/item/facade/StoreFacadeService.java index eb9c9a76..f68a1909 100644 --- a/src/main/java/com/genius/gitget/store/item/facade/StoreFacadeService.java +++ b/src/main/java/com/genius/gitget/store/item/facade/StoreFacadeService.java @@ -2,10 +2,12 @@ import com.genius.gitget.challenge.certification.dto.CertificationRequest; import com.genius.gitget.challenge.certification.service.CertificationService; +import com.genius.gitget.challenge.instance.domain.Instance; +import com.genius.gitget.challenge.instance.service.InstanceService; import com.genius.gitget.challenge.myChallenge.dto.ActivatedResponse; import com.genius.gitget.challenge.myChallenge.dto.DoneResponse; -import com.genius.gitget.challenge.myChallenge.dto.RewardRequest; -import com.genius.gitget.challenge.myChallenge.service.MyChallengeService; +import com.genius.gitget.challenge.participant.domain.Participant; +import com.genius.gitget.challenge.participant.service.ParticipantService; import com.genius.gitget.challenge.user.domain.User; import com.genius.gitget.challenge.user.service.UserService; import com.genius.gitget.global.util.exception.BusinessException; @@ -36,12 +38,11 @@ public class StoreFacadeService implements StoreFacade { private final OrdersService ordersService; private final UserService userService; + private final InstanceService instanceService; + private final ParticipantService participantService; + // TODO: CertificationProvider에만 의존하도록 변경(파사드 패턴 적용 시) private final CertificationService certificationService; - - //TODO: 책임이 분명한 Service를 만들어서 적용하기(MyChallenge 진행 시) - private final MyChallengeService myChallengeService; - private final PaymentRepository paymentRepository; @@ -67,15 +68,16 @@ private ItemResponse getItemResponse(User user, Item item, int numOfItem) { } @Override + @Transactional public ItemResponse orderItem(User user, int identifier) { User persistUser = userService.findUserById(user.getId()); Item item = itemService.findByIdentifier(identifier); persistUser.hasEnoughPoint(item.getCost()); - paymentRepository.save(Payment.create(user, item)); + paymentRepository.save(Payment.create(persistUser, item)); - Orders orders = ordersService.findOrSave(user, item); + Orders orders = ordersService.findOrSave(persistUser, item); int numOfItem = orders.purchase(); persistUser.updatePoints((long) item.getCost() * -1); @@ -83,6 +85,7 @@ public ItemResponse orderItem(User user, int identifier) { } @Override + @Transactional public OrderResponse useItem(User user, int identifier, Long instanceId, LocalDate currentDate) { Item item = itemService.findByIdentifier(identifier); Orders orders = ordersService.findByOrderInfo(user.getId(), item.getId()); @@ -141,12 +144,16 @@ public OrderResponse usePasserItem(Orders orders, Long instanceId, LocalDate cur @Override public OrderResponse useMultiplierItem(Orders orders, Long instanceId, LocalDate currentDate) { User user = orders.getUser(); - DoneResponse doneResponse = myChallengeService.getRewards( - RewardRequest.of(user, instanceId, currentDate), true - ); - doneResponse.setItemId(orders.getItem().getId()); + Instance instance = instanceService.findInstanceById(instanceId); + Participant participant = participantService.findByJoinInfo(user.getId(), instanceId); + + int rewardPoints = instance.getPointPerPerson() * 2; + participantService.getRewards(participant, rewardPoints); ordersService.useItem(orders); - return doneResponse; + + return DoneResponse.builder() + .rewardedPoints(rewardPoints) + .build(); } @Override @@ -155,20 +162,11 @@ public List unmountFrame(User user) { List frameOrders = ordersService.findAllUsingFrames(user.getId()); for (Orders frameOrder : frameOrders) { - validateUnmountCondition(frameOrder); + ordersService.validateUnmountCondition(frameOrder); frameOrder.updateEquipStatus(EquipStatus.AVAILABLE); profileResponses.add(ProfileResponse.createByEntity(frameOrder)); } return profileResponses; } - - private void validateUnmountCondition(Orders orders) { - if (orders.getItem().getItemCategory() != ItemCategory.PROFILE_FRAME) { - throw new BusinessException(ErrorCode.ITEM_NOT_FOUND); - } - if (orders.getEquipStatus() != EquipStatus.IN_USE) { - throw new BusinessException(ErrorCode.IN_USE_FRAME_NOT_FOUND); - } - } } diff --git a/src/main/java/com/genius/gitget/store/item/service/OrdersService.java b/src/main/java/com/genius/gitget/store/item/service/OrdersService.java index 5f05f9b3..26457721 100644 --- a/src/main/java/com/genius/gitget/store/item/service/OrdersService.java +++ b/src/main/java/com/genius/gitget/store/item/service/OrdersService.java @@ -42,6 +42,7 @@ public Orders findByOrderInfo(Long userId, Long itemId) { .orElseThrow(() -> new BusinessException(ORDERS_NOT_FOUND)); } + @Transactional public Orders findOrSave(User user, Item item) { return ordersRepository.findByOrderInfo(user.getId(), item.getId()) .orElseGet(() -> ordersRepository.save(Orders.of(user, item))); @@ -76,6 +77,7 @@ public Item getUsingFrameItem(Long userId) { return usingFrames.get(0).getItem(); } + @Transactional public void useItem(Orders orders) { orders.useItem(); if (!orders.hasItem()) { @@ -88,4 +90,13 @@ public int countNumOfItem(User user, Long itemId) { return optionalUserItem.map(Orders::getCount) .orElse(0); } + + public void validateUnmountCondition(Orders orders) { + if (orders.getItem().getItemCategory() != ItemCategory.PROFILE_FRAME) { + throw new BusinessException(ErrorCode.ITEM_NOT_FOUND); + } + if (orders.getEquipStatus() != EquipStatus.IN_USE) { + throw new BusinessException(ErrorCode.IN_USE_FRAME_NOT_FOUND); + } + } } diff --git a/src/test/java/com/genius/gitget/challenge/instance/service/InstanceDetailServiceTest.java b/src/test/java/com/genius/gitget/challenge/instance/service/InstanceDetailServiceTest.java index 2a3fa6a2..38a34262 100644 --- a/src/test/java/com/genius/gitget/challenge/instance/service/InstanceDetailServiceTest.java +++ b/src/test/java/com/genius/gitget/challenge/instance/service/InstanceDetailServiceTest.java @@ -20,7 +20,7 @@ import com.genius.gitget.challenge.participant.domain.JoinStatus; import com.genius.gitget.challenge.participant.domain.Participant; import com.genius.gitget.challenge.participant.repository.ParticipantRepository; -import com.genius.gitget.challenge.participant.service.ParticipantProvider; +import com.genius.gitget.challenge.participant.service.ParticipantService; import com.genius.gitget.challenge.user.domain.Role; import com.genius.gitget.challenge.user.domain.User; import com.genius.gitget.challenge.user.repository.UserRepository; @@ -47,7 +47,7 @@ class InstanceDetailServiceTest { @Autowired LikesFacade likesFacade; @Autowired - ParticipantProvider participantProvider; + ParticipantService participantService; @Autowired GithubService githubService; @Autowired @@ -202,7 +202,7 @@ public void should_changeParticipantInfo_when_requestQuitInstance() { instanceDetailService.joinNewChallenge(savedUser, joinRequest); savedInstance.updateProgress(Progress.ACTIVITY); instanceDetailService.quitChallenge(savedUser, savedInstance.getId()); - Participant participant = participantProvider.findByJoinInfo(savedUser.getId(), + Participant participant = participantService.findByJoinInfo(savedUser.getId(), savedInstance.getId()); //then diff --git a/src/test/java/com/genius/gitget/challenge/myChallenge/service/MyChallengeFacadeTest.java b/src/test/java/com/genius/gitget/challenge/myChallenge/service/MyChallengeFacadeTest.java new file mode 100644 index 00000000..d75cda1a --- /dev/null +++ b/src/test/java/com/genius/gitget/challenge/myChallenge/service/MyChallengeFacadeTest.java @@ -0,0 +1,287 @@ +package com.genius.gitget.challenge.myChallenge.service; + +import static com.genius.gitget.challenge.participant.domain.JoinResult.FAIL; +import static com.genius.gitget.challenge.participant.domain.JoinResult.SUCCESS; +import static com.genius.gitget.challenge.participant.domain.RewardStatus.NO; +import static com.genius.gitget.challenge.participant.domain.RewardStatus.YES; +import static com.genius.gitget.store.item.domain.ItemCategory.CERTIFICATION_PASSER; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.genius.gitget.challenge.certification.domain.CertificateStatus; +import com.genius.gitget.challenge.certification.repository.CertificationRepository; +import com.genius.gitget.challenge.instance.domain.Instance; +import com.genius.gitget.challenge.instance.repository.InstanceRepository; +import com.genius.gitget.challenge.myChallenge.dto.ActivatedResponse; +import com.genius.gitget.challenge.myChallenge.dto.DoneResponse; +import com.genius.gitget.challenge.myChallenge.dto.PreActivityResponse; +import com.genius.gitget.challenge.myChallenge.dto.RewardRequest; +import com.genius.gitget.challenge.myChallenge.facade.MyChallengeFacade; +import com.genius.gitget.challenge.participant.domain.Participant; +import com.genius.gitget.challenge.participant.repository.ParticipantRepository; +import com.genius.gitget.challenge.user.domain.User; +import com.genius.gitget.challenge.user.repository.UserRepository; +import com.genius.gitget.global.util.exception.BusinessException; +import com.genius.gitget.global.util.exception.ErrorCode; +import com.genius.gitget.store.item.domain.Item; +import com.genius.gitget.store.item.domain.Orders; +import com.genius.gitget.store.item.repository.ItemRepository; +import com.genius.gitget.store.item.repository.OrdersRepository; +import com.genius.gitget.util.certification.CertificationFactory; +import com.genius.gitget.util.instance.InstanceFactory; +import com.genius.gitget.util.participant.ParticipantFactory; +import com.genius.gitget.util.store.StoreFactory; +import com.genius.gitget.util.user.UserFactory; +import java.time.LocalDate; +import java.util.List; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.junit.jupiter.params.provider.EnumSource.Mode; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.transaction.annotation.Transactional; + +@Slf4j +@SpringBootTest +@Transactional +class MyChallengeFacadeTest { + private LocalDate localDate = LocalDate.now(); + private User user; + private Instance instance1; + private Instance instance2; + private Instance instance3; + + @Autowired + private MyChallengeFacade myChallengeFacade; + @Autowired + private UserRepository userRepository; + @Autowired + private InstanceRepository instanceRepository; + @Autowired + private ParticipantRepository participantRepository; + @Autowired + private CertificationRepository certificationRepository; + @Autowired + private ItemRepository itemRepository; + @Autowired + private OrdersRepository ordersRepository; + + @BeforeEach + void setup() { + user = userRepository.save(UserFactory.createUser()); + } + + + @Nested + @DisplayName("시작 전 인스턴스들을 조회할 때") + class context_inquiry_preActivity_instances { + @BeforeEach + void setup() { + instance1 = instanceRepository.save(InstanceFactory.createPreActivity(10)); + instance2 = instanceRepository.save(InstanceFactory.createPreActivity(10)); + instance3 = instanceRepository.save(InstanceFactory.createPreActivity(10)); + } + + @Nested + @DisplayName("참여한 인스턴스 중 시작 전인 인스턴스들이 있을 때") + class describe_joined_preActivity_instance { + @BeforeEach + void setup() { + participantRepository.save(ParticipantFactory.createPreActivity(user, instance1)); + participantRepository.save(ParticipantFactory.createPreActivity(user, instance2)); + participantRepository.save(ParticipantFactory.createPreActivity(user, instance3)); + } + + @Test + @DisplayName("인스턴스 목록들을 조회할 수 있다.") + public void it_returns_instance_list() { + List preActivityInstances = myChallengeFacade.getPreActivityInstances(user, + localDate); + assertThat(preActivityInstances.size()).isEqualTo(3); + } + } + } + + @Nested + @DisplayName("진행 중인 인스턴스 조회 시") + class context_inquiry_activity_instances { + Participant participant; + + @BeforeEach + void setup() { + instance1 = instanceRepository.save(InstanceFactory.createActivity(10)); + participant = participantRepository.save(ParticipantFactory.createPreActivity(user, instance1)); + } + + @Nested + @DisplayName("패스 아이템 사용 여부 조회할 때") + class describe_check_pass_item { + Item item; + Orders orders; + + @BeforeEach + void setup() { + item = itemRepository.findAllByCategory(CERTIFICATION_PASSER).get(0); + orders = ordersRepository.save(StoreFactory.createOrders(user, item, CERTIFICATION_PASSER, 3)); + } + + @Test + @DisplayName("인증 정보가 DB에 저장되어 있지 않다면 사용 가능 여부가 true여야 한다.") + public void it_return_true_when_certification_not_saved_DB() { + List activatedInstances = myChallengeFacade.getActivatedInstances(user, localDate); + for (ActivatedResponse activatedInstance : activatedInstances) { + assertThat(activatedInstance.isCanUsePassItem()).isTrue(); + } + } + + @Test + @DisplayName("인증 정보가 NOT_YET 이라면 사용 가능 여부가 true여야 한다.") + public void it_return_true_when_certification_NOT_YET() { + certificationRepository.save(CertificationFactory.createNotYet(participant, localDate)); + + List activatedInstances = myChallengeFacade.getActivatedInstances(user, localDate); + for (ActivatedResponse activatedInstance : activatedInstances) { + assertThat(activatedInstance.isCanUsePassItem()).isTrue(); + } + } + + @ParameterizedTest + @DisplayName("인증 정보가 PASSED 혹은 CERTIFICATED라면 사용 가능 여부가 false여야 한다.") + @EnumSource(mode = Mode.INCLUDE, names = {"PASSED", "CERTIFICATED"}) + public void it_return_false_when_certification_PASSED_or_CERTIFICATED(CertificateStatus certificateStatus) { + certificationRepository.save(CertificationFactory.create(certificateStatus, localDate, participant)); + + List activatedInstances = myChallengeFacade.getActivatedInstances(user, localDate); + for (ActivatedResponse activatedInstance : activatedInstances) { + assertThat(activatedInstance.isCanUsePassItem()).isFalse(); + } + } + } + } + + @Nested + @DisplayName("완료된 인스턴스 조회 시") + class context_inquiry_done_instances { + Participant participant; + + @BeforeEach + void setup() { + instance1 = instanceRepository.save(InstanceFactory.createDone(10)); + } + + @Nested + @DisplayName("아직 보상받지 않은 인스턴스가 있을 때") + class describe_not_rewarded_challenge_exist { + @BeforeEach + void setup() { + participant = participantRepository.save( + ParticipantFactory.createByRewardStatus(user, instance1, SUCCESS, NO)); + } + + @Test + @DisplayName("실패한 챌린지라면 획득 포인트는 0이어야하고, 달성률 정보를 전달해야 한다.") + public void it_returns_achievementRate() { + List doneInstances = myChallengeFacade.getDoneInstances(user, localDate); + for (DoneResponse doneInstance : doneInstances) { + assertThat(doneInstance.getRewardedPoints()).isEqualTo(0); + assertThat(doneInstance.getAchievementRate()).isEqualTo(0.0); + } + } + + @Test + @DisplayName("성공한 챌린지라면 보상 가능 여부가 true어야 한다.") + public void it_return_true_when_success_instances() { + List doneInstances = myChallengeFacade.getDoneInstances(user, localDate); + for (DoneResponse doneInstance : doneInstances) { + assertThat(doneInstance.isCanGetReward()).isTrue(); + } + } + } + + @Nested + @DisplayName("보상이 완료된 인스턴스가 있을 때") + class describe_rewarded_challenge_exist { + @BeforeEach + void setup() { + participant = participantRepository.save( + ParticipantFactory.createByRewardStatus(user, instance1, SUCCESS, YES)); + } + + @Test + @DisplayName("획득 포인트와 달성률에 대한 정보를 전달해야 한다.") + public void it_returns_point_and_achievementRate() { + List doneInstances = myChallengeFacade.getDoneInstances(user, localDate); + DoneResponse doneResponse = doneInstances.get(0); + assertThat(doneResponse.getRewardedPoints()).isEqualTo(participant.getRewardPoints()); + } + } + } + + @Nested + @DisplayName("챌린지 보상을 받을 때") + class context_ { + Participant participant; + + @BeforeEach + void setup() { + instance1 = instanceRepository.save(InstanceFactory.createDone(10)); + } + + @Nested + @DisplayName("보상을 받을 수 있는 조건인지 확인했을 때") + class describe_validate_reward_condition { + @Test + @DisplayName("JoinResult가 SUCCESS가 아니라면 CAN_NOT_GET_REWARDS 예외가 발생해야 한다.") + public void it_throws_CAN_NOT_GET_REWARDS_exception() { + participant = participantRepository.save( + ParticipantFactory.createByRewardStatus(user, instance1, FAIL, NO)); + assertThatThrownBy(() -> myChallengeFacade.getRewards( + RewardRequest.of(user.getId(), instance1.getId(), localDate))) + .isInstanceOf(BusinessException.class) + .hasMessageContaining(ErrorCode.CAN_NOT_GET_REWARDS.getMessage()); + } + + @Test + @DisplayName("RewardStatus가 YES라면 ALREADY_REWARDED 예외가 발생해야 한다.") + public void it_throws_ALREADY_REWARDED_exception() { + participant = participantRepository.save( + ParticipantFactory.createByRewardStatus(user, instance1, SUCCESS, YES) + ); + assertThatThrownBy(() -> myChallengeFacade.getRewards( + RewardRequest.of(user.getId(), instance1.getId(), localDate) + )) + .isInstanceOf(BusinessException.class) + .hasMessageContaining(ErrorCode.ALREADY_REWARDED.getMessage()); + } + } + + @Nested + @DisplayName("보상을 받을 수 있고, 보상을 받았을 때") + class describe_get_rewards { + @BeforeEach + void setup() { + participant = participantRepository.save( + ParticipantFactory.createByRewardStatus(user, instance1, SUCCESS, NO) + ); + myChallengeFacade.getRewards(RewardRequest.of(user.getId(), instance1.getId(), localDate)); + } + + @Test + @DisplayName("participant의 RewardStatus가 YES가 되어야 한다.") + public void it_change_rewardStatus_YES() { + assertThat(participant.getRewardStatus()).isEqualTo(YES); + } + + @Test + @DisplayName("user의 point의 값이 갱신되어야 한다.") + public void it_change_user_point() { + assertThat(user.getPoint()).isEqualTo(participant.getRewardPoints()); + } + } + } +} \ No newline at end of file diff --git a/src/test/java/com/genius/gitget/challenge/myChallenge/service/MyChallengeServiceTest.java b/src/test/java/com/genius/gitget/challenge/myChallenge/service/MyChallengeServiceTest.java deleted file mode 100644 index ba67aeb6..00000000 --- a/src/test/java/com/genius/gitget/challenge/myChallenge/service/MyChallengeServiceTest.java +++ /dev/null @@ -1,292 +0,0 @@ -package com.genius.gitget.challenge.myChallenge.service; - -import static com.genius.gitget.challenge.participant.domain.JoinResult.FAIL; -import static com.genius.gitget.challenge.participant.domain.JoinResult.PROCESSING; -import static com.genius.gitget.challenge.participant.domain.JoinResult.SUCCESS; -import static org.assertj.core.api.Assertions.assertThat; - -import com.genius.gitget.challenge.certification.domain.CertificateStatus; -import com.genius.gitget.challenge.certification.domain.Certification; -import com.genius.gitget.challenge.certification.repository.CertificationRepository; -import com.genius.gitget.challenge.certification.util.DateUtil; -import com.genius.gitget.challenge.instance.domain.Instance; -import com.genius.gitget.challenge.instance.domain.Progress; -import com.genius.gitget.challenge.instance.repository.InstanceRepository; -import com.genius.gitget.challenge.myChallenge.dto.ActivatedResponse; -import com.genius.gitget.challenge.myChallenge.dto.DoneResponse; -import com.genius.gitget.challenge.myChallenge.dto.PreActivityResponse; -import com.genius.gitget.challenge.participant.domain.JoinResult; -import com.genius.gitget.challenge.participant.domain.JoinStatus; -import com.genius.gitget.challenge.participant.domain.Participant; -import com.genius.gitget.challenge.participant.domain.RewardStatus; -import com.genius.gitget.challenge.participant.repository.ParticipantRepository; -import com.genius.gitget.challenge.user.domain.Role; -import com.genius.gitget.challenge.user.domain.User; -import com.genius.gitget.challenge.user.repository.UserRepository; -import com.genius.gitget.global.security.constants.ProviderInfo; -import com.genius.gitget.store.item.domain.Item; -import com.genius.gitget.store.item.domain.ItemCategory; -import com.genius.gitget.store.item.domain.Orders; -import com.genius.gitget.store.item.repository.ItemRepository; -import com.genius.gitget.store.item.repository.OrdersRepository; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.util.List; -import lombok.extern.slf4j.Slf4j; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.EnumSource; -import org.junit.jupiter.params.provider.EnumSource.Mode; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.transaction.annotation.Transactional; - -@Slf4j -@SpringBootTest -@Transactional -class MyChallengeServiceTest { - @Autowired - private MyChallengeService myChallengeService; - @Autowired - private UserRepository userRepository; - @Autowired - private InstanceRepository instanceRepository; - @Autowired - private ParticipantRepository participantRepository; - @Autowired - private ItemRepository itemRepository; - @Autowired - private OrdersRepository ordersRepository; - @Autowired - private CertificationRepository certificationRepository; - - @Test - @DisplayName("사용자가 참여한 챌린지들 중, 시작 전인 챌린지들을 받아올 수 있다.") - public void should_getPreActivities_when_userJoinChallenges() { - //given - LocalDate targetDate = LocalDate.of(2024, 2, 14); - User user = getSavedUser(); - Instance instance1 = getSavedInstance(Progress.PREACTIVITY); - Instance instance2 = getSavedInstance(Progress.PREACTIVITY); - Instance instance3 = getSavedInstance(Progress.PREACTIVITY); - Participant participant1 = getSavedParticipant(user, instance1, PROCESSING); - Participant participant2 = getSavedParticipant(user, instance2, PROCESSING); - Participant participant3 = getSavedParticipant(user, instance3, PROCESSING); - - //when - List instances = myChallengeService.getPreActivityInstances(user, targetDate); - - //then - assertThat(instances.size()).isEqualTo(3); - assertThat(instances.get(0).instanceId()).isEqualTo(instance1.getId()); - - assertThat(instances.get(0).fileResponse()).isNotNull(); - assertThat(instances.get(1).instanceId()).isEqualTo(instance2.getId()); - assertThat(instances.get(1).fileResponse()).isNotNull(); - assertThat(instances.get(2).instanceId()).isEqualTo(instance3.getId()); - assertThat(instances.get(2).fileResponse()).isNotNull(); - } - - @Test - @DisplayName("진행 중인 챌린지 목록 조회 시, 패스 아이템을 사용할 수 있는 조건이라면 아이템 사용 가능하다는 데이터를 반환한다.") - public void should_getActivatedList_when_userJoinChallenges() { - //given - LocalDate targetDate = LocalDate.of(2024, 2, 14); - User user = getSavedUser(); - Instance instance1 = getSavedInstance(Progress.ACTIVITY); - Instance instance2 = getSavedInstance(Progress.ACTIVITY); - Participant participant1 = getSavedParticipant(user, instance1, PROCESSING); - Participant participant2 = getSavedParticipant(user, instance2, PROCESSING); - - Item item = itemRepository.findAllByCategory(ItemCategory.CERTIFICATION_PASSER).get(0); - Orders orders = getSavedOrder(user, item, item.getItemCategory(), 3); - - //when - getSavedCertification(CertificateStatus.NOT_YET, targetDate, participant2); - List activatedResponses = myChallengeService.getActivatedInstances(user, targetDate); - - //then - assertThat(activatedResponses.size()).isEqualTo(2); - assertThat(activatedResponses.get(0).getNumOfPassItem()).isEqualTo(3); - assertThat(activatedResponses.get(1).getNumOfPassItem()).isEqualTo(3); - } - - @ParameterizedTest - @DisplayName("진행 중인 챌린지 목록 조회 시, NOT_YET을 제외한 챌린지들은 아이템 사용 가능 여부가 false여야 한다.") - @EnumSource(mode = Mode.INCLUDE, names = {"CERTIFICATED", "PASSED"}) - public void should_canUserPassItemIsFalse_when_AlreadyCertificated(CertificateStatus certificateStatus) { - //given - LocalDate targetDate = LocalDate.of(2024, 2, 14); - User user = getSavedUser(); - Instance instance1 = getSavedInstance(Progress.ACTIVITY); - Participant participant1 = getSavedParticipant(user, instance1, PROCESSING); - - Item item = getSavedItem(ItemCategory.CERTIFICATION_PASSER); - getSavedOrder(user, item, item.getItemCategory(), 3); - - //when - getSavedCertification(certificateStatus, targetDate, participant1); - List instances = myChallengeService.getActivatedInstances(user, targetDate); - - //then - assertThat(instances.size()).isEqualTo(1); - assertThat(instances.get(0).getCertificateStatus()).isEqualTo(certificateStatus.getTag()); - assertThat(instances.get(0).getNumOfPassItem()).isEqualTo(0); - assertThat(instances.get(0).getPointPerPerson()).isEqualTo(instance1.getPointPerPerson()); - } - - @Test - @DisplayName("완료된 챌린지 목록 조회 시, 포인트를 아직 수령하지 않은 챌린지에 대해서는 보상 가능 정보를 전달해야 한다.") - public void should_returnTrue_when_ableToReward() { - //given - LocalDateTime targetDate = LocalDateTime.of(2024, 2, 14, 0, 0); - User user = getSavedUser(); - Instance instance = getSavedInstance(Progress.DONE, targetDate, targetDate.plusDays(1)); - Participant participant = getSavedParticipant(user, instance, SUCCESS); - Item item = itemRepository.findAllByCategory(ItemCategory.POINT_MULTIPLIER).get(0); - getSavedOrder(user, item, item.getItemCategory(), 3); - - //when - getSavedCertification(CertificateStatus.PASSED, targetDate.toLocalDate(), participant); - getSavedCertification(CertificateStatus.CERTIFICATED, targetDate.plusDays(1).toLocalDate(), participant); - List doneResponses = myChallengeService.getDoneInstances(user, targetDate.toLocalDate()); - - //then - DoneResponse doneResponse = doneResponses.get(0); - assertThat(doneResponses.size()).isEqualTo(1); - assertThat(doneResponse.getTitle()).isEqualTo(instance.getTitle()); - assertThat(doneResponse.getInstanceId()).isEqualTo(instance.getId()); - assertThat(doneResponse.getRewardedPoints()).isZero(); - assertThat(doneResponse.getJoinResult()).isEqualTo(SUCCESS); - assertThat(doneResponse.getFileResponse()).isNotNull(); - assertThat(doneResponse.isCanGetReward()).isTrue(); - assertThat(doneResponse.getNumOfPointItem()).isEqualTo(3); - } - - @Test - @DisplayName("완료된 챌린지 목록 조회 시, 포인트를 수령한 챌린지에 대해서는 포인트 수령 정보를 전달해야 한다.") - public void should_returnRewardInfo_when_alreadyRewarded() { - LocalDate targetDate = LocalDate.of(2024, 2, 14); - User user = getSavedUser(); - Instance instance = getSavedInstance(Progress.DONE); - getSavedParticipant(user, instance, SUCCESS); - - Item item = getSavedItem(ItemCategory.POINT_MULTIPLIER); - getSavedOrder(user, item, item.getItemCategory(), 3); - - //when - List doneResponses = myChallengeService.getDoneInstances(user, targetDate); - - //then - assertThat(doneResponses.size()).isEqualTo(1); - assertThat(doneResponses.get(0).isCanGetReward()).isTrue(); - } - - @Test - @DisplayName("챌린지는 종료되었으나 실패한 챌린지에 대해, 정보를 전달해야 한다.") - public void should_returnInfo_when_failedChallenge() { - //given - LocalDate targetDate = LocalDate.of(2024, 2, 14); - User user = getSavedUser(); - Instance instance = getSavedInstance(Progress.DONE); - getSavedParticipant(user, instance, FAIL); - - //when - List doneInstances = myChallengeService.getDoneInstances(user, targetDate); - DoneResponse doneResponse = doneInstances.get(0); - - //then - assertThat(doneInstances.size()).isEqualTo(1); - - assertThat(doneResponse.getInstanceId()).isEqualTo(instance.getId()); - assertThat(doneResponse.getTitle()).isEqualTo(instance.getTitle()); - assertThat(doneResponse.getInstanceId()).isEqualTo(instance.getId()); - assertThat(doneResponse.getRewardedPoints()).isZero(); - assertThat(doneResponse.getJoinResult()).isEqualTo(FAIL); - assertThat(doneResponse.getFileResponse()).isNotNull(); - assertThat(doneResponse.isCanGetReward()).isFalse(); - assertThat(doneResponse.getAchievementRate()).isEqualTo(0.0); - assertThat(doneResponse.getNumOfPointItem()).isEqualTo(0); - } - - - private User getSavedUser() { - return userRepository.save( - User.builder() - .role(Role.USER) - .nickname("nickname") - .providerInfo(ProviderInfo.GITHUB) - .identifier("githubId") - .information("information") - .tags("BE,FE") - .build() - ); - } - - private Instance getSavedInstance(Progress progress) { - return instanceRepository.save( - Instance.builder() - .progress(progress) - .pointPerPerson(100) - .title("title") - .startedDate(LocalDateTime.of(2024, 2, 1, 11, 3)) - .completedDate(LocalDateTime.of(2024, 3, 29, 23, 59)) - .build() - ); - } - - private Instance getSavedInstance(Progress progress, LocalDateTime startedDate, LocalDateTime completedDate) { - return instanceRepository.save( - Instance.builder() - .progress(progress) - .pointPerPerson(100) - .title("title") - .startedDate(startedDate) - .completedDate(completedDate) - .build() - ); - } - - private Participant getSavedParticipant(User user, Instance instance, JoinResult joinResult) { - Participant participant = participantRepository.save( - Participant.builder() - .joinResult(joinResult) - .joinStatus(JoinStatus.YES) - .rewardStatus(RewardStatus.NO) - .build() - ); - participant.setUserAndInstance(user, instance); - instance.updateParticipantCount(1); - return participant; - } - - - private Certification getSavedCertification(CertificateStatus status, LocalDate certificatedAt, - Participant participant) { - int attempt = DateUtil.getAttemptCount(participant.getStartedDate(), certificatedAt); - Certification certification = Certification.builder() - .certificationStatus(status) - .currentAttempt(attempt) - .certificatedAt(certificatedAt) - .certificationLinks("certificationLink") - .build(); - certification.setParticipant(participant); - return certificationRepository.save(certification); - } - - private Item getSavedItem(ItemCategory itemCategory) { - return itemRepository.save(Item.builder() - .itemCategory(itemCategory) - .cost(100) - .name(itemCategory.getName()) - .build()); - } - - private Orders getSavedOrder(User user, Item item, ItemCategory itemCategory, int count) { - Orders orders = Orders.of(count, itemCategory); - orders.setItem(item); - orders.setUser(user); - return ordersRepository.save(orders); - } -} \ No newline at end of file diff --git a/src/test/java/com/genius/gitget/challenge/participant/service/ParticipantProviderTest.java b/src/test/java/com/genius/gitget/challenge/participant/service/ParticipantServiceTest.java similarity index 91% rename from src/test/java/com/genius/gitget/challenge/participant/service/ParticipantProviderTest.java rename to src/test/java/com/genius/gitget/challenge/participant/service/ParticipantServiceTest.java index 821817ba..d1d9a38a 100644 --- a/src/test/java/com/genius/gitget/challenge/participant/service/ParticipantProviderTest.java +++ b/src/test/java/com/genius/gitget/challenge/participant/service/ParticipantServiceTest.java @@ -24,9 +24,9 @@ @SpringBootTest @Transactional -class ParticipantProviderTest { +class ParticipantServiceTest { @Autowired - ParticipantProvider participantProvider; + ParticipantService participantService; @Autowired UserRepository userRepository; @Autowired @@ -44,7 +44,7 @@ public void should_getParticipantInfo_when_passUserIdAndInstanceId() { getSavedParticipant(savedUser, savedInstance); //when - Participant participant = participantProvider.findByJoinInfo(savedUser.getId(), + Participant participant = participantService.findByJoinInfo(savedUser.getId(), savedInstance.getId()); //then @@ -61,7 +61,7 @@ public void should_getParticipant_when_passPK() { Participant participant = getSavedParticipant(savedUser, savedInstance); //when - Participant foundParticipant = participantProvider.findById(participant.getId()); + Participant foundParticipant = participantService.findById(participant.getId()); //then assertThat(foundParticipant.getId()).isEqualTo(participant.getId()); @@ -83,7 +83,7 @@ public void should_returnList_when_passProgress() { Participant participant3 = getSavedParticipant(user, instance3); //when - List participants = participantProvider.findJoinedByProgress(user.getId(), PREACTIVITY); + List participants = participantService.findJoinedByProgress(user.getId(), PREACTIVITY); //then assertThat(participants.size()).isEqualTo(2); @@ -102,7 +102,7 @@ public void should_return_activity_participants() { Participant participant3 = getSavedParticipant(user, instance3); //when - List participants = participantProvider.findJoinedByProgress(user.getId(), ACTIVITY); + List participants = participantService.findJoinedByProgress(user.getId(), ACTIVITY); //then assertThat(participants.size()).isEqualTo(1); @@ -121,7 +121,7 @@ public void should_return_quit_instances_when_activity() { Participant participant1 = getSavedParticipant(user, instance3, JoinStatus.NO); //when - List participants = participantProvider.findDoneInstances(user.getId()); + List participants = participantService.findDoneInstances(user.getId()); //then assertThat(participants.size()).isEqualTo(1); @@ -140,7 +140,7 @@ public void should_return_success_instances() { Participant participant1 = getSavedParticipant(user, instance3); //when - List participants = participantProvider.findDoneInstances(user.getId()); + List participants = participantService.findDoneInstances(user.getId()); //then assertThat(participants.size()).isEqualTo(2); diff --git a/src/test/java/com/genius/gitget/store/facade/StoreFacadeTest.java b/src/test/java/com/genius/gitget/store/facade/StoreFacadeTest.java index 8e61297b..d19c83fb 100644 --- a/src/test/java/com/genius/gitget/store/facade/StoreFacadeTest.java +++ b/src/test/java/com/genius/gitget/store/facade/StoreFacadeTest.java @@ -1,5 +1,8 @@ package com.genius.gitget.store.facade; +import static com.genius.gitget.challenge.participant.domain.JoinResult.SUCCESS; +import static com.genius.gitget.challenge.participant.domain.RewardStatus.NO; +import static com.genius.gitget.challenge.participant.domain.RewardStatus.YES; import static com.genius.gitget.global.util.exception.ErrorCode.ALREADY_REWARDED; import static com.genius.gitget.global.util.exception.ErrorCode.CAN_NOT_GET_REWARDS; import static com.genius.gitget.global.util.exception.ErrorCode.CAN_NOT_USE_PASS_ITEM; @@ -15,7 +18,6 @@ import com.genius.gitget.challenge.instance.repository.InstanceRepository; import com.genius.gitget.challenge.participant.domain.JoinResult; import com.genius.gitget.challenge.participant.domain.Participant; -import com.genius.gitget.challenge.participant.domain.RewardStatus; import com.genius.gitget.challenge.participant.repository.ParticipantRepository; import com.genius.gitget.challenge.user.domain.Role; import com.genius.gitget.challenge.user.domain.User; @@ -223,7 +225,7 @@ public void it_returns_200_when_certification_not_exist() { public void it_returns_200_when_certification_is_NOT_YET() { int holding = orders.getCount(); certificationRepository.save( - CertificationFactory.create(CertificateStatus.NOT_YET, currentDate, participant) + CertificationFactory.createNotYet(participant, currentDate) ); storeFacade.useItem(user, item.getIdentifier(), instance.getId(), currentDate); @@ -303,7 +305,7 @@ public void it_returns_200_when_instance_is_DONE() { int holding = orders.getCount(); instance = instanceRepository.save(InstanceFactory.createDone(10)); participant = participantRepository.save( - ParticipantFactory.createByRewardStatus(user, instance, RewardStatus.NO)); + ParticipantFactory.createByRewardStatus(user, instance, SUCCESS, NO)); storeFacade.useItem(user, item.getIdentifier(), instance.getId(), currentDate); @@ -334,7 +336,7 @@ public void it_throws_exception_when_JoinResult_not_SUCCESS(JoinResult joinResul @DisplayName("participant의 RewardStatus가 YES라면 ALREADY_REWARDED 예외가 발생한다.") public void it_throws_exception_when_RewardStatus_is_YES() { participant = participantRepository.save( - ParticipantFactory.createByRewardStatus(user, instance, RewardStatus.YES) + ParticipantFactory.createByRewardStatus(user, instance, SUCCESS, YES) ); assertThatThrownBy(() -> storeFacade.useItem(user, item.getIdentifier(), instance.getId(), currentDate)) .isInstanceOf(BusinessException.class) @@ -349,7 +351,7 @@ class context_item_count_is_zero { void setup() { instance = instanceRepository.save(InstanceFactory.createDone(10)); participant = participantRepository.save( - ParticipantFactory.createByRewardStatus(user, instance, RewardStatus.NO)); + ParticipantFactory.createByRewardStatus(user, instance, SUCCESS, NO)); } @Test diff --git a/src/test/java/com/genius/gitget/util/certification/CertificationFactory.java b/src/test/java/com/genius/gitget/util/certification/CertificationFactory.java index e68b9dd7..572a3717 100644 --- a/src/test/java/com/genius/gitget/util/certification/CertificationFactory.java +++ b/src/test/java/com/genius/gitget/util/certification/CertificationFactory.java @@ -19,4 +19,40 @@ public static Certification create(CertificateStatus status, LocalDate certifica certification.setParticipant(participant); return certification; } + + public static Certification createNotYet(Participant participant, LocalDate certificatedAt) { + int attempt = DateUtil.getAttemptCount(participant.getStartedDate(), certificatedAt); + Certification certification = Certification.builder() + .certificationStatus(CertificateStatus.NOT_YET) + .currentAttempt(attempt) + .certificatedAt(certificatedAt) + .certificationLinks(null) + .build(); + certification.setParticipant(participant); + return certification; + } + + public static Certification createCertificated(Participant participant, LocalDate certificatedAt) { + int attempt = DateUtil.getAttemptCount(participant.getStartedDate(), certificatedAt); + Certification certification = Certification.builder() + .certificationStatus(CertificateStatus.CERTIFICATED) + .currentAttempt(attempt) + .certificatedAt(certificatedAt) + .certificationLinks("certificationLink") + .build(); + certification.setParticipant(participant); + return certification; + } + + public static Certification createPassed(Participant participant, LocalDate certificatedAt) { + int attempt = DateUtil.getAttemptCount(participant.getStartedDate(), certificatedAt); + Certification certification = Certification.builder() + .certificationStatus(CertificateStatus.PASSED) + .currentAttempt(attempt) + .certificatedAt(certificatedAt) + .certificationLinks(null) + .build(); + certification.setParticipant(participant); + return certification; + } } diff --git a/src/test/java/com/genius/gitget/util/instance/InstanceFactory.java b/src/test/java/com/genius/gitget/util/instance/InstanceFactory.java index 1a6de597..8adc887e 100644 --- a/src/test/java/com/genius/gitget/util/instance/InstanceFactory.java +++ b/src/test/java/com/genius/gitget/util/instance/InstanceFactory.java @@ -32,7 +32,7 @@ public static Instance createActivity(int duration) { */ public static Instance createDone(int duration) { return Instance.builder() - .progress(Progress.ACTIVITY) + .progress(Progress.DONE) .startedDate(LocalDateTime.now().minusDays(duration - 1)) .completedDate(LocalDateTime.now().minusDays(1)) .build(); diff --git a/src/test/java/com/genius/gitget/util/participant/ParticipantFactory.java b/src/test/java/com/genius/gitget/util/participant/ParticipantFactory.java index 02b583a0..ba66082f 100644 --- a/src/test/java/com/genius/gitget/util/participant/ParticipantFactory.java +++ b/src/test/java/com/genius/gitget/util/participant/ParticipantFactory.java @@ -8,6 +8,21 @@ import com.genius.gitget.challenge.user.domain.User; public class ParticipantFactory { + /** + * 시작 전인 참여 정보 엔티티 만들어서 반환 + * user, instance를 받아서 연관관계 설정 후 반환 + */ + public static Participant createPreActivity(User user, Instance instance) { + Participant participant = Participant.builder() + .joinResult(JoinResult.READY) + .joinStatus(JoinStatus.YES) + .build(); + participant.setUserAndInstance(user, instance); + participant.updateRepository("targetRepo"); + + return participant; + } + /** * 진행 중인 참여 정보 엔티티 만들어서 반환 * user, instance를 받아서 연관관계 설정 후 반환 @@ -42,9 +57,10 @@ public static Participant createByJoinResult(User user, Instance instance, JoinR * 챌린지가 끝난 참여 정보에 대해, RewardStatus(보상 수령 상태)에 대한 값을 설정 후 반환 * user, instance를 받아서 연관관계 설정 후 반환 */ - public static Participant createByRewardStatus(User user, Instance instance, RewardStatus rewardStatus) { + public static Participant createByRewardStatus(User user, Instance instance, JoinResult joinResult, + RewardStatus rewardStatus) { Participant participant = Participant.builder() - .joinResult(JoinResult.SUCCESS) + .joinResult(joinResult) .joinStatus(JoinStatus.YES) .rewardStatus(rewardStatus) .build(); @@ -53,4 +69,16 @@ public static Participant createByRewardStatus(User user, Instance instance, Rew return participant; } + + public static Participant createQuit(User user, Instance instance, JoinResult joinResult) { + Participant participant = Participant.builder() + .joinResult(joinResult) + .joinStatus(JoinStatus.NO) + .rewardStatus(RewardStatus.NO) + .build(); + participant.setUserAndInstance(user, instance); + participant.updateRepository("targetRepo"); + + return participant; + } } diff --git a/src/test/java/com/genius/gitget/util/user/UserFactory.java b/src/test/java/com/genius/gitget/util/user/UserFactory.java new file mode 100644 index 00000000..7111e8da --- /dev/null +++ b/src/test/java/com/genius/gitget/util/user/UserFactory.java @@ -0,0 +1,30 @@ +package com.genius.gitget.util.user; + +import com.genius.gitget.challenge.user.domain.Role; +import com.genius.gitget.challenge.user.domain.User; +import com.genius.gitget.global.security.constants.ProviderInfo; + +public class UserFactory { + + public static User createUser() { + return User.builder() + .role(Role.USER) + .nickname("nickname") + .providerInfo(ProviderInfo.GITHUB) + .identifier("githubId") + .information("information") + .tags("BE,FE") + .build(); + } + + public static User createAdmin() { + return User.builder() + .role(Role.ADMIN) + .nickname("nickname") + .providerInfo(ProviderInfo.GITHUB) + .identifier("githubId") + .information("information") + .tags("BE,FE") + .build(); + } +}