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

[FEAT] InstanceDetail Facade Pattern 적용 #242

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.genius.gitget.challenge.certification.facade.CertificationFacade;
import com.genius.gitget.challenge.certification.util.DateUtil;
import com.genius.gitget.challenge.instance.domain.Instance;
import com.genius.gitget.challenge.instance.service.InstanceProvider;
import com.genius.gitget.challenge.instance.service.InstanceService;
import com.genius.gitget.challenge.myChallenge.dto.ActivatedResponse;
import com.genius.gitget.challenge.participant.domain.Participant;
import com.genius.gitget.challenge.participant.service.ParticipantService;
Expand Down Expand Up @@ -43,8 +43,8 @@
@RequestMapping("/api/certification")
public class CertificationController {
private final UserService userService;
private final InstanceService instanceService;
private final CertificationFacade certificationFacade;
private final InstanceProvider instanceProvider;
private final ParticipantService participantService;


Expand Down Expand Up @@ -139,7 +139,7 @@ public ResponseEntity<SingleResponse<CertificationInformation>> getCertification
) {

LocalDate kstDate = DateUtil.convertToKST(LocalDateTime.now());
Instance instance = instanceProvider.findById(instanceId);
Instance instance = instanceService.findInstanceById(instanceId);
Participant participant = participantService.findByJoinInfo(
userPrincipal.getUser().getId(),
instanceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.genius.gitget.challenge.instance.dto.detail.InstanceResponse;
import com.genius.gitget.challenge.instance.dto.detail.JoinRequest;
import com.genius.gitget.challenge.instance.dto.detail.JoinResponse;
import com.genius.gitget.challenge.instance.service.InstanceDetailService;
import com.genius.gitget.challenge.instance.service.InstanceDetailFacade;
import com.genius.gitget.global.security.domain.UserPrincipal;
import com.genius.gitget.global.util.response.dto.SingleResponse;
import java.time.LocalDate;
Expand All @@ -30,15 +30,15 @@
@RequiredArgsConstructor
@RequestMapping("/api/challenges")
public class InstanceDetailController {
private final InstanceDetailService instanceDetailService;
private final InstanceDetailFacade instanceDetailFacade;


@GetMapping("/{instanceId}")
public ResponseEntity<SingleResponse<InstanceResponse>> getInstanceDetail(
@AuthenticationPrincipal UserPrincipal userPrincipal,
@PathVariable Long instanceId
) {
InstanceResponse instanceDetailInformation = instanceDetailService.getInstanceDetailInformation(
InstanceResponse instanceDetailInformation = instanceDetailFacade.getInstanceDetailInformation(
userPrincipal.getUser(), instanceId);

return ResponseEntity.ok().body(
Expand All @@ -58,7 +58,7 @@ public ResponseEntity<SingleResponse<JoinResponse>> joinChallenge(
.repository(repo)
.todayDate(kstDate)
.build();
JoinResponse joinResponse = instanceDetailService.joinNewChallenge(userPrincipal.getUser(), joinRequest);
JoinResponse joinResponse = instanceDetailFacade.joinNewChallenge(userPrincipal.getUser(), joinRequest);

return ResponseEntity.ok().body(
new SingleResponse<>(JOIN_SUCCESS.getStatus(), JOIN_SUCCESS.getMessage(), joinResponse)
Expand All @@ -70,7 +70,7 @@ public ResponseEntity<SingleResponse<JoinResponse>> quitChallenge(
@AuthenticationPrincipal UserPrincipal userPrincipal,
@PathVariable Long instanceId
) {
JoinResponse joinResponse = instanceDetailService.quitChallenge(userPrincipal.getUser(), instanceId);
JoinResponse joinResponse = instanceDetailFacade.quitChallenge(userPrincipal.getUser(), instanceId);

return ResponseEntity.ok().body(
new SingleResponse<>(QUIT_SUCCESS.getStatus(), QUIT_SUCCESS.getMessage(), joinResponse)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.genius.gitget.challenge.instance.service;

import com.genius.gitget.challenge.instance.dto.detail.InstanceResponse;
import com.genius.gitget.challenge.instance.dto.detail.JoinRequest;
import com.genius.gitget.challenge.instance.dto.detail.JoinResponse;
import com.genius.gitget.challenge.user.domain.User;

public interface InstanceDetailFacade {
InstanceResponse getInstanceDetailInformation(User user, Long instanceId);

JoinResponse joinNewChallenge(User user, JoinRequest joinRequest);

public JoinResponse quitChallenge(User user, Long instanceId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import com.genius.gitget.challenge.instance.dto.detail.JoinRequest;
import com.genius.gitget.challenge.instance.dto.detail.JoinResponse;
import com.genius.gitget.challenge.instance.dto.detail.LikesInfo;
import com.genius.gitget.challenge.likes.domain.Likes;
import com.genius.gitget.challenge.likes.repository.LikesRepository;
import com.genius.gitget.challenge.likes.service.LikesService;
import com.genius.gitget.challenge.participant.domain.JoinStatus;
import com.genius.gitget.challenge.participant.domain.Participant;
import com.genius.gitget.challenge.participant.service.ParticipantService;
Expand All @@ -21,52 +20,55 @@
import com.genius.gitget.global.file.service.FilesService;
import com.genius.gitget.global.util.exception.BusinessException;
import java.time.LocalDate;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.kohsuke.github.GitHub;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class InstanceDetailService {
private final UserService userService;
import org.springframework.stereotype.Component;

@Component
public class InstanceDetailFacadeService implements InstanceDetailFacade {

private final InstanceService instanceService;
private final FilesService filesService;
private final InstanceProvider instanceProvider;
private final ParticipantService participantService;
private final LikesService likesService;
private final UserService userService;
private final GithubService githubService;
private final LikesRepository likesRepository;

public InstanceDetailFacadeService(InstanceService instanceService, FilesService filesService,
ParticipantService participantService, LikesService likesService,
UserService userService, GithubService githubService) {
this.instanceService = instanceService;
this.filesService = filesService;
this.participantService = participantService;
this.likesService = likesService;
this.userService = userService;
this.githubService = githubService;
}

@Override
public InstanceResponse getInstanceDetailInformation(User user, Long instanceId) {
Instance instance = instanceProvider.findById(instanceId);

// 인스턴스 정보
Instance instance = instanceService.findInstanceById(instanceId);

// 파일 객체 생성
FileResponse fileResponse = filesService.convertToFileResponse(instance.getFiles());
LikesInfo likesInfo = getLikesInfo(user.getId(), instance);

if (participantService.hasJoinedParticipant(user.getId(), instanceId)) {
// 좋아요 정보
LikesInfo likesInfo = likesService.getLikesInfo(user.getId(), instance);

if (participantService.hasJoinedParticipant(user.getId(), instance.getId())) {
return InstanceResponse.createByEntity(instance, likesInfo, JoinStatus.YES, fileResponse);
}

return InstanceResponse.createByEntity(instance, likesInfo, JoinStatus.NO, fileResponse);
}

private LikesInfo getLikesInfo(Long userId, Instance instance) {
Optional<Likes> optionalLikes = likesRepository.findSpecificLike(userId, instance.getId());
if (optionalLikes.isPresent()) {
Likes likes = optionalLikes.get();
return LikesInfo.createExist(likes.getId(), instance.getLikesCount());
}

return LikesInfo.createNotExist(instance.getLikesCount());
}

@Transactional
@Override
public JoinResponse joinNewChallenge(User user, JoinRequest joinRequest) {

User persistUser = userService.findUserById(user.getId());
Instance instance = instanceProvider.findById(joinRequest.instanceId());

Instance instance = instanceService.findInstanceById(joinRequest.instanceId());

String repository = joinRequest.repository();

Expand Down Expand Up @@ -103,9 +105,9 @@ private void validateGithub(User user, String repository) {
githubService.validateGithubRepository(gitHub, repositoryFullName);
}

@Transactional
@Override
public JoinResponse quitChallenge(User user, Long instanceId) {
Instance instance = instanceProvider.findById(instanceId);
Instance instance = instanceService.findInstanceById(instanceId);
Participant participant = participantService.findByJoinInfo(user.getId(), instanceId);

if (instance.getProgress() == Progress.DONE) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.genius.gitget.challenge.likes.service;

import com.genius.gitget.challenge.instance.domain.Instance;
import com.genius.gitget.challenge.instance.dto.detail.LikesInfo;
import com.genius.gitget.challenge.instance.repository.InstanceRepository;
import com.genius.gitget.challenge.likes.domain.Likes;
import com.genius.gitget.challenge.likes.repository.LikesRepository;
Expand All @@ -10,6 +11,7 @@
import com.genius.gitget.global.util.exception.ErrorCode;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -65,6 +67,16 @@ public void deleteLikes(Long likesId) {
likesRepository.deleteById(findLikes.getId());
}

public LikesInfo getLikesInfo(Long userId, Instance instance) {
Optional<Likes> optionalLikes = likesRepository.findSpecificLike(userId, instance.getId());
if (optionalLikes.isPresent()) {
Likes likes = optionalLikes.get();
return LikesInfo.createExist(likes.getId(), instance.getLikesCount());
}

return LikesInfo.createNotExist(instance.getLikesCount());
}

private List<User> verifyUser(User user) {
return userRepository.findAllByIdentifier(user.getIdentifier());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.genius.gitget.challenge.certification.service.CertificationService;
import com.genius.gitget.challenge.instance.domain.Instance;
import com.genius.gitget.challenge.instance.domain.Progress;
import com.genius.gitget.challenge.instance.service.InstanceProvider;
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 java.time.LocalDate;
Expand All @@ -22,13 +22,13 @@
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class ProgressService {
private final InstanceProvider instanceProvider;
private final CertificationService certificationService;
private final InstanceRepository instanceRepository;
private final double SUCCESS_THRESHOLD = 85;

@Transactional
public void updateToActivity(LocalDate currentDate) {
List<Instance> preActivities = instanceProvider.findAllByProgress(Progress.PREACTIVITY);
List<Instance> preActivities = instanceRepository.findAllByProgress(Progress.PREACTIVITY);
for (Instance preActivity : preActivities) {
LocalDate startedDate = preActivity.getStartedDate().toLocalDate();
LocalDate completedDate = preActivity.getCompletedDate().toLocalDate();
Expand All @@ -53,8 +53,8 @@ private void updateActivityInstance(Instance preActivity) {
@Transactional
public void updateToDone(LocalDate currentDate) {
List<Instance> instances = new ArrayList<>();
instances.addAll(instanceProvider.findAllByProgress(Progress.PREACTIVITY));
instances.addAll(instanceProvider.findAllByProgress(Progress.ACTIVITY));
instances.addAll(instanceRepository.findAllByProgress(Progress.PREACTIVITY));
instances.addAll(instanceRepository.findAllByProgress(Progress.ACTIVITY));

for (Instance instance : instances) {
LocalDate startedDate = instance.getStartedDate().toLocalDate();
Expand Down
Loading