Skip to content

Commit b79df31

Browse files
김산김산
authored andcommitted
[#14] Feat: tomorrow, days, specific date
1 parent c61aa9f commit b79df31

File tree

4 files changed

+43
-25
lines changed

4 files changed

+43
-25
lines changed

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ public static Integer getTypeByString(String str){
3434
if (each.getInputs().equals(str)) {
3535
return each.getInputValue();
3636
}
37-
return null;
37+
return -1;
3838
}
3939
}

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;
@@ -95,7 +97,8 @@ public Map<String, Object> readSpecKorMeal(@RequestBody Map<String, Object> requ
9597
String dateCustom = params.get("dateCustom").toString();
9698
String bld = params.get("bld").toString();
9799

98-
String specMeal = mealService.getSpecKorMeal(dateCustom, bld);
100+
LocalDateTime currentDateTime = LocalDateTime.now(ZoneId.of("Asia/Seoul"));
101+
String specMeal = mealService.getSpecKorMeal(dateCustom, bld, currentDateTime);
99102
Map<String, Object> responseBody = mealService.responseMeal(specMeal);
100103

101104
return responseBody;

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

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -102,36 +102,50 @@ public String getNowEngMeal() {
102102
return getNowMeal(Types.LANG_ENG.getType());
103103
}
104104

105-
public String getSpecKorMeal(String dateCustom, String bld) {
105+
public String getSpecKorMeal(String dateCustom, String bld, LocalDateTime currentDateTime) {
106+
// test를 위해 LocalDateTime 객체를 argument로 받도록 변경
107+
106108
if (!specInputValidation(dateCustom, bld)) {
107109
return Messages.NO_MEAL_KOR.getMessages();
108110
}
109111

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

134-
return "2023-01-27 조식\n\n제2학생회관1층\n\n흰밥*김가루양념밥\n";
144+
return result.get().generateMenu();
145+
}
146+
147+
public Integer getDateDifference(String day, LocalDateTime currentDateTime) {
148+
return (Integer) currentDateTime.getDayOfWeek().getValue() - SpecMealInputsKor.getTypeByString(day);
135149
}
136150

137151
public String getSpecEngMeal(String dateCustom, String bld) {

0 commit comments

Comments
 (0)