Skip to content

Commit 04f80be

Browse files
authored
Merge pull request #16 from gist-helper/specmealeng
[#15] Feature: specmenu 기능 구현 with test
2 parents 7aa3c14 + 231a5d9 commit 04f80be

File tree

6 files changed

+196
-15
lines changed

6 files changed

+196
-15
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public enum SpecMealInputsEng {
1111
THR("Thr", Types.DATE_THR.getType()),
1212
FRI("Fri", Types.DATE_FRI.getType()),
1313
SAT("Sat", Types.DATE_SAT.getType()),
14-
SUM("Sun", Types.DATE_SUN.getType()),
14+
SUN("Sun", Types.DATE_SUN.getType()),
1515
DAY_1("st"),
1616
DAY_2("nd"),
1717
DAY_3("rd"),

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ public enum Types {
1010
KIND_LUNCH(1),
1111
KIND_DINNER(2),
1212
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);
13+
DATE_MON(1),
14+
DATE_TUE(2),
15+
DATE_WED(3),
16+
DATE_THR(4),
17+
DATE_FRI(5),
18+
DATE_SAT(6),
19+
DATE_SUN(7);
2020

2121
private Integer type;
2222

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ public String createMeal(@RequestBody Mealdto mealDto) {
5050
BeanUtils.copyProperties(mealDto, meal);
5151

5252
// DB Save
53-
Long saved = mealService.mealCreate(meal);
54-
55-
return "saved";
53+
try {
54+
Long saved = mealService.mealCreate(meal);
55+
return "saved";
56+
} catch (IllegalStateException e) {
57+
throw new ResponseStatusException(
58+
HttpStatus.NOT_ACCEPTABLE, e.getMessage());
59+
}
5660
}
5761

5862
@PostMapping("/kor")

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.example.helper.entity;
22

3+
import com.example.helper.constant.Types;
34
import jakarta.persistence.Column;
45
import jakarta.persistence.Entity;
56
import jakarta.persistence.GeneratedValue;
@@ -45,9 +46,15 @@ public String generateMenu() {
4546
menu += this.date + " " + this.kind + "\n\n";
4647
menu += this.bldg + "\n\n";
4748
menu += this.menu;
48-
if(kindType == 1) {
49-
menu += "\n\\코너\\\n";
50-
menu += this.special;
49+
if(kindType == Types.KIND_LUNCH.getType()) {
50+
if(langType == Types.LANG_KOR.getType()) {
51+
menu += "\n\\코너\\\n";
52+
menu += this.special;
53+
}
54+
else if(langType == Types.LANG_ENG.getType()) {
55+
menu += "\n\\Coner\\\n";
56+
menu += this.special;
57+
}
5158
}
5259
return menu;
5360
}

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

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

33
import com.example.helper.constant.Messages;
4+
import com.example.helper.constant.SpecMealInputsEng;
45
import com.example.helper.constant.SpecMealInputsKor;
56
import com.example.helper.constant.Types;
67
import com.example.helper.dto.DateMealDto;
@@ -11,6 +12,8 @@
1112
import com.fasterxml.jackson.databind.ObjectMapper;
1213
import lombok.extern.slf4j.Slf4j;
1314

15+
import java.text.ParseException;
16+
import java.text.SimpleDateFormat;
1417
import java.util.List;
1518
import org.springframework.beans.factory.annotation.Autowired;
1619
import org.springframework.stereotype.Service;
@@ -120,12 +123,128 @@ else if((dateCustom.charAt(dateCustom.length() - 1) + "").equals(SpecMealInputsK
120123

121124
return "2023-01-27 조식\n\n제2학생회관1층\n\n흰밥*김가루양념밥\n";
122125
}
126+
public Boolean dateFormatValidation(String date) {
127+
Boolean ret = true;
128+
try {
129+
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
130+
sdf.setLenient(false);
131+
sdf.parse(date);
123132

133+
} catch(ParseException e) {
134+
ret = false;
135+
}
136+
return ret;
137+
}
124138
public String getSpecEngMeal(String dateCustom, String bld) {
125139
if (!specInputValidation(dateCustom, bld)) {
126140
return Messages.NO_MEAL_ENG.getMessages();
127141
}
128-
return "2023-01-27 Breakfast\n\nStudent Union Bldg.2 1st floor\n\nWhite rice*Seasoned rice with seaweed\n";
142+
LocalDateTime currentDateTime = LocalDateTime.now(ZoneId.of("Asia/Seoul"));
143+
144+
Integer year = currentDateTime.getYear();
145+
Integer month = currentDateTime.getMonth().getValue();
146+
Integer day = currentDateTime.getDayOfMonth();
147+
Integer currentDateNumber = currentDateTime.getDayOfWeek().getValue();
148+
149+
Integer dateCustomLen = dateCustom.length();
150+
if(dateCustom.equals(SpecMealInputsEng.TODAY.getInputs())) {
151+
// 오늘
152+
}
153+
else if(dateCustom.equals(SpecMealInputsEng.TOMORROW.getInputs())) {
154+
// 내일
155+
currentDateTime = currentDateTime.plusDays(1);
156+
year = currentDateTime.getYear();
157+
month = currentDateTime.getMonth().getValue();
158+
day = currentDateTime.getDayOfMonth();
159+
}
160+
else if(dateCustomLen > 2) {
161+
String dateCustomPostfix = dateCustom.substring(dateCustomLen - 2, dateCustomLen);
162+
if( dateCustomPostfix.equals(SpecMealInputsEng.DAY_1.getInputs()) ||
163+
dateCustomPostfix.equals(SpecMealInputsEng.DAY_2.getInputs()) ||
164+
dateCustomPostfix.equals(SpecMealInputsEng.DAY_3.getInputs()) ||
165+
dateCustomPostfix.equals(SpecMealInputsEng.DAY_OTHER.getInputs())) {
166+
// 특정날짜
167+
String dateCustomPrefix = (dateCustomLen == 3) ? dateCustom.substring(0, 1) : dateCustom.substring(0, 2);
168+
169+
try{
170+
day = Integer.valueOf(dateCustomPrefix);
171+
}
172+
catch (NumberFormatException ex){
173+
day = 0;
174+
}
175+
}
176+
else {
177+
//요일
178+
Integer dateNumber = 0;
179+
if(dateCustom.equals(SpecMealInputsEng.MON.getInputs())) {
180+
dateNumber = SpecMealInputsEng.MON.getInputValue();
181+
}
182+
else if(dateCustom.equals(SpecMealInputsEng.TUE.getInputs())) {
183+
dateNumber = SpecMealInputsEng.TUE.getInputValue();
184+
}
185+
else if(dateCustom.equals(SpecMealInputsEng.WED.getInputs())) {
186+
dateNumber = SpecMealInputsEng.WED.getInputValue();
187+
}
188+
else if(dateCustom.equals(SpecMealInputsEng.THR.getInputs())) {
189+
dateNumber = SpecMealInputsEng.THR.getInputValue();
190+
}
191+
else if(dateCustom.equals(SpecMealInputsEng.FRI.getInputs())) {
192+
dateNumber = SpecMealInputsEng.FRI.getInputValue();
193+
}
194+
else if(dateCustom.equals(SpecMealInputsEng.SAT.getInputs())) {
195+
dateNumber = SpecMealInputsEng.SAT.getInputValue();
196+
}
197+
else if(dateCustom.equals(SpecMealInputsEng.SUN.getInputs())) {
198+
dateNumber = SpecMealInputsEng.SUN.getInputValue();
199+
}
200+
201+
if(dateNumber == 0) {
202+
day = 0;
203+
}
204+
else {
205+
Integer dateDiff = dateNumber - currentDateNumber;
206+
Integer dateDiffAbs = (dateDiff > 0) ? dateDiff : dateDiff * -1;
207+
currentDateTime = (dateDiff > 0) ? currentDateTime.plusDays(dateDiffAbs) : currentDateTime.minusDays(dateDiffAbs);
208+
year = currentDateTime.getYear();
209+
month = currentDateTime.getMonth().getValue();
210+
day = currentDateTime.getDayOfMonth();
211+
}
212+
213+
}
214+
}
215+
216+
Integer kindType = -1;
217+
if(bld.equals(SpecMealInputsEng.BREAKFAST.getInputs())) {
218+
kindType = SpecMealInputsEng.BREAKFAST.getInputValue();
219+
}
220+
else if(bld.equals(SpecMealInputsEng.LUNCH.getInputs())) {
221+
kindType = SpecMealInputsEng.LUNCH.getInputValue();
222+
}
223+
else if(bld.equals(SpecMealInputsEng.DINNER.getInputs())) {
224+
kindType = SpecMealInputsEng.DINNER.getInputValue();
225+
}
226+
227+
if( (day < 1 && day > 31) || (kindType < 0 || kindType > 2)) {
228+
return Messages.NO_MEAL_ENG.getMessages();
229+
}
230+
231+
String date = year + "-";
232+
date += String.format("%02d", month) + "-";
233+
date += String.format("%02d", day) + "";
234+
235+
//형식이 잘못되면 Query 에서 NULL 을 반환한다.
236+
//if(!dateFormatValidation(date)) {
237+
// return Messages.NO_MEAL_ENG.getMessages();
238+
//}
239+
240+
Optional<Meal> result = sqlMealRepository.findByDate(Types.BLDG2_1ST.getType(), Types.LANG_ENG.getType(), kindType, date);
241+
242+
if(result.isEmpty()) {
243+
//throw new IllegalStateException(Messages.EXIST_MEAL_ERROR.getMessages());
244+
return Messages.NO_MEAL_ENG.getMessages();
245+
}
246+
Meal meal = result.get();
247+
return meal.generateMenu();
129248
}
130249

131250
public Map<String, Object> responseMeal(String menu) {

src/test/java/com/example/helper/service/MealServiceTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package com.example.helper.service;
22

3+
import com.example.helper.constant.SpecMealInputsEng;
34
import com.example.helper.repository.SqlMealRepository;
45
import jakarta.persistence.EntityManager;
56
import org.junit.jupiter.api.Test;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
69

710
import java.time.LocalDateTime;
811
import java.time.ZoneId;
12+
import java.util.ArrayList;
13+
import java.util.List;
914

15+
@SpringBootTest
1016
public class MealServiceTest {
17+
@Autowired
18+
private MealService mealService;
1119
@Test
1220
void getNowTest() {
1321
LocalDateTime currentDateTime = LocalDateTime.now(ZoneId.of("Asia/Seoul"));
@@ -39,4 +47,47 @@ else if(19 <= hour && hour < 24) {
3947
System.out.println(kindType);
4048
}
4149

50+
@Test
51+
void specEngTest() {
52+
List<String> dateCustomTestList = new ArrayList<>();
53+
for(int i = 1 ; i <= 31 ; i++) {
54+
String tc = i + "";
55+
if(i == 1) {
56+
tc += "st";
57+
}
58+
else if(i == 2) {
59+
tc += "nd";
60+
}
61+
else if(i == 3) {
62+
tc += "rd";
63+
}
64+
else {
65+
tc += "th";
66+
}
67+
dateCustomTestList.add(tc);
68+
}
69+
dateCustomTestList.add(SpecMealInputsEng.TODAY.getInputs());
70+
dateCustomTestList.add(SpecMealInputsEng.TOMORROW.getInputs());
71+
dateCustomTestList.add(SpecMealInputsEng.MON.getInputs());
72+
dateCustomTestList.add(SpecMealInputsEng.THR.getInputs());
73+
dateCustomTestList.add(SpecMealInputsEng.WED.getInputs());
74+
dateCustomTestList.add(SpecMealInputsEng.THR.getInputs());
75+
dateCustomTestList.add(SpecMealInputsEng.FRI.getInputs());
76+
dateCustomTestList.add(SpecMealInputsEng.SAT.getInputs());
77+
dateCustomTestList.add(SpecMealInputsEng.SUN.getInputs());
78+
79+
List<String> bldTestList = new ArrayList<>();
80+
bldTestList.add(SpecMealInputsEng.BREAKFAST.getInputs());
81+
bldTestList.add(SpecMealInputsEng.LUNCH.getInputs());
82+
bldTestList.add(SpecMealInputsEng.DINNER.getInputs());
83+
84+
for(String dateCustom : dateCustomTestList) {
85+
for(String bld : bldTestList) {
86+
String specMeal = mealService.getSpecEngMeal(dateCustom, bld);
87+
System.out.println(dateCustom + " " + bld);
88+
System.out.println(specMeal);
89+
}
90+
}
91+
}
92+
4293
}

0 commit comments

Comments
 (0)