Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/example/helper/constant/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
14 changes: 7 additions & 7 deletions src/main/java/com/example/helper/constant/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,7 +97,8 @@ public Map<String, Object> readSpecKorMeal(@RequestBody Map<String, Object> 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<String, Object> responseBody = mealService.responseMeal(specMeal);

return responseBody;
Expand Down
59 changes: 43 additions & 16 deletions src/main/java/com/example/helper/service/MealService.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ else if(19 <= hour && hour < 24) {

Optional<Meal> 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) {
Expand All @@ -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() {
Expand All @@ -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<Meal> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Arguments> provideStringsForIsTypes() {
return Stream.of(
Arguments.of(BREAKFAST.getInputs(), BREAKFAST.getInputValue()),
Arguments.of(LUNCH.getInputs(), LUNCH.getInputValue()),
Arguments.of(DINNER.getInputs(), DINNER.getInputValue())
);
}
}