77import com .example .helper .service .MealService ;
88import com .fasterxml .jackson .core .JsonProcessingException ;
99import com .fasterxml .jackson .databind .ObjectMapper ;
10+ import java .time .LocalDateTime ;
11+ import java .time .ZoneId ;
1012import java .util .Map .Entry ;
1113import lombok .extern .slf4j .Slf4j ;
1214import org .springframework .beans .BeanUtils ;
@@ -50,9 +52,13 @@ public String createMeal(@RequestBody Mealdto mealDto) {
5052 BeanUtils .copyProperties (mealDto , meal );
5153
5254 // DB Save
53- Long saved = mealService .mealCreate (meal );
54-
55- return "saved" ;
55+ try {
56+ Long saved = mealService .mealCreate (meal );
57+ return "saved" ;
58+ } catch (IllegalStateException e ) {
59+ throw new ResponseStatusException (
60+ HttpStatus .NOT_ACCEPTABLE , e .getMessage ());
61+ }
5662 }
5763
5864 @ PostMapping ("/kor" )
@@ -62,9 +68,6 @@ public Map<String, Object> readKorMeal() throws JsonProcessingException {
6268
6369 String nowMeal = mealService .getNowKorMeal ();
6470 Map <String , Object > responseBody = mealService .responseMeal (nowMeal );
65- // if need to stratify.
66- // ObjectMapper objectMapper = new ObjectMapper();
67- // String result = objectMapper.writeValueAsString(responseBody);
6871
6972 return responseBody ;
7073 }
@@ -77,104 +80,50 @@ public Map<String, Object> readEngMeal() throws JsonProcessingException {
7780 String nowMeal = mealService .getNowEngMeal ();
7881 Map <String , Object > responseBody = mealService .responseMeal (nowMeal );
7982
80- // if need to stratify.
81- // ObjectMapper objectMapper = new ObjectMapper();
82- // String result = objectMapper.writeValueAsString(responseBody);
83-
8483 return responseBody ;
8584 }
8685
87- @ PostMapping ("/spectest " )
88- public Map <String , Object > readSpecKortest (@ RequestBody Map <String , Object > requestBody ) throws JsonProcessingException {
89- // input : 날짜요일내일 + 아점저 + 1/2학
86+ @ PostMapping ("/speckor " )
87+ public Map <String , Object > readSpecKorMeal (@ RequestBody Map <String , Object > requestBody ) throws JsonProcessingException {
88+ // input : 날짜요일내일 + 아점저
9089 // output : 한국어 식단이 포함된 JSON (단, JSON은 카톡 서버가 받을 수 있는 형식이여야 함.)
9190
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 );
91+ // how to print the request body
92+ //for (Entry<String, Object> entrySet : requestBody.entrySet()) {
93+ // log.info(entrySet.getKey() + " : " + entrySet.getValue());
94+ //}
11695
11796 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 ());
97+ Map <String , Object > action = objectMapper .convertValue (requestBody .get ("action" ), Map .class );
98+ Map <String , Object > params = objectMapper .convertValue (action .get ("params" ), Map .class );
12199
122- //Map<String, String> params2 = new HashMap<>( );
123- //params2.put()
124- //log.info(params2.get("dateCustom") + " " + params2 .get("bld"));
100+ //log.info(params2.get("dateCustom").toString() + " " + params2.get("bld").toString() );
101+ String dateCustom = params . get ( "dateCustom" ). toString ();
102+ String bld = params .get ("bld" ). toString ( );
125103
126- String specMeal = mealService .getSpecKorMeal (dateCustom , bld );
104+ LocalDateTime currentDateTime = LocalDateTime .now (ZoneId .of ("Asia/Seoul" ));
105+ String specMeal = mealService .getSpecKorMeal (dateCustom , bld , currentDateTime );
127106 Map <String , Object > responseBody = mealService .responseMeal (specMeal );
128107
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" );
145-
146- log .info (dateCustom + " " + bld );
147- log .info (dateCustom + " " + bld );
148-
149- String specMeal = mealService .getSpecKorMeal (dateCustom , bld );
150- Map <String , Object > responseBody = mealService .responseMeal (specMeal );
151-
152- // if need to stratify.
153- // ObjectMapper objectMapper = new ObjectMapper();
154- // String result = objectMapper.writeValueAsString(responseBody);
155-
156108 return responseBody ;
157109 }
158110
159111 @ PostMapping ("/speceng" )
160- public Map <String , Object > readSpecEngMeal (@ RequestBody Map <String , Map < String , Map < String , Object >> > requestBody ) throws JsonProcessingException {
161- // input : 날짜요일내일 + 아점저 + 1/2학
112+ public Map <String , Object > readSpecEngMeal (@ RequestBody Map <String , Object > requestBody ) throws JsonProcessingException {
113+ // input : 날짜요일내일 + 아점저
162114 // output : 영어 식단이 포함된 JSON (단, JSON은 카톡 서버가 받을 수 있는 형식이여야 함.)
163115
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" );
116+ ObjectMapper objectMapper = new ObjectMapper ();
117+ Map <String , Object > action = objectMapper .convertValue (requestBody .get ("action" ), Map .class );
118+ Map <String , Object > params = objectMapper .convertValue (action .get ("params" ), Map .class );
168119
169- log .info (dateCustom + " " + bld );
120+ //log.info(params2.get("dateCustom").toString() + " " + params2.get("bld").toString());
121+ String dateCustom = params .get ("dateCustom" ).toString ();
122+ String bld = params .get ("bld" ).toString ();
170123
171124 String specMeal = mealService .getSpecEngMeal (dateCustom , bld );
172125 Map <String , Object > responseBody = mealService .responseMeal (specMeal );
173126
174- // if need to stratify.
175- // ObjectMapper objectMapper = new ObjectMapper();
176- // String result = objectMapper.writeValueAsString(responseBody);
177-
178127 return responseBody ;
179128 }
180129
0 commit comments