Skip to content

Commit

Permalink
Merge pull request #66 from 28th-meetup/feat/menuFilter
Browse files Browse the repository at this point in the history
feat: 카테고리 내에서 메뉴 정렬
  • Loading branch information
summit45 committed Nov 21, 2023
2 parents 7af1a97 + 336df16 commit 1c474d3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
11 changes: 9 additions & 2 deletions src/main/java/com/kusitms/jipbap/food/FoodController.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,16 @@ public CommonResponse<HomeResponseDto> getInfoFromHome(@Auth AuthInfo authInfo)
@Operation(summary = "특정 카테고리에 속하는 메뉴 조회하기")
@GetMapping("/category/{categoryId}")
@ResponseStatus(HttpStatus.OK)
public CommonResponse<List<FoodPreviewResponse>> getFoodByCategory(@Auth AuthInfo authInfo, @PathVariable Long categoryId) {
return new CommonResponse<>(foodService.getFoodByCategory(authInfo, categoryId));
public CommonResponse<List<FoodPreviewResponse>> getFoodByCategory(
@Auth AuthInfo authInfo,
@PathVariable Long categoryId,
@RequestParam(required = false, defaultValue = "RECOMMENDED") SortingType sorting) {
return new CommonResponse<>(foodService.getFoodByCategory(authInfo, categoryId, sorting));
}

}





42 changes: 35 additions & 7 deletions src/main/java/com/kusitms/jipbap/food/FoodService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -250,14 +247,19 @@ private List<FoodPreviewResponse> getLatestSellingFoodByRegion(Long globalRegion
// return latestSellingFoodResponseList;
}

public List<FoodPreviewResponse> 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<FoodPreviewResponse> 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<Food> foodList = foodRepository.findByStoreGlobalRegionAndCategory(globalRegion, category);

System.out.println("sortingType" + sortingType);
foodList = sortFoodList(foodList, sortingType);

List<FoodPreviewResponse> foodDtoList = foodList.stream()
.map(food -> new FoodPreviewResponse(
food.getId(),
Expand All @@ -274,4 +276,30 @@ public List<FoodPreviewResponse> getFoodByCategory(AuthInfo authInfo, Long categ
return foodDtoList;
}

private List<Food> sortFoodList(List<Food> 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;
}

}
10 changes: 10 additions & 0 deletions src/main/java/com/kusitms/jipbap/food/dto/SortingType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.kusitms.jipbap.food.dto;

public enum SortingType {
RECOMMENDED, //추천순
REVIEW_HIGH, //후기많순
RATING_HIGH, //평점높은순
PRICE_HIGH, //가격높은순
PRICE_LOW, //가격낮은순
RECENTLY_ADDED //최근 추가된 메뉴순
}

0 comments on commit 1c474d3

Please sign in to comment.