diff --git a/src/main/java/com/back/domain/post/post/dto/request/PostSortScrollRequestDto.java b/src/main/java/com/back/domain/post/post/dto/request/PostSortScrollRequestDto.java index f4bec59..7060006 100644 --- a/src/main/java/com/back/domain/post/post/dto/request/PostSortScrollRequestDto.java +++ b/src/main/java/com/back/domain/post/post/dto/request/PostSortScrollRequestDto.java @@ -3,6 +3,7 @@ import com.back.domain.post.post.enums.PostSortStatus; public record PostSortScrollRequestDto( + Long categoryId, Long lastId, Integer lastLikeCount, Integer lastCommentCount, diff --git a/src/main/java/com/back/domain/post/post/repository/PostRepository.java b/src/main/java/com/back/domain/post/post/repository/PostRepository.java index f315672..a45fed3 100644 --- a/src/main/java/com/back/domain/post/post/repository/PostRepository.java +++ b/src/main/java/com/back/domain/post/post/repository/PostRepository.java @@ -8,17 +8,29 @@ @Repository public interface PostRepository extends JpaRepository { - // 최신순 + // 최신순 (카테고리 없음) List findTop10ByOrderByIdDesc(); List findTop10ByIdLessThanOrderByIdDesc(Long lastId); - // 추천순 + // 추천순 (카테고리 없음) List findTop10ByOrderByLikeCountDescIdDesc(); List findTop10ByLikeCountLessThanOrLikeCountEqualsAndIdLessThanOrderByLikeCountDescIdDesc( Integer likeCount, Integer likeCount2, Long id); - // 댓글순 + // 댓글순 (카테고리 없음) List findTop10ByOrderByCommentCountDescIdDesc(); List findTop10ByCommentCountLessThanOrCommentCountEqualsAndIdLessThanOrderByCommentCountDescIdDesc( Integer commentCount, Integer commentCount2, Long id); + + // 최신순 (카테고리) + List findTop10ByCategoryIdOrderByIdDesc(Long categoryId); + List findTop10ByCategoryIdAndIdLessThanOrderByIdDesc(Long categoryId, Long id); + + // 추천순 (카테고리) + List findTop10ByCategoryIdOrderByLikeCountDescIdDesc(Long categoryId); + List findTop10ByCategoryIdAndLikeCountLessThanOrLikeCountEqualsAndIdLessThanOrderByLikeCountDescIdDesc(Long categoryId, Integer likeCount, Integer likeCountEquals, Long id); + + // 댓글순 (카테고리) + List findTop10ByCategoryIdOrderByCommentCountDescIdDesc(Long categoryId); + List findTop10ByCategoryIdAndCommentCountLessThanOrCommentCountEqualsAndIdLessThanOrderByCommentCountDescIdDesc(Long categoryId, Integer commentCount, Integer commentCountEquals, Long id); } diff --git a/src/main/java/com/back/domain/post/post/service/PostService.java b/src/main/java/com/back/domain/post/post/service/PostService.java index 8e49745..5dbbe26 100644 --- a/src/main/java/com/back/domain/post/post/service/PostService.java +++ b/src/main/java/com/back/domain/post/post/service/PostService.java @@ -101,31 +101,13 @@ public PostResponseDto createPost(PostCreateRequestDto reqBody, List getPosts(PostSortScrollRequestDto reqBody) { List posts; - switch (reqBody.postSortStatus()) { - case POPULAR -> { - if (reqBody.lastId() == null || reqBody.lastLikeCount() == null) { - posts = postRepository.findTop10ByOrderByLikeCountDescIdDesc(); - } else { - posts = postRepository.findTop10ByLikeCountLessThanOrLikeCountEqualsAndIdLessThanOrderByLikeCountDescIdDesc(reqBody.lastLikeCount(), reqBody.lastLikeCount(), reqBody.lastId()); - } - } - case COMMENTS -> { - if (reqBody.lastId() == null || reqBody.lastCommentCount() == null) { - posts = postRepository.findTop10ByOrderByCommentCountDescIdDesc(); - } else { - posts = postRepository.findTop10ByCommentCountLessThanOrCommentCountEqualsAndIdLessThanOrderByCommentCountDescIdDesc(reqBody.lastCommentCount(), reqBody.lastCommentCount(), reqBody.lastId()); - } - } - case LATEST -> { - if (reqBody.lastId() == null) { - // 첫 페이지 요청 - posts = postRepository.findTop10ByOrderByIdDesc(); - } else { - // 이후 페이지 요청 - posts = postRepository.findTop10ByIdLessThanOrderByIdDesc(reqBody.lastId()); - } - } - default -> throw new IllegalArgumentException("지원하지 않는 정렬 기준: " + reqBody.postSortStatus()); + // 카테고리 ID 유무에 따른 분기 처리 + if (reqBody.categoryId() != null) { + // 카테고리별 조회 로직 + posts = findPostsByCategory(reqBody); + } else { + // 카테고리 없음 (전체) 조회 + posts = findAllPosts(reqBody); } return posts.stream() @@ -301,4 +283,67 @@ private void addTag(List tagNames, Post post) { post.addTag(tag); } } + + // 카테고리 없음 (전체) 조회 메서드 + private List findAllPosts(PostSortScrollRequestDto reqBody) { + return switch (reqBody.postSortStatus()) { + case POPULAR -> { + if (reqBody.lastId() == null || reqBody.lastLikeCount() == null) { + yield postRepository.findTop10ByOrderByLikeCountDescIdDesc(); + } else { + yield postRepository.findTop10ByLikeCountLessThanOrLikeCountEqualsAndIdLessThanOrderByLikeCountDescIdDesc(reqBody.lastLikeCount(), reqBody.lastLikeCount(), reqBody.lastId()); + } + } + case COMMENTS -> { + if (reqBody.lastId() == null || reqBody.lastCommentCount() == null) { + yield postRepository.findTop10ByOrderByCommentCountDescIdDesc(); + } else { + yield postRepository.findTop10ByCommentCountLessThanOrCommentCountEqualsAndIdLessThanOrderByCommentCountDescIdDesc(reqBody.lastCommentCount(), reqBody.lastCommentCount(), reqBody.lastId()); + } + } + case LATEST -> { + if (reqBody.lastId() == null) { + yield postRepository.findTop10ByOrderByIdDesc(); + } else { + yield postRepository.findTop10ByIdLessThanOrderByIdDesc(reqBody.lastId()); + } + } + default -> throw new IllegalArgumentException("지원하지 않는 정렬 기준: " + reqBody.postSortStatus()); + }; + } + + // 카테고리별 조회 메서드 + private List findPostsByCategory(PostSortScrollRequestDto reqBody) { + return switch (reqBody.postSortStatus()) { + case POPULAR -> { + if (reqBody.lastId() == null || reqBody.lastLikeCount() == null) { + yield postRepository.findTop10ByCategoryIdOrderByLikeCountDescIdDesc( + reqBody.categoryId()); + } else { + yield postRepository.findTop10ByCategoryIdAndLikeCountLessThanOrLikeCountEqualsAndIdLessThanOrderByLikeCountDescIdDesc( + reqBody.categoryId(), reqBody.lastLikeCount(), reqBody.lastLikeCount(), + reqBody.lastId()); + } + } + case COMMENTS -> { + if (reqBody.lastId() == null || reqBody.lastCommentCount() == null) { + yield postRepository.findTop10ByCategoryIdOrderByCommentCountDescIdDesc( + reqBody.categoryId()); + } else { + yield postRepository.findTop10ByCategoryIdAndCommentCountLessThanOrCommentCountEqualsAndIdLessThanOrderByCommentCountDescIdDesc( + reqBody.categoryId(), reqBody.lastCommentCount(), reqBody.lastCommentCount(), + reqBody.lastId()); + } + } + case LATEST -> { + if (reqBody.lastId() == null) { + yield postRepository.findTop10ByCategoryIdOrderByIdDesc(reqBody.categoryId()); + } else { + yield postRepository.findTop10ByCategoryIdAndIdLessThanOrderByIdDesc(reqBody.categoryId(), + reqBody.lastId()); + } + } + default -> throw new IllegalArgumentException("지원하지 않는 정렬 기준: " + reqBody.postSortStatus()); + }; + } } diff --git a/src/test/java/com/back/domain/post/post/controller/PostControllerTest.java b/src/test/java/com/back/domain/post/post/controller/PostControllerTest.java index e6d4957..9437e3d 100644 --- a/src/test/java/com/back/domain/post/post/controller/PostControllerTest.java +++ b/src/test/java/com/back/domain/post/post/controller/PostControllerTest.java @@ -122,8 +122,8 @@ void getPosts() throws Exception { secondPage.add(createSampleResponseDto(i)); } - given(postService.getPosts(new PostSortScrollRequestDto(null, 0, 0, PostSortStatus.LATEST))).willReturn(firstPage); // 첫 호출(lastId 없음) - given(postService.getPosts(new PostSortScrollRequestDto(21L, 0, 0, PostSortStatus.LATEST))).willReturn(secondPage); + given(postService.getPosts(new PostSortScrollRequestDto(1L, null, 0, 0, PostSortStatus.LATEST))).willReturn(firstPage); // 첫 호출(lastId 없음) + given(postService.getPosts(new PostSortScrollRequestDto(1L, 21L, 0, 0, PostSortStatus.LATEST))).willReturn(secondPage); // when & then mockMvc.perform(get("/posts"))