Skip to content

Commit

Permalink
[#56] refactor: 응답 정적 팩토리 메서드 사용
Browse files Browse the repository at this point in the history
  • Loading branch information
NaMinhyeok committed Feb 6, 2025
1 parent f8b84e1 commit bf0470c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,21 @@ public record SurveyHistoryResponse(
Long bundleId,
List<SurveyHistoryDetailResponse> surveyHistoryDetails,
Integer nextSurveyIndex,
boolean isCompleted) {}
boolean isCompleted) {

public static SurveyHistoryResponse of(
Long bundleId,
List<SurveyHistoryDetailResponse> surveyHistoryDetails,
Integer nextSurveyIndex,
boolean isCompleted) {
return new SurveyHistoryResponse(bundleId, surveyHistoryDetails, nextSurveyIndex, isCompleted);
}

public static SurveyHistoryResponse createNextBundleSurveyHistory(Long bundleId) {
return new SurveyHistoryResponse(bundleId, List.of(), 1, false);
}

public static SurveyHistoryResponse createInitialSurveyHistory() {
return new SurveyHistoryResponse(1L, List.of(), 1, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private List<Survey> getSubmittedSurvey(final Long memberId) {
public SurveyHistoryResponse getSurveyHistory(final Long memberId) {
return findLatestSubmission(memberId)
.map(this::classifySubmission)
.orElseGet(this::createInitialSurveyHistory);
.orElseGet(SurveyHistoryResponse::createInitialSurveyHistory);
}

private Optional<SurveySubmission> findLatestSubmission(final Long memberId) {
Expand All @@ -64,46 +64,36 @@ private SurveyHistoryResponse classifySubmission(SurveySubmission latestSubmissi
List<SurveySubmission> submissions =
findSubmissionsForBundle(latestSubmission.getMember().getId(), bundle.getId());

boolean isSubmittedToday = latestSubmission.isSubmittedByDate(LocalDate.now());
if (isSubmittedToday) {
return createCurrentBundleSurveyHistory(bundle.getId(), submissions, isSubmittedToday);
boolean isCompleted = latestSubmission.isSubmittedByDate(LocalDate.now());
if (isCompleted) {
return createCurrentBundleSurveyHistory(bundle.getId(), submissions, isCompleted);
}

if (bundle.isAllSubmitted(submissions)) {
return getNextBundleHistory(bundle.getId());
}
return createCurrentBundleSurveyHistory(bundle.getId(), submissions, isSubmittedToday);
return createCurrentBundleSurveyHistory(bundle.getId(), submissions, isCompleted);
}

private List<SurveySubmission> findSubmissionsForBundle(
final Long memberId, final Long bundleId) {
return surveySubmissionRepository.findByMember_IdAndSurvey_SurveyBundle_Id(memberId, bundleId);
}

private SurveyHistoryResponse createInitialSurveyHistory() {
return new SurveyHistoryResponse(1L, List.of(), 1, false);
}

private SurveyHistoryResponse createNextBundleSurveyHistory(final Long bundleId) {
return new SurveyHistoryResponse(bundleId, List.of(), 1, false);
}

private SurveyHistoryResponse createCurrentBundleSurveyHistory(
Long bundleId, List<SurveySubmission> submissions, boolean isSubmittedToday) {
List<SurveyHistoryDetailResponse> historyDetails =
submissions.stream().map(BaseEntity::getId).map(SurveyHistoryDetailResponse::of).toList();
if (isSubmittedToday) {
return new SurveyHistoryResponse(
bundleId, historyDetails, historyDetails.size(), isSubmittedToday);
return SurveyHistoryResponse.of(bundleId, historyDetails, historyDetails.size(), true);
}
return new SurveyHistoryResponse(
bundleId, historyDetails, historyDetails.size() + 1, isSubmittedToday);
return SurveyHistoryResponse.of(bundleId, historyDetails, historyDetails.size() + 1, false);
}

private SurveyHistoryResponse getNextBundleHistory(Long currentBundleId) {
return surveyBundleRepository
.findFirstByIdGreaterThanOrderByIdAsc(currentBundleId)
.map(bundle -> createNextBundleSurveyHistory(bundle.getId()))
.map(bundle -> SurveyHistoryResponse.createNextBundleSurveyHistory(bundle.getId()))
.orElseThrow(() -> CustomException.NOT_READY_FOR_NEXT_BUNDLE);
}

Expand Down

0 comments on commit bf0470c

Please sign in to comment.