Skip to content

Commit

Permalink
Feat: 대시보드 스케줄 중복 시간 체커 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
onetuks committed Dec 4, 2023
1 parent b2fd1a8 commit 4ac2143
Showing 1 changed file with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.guzzing.studayserver.domain.dashboard.service.overlap;

import java.util.List;
import org.guzzing.studayserver.domain.dashboard.model.DashboardSchedule;
import org.guzzing.studayserver.global.time.TimeConverter;
import org.springframework.stereotype.Component;

@Component
public class DashboardScheduleOverlapChecker {

public void checkScheduleOverlap(
List<DashboardSchedule> dashboardSchedules,
List<DashboardSchedule> childDashboardSchedules
) {
final long overlapCount = childDashboardSchedules.stream()
.filter(childDashboardSchedule -> dashboardSchedules.stream()
.anyMatch(dashboardSchedule -> isScheduleOverlap(dashboardSchedule, childDashboardSchedule)))
.count();

if (overlapCount > 0) {
throw new IllegalStateException("시간이 겹치는 대시보드 스케줄이 있습니다.");
}
}

private boolean isScheduleOverlap(DashboardSchedule dashboardSchedule, DashboardSchedule childDashboardSchedule) {
final boolean isSameDayOfWeek = dashboardSchedule.getDayOfWeek() == childDashboardSchedule.getDayOfWeek();
final boolean isAfterSchedule = TimeConverter.getLocalTime(dashboardSchedule.getStartTime()).isAfter(
TimeConverter.getLocalTime(childDashboardSchedule.getEndTime()));
final boolean isBeforeSchedules = TimeConverter.getLocalTime(dashboardSchedule.getEndTime()).isBefore(
TimeConverter.getLocalTime(childDashboardSchedule.getStartTime()));
final boolean isSameStartTime = TimeConverter.getLocalTime(dashboardSchedule.getStartTime()).equals(
TimeConverter.getLocalTime(childDashboardSchedule.getStartTime()));
final boolean isSameEndTime = TimeConverter.getLocalTime(dashboardSchedule.getEndTime()).equals(
TimeConverter.getLocalTime(childDashboardSchedule.getEndTime()));

return isSameDayOfWeek && (isAfterSchedule || isBeforeSchedules || isSameStartTime || isSameEndTime);
}

}

0 comments on commit 4ac2143

Please sign in to comment.