diff --git a/src/main/java/com/kusitms/jipbap/food/FoodController.java b/src/main/java/com/kusitms/jipbap/food/FoodController.java index d441b01..6bf55c9 100644 --- a/src/main/java/com/kusitms/jipbap/food/FoodController.java +++ b/src/main/java/com/kusitms/jipbap/food/FoodController.java @@ -64,9 +64,16 @@ public CommonResponse getInfoFromHome(@Auth AuthInfo authInfo) @Operation(summary = "특정 카테고리에 속하는 메뉴 조회하기") @GetMapping("/category/{categoryId}") @ResponseStatus(HttpStatus.OK) - public CommonResponse> getFoodByCategory(@Auth AuthInfo authInfo, @PathVariable Long categoryId) { - return new CommonResponse<>(foodService.getFoodByCategory(authInfo, categoryId)); + public CommonResponse> getFoodByCategory( + @Auth AuthInfo authInfo, + @PathVariable Long categoryId, + @RequestParam(required = false, defaultValue = "RECOMMENDED") SortingType sorting) { + return new CommonResponse<>(foodService.getFoodByCategory(authInfo, categoryId, sorting)); } + } + + + diff --git a/src/main/java/com/kusitms/jipbap/food/FoodService.java b/src/main/java/com/kusitms/jipbap/food/FoodService.java index 9b5835f..f4ef23b 100644 --- a/src/main/java/com/kusitms/jipbap/food/FoodService.java +++ b/src/main/java/com/kusitms/jipbap/food/FoodService.java @@ -30,10 +30,7 @@ import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; @@ -250,14 +247,19 @@ private List getLatestSellingFoodByRegion(Long globalRegion // return latestSellingFoodResponseList; } - public List getFoodByCategory(AuthInfo authInfo, Long categoryId){ - User user = userRepository.findByEmail(authInfo.getEmail()).orElseThrow(()-> new UserNotFoundException("유저 정보가 존재하지 않습니다.")); - Category category = categoryRepository.findById(categoryId).orElseThrow(()-> new CategoryNotExistsException("해당 카테고리 Id는 유효하지 않습니다.")); + public List getFoodByCategory(AuthInfo authInfo, Long categoryId, SortingType sortingType){ + User user = userRepository.findByEmail(authInfo.getEmail()) + .orElseThrow(()-> new UserNotFoundException("유저 정보가 존재하지 않습니다.")); + Category category = categoryRepository.findById(categoryId) + .orElseThrow(()-> new CategoryNotExistsException("해당 카테고리 Id는 유효하지 않습니다.")); GlobalRegion globalRegion = user.getGlobalRegion(); List foodList = foodRepository.findByStoreGlobalRegionAndCategory(globalRegion, category); + System.out.println("sortingType" + sortingType); + foodList = sortFoodList(foodList, sortingType); + List foodDtoList = foodList.stream() .map(food -> new FoodPreviewResponse( food.getId(), @@ -274,4 +276,30 @@ public List getFoodByCategory(AuthInfo authInfo, Long categ return foodDtoList; } + private List sortFoodList(List foodList, SortingType sortingType) { + switch (sortingType) { + case REVIEW_HIGH: + foodList.sort(Comparator.comparingLong(food -> -food.getStore().getReviewCount())); + break; + case RATING_HIGH: + foodList.sort(Comparator.comparingDouble(food -> -food.getStore().getAvgRate())); + break; + case PRICE_HIGH: + foodList.sort(Comparator.comparingLong(Food::getDollarPrice).reversed()); + break; + case PRICE_LOW: + foodList.sort(Comparator.comparingLong(Food::getDollarPrice)); + break; + case RECENTLY_ADDED: + foodList.sort(Comparator.comparing(Food::getCreatedAt).reversed()); + break; + default: + // 기본은 추천 순으로 정렬 + // 추천 순으로 정렬할 기준이 없다면 아무 처리도 하지 않습니다. + foodList.sort(Comparator.comparingLong(Food::getRecommendCount).reversed()); + break; + } + return foodList; + } + } diff --git a/src/main/java/com/kusitms/jipbap/food/dto/SortingType.java b/src/main/java/com/kusitms/jipbap/food/dto/SortingType.java new file mode 100644 index 0000000..e8bf836 --- /dev/null +++ b/src/main/java/com/kusitms/jipbap/food/dto/SortingType.java @@ -0,0 +1,10 @@ +package com.kusitms.jipbap.food.dto; + +public enum SortingType { + RECOMMENDED, //추천순 + REVIEW_HIGH, //후기많순 + RATING_HIGH, //평점높은순 + PRICE_HIGH, //가격높은순 + PRICE_LOW, //가격낮은순 + RECENTLY_ADDED //최근 추가된 메뉴순 +} \ No newline at end of file