Skip to content

Commit f85be61

Browse files
committed
[Refactor] 식단 메뉴 조회 리팩토링
1 parent 584f432 commit f85be61

File tree

5 files changed

+170
-256
lines changed

5 files changed

+170
-256
lines changed

src/main/java/com/example/Jinus/controller/CafeteriaController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public CafeteriaController(
5353
@PostMapping("/api/spring/cafeteria")
5454
public String getCafeteria(@RequestBody RequestDto requestDto) {
5555
String userId = requestDto.getUserRequest().getUser().getId();
56-
int campusId = userService.getCampusId(userId);
56+
int campusId = userService.getUserCampusId(userId);
5757
int userWantedCampusId = requestDto.getAction().getClientExtra().getSys_campus_id();
5858
// 더보기를 누른 경우
5959
if (userWantedCampusId == -1) {

src/main/java/com/example/Jinus/controller/DishController.java

Lines changed: 13 additions & 246 deletions
Original file line numberDiff line numberDiff line change
@@ -29,271 +29,38 @@
2929
public class DishController {
3030
private static final Logger logger = LoggerFactory.getLogger(NoticeController.class);
3131
private final CafeteriaService cafeteriaService;
32-
private final UserService userService;
3332
private final DepartmentService departmentService;
3433
private final CollegeService collegeService;
3534
private final CafeteriaDietService cafeteriaDietService;
3635
private final CampusService campusService;
3736

3837
public DishController(
3938
CafeteriaService cafeteriaService,
40-
UserService userService,
4139
DepartmentService departmentService,
4240
CollegeService collegeService,
4341
CafeteriaDietService cafeteriaDietService, CampusService campusService) {
4442
this.cafeteriaService = cafeteriaService;
45-
this.userService = userService;
4643
this.departmentService = departmentService;
4744
this.collegeService = collegeService;
4845
this.cafeteriaDietService = cafeteriaDietService;
4946
this.campusService = campusService;
5047
}
5148
@PostMapping("/api/spring/dish")
5249
public String handleRequest(@RequestBody RequestDto requestDto) {
53-
// params 받아오기
54-
// 날짜 (오늘, 내일)
55-
String paramsDate = requestDto.getAction().getParams().getSys_date();
56-
// 캠퍼스 이름 (동의어 사용)
57-
String paramsCampusName = requestDto.getAction().getParams().getSys_campus_name();
58-
// 식당 이름 (동의어 사용)
59-
String paramsCafeteriaName = requestDto.getAction().getParams().getSys_cafeteria_name();
60-
// 식당 이름 (원본)
61-
String originCafeteriaName = requestDto.getAction().getDetailParams().getSys_cafeteria_name().getOrigin();
62-
63-
int campusId = getUserCampusId(requestDto);
64-
int cafeteriaId = cafeteriaService.getCafeteriaIdByCampusId(paramsCafeteriaName, campusId);
65-
// 캠퍼스 값이 존재하는 경우
66-
if (paramsCampusName != null) {
67-
campusId = campusService.getCampusId(paramsCampusName);
68-
cafeteriaId = cafeteriaService.getCafeteriaIdByCampusId(paramsCafeteriaName, campusId);
69-
} else if (campusId != 0) { // 캠퍼스 값이 없는 경우 & 학과 인증된 경우
70-
paramsCampusName = campusService.getCampusName(campusId);
71-
}
72-
// 학과 인증 하지않은 경우
73-
else if (paramsCafeteriaName.equals("교직원식당") || paramsCafeteriaName.equals("학생식당")) { // 식당 중복된 경우
74-
return SimpleTextResponse.simpleTextResponse("캠퍼스와 함께 알려줘 !");
75-
} else {
76-
cafeteriaId = cafeteriaService.getCafeteriaIdByName(paramsCafeteriaName);
77-
campusId = cafeteriaService.getCampusIdByName(paramsCafeteriaName);
78-
paramsCampusName = campusService.getCampusName(campusId);
79-
}
80-
81-
// 시점 (아침, 점심, 저녁)
82-
String paramsPeriod = requestDto.getAction().getParams().getSys_time_period();
83-
84-
// 때에 맞는 식단 조회를 위한 시간 및 날짜 조회
85-
// 현재 날짜 시간
86-
String currentTime = getCurrentTime(); // 16:43:12
87-
String currentDay = getDay(currentTime); // 오늘, 내일
88-
89-
// 현재 식사 시기(사용자가 입력하지 않았을 경우)
90-
if (paramsPeriod == null) {
91-
paramsPeriod = getPeriodOfDay(currentTime); // 아침, 점심, 저녁
92-
}
93-
94-
// 사용자의 입력값이 존재하는 경우
95-
// 날짜
96-
String detailParamsDate = "";
97-
if (paramsDate.equals("sys.date")) {
98-
detailParamsDate = requestDto.getAction().getDetailParams().getSys_date().getOrigin();
99-
}
100-
101-
String currentDate = getCurrentDate(detailParamsDate); // 2024-05-13
102-
103-
// 식사 시기
104-
String detailParamsPeriod = "";
105-
if (paramsPeriod.equals("sys.time.period")) {
106-
detailParamsPeriod = requestDto.getAction().getDetailParams().getSys_time_period().getOrigin();
107-
}
108-
109-
// 캠퍼스에 해당 식당이 존재하지 않는 경우 예외처리
110-
if (cafeteriaId == 0) {
111-
// 칠암캠이면 한번더조회
112-
if (campusId == 2) {
113-
paramsCampusName = "칠암(의대,간호대)캠퍼스";
114-
cafeteriaId = 3;
115-
} else {
116-
String msg = paramsCampusName + "에 " + originCafeteriaName +"이(가) 존재하지 않아." +
117-
" 다시 입력해줘!";
118-
return SimpleTextResponse.simpleTextResponse(msg);
119-
}
120-
}
121-
122-
return getCafeteriaData(detailParamsDate, detailParamsPeriod, currentDate,
123-
paramsPeriod, cafeteriaId, paramsCampusName, originCafeteriaName, currentDay);
124-
}
125-
126-
127-
// 사용자 발화문의 파라미터값에 따른 식단 데이터 조회
128-
public String getCafeteriaData(String detailParamsDate, String detailParamsPeriod,
129-
String currentDate, String paramsPeriod, int cafeteriaId,
130-
String paramsCampusName, String originCafeteriaName,
131-
String currentDay) {
132-
133-
HashMap<String, List<String>> categoryMenuMap; // 식단 map
134-
135-
// 사용자의 입력값 없는 경우(식당 블록 리턴하는 경우) -> paramsCampus 값은 항상 존재
136-
if (detailParamsDate.isEmpty() && detailParamsPeriod.isEmpty()) {
137-
categoryMenuMap = cafeteriaDietService.getCafeteriaDiet(LocalDate.parse(currentDate), paramsPeriod, cafeteriaId);
138-
139-
return responseMapping(cafeteriaId, categoryMenuMap,
140-
paramsCampusName, currentDay, originCafeteriaName, paramsPeriod, currentDate);
141-
142-
} else { // 사용자의 입력값 있는 경우(직접 입력하는 경우)
143-
// date값이 오늘이나 내일이 아닌 경우 -> 예외처리
144-
if (!detailParamsDate.isEmpty() && !(detailParamsDate.equals("오늘") || detailParamsDate.equals("낼") || detailParamsDate.equals("내일"))) {
145-
return SimpleTextResponse.simpleTextResponse("오늘과 내일의 식단만 조회할 수 있어");
146-
147-
} else if (!detailParamsDate.isEmpty() && !detailParamsPeriod.isEmpty()) { // 둘 다 입력된 경우
148-
categoryMenuMap = cafeteriaDietService.getCafeteriaDiet(LocalDate.parse(currentDate), detailParamsPeriod, cafeteriaId);
149-
150-
return responseMapping(cafeteriaId, categoryMenuMap,
151-
paramsCampusName, detailParamsDate, originCafeteriaName, detailParamsPeriod, currentDate);
152-
153-
} else if (!detailParamsDate.isEmpty()) { // date값만 입력된 경우
154-
categoryMenuMap = cafeteriaDietService.getCafeteriaDiet(LocalDate.parse(currentDate), paramsPeriod, cafeteriaId);
155-
156-
return responseMapping(cafeteriaId, categoryMenuMap,
157-
paramsCampusName, detailParamsDate, originCafeteriaName, paramsPeriod, currentDate);
158-
159-
} else { // period 값만 입력된 경우
160-
categoryMenuMap = cafeteriaDietService.getCafeteriaDiet(LocalDate.parse(currentDate), detailParamsPeriod, cafeteriaId);
161-
162-
return responseMapping(cafeteriaId, categoryMenuMap,
163-
paramsCampusName, currentDay, originCafeteriaName, detailParamsPeriod, currentDate);
164-
}
165-
}
166-
}
167-
168-
// 식단 return 블록 생성
169-
public String responseMapping(int cafeteriaId, HashMap<String, List<String>> categoryMenuMap,
170-
String campus, String day, String originCafeteriaName, String period, String currentDate) {
171-
String url = cafeteriaService.getCampusThumnail(cafeteriaId);
172-
ThumbnailDto thumbnailDto = new ThumbnailDto(url);
173-
174-
String title = "\uD83C\uDF71" + " " + originCafeteriaName + "(" + campus.substring(0, 2) + ")" + " 메뉴";
175-
String description = handleCafeteriaDiet(categoryMenuMap, period, currentDate);
176-
177-
List<ButtonDto> buttons = new ArrayList<>();
178-
Map<String, Object> extra = new HashMap<>();
179-
extra.put("cafeteriaId", cafeteriaId);
180-
extra.put("date", currentDate);
181-
extra.put("time", period);
182-
ButtonDto buttonDto = new ButtonDto("공유하기", "share");
183-
buttons.add(buttonDto);
184-
185-
BasicCardDto basicCardDto = new BasicCardDto(title, description, thumbnailDto, buttons);
186-
ComponentDto componentDto = new ComponentDto(basicCardDto);
187-
188-
List<ComponentDto> componentDtoList = new ArrayList<>();
189-
componentDtoList.add(componentDto);
190-
191-
List<QuickReplyDto> quickReplies = new ArrayList<>();
192-
if (period.equals("아침")) {
193-
quickReplies.add(new QuickReplyDto("점심", "message", campus+" "+originCafeteriaName+" "+day+" 점심 메뉴"));
194-
quickReplies.add(new QuickReplyDto("저녁", "message", campus+" "+originCafeteriaName+" "+day+" 저녁 메뉴"));
195-
} else if (period.equals("점심")) {
196-
quickReplies.add(new QuickReplyDto("아침", "message", campus+" "+originCafeteriaName+" "+day+" 아침 메뉴"));
197-
quickReplies.add(new QuickReplyDto("저녁", "message", campus+" "+originCafeteriaName+" "+day+" 저녁 메뉴"));
198-
} else {
199-
quickReplies.add(new QuickReplyDto("아침", "message", campus+" "+originCafeteriaName+" "+day+" 아침 메뉴"));
200-
quickReplies.add(new QuickReplyDto("점심", "message", campus+" "+originCafeteriaName+" "+day+" 점심 메뉴"));
201-
}
202-
203-
TemplateDto templateDto = new TemplateDto(componentDtoList, quickReplies);
204-
ResponseDto responseDto = new ResponseDto("2.0", templateDto);
205-
206-
return JsonUtils.toJsonResponse(responseDto);
207-
}
208-
209-
// 식단 메뉴 블록 내용 정리
210-
public String handleCafeteriaDiet(HashMap<String, List<String>> categoryMenuMap, String period, String currentDate) {
211-
String menuDescription = currentDate + " " + period + " 식단\n\n";
212-
213-
if (categoryMenuMap.isEmpty()) { // 메뉴가 존재하지 않는다면
214-
return menuDescription + "메뉴가 존재하지 않습니다.";
215-
} else { // 메뉴가 존재한다면
216-
for (Map.Entry<String, List<String>> entry : categoryMenuMap.entrySet()) {
217-
String categoryName = entry.getKey();
218-
List<String> cafeteriaDishes = entry.getValue();
219-
menuDescription = joinAllMenus(cafeteriaDishes, categoryName, menuDescription);
220-
}
221-
return menuDescription;
50+
String userId = requestDto.getUserRequest().getUser().getId(); // 유저 id
51+
String rawDate = requestDto.getAction().getDetailParams().getSys_date().getOrigin(); // 날짜 (오늘, 내일)
52+
String campusName = requestDto.getAction().getParams().getSys_campus_name(); // 캠퍼스 이름 (동의어 사용)
53+
String cafeteriaName = requestDto.getAction().getParams().getSys_cafeteria_name(); // 식당 이름 (동의어 사용)
54+
String rawPeriod = (requestDto.getAction().getDetailParams().getSys_time_period() != null)
55+
? requestDto.getAction().getDetailParams().getSys_time_period().getOrigin()
56+
: ""; //
57+
58+
// 유효성 검사
59+
if (!cafeteriaDietService.validateDate(rawDate)) {
60+
return SimpleTextResponse.simpleTextResponse("오늘과 내일의 식단만 조회할 수 있어!");
22261
}
223-
}
224-
225-
public String joinAllMenus(List<String> cafeteriaDishes, String categoryName, String menuDescription) {
226-
String coveredCategory = "[" + categoryName + "]"; // 카테고리 괄호로 감싸기
227-
String joinedMenu = String.join("\n", cafeteriaDishes); // 메뉴들 컴마로 연결
228-
229-
return menuDescription + coveredCategory + "\n" + joinedMenu + "\n\n";
230-
}
231-
232-
// 사용자의 캠퍼스 id 조회
233-
public int getUserCampusId(@RequestBody RequestDto requestDto) {
234-
String userId = requestDto.getUserRequest().getUser().getId();
235-
int campusId = userService.getCampusId(userId);
236-
237-
// user 없는 경우
238-
if (campusId == -1) {
239-
return 0;
240-
} else { // user 존재하는 경우
241-
return campusId;
242-
}
243-
}
24462

245-
// 조회할 날짜 찾는 함수
246-
public String getCurrentDate(String currentDay) {
247-
LocalDateTime currentDateTime = LocalDateTime.now(ZoneId.of("Asia/Seoul"));
248-
LocalDate currentDate = currentDateTime.toLocalDate(); // 현재 날짜
249-
250-
if (currentDay.isEmpty()) {
251-
currentDay = getDay(currentDateTime.toString().split("T")[1]); // 오늘, 내일을 결정하는 로직이 있는 함수
252-
}
253-
254-
if (currentDay.equals("오늘")) {
255-
return currentDate.toString();
256-
} else { // 내일
257-
LocalDate tomorrowDate = currentDate.plusDays(1);
258-
return tomorrowDate.toString();
259-
}
260-
}
261-
262-
// 현재 시간 출력 함수
263-
public String getCurrentTime() {
264-
LocalDateTime currentDateTime = LocalDateTime.now(ZoneId.of("Asia/Seoul"));
265-
String[] dateTimeParts = currentDateTime.toString().split("T");
266-
267-
String timePart = dateTimeParts[1]; // 시간 부분
268-
String[] timeSplt = timePart.split("\\.");
269-
270-
return timeSplt[0];
271-
}
272-
273-
// 시간 범위에 따라 오늘, 내일 판별하는 함수
274-
public static String getDay(String currentTime) {
275-
LocalTime time = LocalTime.parse(currentTime);
276-
277-
if (time.isAfter(LocalTime.parse("00:00:00")) && time.isBefore(LocalTime.parse("18:59:00"))) {
278-
return "오늘";
279-
} else {
280-
return "내일";
281-
}
282-
}
283-
284-
// 시간 범위에 따라 아침, 점심, 저녁을 판별하는 함수
285-
public static String getPeriodOfDay(String currentTime) {
286-
// 현재 시간을 LocalTime 객체로 변환
287-
LocalTime time = LocalTime.parse(currentTime);
288-
289-
if (time.isAfter(LocalTime.parse("00:00:00")) && time.isBefore(LocalTime.parse("09:30:00"))) {
290-
return "아침";
291-
} else if (time.isAfter(LocalTime.parse("09:30:00")) && time.isBefore(LocalTime.parse("13:30:00"))) {
292-
return "점심";
293-
} else if (time.isAfter(LocalTime.parse("13:30:00")) && time.isBefore(LocalTime.parse("19:00:00"))) {
294-
return "저녁";
295-
} else {
296-
return "아침";
297-
}
63+
String dishInfo = cafeteriaDietService.processRequest(userId, rawDate, campusName, cafeteriaName, rawPeriod);
64+
return dishInfo;
29865
}
29966
}

src/main/java/com/example/Jinus/service/UserService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public int getDepartmentId(String userId) {
3434
}
3535
}
3636

37-
public int getCampusId(String userId) {
37+
public int getUserCampusId(String userId) {
3838
UserEntity userEntity = userRepository.findById(userId).orElse(null);
3939
if (userEntity != null) {
4040
return userEntity.getCampusId();

0 commit comments

Comments
 (0)