Skip to content

Commit

Permalink
fix : 리뷰 페이징 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
yeopyeop-82 committed Feb 8, 2024
1 parent dea3752 commit 54d21a1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Review, Long> {
List<Review> findByModel(Model model);

Page<Review> findByPortfolio(Portfolio portfolio, Pageable page);
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,12 @@ public List<ReviewResponseDto> 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<Review> reviewList = portfolio.getReviewList();
Page<Review> reviewPage = getPage(page, reviewList);
Page<Review> reviewPage = reviewRepository.findByPortfolio(portfolio, page);

return ReviewListPageDto.from(reviewPage);
}
Expand All @@ -111,16 +110,4 @@ public void deleteReview(DeleteReviewDto reviewDto){

reviewRepository.delete(review);
}

//TODO: change List -> Page
private Page<Review> getPage(int page, List<Review> 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());
}
}

0 comments on commit 54d21a1

Please sign in to comment.