Skip to content

Commit 63980ef

Browse files
authored
Merge pull request #17 from gist-helper/specmealkor
Specmealkor
2 parents 04f80be + b79df31 commit 63980ef

File tree

6 files changed

+88
-19
lines changed

6 files changed

+88
-19
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ dependencies {
2525

2626
testImplementation 'org.springframework.boot:spring-boot-starter-test'
2727
testImplementation 'org.junit.platform:junit-platform-launcher:1.5.0'
28-
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.0'
29-
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.5.0'
28+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.1'
29+
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.1'
3030

3131
}
3232

src/main/java/com/example/helper/constant/Messages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public enum Messages {
55
NO_EXIST_MEAL_ERROR("조건에 맞는 식단이 존재하지 않습니다."),
66
NO_MEAL_KOR("식단 준비중입니다."),
77
NO_MEAL_ENG("The meal is being prepared."),
8+
INVALID_DATE("유효하지 않은 날짜입니다."),
89
DUMMY_MEAL_KOR("2023-01-27 조식\n\n제2학생회관1층\n\n흰밥*김가루양념밥\n"),
910
DUMMY_MEAL_ENG("2023-01-27 Breakfast\n\nStudent Union Bldg.2 1st floor\n\nWhite rice*Seasoned rice with seaweed\n");
1011
private String message;

src/main/java/com/example/helper/constant/SpecMealInputsKor.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,12 @@ public String getInputs() {
2828
return input;
2929
}
3030
public Integer getInputValue() { return inputValue; }
31+
32+
public static Integer getTypeByString(String str){
33+
for(SpecMealInputsKor each : values())
34+
if (each.getInputs().equals(str)) {
35+
return each.getInputValue();
36+
}
37+
return -1;
38+
}
3139
}

