Skip to content

Commit

Permalink
feat: CalendarService 추가
Browse files Browse the repository at this point in the history
- 전략 + 플라이웨이트 패턴 적용 테스트 포함

Resolves: #38
  • Loading branch information
jiwon83 committed Oct 20, 2024
1 parent 271708e commit 00d8042
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<CalendarType, CalendarStrategy> 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<ScheduleFindResponse> foundScheduleFindResponses = calendarStrategyMap.get(calendarType).getAllSchedule(foundCalendar.getId(), startDate);


CalendarFindResponse calendarFindResponse = new CalendarFindResponse(foundCalendar.getId(), foundMemberCalendar.getColor(), foundCalendar.getName());
return new FindCalendarResponse(calendarFindResponse, foundScheduleFindResponses);
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}

0 comments on commit 00d8042

Please sign in to comment.