Skip to content

Commit

Permalink
[MODIFY] 일정이 끝났을 때, 일정 관련 api에서 가장 마지막 일정을 반환하도록 수정 (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
kseysh authored Nov 14, 2024
2 parents eff9adb + 0475624 commit bb00bcc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.sopt.app.application.calendar;

import java.time.*;
import java.util.*;
import lombok.RequiredArgsConstructor;
import org.sopt.app.common.utils.CurrentDate;
import org.sopt.app.domain.cache.*;
import org.sopt.app.domain.entity.Calendar;
import org.sopt.app.interfaces.postgres.CalendarRepository;
Expand All @@ -26,10 +26,14 @@ public class CalendarServiceImpl implements CalendarService {
@Override
@Transactional
public List<CalendarResponse> getAllCurrentGenerationCalendarResponse() {

return this.getAllCurrentGenerationCalendar().stream()
.map(CalendarResponse::of)
.toList();
List<Calendar> calendars = this.getAllCurrentGenerationCalendar();
Optional<Calendar> recentCalendar = this.getRecentCalendar(calendars);
return recentCalendar.map(value -> calendars.stream()
.map(calendar -> CalendarResponse.of(calendar, calendar.getId().equals(value.getId())))
.toList())
.orElseGet(() -> this.getAllCurrentGenerationCalendar().stream()
.map(calendar -> CalendarResponse.of(calendar, false))
.toList());
}

private List<Calendar> getAllCurrentGenerationCalendar() {
Expand All @@ -48,14 +52,17 @@ private List<Calendar> cacheAllCalendarResponse() {
return calendars;
}

private Optional<Calendar> getRecentCalendar(List<Calendar> calendars) {
return calendars.stream()
.filter(calendar -> !calendar.getStartDate().isBefore(CurrentDate.now))
.findFirst();
}

@Override
@Transactional
public RecentCalendarResponse getRecentCalendarResponse() {
LocalDate now = ZonedDateTime.now(ZoneId.of("Asia/Seoul")).toLocalDate();

return this.getAllCurrentGenerationCalendar().stream()
.filter(calendar -> !calendar.getStartDate().isBefore(now))
.findFirst().map(RecentCalendarResponse::of)
.orElseGet(() -> RecentCalendarResponse.createEmptyCalendar(now));
List<Calendar> calendars = this.getAllCurrentGenerationCalendar();
return this.getRecentCalendar(calendars).map(RecentCalendarResponse::of)
.orElseGet(() -> RecentCalendarResponse.of(calendars.getLast()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ public class CalendarResponse {
private final LocalDate endDate;
private final Boolean isOneDaySchedule;
private final Boolean isOnlyActiveGeneration;
private final Boolean isRecentSchedule;

public static CalendarResponse of(Calendar calendar) {
public static CalendarResponse of(Calendar calendar, Boolean isRecentSchedule) {
return CalendarResponse.builder()
.startDate(calendar.getStartDate())
.endDate(calendar.getEndDate())
.title(calendar.getTitle())
.isOneDaySchedule(calendar.getIsOneDaySchedule())
.isOnlyActiveGeneration(calendar.getIsOnlyActiveGeneration())
.isRecentSchedule(isRecentSchedule)
.build();
}
}

0 comments on commit bb00bcc

Please sign in to comment.