Skip to content

Commit

Permalink
Merge pull request #108 from Cafegory/feature-107
Browse files Browse the repository at this point in the history
[BUILD SUCCESS] [BUILD FAIL] [BUILD FAIL] 카페 영업시간이 다음날 새벽까지인 경우 카공 생성이 안되는 오류 수정
  • Loading branch information
donghyun0304 authored May 29, 2024
2 parents 253d47d + 52cb71e commit e32e94d
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.demo.domain.cafe;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
Expand Down Expand Up @@ -68,9 +69,37 @@ public boolean checkWithBusinessHours(List<BusinessHour> businessHours, LocalDat

public boolean checkBetweenBusinessHours(LocalTime businessStartTime, LocalTime businessEndTime,
LocalTime chosenStartTime, LocalTime chosenEndTime) {
if ((businessStartTime.equals(chosenStartTime) || businessStartTime.isBefore(chosenStartTime))
&& (businessEndTime.equals(chosenEndTime) || businessEndTime.isAfter(chosenEndTime))) {
return true;
// 영업 시작시간이 당일, 영업 종료시간이 당일
if (businessStartTime.isBefore(businessEndTime)) {
return (businessStartTime.equals(chosenStartTime) || businessStartTime.isBefore(chosenStartTime)) && (
businessEndTime.equals(chosenEndTime) || businessEndTime.isAfter(chosenEndTime));
}
// 영업 시작시간이 당일, 영업 종료시간이 다음날
if (businessStartTime.isAfter(businessEndTime)) {
LocalDateTime businessStartDateTime = LocalDateTime.of(LocalDate.now(), businessStartTime);
LocalDateTime businessEndDateTime = LocalDateTime.of(LocalDate.now().plusDays(1), businessEndTime);
// 선택된 시작시간이 당일, 선택된 종료시간이 당일 || 선택된 시작시간이 다음날, 선택된 종료시간이 다음날
if (chosenStartTime.isBefore(chosenEndTime)) {
LocalDate chosenDate = LocalDate.now();

boolean isChosenTimeOvernight = businessStartTime.isAfter(chosenStartTime);
LocalDate date = isChosenTimeOvernight ? chosenDate.plusDays(1) : chosenDate;
LocalDateTime chosenStartDateTime = LocalDateTime.of(date, chosenStartTime);
LocalDateTime chosenEndDateTime = LocalDateTime.of(date, chosenEndTime);

return (businessStartDateTime.isBefore(chosenStartDateTime) || businessStartDateTime.equals(
chosenStartDateTime))
&& (businessEndDateTime.isAfter(chosenEndDateTime) || businessEndDateTime.equals(
chosenEndDateTime));
}
// 선택된 시작시간이 당일, 선택된 종료시간이 다음날
if (chosenStartTime.isAfter(chosenEndTime)) {
LocalDateTime chosenStartDateTime = LocalDateTime.of(LocalDate.now(), chosenStartTime);
LocalDateTime chosenEndDateTime = LocalDateTime.of(LocalDate.now().plusDays(1), chosenEndTime);
return (businessStartDateTime.equals(chosenStartDateTime) || businessStartDateTime.isBefore(
chosenStartDateTime)) && (businessEndDateTime.equals(chosenEndDateTime)
|| businessEndDateTime.isAfter(chosenEndDateTime));
}
}
return false;
}
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/com/example/demo/domain/study/StudyOnce.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -92,24 +93,32 @@ private StudyOnce(Long id, String name, Cafe cafe, LocalDateTime startDateTime,
this.nowMemberCount = 1;
}

private static void validateStartDateTime(LocalDateTime startDateTime) {
private void validateStartDateTime(LocalDateTime startDateTime) {
LocalDateTime now = LocalDateTime.now();
Duration between = Duration.between(now, startDateTime);
if (between.toSeconds() < 3 * 60 * 60) {
throw new CafegoryException(ExceptionType.STUDY_ONCE_WRONG_START_TIME);
}
}

private static void validateStudyOnceTime(LocalDateTime startDateTime, LocalDateTime endDateTime) {
private void validateStudyOnceTime(LocalDateTime startDateTime, LocalDateTime endDateTime) {
Duration between = Duration.between(startDateTime, endDateTime);
if (between.toSeconds() < 60 * 60) {
throw new CafegoryException(STUDY_ONCE_SHORT_DURATION);
LocalTime startLocalTime = startDateTime.toLocalTime();
LocalTime endLocalTime = endDateTime.toLocalTime();
validateStartAndEndTime(startLocalTime, endLocalTime);
}
if (between.toSeconds() > 5 * 60 * 60) {
throw new CafegoryException(STUDY_ONCE_LONG_DURATION);
}
}

private void validateStartAndEndTime(LocalTime startLocalTime, LocalTime endLocalTime) {
if (!(startLocalTime.equals(LocalTime.of(23, 0)) && endLocalTime.equals(LocalTime.MAX))) {
throw new CafegoryException(STUDY_ONCE_SHORT_DURATION);
}
}

private void validateMaxMemberCount(int maxMemberCount) {
if (maxMemberCount > LIMIT_MEMBER_CAPACITY || maxMemberCount < MIN_LIMIT_MEMBER_CAPACITY) {
throw new CafegoryException(STUDY_ONCE_LIMIT_MEMBER_CAPACITY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,75 @@ private static Stream<Arguments> provideChosenTimeAndExpected3() {
);
}

@ParameterizedTest
@MethodSource("provideChosenTimeAndExpected4")
@DisplayName("영업시간이 7시부터 새벽2시까지일 경우")
void when_businessHour_is_7_to_2(LocalTime chosenStartTime, LocalTime chosenEndTime,
boolean expected) {
//given
LocalTime businessStartTime = LocalTime.of(7, 0);
LocalTime businessEndTime = LocalTime.of(2, 0);

//when
boolean isBetween = openChecker.checkBetweenBusinessHours(businessStartTime, businessEndTime,
chosenStartTime, chosenEndTime);
//then
assertThat(isBetween).isEqualTo(expected);
}

private static Stream<Arguments> provideChosenTimeAndExpected4() {
return Stream.of(
Arguments.of(
LocalTime.of(6, 59, 59, 999_999_999),
LocalTime.of(8, 0),
false
),
Arguments.of(
LocalTime.of(7, 0),
LocalTime.of(8, 0),
true
),
Arguments.of(
LocalTime.of(12, 0),
LocalTime.of(13, 0),
true
),
Arguments.of(
LocalTime.of(23, 0),
LocalTime.MAX,
true
),
Arguments.of(
LocalTime.of(23, 0),
LocalTime.of(0, 0),
true
),
Arguments.of(
LocalTime.of(23, 0),
LocalTime.of(2, 0),
true
),
Arguments.of(
LocalTime.of(23, 0),
LocalTime.of(2, 0, 0, 1),
false
),
Arguments.of(
LocalTime.of(0, 0),
LocalTime.of(1, 0),
true
),
Arguments.of(
LocalTime.of(0, 0),
LocalTime.of(2, 0),
true
),
Arguments.of(
LocalTime.of(0, 0),
LocalTime.of(2, 0, 0, 1),
false
)
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,18 @@ private static StudyOnceCreateResponse makeExpectedStudyOnceCreateResult(long ca
.build();
}

@Test
@DisplayName("카공 시작시간이 23시이고 종료시간이 24시(23시 59분 59초 999_999_999초)이면 카공이 생성된다.")
void create_studyOnce_when_studyOnce_startTime_is_23_and_endTime_is_LocalTime_Max() {
LocalDateTime start = LocalDateTime.of(2999, 1, 1, 23, 0);
LocalDateTime end = LocalDateTime.of(2999, 1, 1, 23, 59, 59, 999_999_999);
long cafeId = cafePersistHelper.persistCafeWith24For7().getId();
long leaderId = memberPersistHelper.persistDefaultMember(THUMBNAIL_IMAGE).getId();
StudyOnceCreateRequest studyOnceCreateRequest = makeStudyOnceCreateRequest(start, end, cafeId);

assertDoesNotThrow(() -> studyOnceService.createStudy(leaderId, studyOnceCreateRequest, LocalDate.now()));
}

@Test
@DisplayName("카공 시작 시간이 현재 시간 + 3시간 보다 이전인 경우 실패")
void createFailByStartTime() {
Expand Down

0 comments on commit e32e94d

Please sign in to comment.