Skip to content

Commit

Permalink
Merge pull request #659 from woowacourse-teams/hotfix/be/shedule-update
Browse files Browse the repository at this point in the history
[BE] 빈 스케줄을 등록해도 기존 스케줄이 삭제되지 않는 에러 해결
  • Loading branch information
Yboyu0u authored Oct 12, 2022
2 parents aeeb2a3 + d277eb9 commit fd35940
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.woowacourse.teatime.teatime.repository.ScheduleRepository;
import com.woowacourse.teatime.teatime.repository.jdbc.ScheduleDao;
import com.woowacourse.teatime.util.Date;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -52,20 +53,22 @@ public void update(Long coachId, List<ScheduleUpdateRequest> requests) {
.flatMap(Collection::stream)
.sorted()
.collect(Collectors.toList());
validateDeletable(coachId, localDateTimes);

List<LocalDate> days = requests.stream()
.map(ScheduleUpdateRequest::getDate)
.collect(Collectors.toList());

deleteAllByCoachAndDate(coachId, localDateTimes);
deleteAllByCoachAndDate(coachId, days);
saveAllByCoachAndDate(coachId, localDateTimes);
}

private void deleteAllByCoachAndDate(Long coachId, List<LocalDateTime> localDateTimes) {
validateDeletable(coachId, localDateTimes);
List<String> localDates = localDateTimes.stream()
.map(LocalDateTime::toLocalDate)
.distinct()
private void deleteAllByCoachAndDate(Long coachId, List<LocalDate> localDates) {
List<String> days = localDates.stream()
.map(String::valueOf)
.collect(Collectors.toList());

scheduleRepository.deleteAllReservableByCoachIdBetween(coachId, localDates);
scheduleRepository.deleteAllReservableByCoachIdBetween(coachId, days);
}

private void validateDeletable(Long coachId, List<LocalDateTime> localDateTimes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,30 @@ void update_if_reservation_exist() {

assertThat(totalSchedules).hasSize(4);
}

@DisplayName("코치의 스케쥴을 수정할 때, 빈 스케쥴이 들어오면 모두 삭제한다.")
@Test
void update_allDeleteIfEmptyDates() {
// given
Coach coach = coachRepository.save(COACH_BROWN);
LocalDateTime time1 = LocalDateTime.of(LAST_DATE_OF_MONTH, LocalTime.of(13, 30));
LocalDateTime time2 = LocalDateTime.of(LAST_DATE_OF_MONTH, LocalTime.of(14, 30));

scheduleRepository.save(new Schedule(coach, time1));
scheduleRepository.save(new Schedule(coach, time2));

// when
ScheduleUpdateRequest scheduleUpdateRequest = new ScheduleUpdateRequest(LAST_DATE_OF_MONTH, List.of());
scheduleService.update(coach.getId(), List.of(scheduleUpdateRequest));

// then
List<ScheduleFindResponse> responses = scheduleService.find(coach.getId(),
new ScheduleFindRequest(LAST_DATE_OF_MONTH.getYear(), LAST_DATE_OF_MONTH.getMonthValue()));
List<ScheduleDto> totalSchedules = responses.stream()
.map(ScheduleFindResponse::getSchedules)
.flatMap(Collection::stream)
.collect(Collectors.toList());

assertThat(totalSchedules).isEmpty();
}
}

0 comments on commit fd35940

Please sign in to comment.