diff --git a/build.gradle b/build.gradle index 2bc7dc0..7a01909 100644 --- a/build.gradle +++ b/build.gradle @@ -25,8 +25,8 @@ dependencies { testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.junit.platform:junit-platform-launcher:1.5.0' - testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.0' - testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.5.0' + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.1' + testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.1' } diff --git a/src/main/java/com/example/helper/constant/Messages.java b/src/main/java/com/example/helper/constant/Messages.java index 5edaca9..8cc80fe 100644 --- a/src/main/java/com/example/helper/constant/Messages.java +++ b/src/main/java/com/example/helper/constant/Messages.java @@ -5,6 +5,7 @@ public enum Messages { NO_EXIST_MEAL_ERROR("조건에 맞는 식단이 존재하지 않습니다."), NO_MEAL_KOR("식단 준비중입니다."), NO_MEAL_ENG("The meal is being prepared."), + INVALID_DATE("유효하지 않은 날짜입니다."), DUMMY_MEAL_KOR("2023-01-27 조식\n\n제2학생회관1층\n\n흰밥*김가루양념밥\n"), DUMMY_MEAL_ENG("2023-01-27 Breakfast\n\nStudent Union Bldg.2 1st floor\n\nWhite rice*Seasoned rice with seaweed\n"); private String message; diff --git a/src/main/java/com/example/helper/constant/SpecMealInputsKor.java b/src/main/java/com/example/helper/constant/SpecMealInputsKor.java index 54a088d..fc8af17 100644 --- a/src/main/java/com/example/helper/constant/SpecMealInputsKor.java +++ b/src/main/java/com/example/helper/constant/SpecMealInputsKor.java @@ -28,4 +28,12 @@ public String getInputs() { return input; } public Integer getInputValue() { return inputValue; } + + public static Integer getTypeByString(String str){ + for(SpecMealInputsKor each : values()) + if (each.getInputs().equals(str)) { + return each.getInputValue(); + } + return -1; + } } diff --git a/src/main/java/com/example/helper/constant/Types.java b/src/main/java/com/example/helper/constant/Types.java index 4a2fee1..c646b35 100644 --- a/src/main/java/com/example/helper/constant/Types.java +++ b/src/main/java/com/example/helper/constant/Types.java @@ -10,13 +10,13 @@ public enum Types { KIND_LUNCH(1), KIND_DINNER(2), KIND_LUNCH_CORNER(3), - DATE_MON(0), - DATE_TUE(1), - DATE_WED(2), - DATE_THR(3), - DATE_FRI(4), - DATE_SAT(5), - DATE_SUN(6); + DATE_MON(1), + DATE_TUE(2), + DATE_WED(3), + DATE_THR(4), + DATE_FRI(5), + DATE_SAT(6), + DATE_SUN(7); private Integer type; diff --git a/src/main/java/com/example/helper/controller/MealController.java b/src/main/java/com/example/helper/controller/MealController.java index 16a1848..ff34a88 100644 --- a/src/main/java/com/example/helper/controller/MealController.java +++ b/src/main/java/com/example/helper/controller/MealController.java @@ -7,6 +7,8 @@ import com.example.helper.service.MealService; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import java.time.LocalDateTime; +import java.time.ZoneId; import java.util.Map.Entry; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; @@ -95,7 +97,8 @@ public Map readSpecKorMeal(@RequestBody Map requ String dateCustom = params.get("dateCustom").toString(); String bld = params.get("bld").toString(); - String specMeal = mealService.getSpecKorMeal(dateCustom, bld); + LocalDateTime currentDateTime = LocalDateTime.now(ZoneId.of("Asia/Seoul")); + String specMeal = mealService.getSpecKorMeal(dateCustom, bld, currentDateTime); Map responseBody = mealService.responseMeal(specMeal); return responseBody; diff --git a/src/main/java/com/example/helper/service/MealService.java b/src/main/java/com/example/helper/service/MealService.java index 7c36f8f..8495413 100644 --- a/src/main/java/com/example/helper/service/MealService.java +++ b/src/main/java/com/example/helper/service/MealService.java @@ -72,6 +72,7 @@ else if(19 <= hour && hour < 24) { Optional result = sqlMealRepository.findByDate(Types.BLDG2_1ST.getType(), langType, kindType, date); + // TODO: 함수로 분리 if(result.isEmpty()) { //throw new IllegalStateException(Messages.EXIST_MEAL_ERROR.getMessages()); if(langType == 0) { @@ -86,10 +87,11 @@ else if(19 <= hour && hour < 24) { } private Boolean specInputValidation(String dateCustom, String bld) { - // len = 0 or null or ... - Boolean ret = true; - - return ret; + // input arguments : null, empty, " " + if (dateCustom == null || bld == null || dateCustom.isBlank() || bld.isBlank()) { + return false; + } + return true; } public String getNowKorMeal() { @@ -100,25 +102,50 @@ public String getNowEngMeal() { return getNowMeal(Types.LANG_ENG.getType()); } - public String getSpecKorMeal(String dateCustom, String bld) { + public String getSpecKorMeal(String dateCustom, String bld, LocalDateTime currentDateTime) { + // test를 위해 LocalDateTime 객체를 argument로 받도록 변경 + if (!specInputValidation(dateCustom, bld)) { return Messages.NO_MEAL_KOR.getMessages(); } - if(dateCustom.equals(SpecMealInputsKor.TODAY.getInputs())) { - // 오늘 - } - else if(dateCustom.equals(SpecMealInputsKor.TOMORROW.getInputs())) { - // 내일 - } - else if(dateCustom.length() == 1) { - // 요일 + try { + if (dateCustom.equals(SpecMealInputsKor.TOMORROW.getInputs())) { + // 내일 + currentDateTime = currentDateTime.plusDays(1); + } else if (dateCustom.length() == 1) { + // 요일 + Integer dateDiff = getDateDifference(dateCustom, currentDateTime); + currentDateTime = currentDateTime.plusDays(dateDiff); + } else if ((dateCustom.charAt(dateCustom.length() - 1) + "").equals(SpecMealInputsKor.DAY.getInputs())) { + // 특정날짜 + currentDateTime = currentDateTime.withDayOfMonth( + Integer.parseInt(dateCustom.substring(0, dateCustom.length() - 1))); + } + } catch (Exception e) { + return Messages.INVALID_DATE.getMessages(); } - else if((dateCustom.charAt(dateCustom.length() - 1) + "").equals(SpecMealInputsKor.DAY.getInputs())) { - // 특정날짜 + + String date = currentDateTime.getYear() + "-"; + date += String.format("%02d", currentDateTime.getMonth().getValue()) + "-"; + date += String.format("%02d", currentDateTime.getDayOfMonth()) + ""; + + Optional result = sqlMealRepository.findByDate( + Types.BLDG2_1ST.getType(), + Types.LANG_KOR.getType(), + SpecMealInputsKor.getTypeByString(bld), + date); + + // TODO : 존재하지 않는 식단이면, error message 반환. + if(result.isEmpty()) { + return Messages.NO_MEAL_KOR.getMessages(); } - return "2023-01-27 조식\n\n제2학생회관1층\n\n흰밥*김가루양념밥\n"; + return result.get().generateMenu(); + } + + public Integer getDateDifference(String day, LocalDateTime currentDateTime) { + return (Integer) currentDateTime.getDayOfWeek().getValue() - SpecMealInputsKor.getTypeByString(day); } public String getSpecEngMeal(String dateCustom, String bld) { diff --git a/src/test/java/com/example/helper/constant/SpecMealInputsKorTest.java b/src/test/java/com/example/helper/constant/SpecMealInputsKorTest.java new file mode 100644 index 0000000..3229a9e --- /dev/null +++ b/src/test/java/com/example/helper/constant/SpecMealInputsKorTest.java @@ -0,0 +1,30 @@ +package com.example.helper.constant; + +import static com.example.helper.constant.SpecMealInputsKor.*; +import static org.junit.jupiter.api.Assertions.*; + +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +class SpecMealInputsKorTest { + + @ParameterizedTest + @MethodSource("provideStringsForIsTypes") + void enum_한국어식단_타입변환(String kind, Integer expected) { + // given + // when + Integer result = getTypeByString(kind); + // then + assertEquals(expected, result); + } + + private static Stream provideStringsForIsTypes() { + return Stream.of( + Arguments.of(BREAKFAST.getInputs(), BREAKFAST.getInputValue()), + Arguments.of(LUNCH.getInputs(), LUNCH.getInputValue()), + Arguments.of(DINNER.getInputs(), DINNER.getInputValue()) + ); + } +}