Skip to content

Commit

Permalink
Refactor: 대시보드 활성화 시 스케줄 중복 체크
Browse files Browse the repository at this point in the history
  • Loading branch information
onetuks committed Dec 4, 2023
1 parent 4ac2143 commit f596739
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package org.guzzing.studayserver.domain.dashboard.service;

import jakarta.persistence.EntityNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.guzzing.studayserver.domain.dashboard.model.Dashboard;
import org.guzzing.studayserver.domain.dashboard.model.DashboardSchedule;
import org.guzzing.studayserver.domain.dashboard.model.vo.FeeInfo;
import org.guzzing.studayserver.domain.dashboard.repository.DashboardRepository;
import org.guzzing.studayserver.domain.dashboard.repository.DashboardScheduleJpaRepository;
import org.guzzing.studayserver.domain.dashboard.service.converter.DashboardServiceConverter;
import org.guzzing.studayserver.domain.dashboard.service.dto.request.DashboardPostParam;
import org.guzzing.studayserver.domain.dashboard.service.dto.request.DashboardPutParam;
import org.guzzing.studayserver.domain.dashboard.service.dto.response.DashboardResult;
import org.guzzing.studayserver.domain.dashboard.service.overlap.DashboardScheduleOverlapChecker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -21,15 +27,17 @@ public class DashboardService {
private final DashboardServiceConverter serviceConverter;
private final DashboardRepository dashboardRepository;
private final DashboardScheduleJpaRepository dashboardScheduleJpaRepository;
private final DashboardScheduleOverlapChecker dashboardScheduleOverlapChecker;

public DashboardService(
final DashboardServiceConverter serviceConverter,
final DashboardRepository dashboardRepository,
final DashboardScheduleJpaRepository dashboardScheduleJpaRepository
) {
final DashboardScheduleJpaRepository dashboardScheduleJpaRepository,
final DashboardScheduleOverlapChecker dashboardScheduleOverlapChecker) {
this.serviceConverter = serviceConverter;
this.dashboardRepository = dashboardRepository;
this.dashboardScheduleJpaRepository = dashboardScheduleJpaRepository;
this.dashboardScheduleOverlapChecker = dashboardScheduleOverlapChecker;
}

@Transactional
Expand Down Expand Up @@ -66,8 +74,20 @@ public DashboardResult editDashboard(final DashboardPutParam param) {

@Transactional
public DashboardResult toggleDashboardActiveness(final long dashboardId) {
final Dashboard dashboard = dashboardRepository.findDashboardById(dashboardId)
.toggleActive();
final Dashboard dashboard = dashboardRepository.findDashboardById(dashboardId);

final List<DashboardSchedule> childDashboardSchedules = dashboardRepository.findActiveOnlyByChildId(
dashboard.getChildId())
.stream()
.filter(childDashboard -> !Objects.equals(dashboard.getId(), childDashboard.getId()))
.map(Dashboard::getDashboardSchedules)
.flatMap(List::stream)
.collect(Collectors.toCollection(() -> Collections.synchronizedList(new ArrayList<>())));

dashboardScheduleOverlapChecker.checkScheduleOverlap(dashboard.getDashboardSchedules(),
childDashboardSchedules);

dashboard.toggleActive();

return serviceConverter.from(dashboard);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ void toggleActive_DashboardActiveBoolean_Revert() {
assertThat(result.isActive()).isFalse();
}

@Test
@DisplayName("활성화되어 있는 기존 대시보드와 겹치는 스케쥴이 있을때 대시보드를 활성화하면 예외가 발생한다.")
void toggleActive_DuplicatedDashboardSchedule_Exception() {
// Given
dashboardFixture.createActiveEntity();

final Dashboard duplicatedScheduleDashboard = dashboardFixture.createNotActiveEntity();

// When & Then
assertThatThrownBy(() -> dashboardService.toggleDashboardActiveness(duplicatedScheduleDashboard.getId()))
.isInstanceOf(IllegalStateException.class)
.hasMessage("시간이 겹치는 대시보드 스케줄이 있습니다.");
}

@Test
@DisplayName("아이디로 대시보드를 조회한다.")
void findDashboard_ByDashboardId() {
Expand Down

0 comments on commit f596739

Please sign in to comment.