Skip to content

Commit 005c031

Browse files
authored
Merge pull request #11 from gist-helper/san
San
2 parents c8a1cfb + d9bb5d5 commit 005c031

File tree

8 files changed

+221
-13
lines changed

8 files changed

+221
-13
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.example.helper.constant;
2+
3+
public enum Types {
4+
LANG_KOR(0),
5+
LANG_ENG(1),
6+
BLDG1_1ST(0),
7+
BLDG1_2ND(1),
8+
BLDG2_1ST(2),
9+
KIND_BREAKFAST(0),
10+
KIND_LUNCH(1),
11+
KIND_DINNER(2),
12+
KIND_LUNCH_CORNER(3);
13+
14+
private Integer type;
15+
16+
Types(Integer type) {
17+
this.type = type;
18+
}
19+
20+
public Integer getType() {
21+
return type;
22+
}
23+
}

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

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,48 @@
11
package com.example.helper.controller;
2+
import com.example.helper.dto.DateMealDto;
3+
import com.example.helper.dto.DateReqDto;
24
import com.example.helper.dto.Mealdto;
35
import com.example.helper.entity.Meal;
6+
import com.example.helper.service.DateMealService;
47
import com.example.helper.service.MealService;
58
import com.fasterxml.jackson.core.JsonProcessingException;
69
import com.fasterxml.jackson.databind.ObjectMapper;
710
import lombok.extern.slf4j.Slf4j;
811
import org.springframework.beans.BeanUtils;
912
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.http.HttpStatus;
1014
import org.springframework.web.bind.annotation.*;
1115

1216
import java.util.ArrayList;
1317
import java.util.HashMap;
1418
import java.util.List;
1519
import java.util.Map;
20+
import org.springframework.web.server.ResponseStatusException;
1621

1722

