diff --git a/application/wypl-core/src/main/java/com/wypl/wyplcore/calendar/service/CalendarService.java b/application/wypl-core/src/main/java/com/wypl/wyplcore/calendar/service/CalendarService.java new file mode 100644 index 0000000..90f4de5 --- /dev/null +++ b/application/wypl-core/src/main/java/com/wypl/wyplcore/calendar/service/CalendarService.java @@ -0,0 +1,48 @@ +package com.wypl.wyplcore.calendar.service; + +import com.wypl.jpacalendardomain.calendar.domain.Calendar; +import com.wypl.jpacalendardomain.calendar.domain.MemberCalendar; +import com.wypl.jpacalendardomain.calendar.mapper.ScheduleMapper; +import com.wypl.jpacalendardomain.calendar.repository.ScheduleInfoRepository; +import com.wypl.jpacalendardomain.calendar.repository.ScheduleRepository; +import com.wypl.jpamemberdomain.member.Member; +import com.wypl.wyplcore.auth.domain.AuthMember; +import com.wypl.wyplcore.calendar.data.response.CalendarFindResponse; +import com.wypl.wyplcore.calendar.data.response.FindCalendarResponse; +import com.wypl.wyplcore.calendar.service.strategy.CalendarStrategy; +import com.wypl.wyplcore.schedule.data.CalendarType; +import com.wypl.wyplcore.calendar.data.request.CalendarFindRequest; +import com.wypl.wyplcore.schedule.data.response.ScheduleFindResponse; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.time.LocalDate; +import java.util.List; +import java.util.Map; + +@Service +@RequiredArgsConstructor +public class CalendarService { + + private final ScheduleRepository scheduleRepository; + private final ScheduleInfoRepository scheduleInfoRepository; + private final Map calendarStrategyMap; + + @Transactional + public FindCalendarResponse findCalendar(AuthMember authMember, long calendarId, CalendarFindRequest calendarFindRequest) { + + Calendar foundCalendar = null; // FIXME: calendarId로 foundCalendar 엔티티 검증 필요. + MemberCalendar foundMemberCalendar = null; // Fixme: memberCalendar 엔티티 검증 필요. + Member foundMember = null; // FIXME: member 엔티티 검증 필요. + + CalendarType calendarType = calendarFindRequest.calendarType(); + LocalDate startDate = calendarFindRequest.startDate(); + + List foundScheduleFindResponses = calendarStrategyMap.get(calendarType).getAllSchedule(foundCalendar.getId(), startDate); + + + CalendarFindResponse calendarFindResponse = new CalendarFindResponse(foundCalendar.getId(), foundMemberCalendar.getColor(), foundCalendar.getName()); + return new FindCalendarResponse(calendarFindResponse, foundScheduleFindResponses); + } +} diff --git a/application/wypl-core/src/test/java/com/wypl/wyplcore/calendar/service/strategy/CalendarStrategyConfigTest.java b/application/wypl-core/src/test/java/com/wypl/wyplcore/calendar/service/strategy/CalendarStrategyConfigTest.java new file mode 100644 index 0000000..4f34e28 --- /dev/null +++ b/application/wypl-core/src/test/java/com/wypl/wyplcore/calendar/service/strategy/CalendarStrategyConfigTest.java @@ -0,0 +1,45 @@ +package com.wypl.wyplcore.calendar.service.strategy; + +import com.wypl.wyplcore.schedule.data.CalendarType; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +class CalendarStrategyConfigTest { + + @Autowired + private ApplicationContext applicationContext; + + @Test + void testRegisterBean(){ + assertNotNull(applicationContext.getBean(CalendarStrategyConfig.class)); + assertTrue(applicationContext.containsBean("calendarStrategyMap")); + + Map instance = (Map) applicationContext.getBean("calendarStrategyMap"); + for (Map.Entry entry : instance.entrySet()) { + assertThat(entry.getKey()).isInstanceOf(CalendarType.class); + assertThat(entry.getValue()).isInstanceOf(CalendarStrategy.class); + System.out.println(entry.getKey() + " : " + entry.getValue()); + } + } + + @Test + void testStrategyFlyweight() { + + // Flyweight(CalendarStrategy)의 공유 역할을 하는 calendarStrategyMap이 싱글톤으로 관리되는 지 확인 + Map instance = (Map) applicationContext.getBean("calendarStrategyMap"); + Map instance2 = (Map) applicationContext.getBean("calendarStrategyMap"); + assertEquals(instance, instance2); + + // 여러 번 호출 시 같은 전략 인스턴스가 호출되는 지 확인 + assertEquals(instance.get("DAY"), instance2.get("DAY")); + } +} +