HabitPay / backend

https://habitpay.github.io/backend/
0 stars 0 forks source link

[refactor] 챌린지 참여 기록을 위한 메서드 리팩토링 #294 #295

Closed solgito closed 3 weeks ago

solgito commented 3 weeks ago

개요


코드 주요 내용

// ChallengeRecords.java

public class ChallengeRecords {
    private List<LocalDate> successDayList;
    private List<LocalDate> failureDayList;
    private List<LocalDate> upcomingDayList;

    public ChallengeRecords() {
        successDayList = new ArrayList<>();
        failureDayList = new ArrayList<>();
        upcomingDayList = new ArrayList<>();
    }

    public void addSuccessDay(LocalDate date) {
        successDayList.add(date);
    }

    public void addFailureDay(LocalDate date) {
        failureDayList.add(date);
    }

    public void addUpcomingDay(LocalDate date) {
        upcomingDayList.add(date);
    }
}

// ChallengeRecordsService.java

public SuccessResponse<ChallengeRecordsResponse> getChallengeRecords(Long challengeId, Member member) {
    ...

    ChallengeRecords challengeRecords = new ChallengeRecords();
    List<ChallengeParticipationRecord> recordList = challengeParticipationRecordSearchService.findAllByChallengeEnrollment(enrollment);
    LocalDate today = TimeZoneConverter.convertEtcToLocalTimeZone(ZonedDateTime.now()).toLocalDate();

    recordList.forEach(record -> categorizeRecord(record, today, challengeRecords));

    return SuccessResponse.of(
            SuccessCode.NO_MESSAGE,
            ChallengeRecordsResponse.builder()
                    .successDayList(challengeRecords.getSuccessDayList())
                    .failureDayList(challengeRecords.getFailureDayList())
                    .upcomingDayList(challengeRecords.getUpcomingDayList())
                    .build()
    );
}

private void categorizeRecord(ChallengeParticipationRecord record, LocalDate today, ChallengeRecords challengeRecords) {
    LocalDate targetDate = TimeZoneConverter.convertEtcToLocalTimeZone(record.getTargetDate()).toLocalDate();

    if (targetDate.isBefore(today)) {
        if (record.existsChallengePost()) {
            challengeRecords.addSuccessDay(targetDate);
        } else {
            challengeRecords.addFailureDay(targetDate);
        }
    } else if (targetDate.isEqual(today)) {
        if (record.existsChallengePost()) {
            challengeRecords.addSuccessDay(targetDate);
        } else {
            challengeRecords.addUpcomingDay(targetDate);
        }
    } else {
        challengeRecords.addUpcomingDay(targetDate);
    }
}

기타

// ex

failDayList => failureDayList