Skip to content

Commit

Permalink
Merge pull request #153 from BETTER-iTER/feature/151
Browse files Browse the repository at this point in the history
[FEATURE/151] mypage paging 사용 api 리펙토링
  • Loading branch information
luke0408 authored Jan 24, 2024
2 parents 93ef294 + 89790e6 commit 82f89b2
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,11 +39,11 @@ public class MypageController {
* @param page 페이지 번호
* @return List<MypageResponse.MyReviewDto>
*/
@GetMapping("/review/mine")
@GetMapping("/review/mine/{page}")
public ResponseDto<MypageResponse.ReviewListDto> getReview(
@RequestParam int page
@PathVariable int page
) {
List<Review> reviewList = mypageService.getMyReviewList(page);
Page<Review> reviewList = mypageService.getMyReviewList(page);
return ResponseDto.onSuccess(mypageResponseConverter.toReviewListDto(reviewList));
}

Expand All @@ -50,11 +53,11 @@ public ResponseDto<MypageResponse.ReviewListDto> getReview(
* @param page 페이지 번호
* @return List<MypageResponse.MyReviewDto>
*/
@GetMapping("/review/scrap")
@GetMapping("/review/mine/scrap/{page}")
public ResponseDto<MypageResponse.ReviewListDto> getScrapReview(
@RequestParam int page
@PathVariable int page
) {
List<Review> reviewList = mypageService.getScrapReviewList(page);
Page<Review> reviewList = mypageService.getScrapReviewList(page);
return ResponseDto.onSuccess(mypageResponseConverter.toReviewListDto(reviewList));
}

Expand All @@ -64,11 +67,11 @@ public ResponseDto<MypageResponse.ReviewListDto> getScrapReview(
* @param page 페이지 번호
* @return List<MypageResponse.MyReviewDto>
*/
@GetMapping("/review/like")
@GetMapping("/review/mine/like/{page}")
public ResponseDto<ReviewResponse> getLikeReview(
@RequestParam int page
@PathVariable int page
) {
Slice<Review> reviewList = mypageService.getLikeReviewList(page);
Page<Review> reviewList = mypageService.getLikeReviewList(page);
return ResponseDto.onSuccess(mypageResponseConverter.toReviewResponse(reviewList));
}

Expand All @@ -78,9 +81,9 @@ public ResponseDto<ReviewResponse> getLikeReview(
* @param page 페이지 번호
* @return List<MypageResponse.FollowerDto>
*/
@GetMapping("/followers")
@GetMapping("/followers/mine/{page}")
public ResponseDto<MypageResponse.FollowerListDto> getFollower(
@RequestParam int page
@PathVariable int page
) {
List<Users> followerList = mypageService.getFollowerList(page);
Integer totalCount = mypageService.getFollowerCount();
Expand All @@ -93,9 +96,9 @@ public ResponseDto<MypageResponse.FollowerListDto> getFollower(
* @param page 페이지 번호
* @return List<MypageResponse.FollowerDto>
*/
@GetMapping("/followings")
@GetMapping("/followings/mine/{page}")
public ResponseDto<MypageResponse.FollowerListDto> getFollowee(
@RequestParam int page
@PathVariable int page
) {
List<Users> followeeList = mypageService.getFollowingList(page);
Integer totalCount = mypageService.getFollowingCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -40,14 +42,12 @@ public static MypageResponse.FollowerListDto toFollowerListDto(List<Users> 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();
Expand Down Expand Up @@ -80,30 +80,46 @@ public ReviewResponse toReviewResponse(Slice<Review> reviews) {
return new ReviewResponse(getReviewResponseDtos, reviews.hasNext(), !reviews.isEmpty());
}

public MypageResponse.ReviewListDto toReviewListDto(List<Review> reviewList) {
List<MypageResponse.ReviewDto> 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<Review> reviewList) {
List<MypageResponse.ReviewDto> 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<Review> 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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReviewDto> reviewList;
}

Expand All @@ -46,6 +58,7 @@ public static class FollowerDto {
@Builder
public static class FollowerListDto {
private Integer totalCount;
private PageDto pageInfo;
private List<FollowerDto> followerList;
}

Expand All @@ -57,8 +70,6 @@ public static class UserProfileDto {
private Job job;
private Integer followerCount;
private Integer followingCount;
private Integer reviewCount;
private Integer scrapCount;
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,19 +29,19 @@ public class MypageService {
private final FollowService followService;

@Transactional(readOnly = true)
public List<Review> getMyReviewList(int page) {
public Page<Review> getMyReviewList(int page) {
Users user = userService.getCurrentUser();
return reviewService.getReviewList(user, page, SIZE);
}

@Transactional(readOnly = true)
public List<Review> getScrapReviewList(int page) {
public Page<Review> getScrapReviewList(int page) {
Users user = userService.getCurrentUser();
return reviewService.getScrapReviewList(user, page, SIZE);
}

@Transactional(readOnly = true)
public Slice<Review> getLikeReviewList(int page) {
public Page<Review> getLikeReviewList(int page) {
Users user = userService.getCurrentUser();
return reviewService.getLikeReviewList(user, page, SIZE);
}
Expand All @@ -59,13 +61,11 @@ public List<Users> 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
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Review, Long>, CustomReviewRepository {
List<Review> findFirst7ByCategoryOrderByCreatedAtDesc(Category category);
Expand Down Expand Up @@ -45,20 +48,14 @@ public interface ReviewRepository extends JpaRepository<Review, Long>, CustomRev
"ORDER BY r.id DESC")
List<Review> findAllByTargetUser(@Param("user") Users user);

@Query("SELECT r FROM REVIEW r " +
"WHERE r.writer = :user " +
"ORDER BY r.createdAt desc ")
List<Review> findAllByUser(@Param("user") Users user, Pageable pageable);
@Query("SELECT r FROM REVIEW r WHERE r.writer = :user ORDER BY r.createdAt desc ")
Page<Review> 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<Review> 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<Review> 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<Review> findAllByReviewLikedUser(@Param("user") Users user, Pageable pageable);
Page<Review> findAllByReviewLikedUser(@Param("user") Users user, Pageable pageable);

/* 좋아요 수 많은 리뷰 조회 */
Slice<Review> findByProductNameOrderByLikedCountDescCreatedAtDesc(String name, Pageable pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -292,17 +293,17 @@ public Review findReviewById(Long reviewId) {
.orElseThrow(() -> new ReviewHandler(_REVIEW_NOT_FOUND));
}

public List<Review> getReviewList(Users user, int page, int size) {
public Page<Review> getReviewList(Users user, int page, int size) {
Pageable pageable = PageRequest.of(page, size);
return this.reviewRepository.findAllByUser(user, pageable);
}

public List<Review> getScrapReviewList(Users user, int page, int size) {
public Page<Review> getScrapReviewList(Users user, int page, int size) {
Pageable pageable = PageRequest.of(page, size);
return this.reviewRepository.findAllByReviewScrapedUser(user, pageable);
}

public Slice<Review> getLikeReviewList(Users user, int page, int size) {
public Page<Review> getLikeReviewList(Users user, int page, int size) {
Pageable pageable = PageRequest.of(page, size);
return this.reviewRepository.findAllByReviewLikedUser(user, pageable);
}
Expand Down

0 comments on commit 82f89b2

Please sign in to comment.