18-
@RestController
23+
@RestController // @Controller + @ResponseBody. return이 view가 아닌, http body에 직접 쓰여짐.
1924
@RequestMapping(path = "/meals", produces = "application/json;charset=UTF-8")
2025
@Slf4j
2126
public class MealController {
2227

2328
@Autowired
2429
private MealService mealService;
2530

31+
@Autowired
32+
private DateMealService dateMealService;
33+
2634
@GetMapping("/all")
2735
public String hello() {
2836
return "Hello HELPERs. 초기 세팅 완료.";
2937
}
3038

3139
@PostMapping("/test")
32-
public @ResponseBody void test(String testStr) {
40+
public void test(String testStr) {
3341
log.info(testStr);
3442
}
43+
3544
@PostMapping("/create")
36-
public @ResponseBody String createMeal(@RequestBody Mealdto mealDto) {
45+
public String createMeal(@RequestBody Mealdto mealDto) {
3746
// input : 식단 json
3847
// output : None
3948

@@ -48,7 +57,7 @@ public String hello() {
4857
}
4958

5059
@PostMapping("/kor")
51-
public @ResponseBody String readKorMeal() throws JsonProcessingException {
60+
public String readKorMeal() throws JsonProcessingException {
5261
// input : None (먼저 서버에서 현재 시간 측정)
5362
// output : 한국어 식단이 포함된 JSON (단, JSON은 카톡 서버가 받을 수 있는 형식이여야 함.)
5463

@@ -70,7 +79,7 @@ public String hello() {
7079
// }
7180
// };
7281

73-
Map<String, Object> simpleText = new HashMap<>();
82+
Map<String, Object> simpleText = new HashMap<>();
7483
simpleText.put("text", nowMeal);
7584

7685
Map<String, Object> simpleTextWrapper = new HashMap<>();
@@ -93,7 +102,7 @@ public String hello() {
93102
}
94103

95104
@PostMapping("/eng")
96-
public @ResponseBody String readEngMeal() throws JsonProcessingException {
105+
public String readEngMeal() throws JsonProcessingException {
97106
// input : None (먼저 서버에서 현재 시간 측정)
98107
// output : 영어 식단이 포함된 JSON (단, JSON은 카톡 서버가 받을 수 있는 형식이여야 함.)
99108

@@ -115,7 +124,7 @@ public String hello() {
115124
// }
116125
// };
117126

118-
Map<String, Object> simpleText = new HashMap<>();
127+
Map<String, Object> simpleText = new HashMap<>();
119128
simpleText.put("text", nowMeal);
120129

121130
Map<String, Object> simpleTextWrapper = new HashMap<>();
@@ -138,7 +147,7 @@ public String hello() {
138147
}
139148

140149
@PostMapping("/speckor")
141-
public @ResponseBody String readSpecKorMeal() throws JsonProcessingException {
150+
public String readSpecKorMeal() throws JsonProcessingException {
142151
// input : 날짜요일내일 + 아점저 + 1/2학
143152
// output : 한국어 식단이 포함된 JSON (단, JSON은 카톡 서버가 받을 수 있는 형식이여야 함.)
144153

@@ -160,7 +169,7 @@ public String hello() {
160169
// }
161170
// };
162171

163-
Map<String, Object> simpleText = new HashMap<>();
172+
Map<String, Object> simpleText = new HashMap<>();
164173
simpleText.put("text", specMeal);
165174

166175
Map<String, Object> simpleTextWrapper = new HashMap<>();
@@ -183,7 +192,7 @@ public String hello() {
183192
}
184193

185194
@PostMapping("/speceng")
186-
public @ResponseBody String readSpecEngMeal() throws JsonProcessingException {
195+
public String readSpecEngMeal() throws JsonProcessingException {
187196
// input : 날짜요일내일 + 아점저 + 1/2학
188197
// output : 영어 식단이 포함된 JSON (단, JSON은 카톡 서버가 받을 수 있는 형식이여야 함.)
189198

@@ -205,7 +214,7 @@ public String hello() {
205214
// }
206215
// };
207216

208-
Map<String, Object> simpleText = new HashMap<>();
217+
Map<String, Object> simpleText = new HashMap<>();
209218
simpleText.put("text", specMeal);
210219

211220
Map<String, Object> simpleTextWrapper = new HashMap<>();
@@ -226,4 +235,32 @@ public String hello() {
226235

227236
return result;
228237
}
238+
239+
// FE쪽에서 query에 담아주면 아래처럼 dto 객체 하나만 req로 받으면 되서 코드 깔끔함.
240+
// DateMealDto dateMealDtoList = dateMealService.getDateMeal(dateReqDto);
241+
// 근데 FE에서 보낼때 parameter 일일이 적기 귀찮으니 pathvariable로 받아서 처리
242+
@GetMapping("/date/{year}/{month}/{day}/{bldgType}/{langType}")
243+
public DateMealDto DateMealRead(
244+
@PathVariable("langType") Integer langType,
245+
@PathVariable("bldgType") Integer bldgType,
246+
@PathVariable("year") Integer year,
247+
@PathVariable("month") Integer month,
248+
@PathVariable("day") Integer day) {
249+
250+
DateReqDto dateReqDto = DateReqDto.builder()
251+
.langType(langType)
252+
.bldgType(bldgType)
253+
.year(year.toString())
254+
.month(month.toString())
255+
.date(day.toString())
256+
.build();
257+
258+
try {
259+
DateMealDto dateMealDto = dateMealService.getDateMenus(dateReqDto);
260+
return dateMealDto;
261+
} catch (IllegalStateException e) {
262+
throw new ResponseStatusException(
263+
HttpStatus.NOT_ACCEPTABLE, e.getMessage());
264+
}
265+
}
229266
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.helper.dto;
2+
3+
import lombok.AccessLevel;
4+
import lombok.Builder;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
8+
// https://dev-jhl.tistory.com/entry/Lombok-올바른-Lombok-사용법-Builder
9+
@Getter
10+
@NoArgsConstructor(access = AccessLevel.PROTECTED)
11+
public class DateMealDto {
12+
private String breakfast;
13+
private String lunch;
14+
private String lunch_corner;
15+
private String dinner;
16+
17+
@Builder
18+
public DateMealDto(String breakfast, String lunch, String lunch_corner, String dinner) {
19+
this.breakfast = breakfast;
20+
this.lunch = lunch;
21+
this.lunch_corner = lunch_corner;
22+
this.dinner = dinner;
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.example.helper.dto;
2+
3+
import lombok.AccessLevel;
4+
import lombok.Builder;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
import lombok.ToString;
8+
9+
@Getter
10+
@ToString
11+
@NoArgsConstructor(access = AccessLevel.PROTECTED)
12+
public class DateReqDto {
13+
private Integer langType;
14+
private Integer bldgType;
15+
private String year;
16+
private String month;
17+
private String date;
18+
19+
@Builder
20+
public DateReqDto(Integer langType, Integer bldgType, String year, String month, String date) {
21+
this.langType = langType;
22+
this.bldgType = bldgType;
23+
this.year = year;
24+
this.month = month;
25+
this.date = date;
26+
}
27+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ public Optional<Meal> findByPk(Integer bldgType, Integer langType, Integer dateT
3333
return result.stream().findAny();
3434
}
3535

36+
public Optional<Meal> findByDate(Integer bldgType, Integer langType, Integer kindType, String date) {
37+
List<Meal> result = em.createQuery("select m from Meal m where " +
38+
"m.bldgType = :bldgType and m.langType = :langType and m.kindType = :kindType " +
39+
"and m.date = :date", Meal.class)
40+
.setParameter("bldgType", bldgType)
41+
.setParameter("langType", langType)
42+
.setParameter("kindType", kindType)
43+
.setParameter("date", date)
44+
.getResultList();
45+
return result.stream().findAny();
46+
}
47+
3648
@Override
3749
public Optional<Meal> findById(Long mealId) {
3850
Meal meal = em.find(Meal.class, mealId);
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.example.helper.service;
2+
3+
import com.example.helper.constant.Types;
4+
import com.example.helper.dto.DateMealDto;
5+
import com.example.helper.dto.DateReqDto;
6+
import com.example.helper.entity.Meal;
7+
import com.example.helper.repository.SqlMealRepository;
8+
import java.util.Optional;
9+
import lombok.extern.slf4j.Slf4j;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.stereotype.Service;
12+
import org.springframework.transaction.annotation.Transactional;
13+
14+
@Service
15+
@Transactional
16+
@Slf4j
17+
public class DateMealService {
18+
19+
@Autowired
20+
private SqlMealRepository sqlMealRepository;
21+
22+
public DateMealDto getDateMenus(DateReqDto dateReqDto) {
23+
DateMealDto result = DateMealDto.builder()
24+
.breakfast(getMenuFromMeal(dateReqDto, Types.KIND_BREAKFAST.getType()))
25+
.lunch(getMenuFromMeal(dateReqDto, Types.KIND_LUNCH.getType()))
26+
.dinner(getMenuFromMeal(dateReqDto, Types.KIND_DINNER.getType()))
27+
.lunch_corner((getMenuFromMeal(dateReqDto, Types.KIND_LUNCH_CORNER.getType())))
28+
.build();
29+
30+
// TODO: exception handling(target data is not in DB)
31+
if (checkMenusEmpty(result)) {
32+
throw new IllegalStateException("해당 날짜의 식단이 존재하지 않습니다.");
33+
}
34+
return result;
35+
}
36+
37+
public String getMenuFromMeal(DateReqDto dateReqDto, Integer kindType) {
38+
String result = "";
39+
40+
// DB에는 KIND_TYPE이 0,1,2만 존재. 따라서 KIND_LUNCH_CORNER는 KIND_LUNCH로 찾아서 getSpecial해야함.
41+
Integer temp = kindType; // Integer는 immutable이므로 '='로 복사가능
42+
if (kindType.equals(Types.KIND_LUNCH_CORNER.getType())) {
43+
temp = Types.KIND_LUNCH.getType();
44+
}
45+
46+
Optional<Meal> meal = sqlMealRepository.findByDate(
47+
dateReqDto.getBldgType(),
48+
dateReqDto.getLangType(),
49+
temp,
50+
conv2DateStr(dateReqDto.getYear(), dateReqDto.getMonth(), dateReqDto.getDate()));
51+
52+
if (meal.isPresent()) {
53+
result = meal.get().getMenu();
54+
if (kindType.equals(Types.KIND_LUNCH_CORNER.getType())) {
55+
result = meal.get().getSpecial();
56+
}
57+
}
58+
return result;
59+
}
60+
61+
public String conv2DateStr(String year, String month, String date) {
62+
String result = "";
63+
result = year + "-" + padZero(month) + "-" + padZero(date);
64+
return result;
65+
}
66+
67+
public String padZero(String str) {
68+
String result = "";
69+
if (str.length() == 1) {
70+
result = "0" + str;
71+
} else {
72+
result = str;
73+
}
74+
return result;
75+
}
76+
77+
public boolean checkMenusEmpty(DateMealDto dateMealDto) {
78+
return dateMealDto.getBreakfast().isEmpty() &&
79+
dateMealDto.getLunch().isEmpty() &&
80+
dateMealDto.getDinner().isEmpty() &&
81+
dateMealDto.getLunch_corner().isEmpty();
82+
}
83+
}

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

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

3+
import com.example.helper.dto.DateMealDto;
4+
import com.example.helper.dto.DateReqDto;
35
import com.example.helper.entity.Meal;
46
import com.example.helper.repository.MealRepository;
57
import com.example.helper.repository.SqlMealRepository;
8+
import java.util.List;
69
import org.springframework.beans.factory.annotation.Autowired;
710
import org.springframework.stereotype.Service;
811
import org.springframework.transaction.annotation.Transactional;
@@ -51,5 +54,4 @@ public String getSpecEngMeal() {
5154
//TODO sqlMealRepository.findSpecEngMeal
5255
return "2023-01-27 Breakfast\n\nStudent Union Bldg.2 1st floor\n\nWhite rice*Seasoned rice with seaweed\n";
5356
}
54-
5557
}

src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/helper_db
33
spring.datasource.username=helper
44
spring.datasource.password=12345678
55
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
6-
spring.jpa.hibernate.ddl-auto=create
6+
spring.jpa.hibernate.ddl-auto=validate
77

88
spring.jpa.show-sql=true
99
spring.jpa.properties.hibernate.format_sql=true

0 commit comments

Comments
 (0)