From 54d21a139b0bc6c775b56519b7cea5fc83de4eb7 Mon Sep 17 00:00:00 2001 From: yeopyeop-82 Date: Thu, 8 Feb 2024 14:54:36 +0900 Subject: [PATCH] =?UTF-8?q?fix=20:=20=EB=A6=AC=EB=B7=B0=20=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=95=20=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../review/controller/ReviewController.java | 5 ++++- .../review/repository/ReviewRepository.java | 5 +++++ .../domain/review/service/ReviewService.java | 17 ++--------------- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/main/java/umc/meme/shop/domain/review/controller/ReviewController.java b/src/main/java/umc/meme/shop/domain/review/controller/ReviewController.java index 3f7228d..861c6de 100644 --- a/src/main/java/umc/meme/shop/domain/review/controller/ReviewController.java +++ b/src/main/java/umc/meme/shop/domain/review/controller/ReviewController.java @@ -2,6 +2,9 @@ import io.swagger.v3.oas.annotations.Operation; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.web.PageableDefault; import org.springframework.web.bind.annotation.*; import umc.meme.shop.domain.review.dto.request.ReviewDto; import umc.meme.shop.domain.review.dto.request.DeleteReviewDto; @@ -18,7 +21,7 @@ public class ReviewController { @Operation(summary = "리뷰 리스트 조회", description = "리뷰 리스트를 조회하는 API입니다.") @GetMapping("/{portfolioId}") public ApiResponse getReviewList(@PathVariable Long portfolioId, - @RequestParam(value = "page", defaultValue = "0", required = false) int page + @PageableDefault(size = 30, sort = "portfolio", direction = Sort.Direction.ASC) Pageable page ){ return ApiResponse.SuccessResponse(SuccessStatus.REVIEW_GET, reviewService.getReviewList(portfolioId, page)); } diff --git a/src/main/java/umc/meme/shop/domain/review/repository/ReviewRepository.java b/src/main/java/umc/meme/shop/domain/review/repository/ReviewRepository.java index e052a85..31e6819 100644 --- a/src/main/java/umc/meme/shop/domain/review/repository/ReviewRepository.java +++ b/src/main/java/umc/meme/shop/domain/review/repository/ReviewRepository.java @@ -1,11 +1,16 @@ package umc.meme.shop.domain.review.repository; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import umc.meme.shop.domain.model.entity.Model; +import umc.meme.shop.domain.portfolio.entity.Portfolio; import umc.meme.shop.domain.review.entity.Review; import java.util.List; public interface ReviewRepository extends JpaRepository { List findByModel(Model model); + + Page findByPortfolio(Portfolio portfolio, Pageable page); } diff --git a/src/main/java/umc/meme/shop/domain/review/service/ReviewService.java b/src/main/java/umc/meme/shop/domain/review/service/ReviewService.java index 1eb9ce3..058392a 100644 --- a/src/main/java/umc/meme/shop/domain/review/service/ReviewService.java +++ b/src/main/java/umc/meme/shop/domain/review/service/ReviewService.java @@ -88,13 +88,12 @@ public List getMyReview(Long modelId){ } //리뷰 리스트 조회 - public ReviewListPageDto getReviewList(Long portfolioId, int page) { + public ReviewListPageDto getReviewList(Long portfolioId, Pageable page) { Portfolio portfolio = portfolioRepository.findById(portfolioId) .orElseThrow(() -> new GlobalException(ErrorStatus.NOT_EXIST_PORTFOLIO)); // list를 page로 변환 - List reviewList = portfolio.getReviewList(); - Page reviewPage = getPage(page, reviewList); + Page reviewPage = reviewRepository.findByPortfolio(portfolio, page); return ReviewListPageDto.from(reviewPage); } @@ -111,16 +110,4 @@ public void deleteReview(DeleteReviewDto reviewDto){ reviewRepository.delete(review); } - - //TODO: change List -> Page - private Page getPage(int page, List list){ - Pageable pageable = PageRequest.of(page, 30); - - int start = (int) pageable.getOffset(); - int end = Math.min((start + pageable.getPageSize()), list.size()); - - //list를 page로 변환 - return new PageImpl<>(list.subList(start, end), - pageable, list.size()); - } }