HabitPay / backend

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

[feat] 챌린지 별 참여 기록을 조회 : 날짜 목록 제공하는 API #285 #291

Closed solgito closed 3 weeks ago

solgito commented 3 weeks ago

개요

Screenshot 2024-10-24 at 5 14 32 AM


코드 주요 내용

// ChallengeApi.java

@GetMapping("/challenges/{id}/records")
public SuccessResponse<ChallengeRecordsResponse> getChallengeRecords(@PathVariable("id") Long id,
                                             @AuthenticationPrincipal CustomUserDetails user) {
    return challengeRecordsService.getChallengeRecords(id, user.getMember());
}

// challenge/dto/ChallengeRecordsResponse.java

public class ChallengeRecordsResponse {
    List<LocalDate> successDayList;
    List<LocalDate> failDayList;
    List<LocalDate> upcomingDayList;
}

// ChallengeRecordsService.java

public SuccessResponse<ChallengeRecordsResponse> getChallengeRecords(Long challengeId, Member member) {
    Challenge challenge = challengeSearchService.getChallengeById(challengeId);
    ChallengeEnrollment enrollment = challengeEnrollmentSearchService.getByMemberAndChallenge(member, challenge);

    List<LocalDate> successDayList = new ArrayList<>();
    List<LocalDate> failDayList = new ArrayList<>();
    List<LocalDate> upcomingDayList = new ArrayList<>();

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

    // 핵심 로직
    recordList
            .forEach(record -> {
                LocalDate targetDate = TimeZoneConverter.convertEtcToLocalTimeZone(record.getTargetDate()).toLocalDate();
                if (targetDate.isBefore(today)) {
                    List<LocalDate> targetList = record.existsChallengePost() ? successDayList : failDayList;
                    targetList.add(targetDate);
                } else if (targetDate.isEqual(today)) {
                    List<LocalDate> targetList = record.existsChallengePost() ? successDayList : upcomingDayList;
                    targetList.add(targetDate);
                } else {
                    upcomingDayList.add(targetDate);
                }
            });

    return SuccessResponse.of(
            SuccessCode.NO_MESSAGE,
            ChallengeRecordsResponse.builder()
                    .successDayList(successDayList)
                    .failDayList(failDayList)
                    .upcomingDayList(upcomingDayList)
                    .build()
    );
}