Skip to content

Commit

Permalink
refactor: 캘린더 기간을 나타내는 객체 분리
Browse files Browse the repository at this point in the history
- CalendarPeriod 레코드 생성
- 기간계산 로직 분리
  • Loading branch information
pilyang committed Jul 22, 2023
1 parent 3078ca0 commit 6553642
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,19 @@
import team.teamby.teambyteam.member.domain.vo.Email;
import team.teamby.teambyteam.member.exception.MemberException;
import team.teamby.teambyteam.schedule.application.dto.*;
import team.teamby.teambyteam.schedule.domain.Schedule;
import team.teamby.teambyteam.schedule.domain.ScheduleRepository;
import team.teamby.teambyteam.schedule.domain.Span;
import team.teamby.teambyteam.schedule.domain.Title;
import team.teamby.teambyteam.schedule.domain.*;
import team.teamby.teambyteam.schedule.exception.ScheduleException;
import team.teamby.teambyteam.teamplace.domain.TeamPlace;
import team.teamby.teambyteam.teamplace.domain.TeamPlaceRepository;
import team.teamby.teambyteam.teamplace.exception.TeamPlaceException;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

@Service
@Transactional
@RequiredArgsConstructor
public class ScheduleService {

private static final int FIRST_DAY_OF_MONTH = 1;
private static final LocalTime START_TIME_OF_DAY = LocalTime.of(0, 0, 0);
public static final int NEXT_MONTH_OFFSET = 1;
public static final Integer START_DATE_TIME_KEY = 1;
public static final Integer END_DATE_TIME_KEY = 2;

private final MemberRepository memberRepository;
private final ScheduleRepository scheduleRepository;
private final TeamPlaceRepository teamPlaceRepository;
Expand Down Expand Up @@ -86,23 +73,13 @@ public SchedulesResponse findScheduleInPeriod(final Long teamPlaceId, final int
// TODO: 상의해보기 - 팀플레이스 소속 멤버 검증시 팀플레이스 아이디가 검증이 될 건데 해당 붑ㄴ에 대한 재 검증이 필요한가?
checkTeamPlaceExist(teamPlaceId);

final Map<Integer, LocalDateTime> period = calculatePeriod(targetYear, targetMonth);
final CalendarPeriod period = CalendarPeriod.of(targetYear, targetMonth);
final List<Schedule> schedules = scheduleRepository
.findAllByTeamPlaceIdAndPeriod(teamPlaceId, period.get(START_DATE_TIME_KEY), period.get(END_DATE_TIME_KEY));
.findAllByTeamPlaceIdAndPeriod(teamPlaceId, period.startDateTime(), period.endDatetime());

return SchedulesResponse.of(schedules);
}

public Map<Integer, LocalDateTime> calculatePeriod(final int year, final int month) {
final LocalDate startDate = LocalDate.of(year, month, FIRST_DAY_OF_MONTH);
final LocalDate endDate = startDate.plusMonths(NEXT_MONTH_OFFSET).withDayOfMonth(FIRST_DAY_OF_MONTH);

return Map.ofEntries(
Map.entry(START_DATE_TIME_KEY, LocalDateTime.of(startDate, START_TIME_OF_DAY)),
Map.entry(END_DATE_TIME_KEY, LocalDateTime.of(endDate, START_TIME_OF_DAY))
);
}

@Transactional(readOnly = true)
public SchedulesWithTeamPlaceIdResponse findScheduleInPeriod(
final MemberEmailDto memberEmailDto,
Expand All @@ -117,9 +94,9 @@ public SchedulesWithTeamPlaceIdResponse findScheduleInPeriod(
.map(TeamPlace::getId)
.toList();

final Map<Integer, LocalDateTime> period = calculatePeriod(targetYear, targetMonth);
final CalendarPeriod period = CalendarPeriod.of(targetYear, targetMonth);
final List<Schedule> schedules = scheduleRepository
.findAllByTeamPlaceIdAndPeriod(participatedTeamPlaceIds, period.get(START_DATE_TIME_KEY), period.get(END_DATE_TIME_KEY));
.findAllByTeamPlaceIdAndPeriod(participatedTeamPlaceIds, period.startDateTime(), period.endDatetime());

return SchedulesWithTeamPlaceIdResponse.of(schedules);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package team.teamby.teambyteam.schedule.domain;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public record CalendarPeriod(
LocalDateTime startDateTime,
LocalDateTime endDatetime
) {
private static final int FIRST_DAY_OF_MONTH = 1;
private static final LocalTime START_TIME_OF_DAY = LocalTime.of(0, 0, 0);
public static final int NEXT_MONTH_OFFSET = 1;

public static CalendarPeriod of(final int year, final int month) {
final LocalDate startDate = LocalDate.of(year, month, FIRST_DAY_OF_MONTH);
final LocalDate endDate = startDate.plusMonths(NEXT_MONTH_OFFSET).withDayOfMonth(FIRST_DAY_OF_MONTH);

return new CalendarPeriod(LocalDateTime.of(startDate, START_TIME_OF_DAY), LocalDateTime.of(endDate, START_TIME_OF_DAY));
}

}

0 comments on commit 6553642

Please sign in to comment.