diff --git a/src/main/java/com/example/betteriter/fo_domain/mypage/controller/MypageController.java b/src/main/java/com/example/betteriter/fo_domain/mypage/controller/MypageController.java index fb1924f..2e167ec 100644 --- a/src/main/java/com/example/betteriter/fo_domain/mypage/controller/MypageController.java +++ b/src/main/java/com/example/betteriter/fo_domain/mypage/controller/MypageController.java @@ -11,8 +11,11 @@ import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; + +import org.springframework.data.domain.Page; import org.springframework.data.domain.Slice; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -36,11 +39,11 @@ public class MypageController { * @param page 페이지 번호 * @return List */ - @GetMapping("/review/mine") + @GetMapping("/review/mine/{page}") public ResponseDto getReview( - @RequestParam int page + @PathVariable int page ) { - List reviewList = mypageService.getMyReviewList(page); + Page reviewList = mypageService.getMyReviewList(page); return ResponseDto.onSuccess(mypageResponseConverter.toReviewListDto(reviewList)); } @@ -50,11 +53,11 @@ public ResponseDto getReview( * @param page 페이지 번호 * @return List */ - @GetMapping("/review/scrap") + @GetMapping("/review/mine/scrap/{page}") public ResponseDto getScrapReview( - @RequestParam int page + @PathVariable int page ) { - List reviewList = mypageService.getScrapReviewList(page); + Page reviewList = mypageService.getScrapReviewList(page); return ResponseDto.onSuccess(mypageResponseConverter.toReviewListDto(reviewList)); } @@ -64,11 +67,11 @@ public ResponseDto getScrapReview( * @param page 페이지 번호 * @return List */ - @GetMapping("/review/like") + @GetMapping("/review/mine/like/{page}") public ResponseDto getLikeReview( - @RequestParam int page + @PathVariable int page ) { - Slice reviewList = mypageService.getLikeReviewList(page); + Page reviewList = mypageService.getLikeReviewList(page); return ResponseDto.onSuccess(mypageResponseConverter.toReviewResponse(reviewList)); } @@ -78,9 +81,9 @@ public ResponseDto getLikeReview( * @param page 페이지 번호 * @return List */ - @GetMapping("/followers") + @GetMapping("/followers/mine/{page}") public ResponseDto getFollower( - @RequestParam int page + @PathVariable int page ) { List followerList = mypageService.getFollowerList(page); Integer totalCount = mypageService.getFollowerCount(); @@ -93,9 +96,9 @@ public ResponseDto getFollower( * @param page 페이지 번호 * @return List */ - @GetMapping("/followings") + @GetMapping("/followings/mine/{page}") public ResponseDto getFollowee( - @RequestParam int page + @PathVariable int page ) { List followeeList = mypageService.getFollowingList(page); Integer totalCount = mypageService.getFollowingCount(); diff --git a/src/main/java/com/example/betteriter/fo_domain/mypage/converter/MypageResponseConverter.java b/src/main/java/com/example/betteriter/fo_domain/mypage/converter/MypageResponseConverter.java index 03508c9..399b7f9 100644 --- a/src/main/java/com/example/betteriter/fo_domain/mypage/converter/MypageResponseConverter.java +++ b/src/main/java/com/example/betteriter/fo_domain/mypage/converter/MypageResponseConverter.java @@ -7,6 +7,8 @@ import com.example.betteriter.fo_domain.review.service.ReviewConnector; import com.example.betteriter.fo_domain.user.domain.Users; import lombok.RequiredArgsConstructor; + +import org.springframework.data.domain.Page; import org.springframework.data.domain.Slice; import org.springframework.stereotype.Component; @@ -40,14 +42,12 @@ public static MypageResponse.FollowerListDto toFollowerListDto(List follo } public static MypageResponse.UserProfileDto toUserProfileDto( - Users user, Integer reviewCount, Integer scrapCount, Integer followerCount, Integer followingCount + Users user, Integer followerCount, Integer followingCount ) { return MypageResponse.UserProfileDto.builder() .profileImage(user.getUsersDetail().getProfileImage()) .nickname(user.getUsersDetail().getNickName()) .job(user.getUsersDetail().getJob()) - .reviewCount(reviewCount) - .scrapCount(scrapCount) .followerCount(followerCount) .followingCount(followingCount) .build(); @@ -80,30 +80,46 @@ public ReviewResponse toReviewResponse(Slice reviews) { return new ReviewResponse(getReviewResponseDtos, reviews.hasNext(), !reviews.isEmpty()); } - public MypageResponse.ReviewListDto toReviewListDto(List reviewList) { - List myReviewList = new ArrayList<>(); - - reviewList.forEach(r -> { - MypageResponse.ReviewDto reviewDto = MypageResponse.ReviewDto.builder() - .reviewId(r.getId()) - .title(r.getProductName()) - .thumbnailImage((!r.getReviewImages().isEmpty()) ? - r.getReviewImages().get(0).getImgUrl() : null) - .writerId(r.getWriter().getId()) - .writerJob(r.getWriter().getUsersDetail().getJob()) - .writerNickname(r.getWriter().getUsersDetail().getNickName()) - .profileImage(r.getWriter().getUsersDetail().getProfileImage()) - .likeCount((long) r.getReviewLiked().size()) - .scrapCount((long) r.getReviewScraped().size()) - .isLike(r.getReviewLiked().stream().anyMatch(rl -> rl.getUsers().getId().equals(r.getWriter().getId()))) - .isScrap(r.getReviewScraped().stream().anyMatch(rs -> rs.getUsers().getId().equals(r.getWriter().getId()))) - .build(); - myReviewList.add(reviewDto); - }); + public MypageResponse.ReviewListDto toReviewListDto(Page reviewList) { + List myReviewList = reviewList.stream() + .map(MypageResponseConverter::getReviewDto) + .collect(Collectors.toList()); - return MypageResponse.ReviewListDto.builder() - .reviewCount(reviewList.size()) + MypageResponse.PageDto pageInfo = getReviewPageInfo(reviewList); + + return MypageResponse.ReviewListDto.builder() .reviewList(myReviewList) + .pageInfo(pageInfo) + .build(); + } + + private static MypageResponse.PageDto getReviewPageInfo(Page reviewList) { + return MypageResponse.PageDto.builder() + .page(reviewList.getNumber()) + .size(reviewList.getSize()) + .totalPage(reviewList.getTotalPages()) + .totalCount(reviewList.getTotalElements()) + .hasNext(reviewList.hasNext()) + .hasPrev(reviewList.hasPrevious()) + .build(); + } + + private static MypageResponse.ReviewDto getReviewDto(Review r) { + return MypageResponse.ReviewDto.builder() + .reviewId(r.getId()) + .title(r.getProductName()) + .thumbnailImage((!r.getReviewImages().isEmpty()) ? + r.getReviewImages().get(0).getImgUrl() : null) + .writerId(r.getWriter().getId()) + .writerJob(r.getWriter().getUsersDetail().getJob()) + .writerNickname(r.getWriter().getUsersDetail().getNickName()) + .profileImage(r.getWriter().getUsersDetail().getProfileImage()) + .likeCount((long)r.getReviewLiked().size()) + .scrapCount((long)r.getReviewScraped().size()) + .isLike(r.getReviewLiked().stream().anyMatch(rl -> rl.getUsers().getId().equals(r.getWriter().getId()))) + .isScrap(r.getReviewScraped() + .stream() + .anyMatch(rs -> rs.getUsers().getId().equals(r.getWriter().getId()))) .build(); } diff --git a/src/main/java/com/example/betteriter/fo_domain/mypage/dto/MypageResponse.java b/src/main/java/com/example/betteriter/fo_domain/mypage/dto/MypageResponse.java index 7cdaf8d..458d5be 100644 --- a/src/main/java/com/example/betteriter/fo_domain/mypage/dto/MypageResponse.java +++ b/src/main/java/com/example/betteriter/fo_domain/mypage/dto/MypageResponse.java @@ -26,10 +26,22 @@ public static class ReviewDto { private Boolean isScrap; } + @Getter + @Builder + public static class PageDto { + private Integer page; // 현재 페이지 번호 + private Integer size; // 한 페이지에 보여줄 요소 개수 + private Integer totalPage; // 전체 페이지 개수 + private Long totalCount; // 전체 요소 개수 + + private Boolean hasNext; // 다음 페이지 존재 여부 + private Boolean hasPrev; // 이전 페이지 존재 여부 + } + @Getter @Builder public static class ReviewListDto { - private Integer reviewCount; + private PageDto pageInfo; private List reviewList; } @@ -46,6 +58,7 @@ public static class FollowerDto { @Builder public static class FollowerListDto { private Integer totalCount; + private PageDto pageInfo; private List followerList; } @@ -57,8 +70,6 @@ public static class UserProfileDto { private Job job; private Integer followerCount; private Integer followingCount; - private Integer reviewCount; - private Integer scrapCount; } @Getter diff --git a/src/main/java/com/example/betteriter/fo_domain/mypage/service/MypageService.java b/src/main/java/com/example/betteriter/fo_domain/mypage/service/MypageService.java index 90394dd..92211ce 100644 --- a/src/main/java/com/example/betteriter/fo_domain/mypage/service/MypageService.java +++ b/src/main/java/com/example/betteriter/fo_domain/mypage/service/MypageService.java @@ -9,6 +9,8 @@ import com.example.betteriter.fo_domain.user.service.UserService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; + +import org.springframework.data.domain.Page; import org.springframework.data.domain.Slice; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -27,19 +29,19 @@ public class MypageService { private final FollowService followService; @Transactional(readOnly = true) - public List getMyReviewList(int page) { + public Page getMyReviewList(int page) { Users user = userService.getCurrentUser(); return reviewService.getReviewList(user, page, SIZE); } @Transactional(readOnly = true) - public List getScrapReviewList(int page) { + public Page getScrapReviewList(int page) { Users user = userService.getCurrentUser(); return reviewService.getScrapReviewList(user, page, SIZE); } @Transactional(readOnly = true) - public Slice getLikeReviewList(int page) { + public Page getLikeReviewList(int page) { Users user = userService.getCurrentUser(); return reviewService.getLikeReviewList(user, page, SIZE); } @@ -59,13 +61,11 @@ public List getFollowingList(int page) { @Transactional(readOnly = true) public MypageResponse.UserProfileDto getUserProfile() { Users user = userService.getCurrentUser(); - Integer reviewCount = reviewService.getReviewCount(user); - Integer scrapCount = reviewService.getScrapCount(user); Integer followerCount = followService.getFollowerCount(user); Integer followeeCount = followService.getFolloweeCount(user); return MypageResponseConverter.toUserProfileDto( - user, reviewCount, scrapCount, followerCount, followeeCount + user, followerCount, followeeCount ); } diff --git a/src/main/java/com/example/betteriter/fo_domain/review/repository/ReviewRepository.java b/src/main/java/com/example/betteriter/fo_domain/review/repository/ReviewRepository.java index 232ac46..ef350ca 100644 --- a/src/main/java/com/example/betteriter/fo_domain/review/repository/ReviewRepository.java +++ b/src/main/java/com/example/betteriter/fo_domain/review/repository/ReviewRepository.java @@ -1,16 +1,19 @@ package com.example.betteriter.fo_domain.review.repository; -import com.example.betteriter.fo_domain.review.domain.Review; -import com.example.betteriter.fo_domain.review.repository.custom.CustomReviewRepository; -import com.example.betteriter.fo_domain.user.domain.Users; -import com.example.betteriter.global.constant.Category; -import io.lettuce.core.dynamic.annotation.Param; +import java.util.List; + +import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; -import java.util.List; +import com.example.betteriter.fo_domain.review.domain.Review; +import com.example.betteriter.fo_domain.review.repository.custom.CustomReviewRepository; +import com.example.betteriter.fo_domain.user.domain.Users; +import com.example.betteriter.global.constant.Category; + +import io.lettuce.core.dynamic.annotation.Param; public interface ReviewRepository extends JpaRepository, CustomReviewRepository { List findFirst7ByCategoryOrderByCreatedAtDesc(Category category); @@ -45,20 +48,14 @@ public interface ReviewRepository extends JpaRepository, CustomRev "ORDER BY r.id DESC") List findAllByTargetUser(@Param("user") Users user); - @Query("SELECT r FROM REVIEW r " + - "WHERE r.writer = :user " + - "ORDER BY r.createdAt desc ") - List findAllByUser(@Param("user") Users user, Pageable pageable); + @Query("SELECT r FROM REVIEW r WHERE r.writer = :user ORDER BY r.createdAt desc ") + Page findAllByUser(@Param("user") Users user, Pageable pageable); - @Query("SELECT r FROM REVIEW r " + - "LEFT JOIN r.reviewLiked rl " + - "LEFT JOIN r.reviewScraped rs " + - "WHERE rs.users = :user " + - "ORDER BY r.id DESC") - List findAllByReviewScrapedUser(@Param("user") Users user, Pageable pageable); + @Query("SELECT r FROM REVIEW r LEFT JOIN r.reviewScraped rs WHERE rs.users = :user ORDER BY r.id DESC") + Page findAllByReviewScrapedUser(@Param("user") Users user, Pageable pageable); @Query("select rl.review from REVIEW_LIKE rl where rl.users = :user order by rl.createdAt desc") - Slice findAllByReviewLikedUser(@Param("user") Users user, Pageable pageable); + Page findAllByReviewLikedUser(@Param("user") Users user, Pageable pageable); /* 좋아요 수 많은 리뷰 조회 */ Slice findByProductNameOrderByLikedCountDescCreatedAtDesc(String name, Pageable pageable); diff --git a/src/main/java/com/example/betteriter/fo_domain/review/service/ReviewService.java b/src/main/java/com/example/betteriter/fo_domain/review/service/ReviewService.java index 4f59a96..09e3951 100644 --- a/src/main/java/com/example/betteriter/fo_domain/review/service/ReviewService.java +++ b/src/main/java/com/example/betteriter/fo_domain/review/service/ReviewService.java @@ -15,6 +15,7 @@ import lombok.extern.slf4j.Slf4j; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; @@ -292,17 +293,17 @@ public Review findReviewById(Long reviewId) { .orElseThrow(() -> new ReviewHandler(_REVIEW_NOT_FOUND)); } - public List getReviewList(Users user, int page, int size) { + public Page getReviewList(Users user, int page, int size) { Pageable pageable = PageRequest.of(page, size); return this.reviewRepository.findAllByUser(user, pageable); } - public List getScrapReviewList(Users user, int page, int size) { + public Page getScrapReviewList(Users user, int page, int size) { Pageable pageable = PageRequest.of(page, size); return this.reviewRepository.findAllByReviewScrapedUser(user, pageable); } - public Slice getLikeReviewList(Users user, int page, int size) { + public Page getLikeReviewList(Users user, int page, int size) { Pageable pageable = PageRequest.of(page, size); return this.reviewRepository.findAllByReviewLikedUser(user, pageable); }