|
| 1 | +package com.wypl.wyplcore.calendar.service; |
| 2 | + |
| 3 | +import java.time.LocalDate; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +import org.springframework.stereotype.Service; |
| 8 | +import org.springframework.transaction.annotation.Transactional; |
| 9 | + |
| 10 | +import com.wypl.googleoauthclient.domain.AuthMember; |
| 11 | +import com.wypl.jpacalendardomain.calendar.domain.Calendar; |
| 12 | +import com.wypl.jpacalendardomain.calendar.domain.MemberCalendar; |
| 13 | +import com.wypl.jpacalendardomain.calendar.domain.Schedule; |
| 14 | +import com.wypl.jpacalendardomain.calendar.repository.ScheduleRepository; |
| 15 | +import com.wypl.jpamemberdomain.member.domain.Member; |
| 16 | +import com.wypl.wyplcore.calendar.data.DateSearchCondition; |
| 17 | +import com.wypl.wyplcore.calendar.data.request.CalendarFindRequest; |
| 18 | +import com.wypl.wyplcore.calendar.data.response.CalendarSchedulesResponse; |
| 19 | +import com.wypl.wyplcore.calendar.service.strategy.CalendarStrategy; |
| 20 | +import com.wypl.wyplcore.schedule.data.CalendarType; |
| 21 | +import com.wypl.wyplcore.schedule.data.response.ScheduleFindResponse; |
| 22 | +import com.wypl.wyplcore.schedule.service.repetition.RepetitionService; |
| 23 | + |
| 24 | +import lombok.RequiredArgsConstructor; |
| 25 | + |
| 26 | +@Service |
| 27 | +@RequiredArgsConstructor |
| 28 | +public class CalendarService { |
| 29 | + |
| 30 | + private final ScheduleRepository scheduleRepository; |
| 31 | + |
| 32 | + private final Map<CalendarType, CalendarStrategy> calendarStrategyMap; |
| 33 | + |
| 34 | + /** |
| 35 | + * 캘린더의 일정을 조회한다. |
| 36 | + * @param authMember : 인증된 사용자 정보 |
| 37 | + * @param calendarId : 조회할 캘린더 ID |
| 38 | + * @param calendarFindRequest : 캘린더 조회 조건 |
| 39 | + * @return FindCalendarResponse |
| 40 | + */ |
| 41 | + @Transactional(readOnly = true) |
| 42 | + public CalendarSchedulesResponse findCalendar(AuthMember authMember, long calendarId, |
| 43 | + CalendarFindRequest calendarFindRequest) { |
| 44 | + |
| 45 | + Calendar foundCalendar = null; // FIXME: calendarId로 foundCalendar 엔티티 검증 필요. |
| 46 | + MemberCalendar foundMemberCalendar = null; // FIXME: memberCalendar 엔티티 검증 필요. |
| 47 | + Member foundMember = null; // FIXME: member 엔티티 검증 필요. |
| 48 | + |
| 49 | + DateSearchCondition dateSearchCondition = getDateSearchCondition(calendarFindRequest.today(), |
| 50 | + calendarFindRequest.calendarType()); |
| 51 | + |
| 52 | + List<Schedule> schedules = scheduleRepository.findByCalendarIdAndBetweenStartDateAndEndDate(calendarId, |
| 53 | + dateSearchCondition.startDate(), dateSearchCondition.endDate()); |
| 54 | + |
| 55 | + List<ScheduleFindResponse> responses = schedules.stream() |
| 56 | + .flatMap(schedule -> getScheduleResponsesWithRepetition(schedule, dateSearchCondition.startDate(), |
| 57 | + dateSearchCondition.endDate()).stream()) |
| 58 | + .toList(); |
| 59 | + |
| 60 | + return new CalendarSchedulesResponse(responses.size(), responses); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * RepetitionService를 통해 반복 일정을 가공하여 조회한다. |
| 65 | + * @param schedule : 일정 정보 |
| 66 | + * @param startDate : 조회 시작일 |
| 67 | + * @param endDate : 조회 종료일 |
| 68 | + * @return List<ScheduleFindResponse> : 일정 반복 정보를 통해 리스트 형태로 가공하여 반환된다. |
| 69 | + */ |
| 70 | + private List<ScheduleFindResponse> getScheduleResponsesWithRepetition(Schedule schedule, LocalDate startDate, |
| 71 | + LocalDate endDate) { |
| 72 | + return RepetitionService.getScheduleResponses(schedule, startDate, endDate); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * CalendarType에 따라 DateSearchCondition을 반환한다. |
| 77 | + * @param today : 조회 기준일 |
| 78 | + * @param calendarType : 조회할 캘린더 타입 |
| 79 | + * @return DateSearchCondition : DateSearchCondition 객체를 반환한다. |
| 80 | + */ |
| 81 | + private DateSearchCondition getDateSearchCondition(LocalDate today, CalendarType calendarType) { |
| 82 | + return calendarStrategyMap.get(calendarType).getDateSearchCondition(today); |
| 83 | + } |
| 84 | +} |
0 commit comments