Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.back.domain.post.post.enums.PostSortStatus;

public record PostSortScrollRequestDto(
Long categoryId,
Long lastId,
Integer lastLikeCount,
Integer lastCommentCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,29 @@
@Repository
public interface PostRepository extends JpaRepository<Post, Long> {

// 최신순
// 최신순 (카테고리 없음)
List<Post> findTop10ByOrderByIdDesc();
List<Post> findTop10ByIdLessThanOrderByIdDesc(Long lastId);

// 추천순
// 추천순 (카테고리 없음)
List<Post> findTop10ByOrderByLikeCountDescIdDesc();
List<Post> findTop10ByLikeCountLessThanOrLikeCountEqualsAndIdLessThanOrderByLikeCountDescIdDesc(
Integer likeCount, Integer likeCount2, Long id);

// 댓글순
// 댓글순 (카테고리 없음)
List<Post> findTop10ByOrderByCommentCountDescIdDesc();
List<Post> findTop10ByCommentCountLessThanOrCommentCountEqualsAndIdLessThanOrderByCommentCountDescIdDesc(
Integer commentCount, Integer commentCount2, Long id);

// 최신순 (카테고리)
List<Post> findTop10ByCategoryIdOrderByIdDesc(Long categoryId);
List<Post> findTop10ByCategoryIdAndIdLessThanOrderByIdDesc(Long categoryId, Long id);

// 추천순 (카테고리)
List<Post> findTop10ByCategoryIdOrderByLikeCountDescIdDesc(Long categoryId);
List<Post> findTop10ByCategoryIdAndLikeCountLessThanOrLikeCountEqualsAndIdLessThanOrderByLikeCountDescIdDesc(Long categoryId, Integer likeCount, Integer likeCountEquals, Long id);

// 댓글순 (카테고리)
List<Post> findTop10ByCategoryIdOrderByCommentCountDescIdDesc(Long categoryId);
List<Post> findTop10ByCategoryIdAndCommentCountLessThanOrCommentCountEqualsAndIdLessThanOrderByCommentCountDescIdDesc(Long categoryId, Integer commentCount, Integer commentCountEquals, Long id);
}
95 changes: 70 additions & 25 deletions src/main/java/com/back/domain/post/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,31 +101,13 @@ public PostResponseDto createPost(PostCreateRequestDto reqBody, List<MultipartFi
public List<PostResponseDto> getPosts(PostSortScrollRequestDto reqBody) {
List<Post> 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()
Expand Down Expand Up @@ -301,4 +283,67 @@ private void addTag(List<String> tagNames, Post post) {
post.addTag(tag);
}
}

// 카테고리 없음 (전체) 조회 메서드
private List<Post> 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<Post> 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());
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down