diff --git a/src/main/java/zipdabang/server/converter/RecipeConverter.java b/src/main/java/zipdabang/server/converter/RecipeConverter.java index 373c76c..d4c4b8d 100644 --- a/src/main/java/zipdabang/server/converter/RecipeConverter.java +++ b/src/main/java/zipdabang/server/converter/RecipeConverter.java @@ -17,6 +17,8 @@ import javax.annotation.PostConstruct; import java.io.IOException; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; import java.util.stream.Collectors; @Slf4j @@ -80,6 +82,26 @@ public static RecipeResponseDto.RecipeListDto toPreviewRecipeDtoList(List> recipeLists, Member member) { + AtomicLong index = new AtomicLong(1); + + return RecipeResponseDto.SearchRecipePreviewListDto.builder() + .recipeList(recipeLists.stream() + .map(recipeList -> toSearchRecipePreviewByCategoryDto(index.getAndIncrement(), recipeList, member)) + .collect(Collectors.toList())) + .build(); + } + + private static RecipeResponseDto.SearchRecipePreviewByCategoryDto toSearchRecipePreviewByCategoryDto(Long index, List recipeList, Member member) { + return RecipeResponseDto.SearchRecipePreviewByCategoryDto.builder() + .recipeList(recipeList.stream() + .map(recipe -> toResponseRecipeSimpleDto(recipe, member)) + .collect(Collectors.toList())) + .categoryId(index) + .elements(recipeList.size()) + .build(); + } + private static RecipeResponseDto.RecipeSimpleDto toResponseRecipeSimpleDto(Recipe recipe, Member member) { return RecipeResponseDto.RecipeSimpleDto.builder() .recipeId(recipe.getId()) diff --git a/src/main/java/zipdabang/server/service/RecipeService.java b/src/main/java/zipdabang/server/service/RecipeService.java index 5822b45..013370a 100644 --- a/src/main/java/zipdabang/server/service/RecipeService.java +++ b/src/main/java/zipdabang/server/service/RecipeService.java @@ -37,7 +37,7 @@ public interface RecipeService { List getAllRecipeCategories(); - List searchRecipePreview(Long categoryId, String keyword, Member member); + List> searchRecipePreview(String keyword, Member member); boolean checkRecipeCategoryExist(Long categoryId); diff --git a/src/main/java/zipdabang/server/service/serviceImpl/RecipeServiceImpl.java b/src/main/java/zipdabang/server/service/serviceImpl/RecipeServiceImpl.java index 6404504..49abddc 100644 --- a/src/main/java/zipdabang/server/service/serviceImpl/RecipeServiceImpl.java +++ b/src/main/java/zipdabang/server/service/serviceImpl/RecipeServiceImpl.java @@ -265,24 +265,29 @@ public boolean checkRecipeCategoryExist(Long categoryId) { } @Override - public List searchRecipePreview(Long categoryId, String keyword, Member member) { + public List> searchRecipePreview(String keyword, Member member) { + Long recipeCategorySize = recipeCategoryRepository.count(); + List blockedMember = blockedMemberRepository.findByOwner(member).stream() .map(blockedInfo -> blockedInfo.getBlocked()) .collect(Collectors.toList()); - List recipeCategory = recipeCategoryRepository.findAllById(categoryId); + List> recipeList = new ArrayList<>(); - if (recipeCategory.isEmpty()) - throw new RecipeException(Code.RECIPE_NOT_FOUND); + for(Long categoryId = 1L; categoryId <= recipeCategorySize; categoryId++) { + List recipeCategory = recipeCategoryRepository.findAllById(categoryId); - List recipeIdList = recipeCategoryMappingRepository.findByCategoryIn(recipeCategory).stream() - .map(categoryMapping -> categoryMapping.getRecipe().getId()) - .collect(Collectors.toList()); + List recipeIdList = recipeCategoryMappingRepository.findByCategoryIn(recipeCategory).stream() + .map(categoryMapping -> categoryMapping.getRecipe().getId()) + .collect(Collectors.toList()); - if (blockedMember.isEmpty()) - return recipeRepository.findTop5ByIdInAndNameContainingOrderByCreatedAtDesc(recipeIdList, keyword); - else - return recipeRepository.findTop5ByIdInAndNameContainingAndMemberNotInOrderByCreatedAtDesc(recipeIdList, keyword, blockedMember); + if (blockedMember.isEmpty()) + recipeList.add(recipeRepository.findTop5ByIdInAndNameContainingOrderByCreatedAtDesc(recipeIdList, keyword)); + else + recipeList.add(recipeRepository.findTop5ByIdInAndNameContainingAndMemberNotInOrderByCreatedAtDesc(recipeIdList, keyword, blockedMember)); + } + + return recipeList; } public List getRecipeBannerList() { diff --git a/src/main/java/zipdabang/server/web/controller/RecipeController.java b/src/main/java/zipdabang/server/web/controller/RecipeController.java index c0aed70..eff51dd 100644 --- a/src/main/java/zipdabang/server/web/controller/RecipeController.java +++ b/src/main/java/zipdabang/server/web/controller/RecipeController.java @@ -119,7 +119,6 @@ public ResponseDto deleteRecipe(@PathVariable @ApiResponse(responseCode = "4003",description = "UNAUTHORIZED, 토큰 모양이 이상함, 토큰 제대로 주세요",content = @Content(schema = @Schema(implementation = ResponseDto.class))), @ApiResponse(responseCode = "4005",description = "UNAUTHORIZED, 엑세스 토큰 만료, 리프레시 토큰 사용",content = @Content(schema = @Schema(implementation = ResponseDto.class))), @ApiResponse(responseCode = "4008",description = "UNAUTHORIZED, 토큰 없음, 토큰 줘요",content = @Content(schema = @Schema(implementation = ResponseDto.class))), - @ApiResponse(responseCode = "4105",description = "BAD_REQUEST, 해당 id를 가진 레시피 카테고리가 없습니다. 잘못 보내줬어요",content = @Content(schema = @Schema(implementation = ResponseDto.class))), @ApiResponse(responseCode = "4052",description = "BAD_REQUEST, 사용자가 없습니다. 이 api에서 이거 생기면 백앤드 개발자 호출",content = @Content(schema = @Schema(implementation = ResponseDto.class))), @ApiResponse(responseCode = "5000",description = "SERVER ERROR, 백앤드 개발자에게 알려주세요",content = @Content(schema = @Schema(implementation = ResponseDto.class))), }) @@ -127,20 +126,15 @@ public ResponseDto deleteRecipe(@PathVariable @Parameter(name = "member", hidden = true), @Parameter(name = "keyword", description = "query string 검색할 단어") }) - @GetMapping(value = "/members/recipes/search/prieview/{categoryId}") - public ResponseDto searchRecipePreview(@PathVariable Long categoryId, @RequestParam(name = "keyword", required = false) String keyword, @AuthMember Member member) { - - if (recipeService.checkRecipeCategoryExist(categoryId) == false) - throw new RecipeException(Code.NO_RECIPE_CATEGORY_EXIST); + @GetMapping(value = "/members/recipes/search/prieview") + public ResponseDto searchRecipePreview(@RequestParam(name = "keyword", required = false) String keyword, @AuthMember Member member) { - List recipes = recipeService.searchRecipePreview(categoryId, keyword, member); - log.info(recipes.toString()); + List> recipeLists = recipeService.searchRecipePreview(keyword, member); - if(recipes.size() == 0) - throw new RecipeException(Code.RECIPE_NOT_FOUND); + log.info(recipeLists.toString()); - return ResponseDto.of(RecipeConverter.toPreviewRecipeDtoList(recipes, member)); + return ResponseDto.of(RecipeConverter.toSearchRecipePreviewListDto(recipeLists, member)); } @Operation(summary = "🍹figma 레시피2, 레시피 검색 목록조회 화면 API 🔑 ✔", description = "검색한 레시피 조회 화면 API입니다. pageIndex로 페이징") @ApiResponses({ diff --git a/src/main/java/zipdabang/server/web/dto/responseDto/RecipeResponseDto.java b/src/main/java/zipdabang/server/web/dto/responseDto/RecipeResponseDto.java index d46cdd3..f3149fa 100644 --- a/src/main/java/zipdabang/server/web/dto/responseDto/RecipeResponseDto.java +++ b/src/main/java/zipdabang/server/web/dto/responseDto/RecipeResponseDto.java @@ -82,6 +82,24 @@ public static class RecipePageListDto { Boolean isLast; } + @Builder + @Getter + @AllArgsConstructor(access = AccessLevel.PROTECTED) + @NoArgsConstructor(access = AccessLevel.PROTECTED) + public static class SearchRecipePreviewListDto { + private List recipeList; + } + + @Builder + @Getter + @AllArgsConstructor(access = AccessLevel.PROTECTED) + @NoArgsConstructor(access = AccessLevel.PROTECTED) + public static class SearchRecipePreviewByCategoryDto { + private List recipeList; + Long categoryId; + Integer elements; + } + @Builder @Getter @AllArgsConstructor(access = AccessLevel.PROTECTED)