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] MyChallengeController에 Facade & DCI 패턴 적용 #235

Merged
merged 12 commits into from
Aug 6, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -92,7 +92,7 @@ public ResponseEntity<SingleResponse<WeekResponse>> 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(
Expand Down Expand Up @@ -123,7 +123,7 @@ public ResponseEntity<SingleResponse<TotalResponse>> 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);

Expand All @@ -140,7 +140,7 @@ public ResponseEntity<SingleResponse<CertificationInformation>> getCertification

LocalDate kstDate = DateUtil.convertToKST(LocalDateTime.now());
Instance instance = instanceProvider.findById(instanceId);
Participant participant = participantProvider.findByJoinInfo(
Participant participant = participantService.findByJoinInfo(
userPrincipal.getUser().getId(),
instanceId);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -83,4 +90,12 @@ private CertificateStatus getCertificateStatus(List<String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<WeekResponse> getOthersWeekCertifications(Long userId, Long instanceId,
LocalDate currentDate, Pageable pageable) {
Slice<Participant> participants = participantProvider.findAllByInstanceId(userId, instanceId, pageable);
Slice<Participant> participants = participantService.findAllByInstanceId(userId, instanceId, pageable);
return participants.map(
participant -> getWeekResponse(participant, currentDate)
);
Expand Down Expand Up @@ -107,7 +107,7 @@ private List<CertificationResponse> getWeekCertifications(List<Certification> 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();
Expand Down Expand Up @@ -156,7 +156,7 @@ private List<CertificationResponse> getTotalCertifications(List<Certification> 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<Certification> optionalCertification = certificationProvider.findByDate(targetDate,
Expand All @@ -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);
}

Expand All @@ -193,7 +193,7 @@ private void validatePassCondition(Optional<Certification> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand All @@ -106,15 +106,15 @@ 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);
}

if (instance.getProgress() == Progress.PREACTIVITY) {
instance.updateParticipantCount(-1);
participantProvider.delete(participant);
participantService.delete(participant);
return JoinResponse.createQuitResponse();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,14 +28,13 @@
@RequiredArgsConstructor
@CrossOrigin
public class MyChallengeController {
private final MyChallengeService myChallengeService;

private final MyChallengeFacade myChallengeFacade;

@GetMapping("/my/pre-activity")
public ResponseEntity<ListResponse<PreActivityResponse>> getPreActivityChallenges(
@AuthenticationPrincipal UserPrincipal userPrincipal
) {
List<PreActivityResponse> preActivityInstances = myChallengeService.getPreActivityInstances(
List<PreActivityResponse> preActivityInstances = myChallengeFacade.getPreActivityInstances(
userPrincipal.getUser(),
DateUtil.convertToKST(LocalDateTime.now()));

Expand All @@ -49,7 +48,7 @@ public ResponseEntity<ListResponse<PreActivityResponse>> getPreActivityChallenge
public ResponseEntity<ListResponse<ActivatedResponse>> getActivatedChallenges(
@AuthenticationPrincipal UserPrincipal userPrincipal
) {
List<ActivatedResponse> activatedInstances = myChallengeService.getActivatedInstances(
List<ActivatedResponse> activatedInstances = myChallengeFacade.getActivatedInstances(
userPrincipal.getUser(),
DateUtil.convertToKST(LocalDateTime.now()));

Expand All @@ -62,7 +61,7 @@ public ResponseEntity<ListResponse<ActivatedResponse>> getActivatedChallenges(
public ResponseEntity<ListResponse<DoneResponse>> getDoneChallenges(
@AuthenticationPrincipal UserPrincipal userPrincipal
) {
List<DoneResponse> doneInstances = myChallengeService.getDoneInstances(
List<DoneResponse> doneInstances = myChallengeFacade.getDoneInstances(
userPrincipal.getUser(),
DateUtil.convertToKST(LocalDateTime.now()));

Expand All @@ -77,8 +76,8 @@ public ResponseEntity<SingleResponse<DoneResponse>> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<PreActivityResponse> getPreActivityInstances(User user, LocalDate targetDate);

List<ActivatedResponse> getActivatedInstances(User user, LocalDate targetDate);

List<DoneResponse> getDoneInstances(User user, LocalDate targetDate);

DoneResponse getRewards(RewardRequest rewardRequest);
}
Loading