-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
달력 조회 API를 작성 #50
base: dev
Are you sure you want to change the base?
달력 조회 API를 작성 #50
Changes from all commits
52eec9f
8689bf2
1c7b4c9
271708e
00d8042
42ec4e4
85a1f70
3c99aa5
6d2f219
979305d
014915d
0720586
01c0a28
ed2643d
3378cff
f19b02a
2c62b29
0ddc92b
e023533
7527b60
697c02e
2721398
f609235
9bb8664
8ff0080
5c43dcd
93200ad
5bd8b96
dcfe121
98a55a3
23f3794
9b1b408
d25eb0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.wypl.wyplcore.calendar.data; | ||
|
||
import java.time.LocalDate; | ||
|
||
public record DateSearchCondition(LocalDate startDate, LocalDate endDate) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.wypl.wyplcore.calendar.data.request; | ||
|
||
import java.time.LocalDate; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.wypl.wyplcore.schedule.data.CalendarType; | ||
|
||
public record CalendarFindRequest( | ||
|
||
@JsonProperty("calendar_type") | ||
CalendarType calendarType, | ||
|
||
@JsonProperty("start_date") | ||
LocalDate today | ||
) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.wypl.wyplcore.calendar.data.response; | ||
|
||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.wypl.wyplcore.schedule.data.response.ScheduleFindResponse; | ||
|
||
public record CalendarSchedulesResponse( | ||
|
||
@JsonProperty("schedule_count") | ||
int scheduleCount, | ||
|
||
@JsonProperty("schedules") | ||
List<ScheduleFindResponse> schedules | ||
) { | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,84 @@ | ||||||||||||||||
package com.wypl.wyplcore.calendar.service; | ||||||||||||||||
|
||||||||||||||||
import java.time.LocalDate; | ||||||||||||||||
import java.util.List; | ||||||||||||||||
import java.util.Map; | ||||||||||||||||
|
||||||||||||||||
import org.springframework.stereotype.Service; | ||||||||||||||||
import org.springframework.transaction.annotation.Transactional; | ||||||||||||||||
|
||||||||||||||||
import com.wypl.googleoauthclient.domain.AuthMember; | ||||||||||||||||
import com.wypl.jpacalendardomain.calendar.domain.Calendar; | ||||||||||||||||
import com.wypl.jpacalendardomain.calendar.domain.MemberCalendar; | ||||||||||||||||
import com.wypl.jpacalendardomain.calendar.domain.Schedule; | ||||||||||||||||
import com.wypl.jpacalendardomain.calendar.repository.ScheduleRepository; | ||||||||||||||||
import com.wypl.jpamemberdomain.member.domain.Member; | ||||||||||||||||
import com.wypl.wyplcore.calendar.data.DateSearchCondition; | ||||||||||||||||
import com.wypl.wyplcore.calendar.data.request.CalendarFindRequest; | ||||||||||||||||
import com.wypl.wyplcore.calendar.data.response.CalendarSchedulesResponse; | ||||||||||||||||
import com.wypl.wyplcore.calendar.service.strategy.CalendarStrategy; | ||||||||||||||||
import com.wypl.wyplcore.schedule.data.CalendarType; | ||||||||||||||||
import com.wypl.wyplcore.schedule.data.response.ScheduleFindResponse; | ||||||||||||||||
import com.wypl.wyplcore.schedule.service.repetition.RepetitionService; | ||||||||||||||||
|
||||||||||||||||
import lombok.RequiredArgsConstructor; | ||||||||||||||||
|
||||||||||||||||
@Service | ||||||||||||||||
@RequiredArgsConstructor | ||||||||||||||||
public class CalendarService { | ||||||||||||||||
|
||||||||||||||||
private final ScheduleRepository scheduleRepository; | ||||||||||||||||
|
||||||||||||||||
private final Map<CalendarType, CalendarStrategy> calendarStrategyMap; | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* 캘린더의 일정을 조회한다. | ||||||||||||||||
* @param authMember : 인증된 사용자 정보 | ||||||||||||||||
* @param calendarId : 조회할 캘린더 ID | ||||||||||||||||
* @param calendarFindRequest : 캘린더 조회 조건 | ||||||||||||||||
* @return FindCalendarResponse | ||||||||||||||||
*/ | ||||||||||||||||
@Transactional(readOnly = true) | ||||||||||||||||
public CalendarSchedulesResponse findCalendar(AuthMember authMember, long calendarId, | ||||||||||||||||
CalendarFindRequest calendarFindRequest) { | ||||||||||||||||
Comment on lines
+42
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
이런 형식으로 매개변수를 작성해보는건 어떨까요?! |
||||||||||||||||
|
||||||||||||||||
Calendar foundCalendar = null; // FIXME: calendarId로 foundCalendar 엔티티 검증 필요. | ||||||||||||||||
MemberCalendar foundMemberCalendar = null; // FIXME: memberCalendar 엔티티 검증 필요. | ||||||||||||||||
Member foundMember = null; // FIXME: member 엔티티 검증 필요. | ||||||||||||||||
|
||||||||||||||||
DateSearchCondition dateSearchCondition = getDateSearchCondition(calendarFindRequest.today(), | ||||||||||||||||
calendarFindRequest.calendarType()); | ||||||||||||||||
|
||||||||||||||||
List<Schedule> schedules = scheduleRepository.findByCalendarIdAndBetweenStartDateAndEndDate(calendarId, | ||||||||||||||||
dateSearchCondition.startDate(), dateSearchCondition.endDate()); | ||||||||||||||||
|
||||||||||||||||
List<ScheduleFindResponse> responses = schedules.stream() | ||||||||||||||||
.flatMap(schedule -> getScheduleResponsesWithRepetition(schedule, dateSearchCondition.startDate(), | ||||||||||||||||
dateSearchCondition.endDate()).stream()) | ||||||||||||||||
.toList(); | ||||||||||||||||
|
||||||||||||||||
return new CalendarSchedulesResponse(responses.size(), responses); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* RepetitionService를 통해 반복 일정을 가공하여 조회한다. | ||||||||||||||||
* @param schedule : 일정 정보 | ||||||||||||||||
* @param startDate : 조회 시작일 | ||||||||||||||||
* @param endDate : 조회 종료일 | ||||||||||||||||
* @return List<ScheduleFindResponse> : 일정 반복 정보를 통해 리스트 형태로 가공하여 반환된다. | ||||||||||||||||
*/ | ||||||||||||||||
private List<ScheduleFindResponse> getScheduleResponsesWithRepetition(Schedule schedule, LocalDate startDate, | ||||||||||||||||
LocalDate endDate) { | ||||||||||||||||
return RepetitionService.getScheduleResponses(schedule, startDate, endDate); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
/** | ||||||||||||||||
* CalendarType에 따라 DateSearchCondition을 반환한다. | ||||||||||||||||
* @param today : 조회 기준일 | ||||||||||||||||
* @param calendarType : 조회할 캘린더 타입 | ||||||||||||||||
* @return DateSearchCondition : DateSearchCondition 객체를 반환한다. | ||||||||||||||||
*/ | ||||||||||||||||
private DateSearchCondition getDateSearchCondition(LocalDate today, CalendarType calendarType) { | ||||||||||||||||
return calendarStrategyMap.get(calendarType).getDateSearchCondition(today); | ||||||||||||||||
} | ||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.wypl.wyplcore.calendar.service; | ||
|
||
import java.time.LocalDate; | ||
|
||
public class CalendarServiceUtil { | ||
|
||
public static LocalDate getMaxDate(LocalDate date1, LocalDate date2) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 음 혹시 해당 반환값이 |
||
return date1.isBefore(date2) ? date2 : date1; | ||
} | ||
|
||
public static LocalDate getMinDate(LocalDate date1, LocalDate date2) { | ||
return date1.isBefore(date2) ? date1 : date2; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.wypl.wyplcore.calendar.service.strategy; | ||
|
||
import java.time.LocalDate; | ||
|
||
import com.wypl.wyplcore.calendar.data.DateSearchCondition; | ||
import com.wypl.wyplcore.schedule.data.CalendarType; | ||
|
||
public interface CalendarStrategy { | ||
|
||
CalendarType getCalendarType(); | ||
|
||
DateSearchCondition getDateSearchCondition(LocalDate today); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.wypl.wyplcore.calendar.service.strategy; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import com.wypl.wyplcore.schedule.data.CalendarType; | ||
|
||
@Configuration | ||
public class CalendarStrategyConfig { | ||
|
||
private final Map<CalendarType, CalendarStrategy> calendarStrategyMap = new HashMap<>(); | ||
|
||
@Autowired | ||
public CalendarStrategyConfig(List<CalendarStrategy> calendarStrategies) { | ||
calendarStrategies.forEach(calendarStrategy -> { | ||
calendarStrategyMap.put(calendarStrategy.getCalendarType(), calendarStrategy); | ||
}); | ||
} | ||
|
||
@Bean | ||
public Map<CalendarType, CalendarStrategy> calendarStrategyMap() { | ||
return calendarStrategyMap; | ||
} | ||
Comment on lines
+25
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.wypl.wyplcore.calendar.service.strategy; | ||
|
||
import java.time.LocalDate; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import com.wypl.wyplcore.calendar.data.DateSearchCondition; | ||
import com.wypl.wyplcore.schedule.data.CalendarType; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class DayCalendarStrategy implements CalendarStrategy { | ||
|
||
@Override | ||
public CalendarType getCalendarType() { | ||
return CalendarType.DAY; | ||
} | ||
|
||
@Override | ||
public DateSearchCondition getDateSearchCondition(LocalDate today) { | ||
return new DateSearchCondition(today, today); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.wypl.wyplcore.calendar.service.strategy; | ||
|
||
import java.time.LocalDate; | ||
import java.time.temporal.TemporalAdjusters; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import com.wypl.wyplcore.calendar.data.DateSearchCondition; | ||
import com.wypl.wyplcore.schedule.data.CalendarType; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class MonthCalendarStrategy implements CalendarStrategy { | ||
|
||
@Override | ||
public CalendarType getCalendarType() { | ||
return CalendarType.MONTH; | ||
} | ||
|
||
@Override | ||
public DateSearchCondition getDateSearchCondition(LocalDate today) { | ||
LocalDate searchStartDate = today.withDayOfMonth(1); | ||
LocalDate searchEndDate = today.with(TemporalAdjusters.lastDayOfMonth()); | ||
return new DateSearchCondition(searchStartDate, searchEndDate); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.wypl.wyplcore.calendar.service.strategy; | ||
|
||
import java.time.DayOfWeek; | ||
import java.time.LocalDate; | ||
import java.time.temporal.TemporalAdjusters; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import com.wypl.wyplcore.calendar.data.DateSearchCondition; | ||
import com.wypl.wyplcore.schedule.data.CalendarType; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class WeekCalendarStrategy implements CalendarStrategy { | ||
|
||
@Override | ||
public CalendarType getCalendarType() { | ||
return CalendarType.WEEK; | ||
} | ||
|
||
@Override | ||
public DateSearchCondition getDateSearchCondition(LocalDate today) { | ||
LocalDate searchStartDate = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); | ||
LocalDate searchEndDate = today.plusDays(6); | ||
return new DateSearchCondition(searchStartDate, searchEndDate); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -19,11 +19,12 @@ | |||||||||||||
@RequestMapping("/schedule/v2/schedules") | ||||||||||||||
public class ScheduleController { | ||||||||||||||
|
||||||||||||||
private final ScheduleService scheduleService; | ||||||||||||||
private final ScheduleService scheduleService; | ||||||||||||||
|
||||||||||||||
@PostMapping | ||||||||||||||
public WyplResponseEntity<ScheduleInfoCreateResponse> addSchedule(@Authenticated AuthMember authMember, @RequestBody ScheduleCreateRequest scheduleCreateRequest) { | ||||||||||||||
ScheduleInfoCreateResponse response = scheduleService.createSchedule(authMember, scheduleCreateRequest); | ||||||||||||||
return WyplResponseEntity.created(response, "일정이 생성됐습니다."); | ||||||||||||||
} | ||||||||||||||
@PostMapping | ||||||||||||||
public WyplResponseEntity<ScheduleInfoCreateResponse> addSchedule(@Authenticated AuthMember authMember, | ||||||||||||||
@RequestBody ScheduleCreateRequest scheduleCreateRequest) { | ||||||||||||||
Comment on lines
+25
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 내용도 위에는 있지만!
Suggested change
와 같이 수정하는게 더 보기 편하다고 생각하는데 어떠신가요? |
||||||||||||||
ScheduleInfoCreateResponse response = scheduleService.createSchedule(authMember, scheduleCreateRequest); | ||||||||||||||
return WyplResponseEntity.created(response, "일정이 생성됐습니다."); | ||||||||||||||
} | ||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.wypl.wyplcore.schedule.data; | ||
|
||
public enum CalendarType { | ||
DAY, WEEK, MONTH | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
혹시 개인적인 의견이지만
calendarStrategies
로 수정하는게 어떨까요?! 변수명에 자료구조 형식이 드러나는 것이 좋아보이지 않습니다!