Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 카테고리 내에서 메뉴 정렬 #66

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 //최근 추가된 메뉴순
}
Loading