Skip to content

Commit 7aa3c14

Browse files
author
HoyoenKim
committed
[#13] Feat: specmeal input done
1 parent c4ffff2 commit 7aa3c14

File tree

6 files changed

+162
-101
lines changed

6 files changed

+162
-101
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.example.helper.constant;
2+
3+
public enum Messages {
4+
EXIST_MEAL_ERROR("이미 존재하는 식단입니다."),
5+
NO_EXIST_MEAL_ERROR("조건에 맞는 식단이 존재하지 않습니다."),
6+
NO_MEAL_KOR("식단 준비중입니다."),
7+
NO_MEAL_ENG("The meal is being prepared."),
8+
DUMMY_MEAL_KOR("2023-01-27 조식\n\n제2학생회관1층\n\n흰밥*김가루양념밥\n"),
9+
DUMMY_MEAL_ENG("2023-01-27 Breakfast\n\nStudent Union Bldg.2 1st floor\n\nWhite rice*Seasoned rice with seaweed\n");
10+
private String message;
11+
Messages(String message) {
12+
this.message = message;
13+
}
14+
public String getMessages() {
15+
return message;
16+
}
17+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.example.helper.constant;
2+
3+
import jakarta.persistence.criteria.CriteriaBuilder;
4+
5+
public enum SpecMealInputsEng {
6+
TODAY("today"),
7+
TOMORROW("tomorrow"),
8+
MON("Mon", Types.DATE_MON.getType()),
9+
TUE("Tue", Types.DATE_TUE.getType()),
10+
WED("Wed", Types.DATE_WED.getType()),
11+
THR("Thr", Types.DATE_THR.getType()),
12+
FRI("Fri", Types.DATE_FRI.getType()),
13+
SAT("Sat", Types.DATE_SAT.getType()),
14+
SUM("Sun", Types.DATE_SUN.getType()),
15+
DAY_1("st"),
16+
DAY_2("nd"),
17+
DAY_3("rd"),
18+
DAY_OTHER("th"),
19+
BREAKFAST("breakfast", Types.KIND_BREAKFAST.getType()),
20+
LUNCH("lunch", Types.KIND_LUNCH.getType()),
21+
DINNER("dinner", Types.KIND_DINNER.getType());
22+
private String input;
23+
private Integer inputValue;
24+
SpecMealInputsEng (String input) {
25+
this.input = input;
26+
}
27+
28+
SpecMealInputsEng (String input, Integer inputValue) {
29+
this.input = input;
30+
this.inputValue = inputValue;
31+
}
32+
public String getInputs() {
33+
return input;
34+
}
35+
public Integer getInputValue() { return inputValue; }
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example.helper.constant;
2+
3+
4+
public enum SpecMealInputsKor {
5+
TODAY("오늘"),
6+
TOMORROW("내일"),
7+
MON("월", Types.DATE_MON.getType()),
8+
TUE("화", Types.DATE_TUE.getType()),
9+
WED("수", Types.DATE_WED.getType()),
10+
THR("목", Types.DATE_THR.getType()),
11+
FRI("금", Types.DATE_FRI.getType()),
12+
SAT("토", Types.DATE_SAT.getType()),
13+
SUM("일", Types.DATE_SUN.getType()),
14+
DAY("일"),
15+
BREAKFAST("조식", Types.KIND_BREAKFAST.getType()),
16+
LUNCH("중식", Types.KIND_LUNCH.getType()),
17+
DINNER("석식", Types.KIND_DINNER.getType());
18+
private String input;
19+
private Integer inputValue;
20+
SpecMealInputsKor(String input) {
21+
this.input = input;
22+
}
23+
SpecMealInputsKor(String input, Integer inputValue) {
24+
this.input = input;
25+
this.inputValue = inputValue;
26+
}
27+
public String getInputs() {
28+
return input;
29+
}
30+
public Integer getInputValue() { return inputValue; }
31+
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ public enum Types {
99
KIND_BREAKFAST(0),
1010
KIND_LUNCH(1),
1111
KIND_DINNER(2),
12-
KIND_LUNCH_CORNER(3);
12+
KIND_LUNCH_CORNER(3),
13+
DATE_MON(0),
14+
DATE_TUE(1),
15+
DATE_WED(2),
16+
DATE_THR(3),
17+
DATE_FRI(4),
18+
DATE_SAT(5),
19+
DATE_SUN(6);
1320

1421
private Integer type;
1522

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

Lines changed: 20 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ public Map<String, Object> readKorMeal() throws JsonProcessingException {
6262

6363
String nowMeal = mealService.getNowKorMeal();
6464
Map<String, Object> responseBody = mealService.responseMeal(nowMeal);
65-
// if need to stratify.
66-
// ObjectMapper objectMapper = new ObjectMapper();
67-
// String result = objectMapper.writeValueAsString(responseBody);
6865

6966
return responseBody;
7067
}
@@ -77,104 +74,49 @@ public Map<String, Object> readEngMeal() throws JsonProcessingException {
7774
String nowMeal = mealService.getNowEngMeal();
7875
Map<String, Object> responseBody = mealService.responseMeal(nowMeal);
7976

80-
// if need to stratify.
81-
// ObjectMapper objectMapper = new ObjectMapper();
82-
// String result = objectMapper.writeValueAsString(responseBody);
83-
8477
return responseBody;
8578
}
8679

87-
@PostMapping("/spectest")
88-
public Map<String, Object> readSpecKortest(@RequestBody Map<String, Object> requestBody) throws JsonProcessingException {
89-
// input : 날짜요일내일 + 아점저 + 1/2학
80+
@PostMapping("/speckor")
81+
public Map<String, Object> readSpecKorMeal(@RequestBody Map<String, Object> requestBody) throws JsonProcessingException {
82+
// input : 날짜요일내일 + 아점저
9083
// output : 한국어 식단이 포함된 JSON (단, JSON은 카톡 서버가 받을 수 있는 형식이여야 함.)
9184

92-
Map<String, Object> action = new HashMap<>();
93-
action.put("action", requestBody.get("action"));
94-
95-
Map<String, Object> params = new HashMap<>();
96-
params.put("params", action.get("action"));
97-
98-
Map<String, Object> paramssss = new HashMap<>();
99-
paramssss.put("params", params.get("params"));
100-
101-
String dateCustom = (String) paramssss.get("dateCustom");
102-
String bld = (String) paramssss.get("bld");
103-
104-
log.info("req body : " + requestBody.toString());
105-
log.info("action body : " + action.toString());
106-
log.info("params body : " + params.toString());
107-
log.info("paramssss body : " + paramssss.toString());
108-
109-
for (Entry<String, Object> entrySet : requestBody.entrySet()) {
110-
log.info(entrySet.getKey() + " : " + entrySet.getValue());
111-
}
112-
113-
log.info("###########RESULT############");
114-
log.info(dateCustom + " " + bld);
115-
log.info(dateCustom + " " + bld);
85+
// how to print the request body
86+
//for (Entry<String, Object> entrySet : requestBody.entrySet()) {
87+
// log.info(entrySet.getKey() + " : " + entrySet.getValue());
88+
//}
11689

11790
ObjectMapper objectMapper = new ObjectMapper();
118-
Map<String, Object> action2 = objectMapper.convertValue(requestBody.get("action"), Map.class);
119-
Map<String, Object> params2 = objectMapper.convertValue(action2.get("params"), Map.class);
120-
log.info(params2.get("dateCustom").toString() + " " + params2.get("bld").toString());
121-
122-
//Map<String, String> params2 = new HashMap<>();
123-
//params2.put()
124-
//log.info(params2.get("dateCustom") + " " + params2.get("bld"));
125-
126-
String specMeal = mealService.getSpecKorMeal(dateCustom, bld);
127-
Map<String, Object> responseBody = mealService.responseMeal(specMeal);
128-
129-
// if need to stratify.
130-
// ObjectMapper objectMapper = new ObjectMapper();
131-
// String result = objectMapper.writeValueAsString(responseBody);
132-
133-
return responseBody;
134-
}
135-
136-
@PostMapping("/speckor")
137-
public Map<String, Object> readSpecKorMeal(@RequestBody Map<String, Map<String, Map<String, Object>>> requestBody) throws JsonProcessingException {
138-
// input : 날짜요일내일 + 아점저 + 1/2학
139-
// output : 한국어 식단이 포함된 JSON (단, JSON은 카톡 서버가 받을 수 있는 형식이여야 함.)
140-
141-
Map<String, Map<String, Object>> action = requestBody.get("action");
142-
Map<String, Object> params = action.get("params");
143-
String dateCustom = (String) params.get("dateCustom");
144-
String bld = (String) params.get("bld");
91+
Map<String, Object> action = objectMapper.convertValue(requestBody.get("action"), Map.class);
92+
Map<String, Object> params = objectMapper.convertValue(action.get("params"), Map.class);
14593

146-
log.info(dateCustom + " " + bld);
147-
log.info(dateCustom + " " + bld);
94+
//log.info(params2.get("dateCustom").toString() + " " + params2.get("bld").toString());
95+
String dateCustom = params.get("dateCustom").toString();
96+
String bld = params.get("bld").toString();
14897

14998
String specMeal = mealService.getSpecKorMeal(dateCustom, bld);
15099
Map<String, Object> responseBody = mealService.responseMeal(specMeal);
151100

152-
// if need to stratify.
153-
// ObjectMapper objectMapper = new ObjectMapper();
154-
// String result = objectMapper.writeValueAsString(responseBody);
155-
156101
return responseBody;
157102
}
158103

159104
@PostMapping("/speceng")
160-
public Map<String, Object> readSpecEngMeal(@RequestBody Map<String, Map<String, Map<String, Object>>> requestBody) throws JsonProcessingException {
161-
// input : 날짜요일내일 + 아점저 + 1/2학
105+
public Map<String, Object> readSpecEngMeal(@RequestBody Map<String, Object> requestBody) throws JsonProcessingException {
106+
// input : 날짜요일내일 + 아점저
162107
// output : 영어 식단이 포함된 JSON (단, JSON은 카톡 서버가 받을 수 있는 형식이여야 함.)
163108

164-
Map<String, Map<String, Object>> action = requestBody.get("action");
165-
Map<String, Object> params = action.get("params");
166-
String dateCustom = (String) params.get("dateCustom");
167-
String bld = (String) params.get("bld");
109+
ObjectMapper objectMapper = new ObjectMapper();
110+
Map<String, Object> action = objectMapper.convertValue(requestBody.get("action"), Map.class);
111+
Map<String, Object> params = objectMapper.convertValue(action.get("params"), Map.class);
168112

169-
log.info(dateCustom + " " + bld);
113+
//log.info(params2.get("dateCustom").toString() + " " + params2.get("bld").toString());
114+
String dateCustom = params.get("dateCustom").toString();
115+
String bld = params.get("bld").toString();
170116

171117
String specMeal = mealService.getSpecEngMeal(dateCustom, bld);
172118
Map<String, Object> responseBody = mealService.responseMeal(specMeal);
173119

174-
// if need to stratify.
175-
// ObjectMapper objectMapper = new ObjectMapper();
176-
// String result = objectMapper.writeValueAsString(responseBody);
177-
178120
return responseBody;
179121
}
180122

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

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.example.helper.service;
22

3+
import com.example.helper.constant.Messages;
4+
import com.example.helper.constant.SpecMealInputsKor;
5+
import com.example.helper.constant.Types;
36
import com.example.helper.dto.DateMealDto;
47
import com.example.helper.dto.DateReqDto;
58
import com.example.helper.entity.Meal;
@@ -39,7 +42,7 @@ private void validateDuplicateMeal(Meal meal) {
3942
// DB 중복 체크
4043
sqlMealRepository.findByPk(meal.getBldgType(), meal.getLangType(), meal.getDateType(), meal.getKindType(), meal.getDate())
4144
.ifPresent(m -> {
42-
throw new IllegalStateException("이미 존재하는 식단입니다.");
45+
throw new IllegalStateException(Messages.EXIST_MEAL_ERROR.getMessages());
4346
});
4447
}
4548

@@ -63,62 +66,83 @@ else if(19 <= hour && hour < 24) {
6366
currentDateTime = currentDateTime.plusDays(1);
6467
}
6568

66-
log.info("currentDateTime Obj : " + currentDateTime.toString());
67-
6869
String date = currentDateTime.getYear() + "-";
6970
date += String.format("%02d", currentDateTime.getMonth().getValue()) + "-";
7071
date += String.format("%02d", currentDateTime.getDayOfMonth()) + "";
7172

72-
log.info("date Obj : " + date);
73-
log.info("langType Obj : " + langType);
74-
log.info("kindType Obj : " + kindType);
75-
Optional<Meal> result = sqlMealRepository.findByDate(2, langType, kindType, date);
73+
Optional<Meal> result = sqlMealRepository.findByDate(Types.BLDG2_1ST.getType(), langType, kindType, date);
7674

7775
if(result.isEmpty()) {
78-
//throw new IllegalStateException("조건에 맞는 식단이 존재하지 않습니다.");
76+
//throw new IllegalStateException(Messages.EXIST_MEAL_ERROR.getMessages());
7977
if(langType == 0) {
80-
return "식단 준비중입니다.";
78+
return Messages.NO_MEAL_KOR.getMessages();
8179
}
8280
else {
83-
return "The meal is being prepared.";
81+
return Messages.NO_MEAL_ENG.getMessages();
8482
}
8583
}
8684
Meal meal = result.get();
8785
return meal.generateMenu();
8886
}
8987

88+
private Boolean specInputValidation(String dateCustom, String bld) {
89+
// len = 0 or null or ...
90+
Boolean ret = true;
91+
92+
return ret;
93+
}
94+
9095
public String getNowKorMeal() {
91-
return getNowMeal(0);
96+
return getNowMeal(Types.LANG_KOR.getType());
9297
}
9398

9499
public String getNowEngMeal() {
95-
return getNowMeal(1);
100+
return getNowMeal(Types.LANG_ENG.getType());
96101
}
97102

98103
public String getSpecKorMeal(String dateCustom, String bld) {
99-
//TODO sqlMealRepository.findSpecKorMeal
104+
if (!specInputValidation(dateCustom, bld)) {
105+
return Messages.NO_MEAL_KOR.getMessages();
106+
}
107+
108+
if(dateCustom.equals(SpecMealInputsKor.TODAY.getInputs())) {
109+
// 오늘
110+
}
111+
else if(dateCustom.equals(SpecMealInputsKor.TOMORROW.getInputs())) {
112+
// 내일
113+
}
114+
else if(dateCustom.length() == 1) {
115+
// 요일
116+
}
117+
else if((dateCustom.charAt(dateCustom.length() - 1) + "").equals(SpecMealInputsKor.DAY.getInputs())) {
118+
// 특정날짜
119+
}
120+
100121
return "2023-01-27 조식\n\n제2학생회관1층\n\n흰밥*김가루양념밥\n";
101122
}
102123

103124
public String getSpecEngMeal(String dateCustom, String bld) {
104-
//TODO sqlMealRepository.findSpecEngMeal
125+
if (!specInputValidation(dateCustom, bld)) {
126+
return Messages.NO_MEAL_ENG.getMessages();
127+
}
105128
return "2023-01-27 Breakfast\n\nStudent Union Bldg.2 1st floor\n\nWhite rice*Seasoned rice with seaweed\n";
106129
}
107130

108131
public Map<String, Object> responseMeal(String menu) {
109132
// Response Body Construct
110-
// const responseBody = {
111-
// version: "2.0",
133+
// responseBody: {
134+
// version: "2.0",
112135
// template: {
113136
// outputs: [
137+
// {
138+
// simpleText:
114139
// {
115-
// simpleText: {
116-
// text: nowMeal
117-
// }
140+
// text: meal
118141
// }
119-
// ]
120-
// }
121-
// };
142+
// }
143+
// ]
144+
// }
145+
// };
122146

123147
Map<String, Object> simpleText = new HashMap<>();
124148
simpleText.put("text", menu);
@@ -136,6 +160,10 @@ public Map<String, Object> responseMeal(String menu) {
136160
responseBody.put("version", "2.0");
137161
responseBody.put("template", template);
138162

163+
// stratify json to string
164+
// ObjectMapper objectMapper = new ObjectMapper();
165+
// String result = objectMapper.writeValueAsString(responseBody);
166+
139167
return responseBody;
140168
}
141169
}

0 commit comments

Comments
 (0)