Skip to content

Commit

Permalink
Merge pull request #111 from MEME-UMC/fix/#105
Browse files Browse the repository at this point in the history
[Fix] 페이징 처리 로직 수정
  • Loading branch information
yeopyeop-82 authored Feb 8, 2024
2 parents 07327e6 + 54d21a1 commit b63ea36
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 45 deletions.
18 changes: 7 additions & 11 deletions src/main/java/umc/meme/shop/domain/model/service/ModelService.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,13 @@ public List<SimplePortfolioDto> recommendRecent(){
//검색하기 정렬 기준 설정
private Pageable setPageRequest(int page, String sortBy){

Sort sort;
if(sortBy.equals("desc"))
sort = Sort.by("price").descending();
else if(sortBy.equals("asc"))
sort = Sort.by("price").ascending();
else if(sortBy.equals("review"))
sort = Sort.by("averageStars").descending();
else if(sortBy.equals("recent"))
sort = Sort.by("createdAt").descending();
else
throw new GlobalException(ErrorStatus.INVALID_SORT_CRITERIA);
Sort sort = switch (sortBy) {
case "desc" -> Sort.by("price").descending();
case "asc" -> Sort.by("price").ascending();
case "review" -> Sort.by("averageStars").descending();
case "recent" -> Sort.by("createdAt").descending();
default -> throw new GlobalException(ErrorStatus.INVALID_SORT_CRITERIA);
};

//별점 높은 순 정렬 추가
Sort finalSort = sort.and(Sort.by("averageStars").descending());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import lombok.Data;
import lombok.NoArgsConstructor;
import umc.meme.shop.domain.mypage.entity.Inquiry;
import umc.meme.shop.domain.user.User;

import java.util.List;

@Data
@Builder
Expand Down
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.portfolio.dto.request.CreatePortfolioDto;
import umc.meme.shop.domain.portfolio.dto.request.UpdatePortfolioDto;
Expand All @@ -25,7 +28,7 @@ public ApiResponse createPortfolio(@RequestBody CreatePortfolioDto portfolioDto)
@Operation(summary = "포트폴리오 전체 조회", description = "포트폴리오 전체를 조회하는 API입니다.")
@GetMapping("/{artistId}")
public ApiResponse getPortfolio(@PathVariable Long artistId,
@RequestParam(value = "page", defaultValue = "0", required = false) int page
@PageableDefault(size = 30, sort = "id", direction = Sort.Direction.ASC) Pageable page
){
return ApiResponse.SuccessResponse(SuccessStatus.PORTFOLIO_GET, portfolioService.getPortfolio(artistId, page));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ public interface PortfolioRepository extends JpaRepository<Portfolio, Long> {
Page<Portfolio> findAllNotBlocked(Pageable pageable);

boolean existsByMakeupName(String makeupName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void createPortfolio(CreatePortfolioDto portfolioDto) {

// 포트폴리오 전체 조회
@Transactional
public PortfolioPageDto getPortfolio(Long artistId, int page) {
public PortfolioPageDto getPortfolio(Long artistId, Pageable page) {
Artist artist = artistRepository.findById(artistId)
.orElseThrow(() -> new GlobalException(ErrorStatus.NOT_EXIST_ARTIST));

Expand All @@ -71,7 +71,7 @@ public PortfolioPageDto getPortfolio(Long artistId, int page) {
portfolioList.removeIf(Portfolio::isBlock);

//list를 page로 변환
Page<Portfolio> portfolioPage = getPage(page, portfolioList);
Page<Portfolio> portfolioPage = portfolioRepository.findByArtist(artist, page);

return PortfolioPageDto.from(portfolioPage);
}
Expand Down Expand Up @@ -127,16 +127,4 @@ public void updatePortfolio(UpdatePortfolioDto request) {
portfolio.updatePortfolio(request);
}

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

}
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 b63ea36

Please sign in to comment.