src/main/java/com/example/helper/controller/MealController.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import com.example.helper.service.MealService;
88
import com.fasterxml.jackson.core.JsonProcessingException;
99
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import java.time.LocalDateTime;
11+
import java.time.ZoneId;
1012
import java.util.Map.Entry;
1113
import lombok.extern.slf4j.Slf4j;
1214
import org.springframework.beans.BeanUtils;
@@ -99,7 +101,8 @@ public Map<String, Object> readSpecKorMeal(@RequestBody Map<String, Object> requ
99101
String dateCustom = params.get("dateCustom").toString();
100102
String bld = params.get("bld").toString();
101103

102-
String specMeal = mealService.getSpecKorMeal(dateCustom, bld);
104+
LocalDateTime currentDateTime = LocalDateTime.now(ZoneId.of("Asia/Seoul"));
105+
String specMeal = mealService.getSpecKorMeal(dateCustom, bld, currentDateTime);
103106
Map<String, Object> responseBody = mealService.responseMeal(specMeal);
104107

105108
return responseBody;

src/main/java/com/example/helper/service/MealService.java

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ else if(19 <= hour && hour < 24) {
7575

7676
Optional<Meal> result = sqlMealRepository.findByDate(Types.BLDG2_1ST.getType(), langType, kindType, date);
7777

78+
// TODO: 함수로 분리
7879
if(result.isEmpty()) {
7980
//throw new IllegalStateException(Messages.EXIST_MEAL_ERROR.getMessages());
8081
if(langType == 0) {
@@ -89,10 +90,11 @@ else if(19 <= hour && hour < 24) {
8990
}
9091

9192
private Boolean specInputValidation(String dateCustom, String bld) {
92-
// len = 0 or null or ...
93-
Boolean ret = true;
94-
95-
return ret;
93+
// input arguments : null, empty, " "
94+
if (dateCustom == null || bld == null || dateCustom.isBlank() || bld.isBlank()) {
95+
return false;
96+
}
97+
return true;
9698
}
9799

98100
public String getNowKorMeal() {
@@ -103,25 +105,50 @@ public String getNowEngMeal() {
103105
return getNowMeal(Types.LANG_ENG.getType());
104106
}
105107

106-
public String getSpecKorMeal(String dateCustom, String bld) {
108+
public String getSpecKorMeal(String dateCustom, String bld, LocalDateTime currentDateTime) {
109+
// test를 위해 LocalDateTime 객체를 argument로 받도록 변경
110+
107111
if (!specInputValidation(dateCustom, bld)) {
108112
return Messages.NO_MEAL_KOR.getMessages();
109113
}
110114

111-
if(dateCustom.equals(SpecMealInputsKor.TODAY.getInputs())) {
112-
// 오늘
113-
}
114-
else if(dateCustom.equals(SpecMealInputsKor.TOMORROW.getInputs())) {
115-
// 내일
116-
}
117-
else if(dateCustom.length() == 1) {
118-
// 요일
115+
try {
116+
if (dateCustom.equals(SpecMealInputsKor.TOMORROW.getInputs())) {
117+
// 내일
118+
currentDateTime = currentDateTime.plusDays(1);
119+
} else if (dateCustom.length() == 1) {
120+
// 요일
121+
Integer dateDiff = getDateDifference(dateCustom, currentDateTime);
122+
currentDateTime = currentDateTime.plusDays(dateDiff);
123+
} else if ((dateCustom.charAt(dateCustom.length() - 1) + "").equals(SpecMealInputsKor.DAY.getInputs())) {
124+
// 특정날짜
125+
currentDateTime = currentDateTime.withDayOfMonth(
126+
Integer.parseInt(dateCustom.substring(0, dateCustom.length() - 1)));
127+
}
128+
} catch (Exception e) {
129+
return Messages.INVALID_DATE.getMessages();
119130
}
120-
else if((dateCustom.charAt(dateCustom.length() - 1) + "").equals(SpecMealInputsKor.DAY.getInputs())) {
121-
// 특정날짜
131+
132+
String date = currentDateTime.getYear() + "-";
133+
date += String.format("%02d", currentDateTime.getMonth().getValue()) + "-";
134+
date += String.format("%02d", currentDateTime.getDayOfMonth()) + "";
135+
136+
Optional<Meal> result = sqlMealRepository.findByDate(
137+
Types.BLDG2_1ST.getType(),
138+
Types.LANG_KOR.getType(),
139+
SpecMealInputsKor.getTypeByString(bld),
140+
date);
141+
142+
// TODO : 존재하지 않는 식단이면, error message 반환.
143+
if(result.isEmpty()) {
144+
return Messages.NO_MEAL_KOR.getMessages();
122145
}
123146

124-
return "2023-01-27 조식\n\n제2학생회관1층\n\n흰밥*김가루양념밥\n";
147+
return result.get().generateMenu();
148+
}
149+
150+
public Integer getDateDifference(String day, LocalDateTime currentDateTime) {
151+
return (Integer) currentDateTime.getDayOfWeek().getValue() - SpecMealInputsKor.getTypeByString(day);
125152
}
126153
public Boolean dateFormatValidation(String date) {
127154
Boolean ret = true;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.example.helper.constant;
2+
3+
import static com.example.helper.constant.SpecMealInputsKor.*;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
class SpecMealInputsKorTest {
12+
13+
@ParameterizedTest
14+
@MethodSource("provideStringsForIsTypes")
15+
void enum_한국어식단_타입변환(String kind, Integer expected) {
16+
// given
17+
// when
18+
Integer result = getTypeByString(kind);
19+
// then
20+
assertEquals(expected, result);
21+
}
22+
23+
private static Stream<Arguments> provideStringsForIsTypes() {
24+
return Stream.of(
25+
Arguments.of(BREAKFAST.getInputs(), BREAKFAST.getInputValue()),
26+
Arguments.of(LUNCH.getInputs(), LUNCH.getInputValue()),
27+
Arguments.of(DINNER.getInputs(), DINNER.getInputValue())
28+
);
29+
}
30+
}

0 commit comments

Comments
 (0)