diff --git a/src/main/java/com/dnd/jjakkak/domain/dateofschedule/service/DateOfScheduleService.java b/src/main/java/com/dnd/jjakkak/domain/dateofschedule/service/DateOfScheduleService.java index f60968b..8398b3d 100644 --- a/src/main/java/com/dnd/jjakkak/domain/dateofschedule/service/DateOfScheduleService.java +++ b/src/main/java/com/dnd/jjakkak/domain/dateofschedule/service/DateOfScheduleService.java @@ -37,52 +37,40 @@ public class DateOfScheduleService { * @param requestDto 일정 날짜 생성 요청 DTO */ @Transactional - public void createDateOfSchedule(Long scheduleId, DateOfScheduleCreateRequestDto requestDto) { + public void createDateOfSchedule(Long scheduleId, List requestDto) { Schedule schedule = scheduleRepository.findById(scheduleId) .orElseThrow(ScheduleNotFoundException::new); validateDateOfSchedule(schedule, requestDto); - LocalDateTime startTime = requestDto.getStartTime(); - LocalDateTime endTime = requestDto.getEndTime(); + requestDto.forEach(dto -> { - // TODO : rank 값을 추후에 변경해야 함. - Integer rank = 1; + LocalDateTime startTime = dto.getStartTime(); + LocalDateTime endTime = dto.getEndTime(); - Duration interval = Duration.ofHours(1); - while (startTime.isBefore(endTime)) { - LocalDateTime nextEndTime = startTime.plus(interval); + Duration interval = Duration.ofHours(1); + while (startTime.isBefore(endTime)) { + LocalDateTime nextEndTime = startTime.plus(interval); - if (nextEndTime.isAfter(endTime)) { - nextEndTime = endTime; - } + if (nextEndTime.isAfter(endTime)) { + nextEndTime = endTime; + } - DateOfSchedule dateOfSchedule = DateOfSchedule.builder() - .schedule(schedule) - .dateOfScheduleStart(startTime) - .dateOfScheduleEnd(nextEndTime) - .dateOfScheduleRank(rank) - .build(); + DateOfSchedule dateOfSchedule = DateOfSchedule.builder() + .schedule(schedule) + .dateOfScheduleStart(startTime) + .dateOfScheduleEnd(nextEndTime) + .dateOfScheduleRank(1) + .build(); - dateOfScheduleRepository.save(dateOfSchedule); + dateOfScheduleRepository.save(dateOfSchedule); - startTime = nextEndTime; - } + startTime = nextEndTime; + } + }); } - private void validateDateOfSchedule(Schedule schedule, DateOfScheduleCreateRequestDto requestDto) { - Meeting meeting = schedule.getMeeting(); - LocalDate meetingStartDate = meeting.getMeetingStartDate(); - LocalDate meetingEndDate = meeting.getMeetingEndDate(); - - LocalDateTime startTime = requestDto.getStartTime(); - LocalDateTime endTime = requestDto.getEndTime(); - - if (startTime.toLocalDate().isBefore(meetingStartDate) || endTime.toLocalDate().isAfter(meetingEndDate)) { - throw new ScheduleDateOutOfMeetingDateException(); - } - } /** * 일정 날짜 리스트를 업데이트하는 메서드입니다. (모든 일정 날짜 삭제 후 새로 생성) @@ -98,17 +86,49 @@ public void updateDateList(Long scheduleId, List Schedule schedule = scheduleRepository.findById(scheduleId) .orElseThrow(ScheduleNotFoundException::new); - // dateOfSchedule 생성 + validateDateOfSchedule(schedule, requestDto); + // TODO: rank 값 추후에 변경해줘야 함! requestDto.forEach(dto -> { - DateOfSchedule dateOfSchedule = DateOfSchedule.builder() - .schedule(schedule) - .dateOfScheduleStart(dto.getStartTime()) - .dateOfScheduleEnd(dto.getEndTime()) - .dateOfScheduleRank(1) - .build(); - - dateOfScheduleRepository.save(dateOfSchedule); + + LocalDateTime startTime = dto.getStartTime(); + LocalDateTime endTime = dto.getEndTime(); + + Duration interval = Duration.ofHours(1); + while (startTime.isBefore(endTime)) { + LocalDateTime nextEndTime = startTime.plus(interval); + + if (nextEndTime.isAfter(endTime)) { + nextEndTime = endTime; + } + + DateOfSchedule dateOfSchedule = DateOfSchedule.builder() + .schedule(schedule) + .dateOfScheduleStart(startTime) + .dateOfScheduleEnd(nextEndTime) + .dateOfScheduleRank(1) + .build(); + + dateOfScheduleRepository.save(dateOfSchedule); + + startTime = nextEndTime; + } }); } + + private void validateDateOfSchedule(Schedule schedule, List requestDtos) { + Meeting meeting = schedule.getMeeting(); + LocalDate meetingStartDate = meeting.getMeetingStartDate(); + LocalDate meetingEndDate = meeting.getMeetingEndDate(); + + for (DateOfScheduleCreateRequestDto requestDto : requestDtos) { + + LocalDateTime startTime = requestDto.getStartTime(); + LocalDateTime endTime = requestDto.getEndTime(); + + if (startTime.toLocalDate().isBefore(meetingStartDate) || endTime.toLocalDate().isAfter(meetingEndDate)) { + throw new ScheduleDateOutOfMeetingDateException(); + } + } + } } diff --git a/src/main/java/com/dnd/jjakkak/domain/schedule/service/ScheduleService.java b/src/main/java/com/dnd/jjakkak/domain/schedule/service/ScheduleService.java index be508b8..11d7cf9 100644 --- a/src/main/java/com/dnd/jjakkak/domain/schedule/service/ScheduleService.java +++ b/src/main/java/com/dnd/jjakkak/domain/schedule/service/ScheduleService.java @@ -1,6 +1,5 @@ package com.dnd.jjakkak.domain.schedule.service; -import com.dnd.jjakkak.domain.dateofschedule.dto.request.DateOfScheduleCreateRequestDto; import com.dnd.jjakkak.domain.dateofschedule.service.DateOfScheduleService; import com.dnd.jjakkak.domain.meeting.entity.Meeting; import com.dnd.jjakkak.domain.meeting.exception.MeetingAlreadyEndedException; @@ -272,12 +271,7 @@ private void validateAndAssignSchedule(ScheduleAssignRequestDto requestDto, Sche // 닉네임 변경 schedule.updateScheduleNickname(requestDto.getNickname() == null ? schedule.getScheduleNickname() : requestDto.getNickname()); - // 일정 날짜 저장 - for (DateOfScheduleCreateRequestDto dateOfScheduleCreateRequestDto : requestDto.getDateOfScheduleList()) { - dateOfScheduleService.createDateOfSchedule(schedule.getScheduleId(), dateOfScheduleCreateRequestDto); - } - - // isAssigned -> true + dateOfScheduleService.createDateOfSchedule(schedule.getScheduleId(), requestDto.getDateOfScheduleList()); schedule.scheduleAssign(); } diff --git a/src/test/java/com/dnd/jjakkak/domain/schedule/service/ScheduleServiceTest.java b/src/test/java/com/dnd/jjakkak/domain/schedule/service/ScheduleServiceTest.java index 6c5c800..a0da6b3 100644 --- a/src/test/java/com/dnd/jjakkak/domain/schedule/service/ScheduleServiceTest.java +++ b/src/test/java/com/dnd/jjakkak/domain/schedule/service/ScheduleServiceTest.java @@ -123,7 +123,7 @@ void assign_schedule_to_guest_success() { // then verify(scheduleRepository, times(1)).findNotAssignedScheduleByMeetingUuid(MEETING_UUID); verify(meetingRepository, times(1)).checkMeetingFull(any()); - verify(dateOfScheduleService, times(2)).createDateOfSchedule(anyLong(), any()); + verify(dateOfScheduleService, times(1)).createDateOfSchedule(anyLong(), any()); } @Test @@ -253,7 +253,7 @@ void assign_schedule_to_member_success() { // then verify(scheduleRepository, times(1)).findNotAssignedScheduleByMeetingUuid(MEETING_UUID); verify(meetingRepository, times(1)).checkMeetingFull(any()); - verify(dateOfScheduleService, times(2)).createDateOfSchedule(anyLong(), any()); + verify(dateOfScheduleService, times(1)).createDateOfSchedule(anyLong(), any()); verify(meetingMemberService, times(1)).createMeetingMemberBySchedule(anyLong(), anyLong()); }