Skip to content

Commit dd62d57

Browse files
author
HoyoenKim
committed
[#3] create meal with test, now meal, spec meal api with test
1 parent d17d4b4 commit dd62d57

File tree

7 files changed

+252
-83
lines changed

7 files changed

+252
-83
lines changed
Lines changed: 187 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package com.example.helper.controller;
2-
import com.example.helper.dto.MealWrapper;
2+
import com.example.helper.dto.Mealdto;
33
import com.example.helper.entity.Meal;
44
import com.example.helper.service.MealService;
5+
import com.fasterxml.jackson.core.JsonProcessingException;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
57
import lombok.extern.slf4j.Slf4j;
8+
import org.springframework.beans.BeanUtils;
69
import org.springframework.beans.factory.annotation.Autowired;
7-
import org.springframework.web.bind.annotation.GetMapping;
8-
import org.springframework.web.bind.annotation.PostMapping;
9-
import org.springframework.web.bind.annotation.RequestMapping;
10-
import org.springframework.web.bind.annotation.ResponseBody;
11-
import org.springframework.web.bind.annotation.RestController;
10+
import org.springframework.web.bind.annotation.*;
1211

12+
import java.util.ArrayList;
13+
import java.util.HashMap;
14+
import java.util.List;
15+
import java.util.Map;
1316

1417

1518
@RestController
@@ -25,57 +28,202 @@ public String hello() {
2528
return "Hello HELPERs. 초기 세팅 완료.";
2629
}
2730

31+
@PostMapping("/test")
32+
public @ResponseBody void test(String testStr) {
33+
log.info(testStr);
34+
}
2835
@PostMapping("/create")
29-
public @ResponseBody String createMeal(MealWrapper mealWrapper) {
30-
// 여기 구현해주세 222
31-
Meal meal = mealWrapper.getMeal();
32-
// merge test
33-
// Meal saved = mealService.save(meal);
36+
public @ResponseBody String createMeal(@RequestBody Mealdto mealDto) {
37+
// input : 식단 json
38+
// output : None
39+
40+
// DTO to Entity
41+
Meal meal = new Meal();
42+
BeanUtils.copyProperties(mealDto, meal);
43+
44+
// DB Save
45+
Long saved = mealService.mealCreate(meal);
3446

35-
return "saved"; // Responsebody 넣어서 보내는거 해보세요
47+
return "saved";
3648
}
3749

3850
@PostMapping("/kor")
39-
public @ResponseBody String readKorMeal() {
40-
// input : utc
41-
// output : 카톡 서버맞게 한국어식단 JSON으로
42-
// const responseBody = {
43-
// version: "2.0",
44-
// template: {
45-
// outputs: [
46-
// {
47-
// simpleText: {
48-
// text: rows[0].menu
49-
// }
50-
// }
51-
// ]
52-
// }
53-
// };
54-
// res.status(200).send(responseBody);
55-
return "saved"; // Responsebody 넣어서 보내는거 해보세요
51+
public @ResponseBody String readKorMeal() throws JsonProcessingException {
52+
// input : None (먼저 서버에서 현재 시간 측정)
53+
// output : 한국어 식단이 포함된 JSON (단, JSON은 카톡 서버가 받을 수 있는 형식이여야 함.)
54+
55+
// TODO 아래 기능 구현해 주세요.
56+
String nowMeal = mealService.getNowKorMeal();
57+
58+
// Response Body Construct
59+
// TODO 더 좋은 방식이 있으면 구현해 주세요. ex) dto 이용.
60+
// const responseBody = {
61+
// version: "2.0",
62+
// template: {
63+
// outputs: [
64+
// {
65+
// simpleText: {
66+
// text: nowMeal
67+
// }
68+
// }
69+
// ]
70+
// }
71+
// };
72+
73+
Map<String, Object> simpleText = new HashMap<>();
74+
simpleText.put("text", nowMeal);
75+
76+
Map<String, Object> simpleTextWrapper = new HashMap<>();
77+
simpleTextWrapper.put("simpleText", simpleText);
78+
79+
List<Object> outputs = new ArrayList(1);
80+
outputs.add(simpleTextWrapper);
81+
82+
Map<String, Object> template = new HashMap<>();
83+
template.put("outputs", outputs);
84+
85+
Map<String, Object> responseBody = new HashMap<>();
86+
responseBody.put("version", "2.0");
87+
responseBody.put("template", template);
88+
89+
ObjectMapper objectMapper = new ObjectMapper();
90+
String result = objectMapper.writeValueAsString(responseBody);
91+
92+
return result;
5693
}
5794

5895
@PostMapping("/eng")
59-
public @ResponseBody String readEngMeal() {
60-
// input : utc
61-
// output : 카톡 서버맞게 영어식단 JSON으로
96+
public @ResponseBody String readEngMeal() throws JsonProcessingException {
97+
// input : None (먼저 서버에서 현재 시간 측정)
98+
// output : 영어 식단이 포함된 JSON (단, JSON은 카톡 서버가 받을 수 있는 형식이여야 함.)
99+
100+
// TODO 아래 기능 구현해 주세요.
101+
String nowMeal = mealService.getNowEngMeal();
102+
103+
// Response Body Construct
104+
// TODO 더 좋은 방식이 있으면 구현해 주세요. ex) dto 이용.
105+
// const responseBody = {
106+
// version: "2.0",
107+
// template: {
108+
// outputs: [
109+
// {
110+
// simpleText: {
111+
// text: nowMeal
112+
// }
113+
// }
114+
// ]
115+
// }
116+
// };
117+
118+
Map<String, Object> simpleText = new HashMap<>();
119+
simpleText.put("text", nowMeal);
120+
121+
Map<String, Object> simpleTextWrapper = new HashMap<>();
122+
simpleTextWrapper.put("simpleText", simpleText);
123+
124+
List<Object> outputs = new ArrayList(1);
125+
outputs.add(simpleTextWrapper);
126+
127+
Map<String, Object> template = new HashMap<>();
128+
template.put("outputs", outputs);
62129

63-
return "saved"; // Responsebody 넣어서 보내는거 해보세요
130+
Map<String, Object> responseBody = new HashMap<>();
131+
responseBody.put("version", "2.0");
132+
responseBody.put("template", template);
133+
134+
ObjectMapper objectMapper = new ObjectMapper();
135+
String result = objectMapper.writeValueAsString(responseBody);
136+
137+
return result;
64138
}
65139

66140
@PostMapping("/speckor")
67-
public @ResponseBody String readSpecKorMeal() {
141+
public @ResponseBody String readSpecKorMeal() throws JsonProcessingException {
68142
// input : 날짜요일내일 + 아점저 + 1/2학
69-
// output : 해당 식단 찾아 카톡 서버맞게 JSON으로
143+
// output : 한국어 식단이 포함된 JSON (단, JSON은 카톡 서버가 받을 수 있는 형식이여야 함.)
144+
145+
// TODO 아래 기능 구현해 주세요.
146+
String specMeal = mealService.getSpecKorMeal();
147+
148+
// Response Body Construct
149+
// TODO 더 좋은 방식이 있으면 구현해 주세요. ex) dto 이용.
150+
// const responseBody = {
151+
// version: "2.0",
152+
// template: {
153+
// outputs: [
154+
// {
155+
// simpleText: {
156+
// text: specMeal
157+
// }
158+
// }
159+
// ]
160+
// }
161+
// };
162+
163+
Map<String, Object> simpleText = new HashMap<>();
164+
simpleText.put("text", specMeal);
165+
166+
Map<String, Object> simpleTextWrapper = new HashMap<>();
167+
simpleTextWrapper.put("simpleText", simpleText);
168+
169+
List<Object> outputs = new ArrayList(1);
170+
outputs.add(simpleTextWrapper);
171+
172+
Map<String, Object> template = new HashMap<>();
173+
template.put("outputs", outputs);
174+
175+
Map<String, Object> responseBody = new HashMap<>();
176+
responseBody.put("version", "2.0");
177+
responseBody.put("template", template);
70178

71-
return "saved"; // Responsebody 넣어서 보내는거 해보세요
179+
ObjectMapper objectMapper = new ObjectMapper();
180+
String result = objectMapper.writeValueAsString(responseBody);
181+
182+
return result;
72183
}
73184

74185
@PostMapping("/speceng")
75-
public @ResponseBody String readSpecEngMeal() {
186+
public @ResponseBody String readSpecEngMeal() throws JsonProcessingException {
76187
// input : 날짜요일내일 + 아점저 + 1/2학
77-
// output : 해당 식단 찾아 카톡 서버맞게 JSON으로
188+
// output : 영어 식단이 포함된 JSON (단, JSON은 카톡 서버가 받을 수 있는 형식이여야 함.)
189+
190+
// TODO 아래 기능 구현해 주세요.
191+
String specMeal = mealService.getSpecEngMeal();
192+
193+
// Response Body Construct
194+
// TODO 더 좋은 방식이 있으면 구현해 주세요. ex) dto 이용.
195+
// const responseBody = {
196+
// version: "2.0",
197+
// template: {
198+
// outputs: [
199+
// {
200+
// simpleText: {
201+
// text: specMeal
202+
// }
203+
// }
204+
// ]
205+
// }
206+
// };
207+
208+
Map<String, Object> simpleText = new HashMap<>();
209+
simpleText.put("text", specMeal);
210+
211+
Map<String, Object> simpleTextWrapper = new HashMap<>();
212+
simpleTextWrapper.put("simpleText", simpleText);
213+
214+
List<Object> outputs = new ArrayList(1);
215+
outputs.add(simpleTextWrapper);
216+
217+
Map<String, Object> template = new HashMap<>();
218+
template.put("outputs", outputs);
219+
220+
Map<String, Object> responseBody = new HashMap<>();
221+
responseBody.put("version", "2.0");
222+
responseBody.put("template", template);
223+
224+
ObjectMapper objectMapper = new ObjectMapper();
225+
String result = objectMapper.writeValueAsString(responseBody);
78226

79-
return "saved"; // Responsebody 넣어서 보내는거 해보세요
227+
return result;
80228
}
81229
}

src/main/java/com/example/helper/dto/MealWrapper.java

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package com.example.helper.dto;
22

3-
import lombok.Getter;
4-
import lombok.Setter;
5-
import lombok.ToString;
3+
import jakarta.persistence.Column;
4+
import lombok.*;
65

76
@ToString
87
@Getter
98
@Setter
9+
@AllArgsConstructor
10+
@NoArgsConstructor
1011
public class Mealdto {
11-
public Mealdto(String title, String meal_date, String kind_of_meal, String menu) {
12-
this.title = title;
13-
this.meal_date = meal_date;
14-
this.kind_of_meal = kind_of_meal;
15-
this.menu = menu;
16-
}
17-
private String title;
18-
private String meal_date;
19-
private String kind_of_meal;
12+
private Integer bldgType;
13+
private Integer langType;
14+
private Integer dateType;
15+
private Integer kindType;
16+
private String bldg;
17+
private String date;
18+
private String kind;
2019
private String menu;
20+
private String special;
2121
}

src/main/java/com/example/helper/entity/Meal.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,22 @@ public class Meal {
2121
@Id
2222
@GeneratedValue
2323
private Long id;
24-
2524
@Column
26-
private String title;
27-
25+
private Integer bldgType;
2826
@Column
29-
private String meal_date;
30-
27+
private Integer langType;
3128
@Column
32-
private String kind_of_meal;
33-
29+
private Integer dateType;
30+
@Column
31+
private Integer kindType;
32+
@Column
33+
private String bldg;
34+
@Column
35+
private String date;
36+
@Column
37+
private String kind;
3438
@Column
3539
private String menu;
40+
@Column
41+
private String special;
3642
}

src/main/java/com/example/helper/repository/MealRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public interface MealRepository {
88
Meal save(Meal meal);
99

10-
Optional<Meal> findIdByDateTitleKind(String title, String meal_date, String kind_of_meal);
10+
Optional<Meal> findByPk(Integer bldgType, Integer langType, Integer dateType, Integer kindType, String date);
1111

1212
Optional<Meal> findById(Long mealId);
1313

src/main/java/com/example/helper/repository/SqlMealRepository.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ public Meal save(Meal meal) {
2020
}
2121

2222
@Override
23-
public Optional<Meal> findIdByDateTitleKind(String title, String meal_date, String kind_of_meal) {
24-
List<Meal> result = em.createQuery("select m from Member m where m.title = :title and m.meal_date = :meal_date and m.kind_of_meal = :kind_of_meal", Meal.class)
25-
.setParameter("title", title)
26-
.setParameter("meal_date", meal_date)
27-
.setParameter("kind_of_meal", kind_of_meal)
23+
public Optional<Meal> findByPk(Integer bldgType, Integer langType, Integer dateType, Integer kindType, String date) {
24+
List<Meal> result = em.createQuery("select m from Meal m where " +
25+
"m.bldgType = :bldgType and m.langType = :langType and m.dateType = :dateType " +
26+
"and m.kindType = :kindType and m.date = :date", Meal.class)
27+
.setParameter("bldgType", bldgType)
28+
.setParameter("langType", langType)
29+
.setParameter("dateType", dateType)
30+
.setParameter("kindType", kindType)
31+
.setParameter("date", date)
2832
.getResultList();
2933
return result.stream().findAny();
3034
}

0 commit comments

Comments
 (0)