From c7f78fde34175d654959513ca3edbae3c274c1f0 Mon Sep 17 00:00:00 2001
From: Hanvp <nwactris@naver.com>
Date: Wed, 6 Sep 2023 12:32:36 +0900
Subject: [PATCH] =?UTF-8?q?:recycle:=20Feature/107=20=EB=A0=88=EC=8B=9C?=
 =?UTF-8?q?=ED=94=BC=20=EA=B2=80=EC=83=89=20=EB=AF=B8=EB=A6=AC=EB=B3=B4?=
 =?UTF-8?q?=EA=B8=B0=20=EB=A1=9C=EC=A7=81=20=EB=B3=80=EA=B2=BD=20=EC=A0=81?=
 =?UTF-8?q?=EC=9A=A9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

한번에 카테고리별 미리보기 5개 다 가져오기
---
 .../server/converter/RecipeConverter.java     | 22 +++++++++++++++
 .../server/service/RecipeService.java         |  2 +-
 .../serviceImpl/RecipeServiceImpl.java        | 27 +++++++++++--------
 .../web/controller/RecipeController.java      | 16 ++++-------
 .../dto/responseDto/RecipeResponseDto.java    | 18 +++++++++++++
 5 files changed, 62 insertions(+), 23 deletions(-)

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<Recipe
                 .build();
     }
 
+    public static RecipeResponseDto.SearchRecipePreviewListDto toSearchRecipePreviewListDto(List<List<Recipe>> 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<Recipe> 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<RecipeCategory> getAllRecipeCategories();
 
-    List<Recipe> searchRecipePreview(Long categoryId, String keyword, Member member);
+    List<List<Recipe>> 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<Recipe> searchRecipePreview(Long categoryId, String keyword, Member member) {
+    public List<List<Recipe>> searchRecipePreview(String keyword, Member member) {
+        Long recipeCategorySize = recipeCategoryRepository.count();
+
         List<Member> blockedMember = blockedMemberRepository.findByOwner(member).stream()
                 .map(blockedInfo -> blockedInfo.getBlocked())
                 .collect(Collectors.toList());
 
-        List<RecipeCategory> recipeCategory = recipeCategoryRepository.findAllById(categoryId);
+        List<List<Recipe>> recipeList = new ArrayList<>();
 
-        if (recipeCategory.isEmpty())
-            throw new RecipeException(Code.RECIPE_NOT_FOUND);
+        for(Long categoryId = 1L; categoryId <= recipeCategorySize; categoryId++) {
+            List<RecipeCategory> recipeCategory = recipeCategoryRepository.findAllById(categoryId);
 
-        List<Long> recipeIdList = recipeCategoryMappingRepository.findByCategoryIn(recipeCategory).stream()
-                .map(categoryMapping -> categoryMapping.getRecipe().getId())
-                .collect(Collectors.toList());
+            List<Long> 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<RecipeBanner> 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<RecipeResponseDto.RecipeStatusDto> 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<RecipeResponseDto.RecipeStatusDto> deleteRecipe(@PathVariable
             @Parameter(name = "member", hidden = true),
             @Parameter(name = "keyword", description = "query string 검색할 단어")
     })
-    @GetMapping(value = "/members/recipes/search/prieview/{categoryId}")
-    public ResponseDto<RecipeResponseDto.RecipeListDto> 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<RecipeResponseDto.SearchRecipePreviewListDto> searchRecipePreview(@RequestParam(name = "keyword", required = false) String keyword, @AuthMember Member member) {
 
-        List<Recipe> recipes = recipeService.searchRecipePreview(categoryId, keyword, member);
 
-        log.info(recipes.toString());
+        List<List<Recipe>> 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<SearchRecipePreviewByCategoryDto> recipeList;
+    }
+
+    @Builder
+    @Getter
+    @AllArgsConstructor(access = AccessLevel.PROTECTED)
+    @NoArgsConstructor(access = AccessLevel.PROTECTED)
+    public static class SearchRecipePreviewByCategoryDto {
+        private List<RecipeSimpleDto> recipeList;
+        Long categoryId;
+        Integer elements;
+    }
+
     @Builder
     @Getter
     @AllArgsConstructor(access = AccessLevel.PROTECTED)