diff --git a/src/main/java/zipdabang/server/base/Code.java b/src/main/java/zipdabang/server/base/Code.java index e60991d..72b3c4f 100644 --- a/src/main/java/zipdabang/server/base/Code.java +++ b/src/main/java/zipdabang/server/base/Code.java @@ -22,7 +22,7 @@ public enum Code { //recipe response - RECIPE_NOT_FOUND(HttpStatus.OK, 2100, "검색어와 일치하는 레시피가 없습니다"), + RECIPE_NOT_FOUND(HttpStatus.OK, 2100, "조회된 목록이 없습니다"), // market response @@ -63,7 +63,7 @@ public enum Code { NO_RECIPE_EXIST(HttpStatus.BAD_REQUEST, 4101, "해당 레시피가 존재하지 않습니다."), BLOCKED_USER_RECIPE(HttpStatus.BAD_REQUEST, 4102, "차단한 사용자의 레시피입니다."), WRITTEN_BY_TYPE_ERROR(HttpStatus.BAD_REQUEST, 4103, "레시피 작성자 타입이 잘못되었습니다. all, influencer, common중 하나로 보내주세요."), - + ORDER_BY_TYPE_ERROR(HttpStatus.BAD_REQUEST, 4104, "조회 방식 타입이 잘못되었습니다. likes, views, latest중 하나로 보내주세요."), INTERNAL_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, 5000, "Internal server Error"), diff --git a/src/main/java/zipdabang/server/domain/recipe/Recipe.java b/src/main/java/zipdabang/server/domain/recipe/Recipe.java index a37be97..9242f5f 100644 --- a/src/main/java/zipdabang/server/domain/recipe/Recipe.java +++ b/src/main/java/zipdabang/server/domain/recipe/Recipe.java @@ -100,4 +100,9 @@ public Recipe updateToTalScrap(Integer i){ this.totalScrap += i; return this; } + + public Recipe updateTotalView(){ + this.totalLike += 1; + return this; + } } diff --git a/src/main/java/zipdabang/server/repository/recipeRepositories/RecipeCategoryMappingRepository.java b/src/main/java/zipdabang/server/repository/recipeRepositories/RecipeCategoryMappingRepository.java index 9628bff..45f40b2 100644 --- a/src/main/java/zipdabang/server/repository/recipeRepositories/RecipeCategoryMappingRepository.java +++ b/src/main/java/zipdabang/server/repository/recipeRepositories/RecipeCategoryMappingRepository.java @@ -1,7 +1,12 @@ package zipdabang.server.repository.recipeRepositories; import org.springframework.data.jpa.repository.JpaRepository; +import zipdabang.server.domain.recipe.RecipeCategory; import zipdabang.server.domain.recipe.RecipeCategoryMapping; +import java.util.List; +import java.util.Optional; + public interface RecipeCategoryMappingRepository extends JpaRepository { + List findByCategoryIn(List recipeCategories); } diff --git a/src/main/java/zipdabang/server/repository/recipeRepositories/RecipeCategoryRepository.java b/src/main/java/zipdabang/server/repository/recipeRepositories/RecipeCategoryRepository.java index bbe9e4f..02f184a 100644 --- a/src/main/java/zipdabang/server/repository/recipeRepositories/RecipeCategoryRepository.java +++ b/src/main/java/zipdabang/server/repository/recipeRepositories/RecipeCategoryRepository.java @@ -3,5 +3,9 @@ import org.springframework.data.jpa.repository.JpaRepository; import zipdabang.server.domain.recipe.RecipeCategory; +import java.util.List; + public interface RecipeCategoryRepository extends JpaRepository { + + List findAllById(Long categoryId); } diff --git a/src/main/java/zipdabang/server/repository/recipeRepositories/RecipeRepository.java b/src/main/java/zipdabang/server/repository/recipeRepositories/RecipeRepository.java index d7bb1e5..3b8dad5 100644 --- a/src/main/java/zipdabang/server/repository/recipeRepositories/RecipeRepository.java +++ b/src/main/java/zipdabang/server/repository/recipeRepositories/RecipeRepository.java @@ -1,6 +1,7 @@ package zipdabang.server.repository.recipeRepositories; import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import zipdabang.server.domain.member.Member; @@ -23,4 +24,8 @@ public interface RecipeRepository extends JpaRepository { List findTop5ByIsInfluencerTrueOrderByCreatedAtDesc(); List findTop5ByIsInfluencerFalseOrderByCreatedAtDesc(); + + Page findByIdIn(List recipeIdList, PageRequest createdAt); + + Page findByIdInAndMemberNotIn(List recipeIdList, List blockedMember, PageRequest createdAt); } diff --git a/src/main/java/zipdabang/server/service/RecipeService.java b/src/main/java/zipdabang/server/service/RecipeService.java index f51111f..cec358f 100644 --- a/src/main/java/zipdabang/server/service/RecipeService.java +++ b/src/main/java/zipdabang/server/service/RecipeService.java @@ -30,7 +30,11 @@ public interface RecipeService { Recipe updateScrapOnRecipe(Long recipeId, Member member); - Page recipeListByCategory(Long categoryId, Integer pageIndex, Member member); + Page recipeListByCategory(Long categoryId, Integer pageIndex, Member member, String order); List getAllRecipeCategories(); + +// Page recipeListByCategoryAndLikes(Long categoryId, Integer pageIndex, Member member); + +// Page recipeListByCategoryAndViews(Long categoryId, Integer pageIndex, Member member); } diff --git a/src/main/java/zipdabang/server/service/serviceImpl/RecipeServiceImpl.java b/src/main/java/zipdabang/server/service/serviceImpl/RecipeServiceImpl.java index 4e44c30..98f5469 100644 --- a/src/main/java/zipdabang/server/service/serviceImpl/RecipeServiceImpl.java +++ b/src/main/java/zipdabang/server/service/serviceImpl/RecipeServiceImpl.java @@ -10,8 +10,6 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import zipdabang.server.base.Code; -import zipdabang.server.base.ResponseDto; -import zipdabang.server.base.exception.handler.MemberException; import zipdabang.server.base.exception.handler.RecipeException; import zipdabang.server.converter.RecipeConverter; import zipdabang.server.domain.member.BlockedMember; @@ -216,9 +214,81 @@ public List getAllRecipeCategories() { return recipeCategoryRepository.findAll(); } + /* @Override - public Page recipeListByCategory(Long categoryId, Integer pageIndex, Member member) { + public Page recipeListByCategoryAndLikes(Long categoryId, Integer pageIndex, Member member) { - return null; + List blockedMember= blockedMemberRepository.findByOwner(member).stream() + .map(blockedInfo -> blockedInfo.getBlocked()) + .collect(Collectors.toList()); + + List recipeIdList = recipeCategoryMappingRepository.findByCategory(categoryId).stream() + .map(categoryMapping -> categoryMapping.getRecipe().getId()) + .collect(Collectors.toList()); + + if(blockedMember.isEmpty()) + return recipeRepository.findByIdIn(recipeIdList, + PageRequest.of(pageIndex, pageSize, Sort.by(Sort.Direction.DESC, "totalLike"))); + else + return recipeRepository.findByIdInAndMemberNotIn(recipeIdList,blockedMember, + PageRequest.of(pageIndex, pageSize, Sort.by(Sort.Direction.DESC, "totalLike"))); + } + + + @Override + public Page recipeListByCategoryAndViews(Long categoryId, Integer pageIndex, Member member) { + + List blockedMember= blockedMemberRepository.findByOwner(member).stream() + .map(blockedInfo -> blockedInfo.getBlocked()) + .collect(Collectors.toList()); + + List recipeIdList = recipeCategoryMappingRepository.findByCategory(categoryId).stream() + .map(categoryMapping -> categoryMapping.getRecipe().getId()) + .collect(Collectors.toList()); + + if(blockedMember.isEmpty()) + return recipeRepository.findByIdIn(recipeIdList, + PageRequest.of(pageIndex, pageSize, Sort.by(Sort.Direction.DESC, "totalView"))); + else + return recipeRepository.findByIdInAndMemberNotIn(recipeIdList,blockedMember, + PageRequest.of(pageIndex, pageSize, Sort.by(Sort.Direction.DESC, "totalView"))); + } +*/ + @Override + public Page recipeListByCategory(Long categoryId, Integer pageIndex, Member member, String order) { + + List blockedMember= blockedMemberRepository.findByOwner(member).stream() + .map(blockedInfo -> blockedInfo.getBlocked()) + .collect(Collectors.toList()); + + List recipeCategory = recipeCategoryRepository.findAllById(categoryId); + + if(recipeCategory.isEmpty()) + throw new RecipeException(Code.RECIPE_NOT_FOUND); + + List recipeIdList = recipeCategoryMappingRepository.findByCategoryIn(recipeCategory).stream() + .map(categoryMapping -> categoryMapping.getRecipe().getId()) + .collect(Collectors.toList()); + + String orderBy = null; + + if(order == null) + order = "latest"; + + if(order.equals("likes")) + orderBy = "totalLike"; + else if(order.equals("views")) + orderBy = "totalView"; + else if(order.equals("latest")) + orderBy = "createdAt"; + else + throw new RecipeException(Code.ORDER_BY_TYPE_ERROR); + + if(blockedMember.isEmpty()) + return recipeRepository.findByIdIn(recipeIdList, + PageRequest.of(pageIndex, pageSize, Sort.by(Sort.Direction.DESC, orderBy))); + else + return recipeRepository.findByIdInAndMemberNotIn(recipeIdList,blockedMember, + PageRequest.of(pageIndex, pageSize, Sort.by(Sort.Direction.DESC, orderBy))); } } diff --git a/src/main/java/zipdabang/server/web/controller/RecipeController.java b/src/main/java/zipdabang/server/web/controller/RecipeController.java index 44e200d..b802e06 100644 --- a/src/main/java/zipdabang/server/web/controller/RecipeController.java +++ b/src/main/java/zipdabang/server/web/controller/RecipeController.java @@ -11,6 +11,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -93,6 +94,8 @@ public ResponseDto recipeDetail(@PathVariable(n Boolean isLiked = recipeService.getLike(recipe, member); Boolean isScrapped = recipeService.getScrap(recipe, member); + recipe.updateTotalView(); + return ResponseDto.of(RecipeConverter.toRecipeInfoDto(recipe, isOwner, isLiked, isScrapped, member)); } @@ -163,17 +166,18 @@ else if (pageIndex < 1) @ApiResponse(responseCode = "4008",description = "UNAUTHORIZED, 토큰 없음, 토큰 줘요",content = @Content(schema = @Schema(implementation = ResponseDto.class))), @ApiResponse(responseCode = "4052",description = "BAD_REQUEST, 사용자가 없습니다. 이 api에서 이거 생기면 백앤드 개발자 호출",content = @Content(schema = @Schema(implementation = ResponseDto.class))), @ApiResponse(responseCode = "4053",description = "BAD_REQUEST, 넘겨받은 categoryId와 일치하는 카테고리 없음. 1~6 사이로 보내세요",content = @Content(schema = @Schema(implementation = ResponseDto.class))), - @ApiResponse(responseCode = "4054",description = "BAD_REQUEST, 페이지 번호 -1 이하입니다. 0 이상으로 주세요.",content = @Content(schema = @Schema(implementation = ResponseDto.class))), - @ApiResponse(responseCode = "4049",description = "BAD_REQUEST, 페이지 인덱스 범위 초과함",content = @Content(schema = @Schema(implementation = ResponseDto.class))), + @ApiResponse(responseCode = "4054",description = "BAD_REQUEST, 페이지 번호 0 이하입니다. 1 이상으로 주세요.",content = @Content(schema = @Schema(implementation = ResponseDto.class))), + @ApiResponse(responseCode = "4055",description = "BAD_REQUEST, 페이지 인덱스 범위 초과함",content = @Content(schema = @Schema(implementation = ResponseDto.class))), + @ApiResponse(responseCode = "4104",description = "BAD_REQUEST, 조회 방식 타입이 잘못되었습니다. likes, views, lastest중 하나로 보내주세요.",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 = "pageIndex", description = "query string 페이지 번호, 안주면 0으로(최초 페이지) 설정함, -1 이런거 주면 에러 뱉음"), - @Parameter(name = "order", description = "query string 조회 방식. 인기순: likes, 조회순: views, 최신순: latest로 넘겨주세요") + @Parameter(name = "order", description = "query string 조회 방식. 인기순: likes, 조회순: views, 최신순: latest로 넘겨주세요, 기본값 latest") }) @GetMapping(value = "/members/recipes/categories/{categoryId}") - public ResponseDto recipeListByCategory(@PathVariable Long categoryId, @RequestParam(name = "order") String order, @RequestParam(name = "pageIndex", required = false) Integer pageIndex, @AuthMember Member member){ + public ResponseDto recipeListByCategory(@PathVariable Long categoryId, @RequestParam(name = "order", required = false) String order, @RequestParam(name = "pageIndex", required = false) Integer pageIndex, @AuthMember Member member){ if(pageIndex == null) pageIndex =1; else if (pageIndex < 1) @@ -181,18 +185,17 @@ else if (pageIndex < 1) pageIndex -= 1; -// Page recipes= recipeService.recipeListByCategory(categoryId,pageIndex,member); -// -// log.info(recipes.toString()); -// -// if(recipes.getTotalElements() == 0) -// throw new RecipeException(Code.RECIPE_NOT_FOUND); -// if(pageIndex >= recipes.getTotalPages()) -// throw new RecipeException(Code.OVER_PAGE_INDEX_ERROR); -// -// return ResponseDto.of(RecipeConverter.toPagingRecipeDtoList(recipes, member)); + Page recipes = recipeService.recipeListByCategory(categoryId,pageIndex,member,order); - return null; + + log.info(recipes.toString()); + + if(recipes.getTotalElements() == 0) + throw new RecipeException(Code.RECIPE_NOT_FOUND); + if(pageIndex >= recipes.getTotalPages()) + throw new RecipeException(Code.OVER_PAGE_INDEX_ERROR); + + return ResponseDto.of(RecipeConverter.toPagingRecipeDtoList(recipes, member)); } @Operation(summary = "🍹figma 레시피1, 모든사람/인플루언서/우리들의 레시피 미리보기 API 🔑 ✔", description = "5개씩 미리보기로 가져오는 API입니다.") @@ -226,8 +229,8 @@ public ResponseDto recipeListPreviewWrittenBy(@ @ApiResponse(responseCode = "4008",description = "UNAUTHORIZED, 토큰 없음, 토큰 줘요",content = @Content(schema = @Schema(implementation = ResponseDto.class))), @ApiResponse(responseCode = "4052",description = "BAD_REQUEST, 사용자가 없습니다. 이 api에서 이거 생기면 백앤드 개발자 호출",content = @Content(schema = @Schema(implementation = ResponseDto.class))), @ApiResponse(responseCode = "4053",description = "BAD_REQUEST, 넘겨받은 categoryId와 일치하는 카테고리 없음. 1~6 사이로 보내세요",content = @Content(schema = @Schema(implementation = ResponseDto.class))), - @ApiResponse(responseCode = "4054",description = "BAD_REQUEST, 페이지 번호 -1 이하입니다. 0 이상으로 주세요.",content = @Content(schema = @Schema(implementation = ResponseDto.class))), - @ApiResponse(responseCode = "4049",description = "BAD_REQUEST, 페이지 인덱스 범위 초과함",content = @Content(schema = @Schema(implementation = ResponseDto.class))), + @ApiResponse(responseCode = "4054",description = "BAD_REQUEST, 페이지 번호 0 이하입니다. 1 이상으로 주세요.",content = @Content(schema = @Schema(implementation = ResponseDto.class))), + @ApiResponse(responseCode = "4055",description = "BAD_REQUEST, 페이지 인덱스 범위 초과함",content = @Content(schema = @Schema(implementation = ResponseDto.class))), @ApiResponse(responseCode = "5000",description = "SERVER ERROR, 백앤드 개발자에게 알려주세요",content = @Content(schema = @Schema(implementation = ResponseDto.class))), }) @Parameters({ diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 25f8158..0563a82 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -12,11 +12,11 @@ spring: enabled: always ## # local redis - redis: - host: localhost - # redis: -# host: zipdabang-redis.osattk.ng.0001.apn2.cache.amazonaws.com +# host: localhost + + redis: + host: zipdabang-redis.osattk.ng.0001.apn2.cache.amazonaws.com batch: jdbc: initialize-schema: always