Skip to content

Commit

Permalink
♻️ Feature/107 레시피 검색 미리보기 로직 변경 적용
Browse files Browse the repository at this point in the history
한번에 카테고리별 미리보기 5개 다 가져오기
  • Loading branch information
Hanvp committed Sep 6, 2023
1 parent 05bbc43 commit c7f78fd
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 23 deletions.
22 changes: 22 additions & 0 deletions src/main/java/zipdabang/server/converter/RecipeConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/zipdabang/server/service/RecipeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,28 +119,22 @@ 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))),
})
@Parameters({
@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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c7f78fd

Please sign in to comment.