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
@@ -1,6 +1,7 @@
package com.back.domain.post.post.repository;

import com.back.domain.post.post.entity.Post;
import com.back.domain.post.post.enums.PostStatus;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
Expand All @@ -9,28 +10,31 @@
public interface PostRepository extends JpaRepository<Post, Long> {

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

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

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

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

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

// 댓글순 (카테고리)
List<Post> findTop10ByCategoryIdOrderByCommentCountDescIdDesc(Long categoryId);
List<Post> findTop10ByCategoryIdAndCommentCountLessThanOrCommentCountEqualsAndIdLessThanOrderByCommentCountDescIdDesc(Long categoryId, Integer commentCount, Integer commentCountEquals, Long id);
List<Post> findTop10ByCategoryIdAndStatusNotOrderByCommentCountDescIdDesc(Long categoryId, PostStatus status);
List<Post> findTop10ByCategoryIdAndStatusNotAndCommentCountLessThanOrCommentCountEqualsAndIdLessThanOrderByCommentCountDescIdDesc(
Long categoryId, PostStatus status, Integer commentCount, Integer commentCountEquals, Long id);
}

32 changes: 16 additions & 16 deletions src/main/java/com/back/domain/post/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,23 +292,23 @@ private List<Post> findAllPosts(PostSortScrollRequestDto reqBody) {
return switch (reqBody.postSortStatus()) {
case POPULAR -> {
if (reqBody.lastId() == null || reqBody.lastLikeCount() == null) {
yield postRepository.findTop10ByOrderByLikeCountDescIdDesc();
yield postRepository.findTop10ByStatusNotOrderByLikeCountDescIdDesc(PostStatus.DELETED);
} else {
yield postRepository.findTop10ByLikeCountLessThanOrLikeCountEqualsAndIdLessThanOrderByLikeCountDescIdDesc(reqBody.lastLikeCount(), reqBody.lastLikeCount(), reqBody.lastId());
yield postRepository.findTop10ByStatusNotAndLikeCountLessThanOrLikeCountEqualsAndIdLessThanOrderByLikeCountDescIdDesc(PostStatus.DELETED, reqBody.lastLikeCount(), reqBody.lastLikeCount(), reqBody.lastId());
}
}
case COMMENTS -> {
if (reqBody.lastId() == null || reqBody.lastCommentCount() == null) {
yield postRepository.findTop10ByOrderByCommentCountDescIdDesc();
yield postRepository.findTop10ByStatusNotOrderByCommentCountDescIdDesc(PostStatus.DELETED);
} else {
yield postRepository.findTop10ByCommentCountLessThanOrCommentCountEqualsAndIdLessThanOrderByCommentCountDescIdDesc(reqBody.lastCommentCount(), reqBody.lastCommentCount(), reqBody.lastId());
yield postRepository.findTop10ByStatusNotAndCommentCountLessThanOrCommentCountEqualsAndIdLessThanOrderByCommentCountDescIdDesc(PostStatus.DELETED, reqBody.lastCommentCount(), reqBody.lastCommentCount(), reqBody.lastId());
}
}
case LATEST -> {
if (reqBody.lastId() == null) {
yield postRepository.findTop10ByOrderByIdDesc();
yield postRepository.findTop10ByStatusNotOrderByIdDesc(PostStatus.DELETED);
} else {
yield postRepository.findTop10ByIdLessThanOrderByIdDesc(reqBody.lastId());
yield postRepository.findTop10ByStatusNotAndIdLessThanOrderByIdDesc(PostStatus.DELETED, reqBody.lastId());
}
}
default -> throw new IllegalArgumentException("지원하지 않는 정렬 기준: " + reqBody.postSortStatus());
Expand All @@ -320,29 +320,29 @@ private List<Post> findPostsByCategory(PostSortScrollRequestDto reqBody) {
return switch (reqBody.postSortStatus()) {
case POPULAR -> {
if (reqBody.lastId() == null || reqBody.lastLikeCount() == null) {
yield postRepository.findTop10ByCategoryIdOrderByLikeCountDescIdDesc(
reqBody.categoryId());
yield postRepository.findTop10ByCategoryIdAndStatusNotOrderByLikeCountDescIdDesc(
reqBody.categoryId(), PostStatus.DELETED);
} else {
yield postRepository.findTop10ByCategoryIdAndLikeCountLessThanOrLikeCountEqualsAndIdLessThanOrderByLikeCountDescIdDesc(
reqBody.categoryId(), reqBody.lastLikeCount(), reqBody.lastLikeCount(),
yield postRepository.findTop10ByCategoryIdAndStatusNotAndLikeCountLessThanOrLikeCountEqualsAndIdLessThanOrderByLikeCountDescIdDesc(
reqBody.categoryId(), PostStatus.DELETED, reqBody.lastLikeCount(), reqBody.lastLikeCount(),
reqBody.lastId());
}
}
case COMMENTS -> {
if (reqBody.lastId() == null || reqBody.lastCommentCount() == null) {
yield postRepository.findTop10ByCategoryIdOrderByCommentCountDescIdDesc(
reqBody.categoryId());
yield postRepository.findTop10ByCategoryIdAndStatusNotOrderByCommentCountDescIdDesc(
reqBody.categoryId(), PostStatus.DELETED);
} else {
yield postRepository.findTop10ByCategoryIdAndCommentCountLessThanOrCommentCountEqualsAndIdLessThanOrderByCommentCountDescIdDesc(
reqBody.categoryId(), reqBody.lastCommentCount(), reqBody.lastCommentCount(),
yield postRepository.findTop10ByCategoryIdAndStatusNotAndCommentCountLessThanOrCommentCountEqualsAndIdLessThanOrderByCommentCountDescIdDesc(
reqBody.categoryId(), PostStatus.DELETED, reqBody.lastCommentCount(), reqBody.lastCommentCount(),
reqBody.lastId());
}
}
case LATEST -> {
if (reqBody.lastId() == null) {
yield postRepository.findTop10ByCategoryIdOrderByIdDesc(reqBody.categoryId());
yield postRepository.findTop10ByCategoryIdAndStatusNotOrderByIdDesc(reqBody.categoryId(), PostStatus.DELETED);
} else {
yield postRepository.findTop10ByCategoryIdAndIdLessThanOrderByIdDesc(reqBody.categoryId(),
yield postRepository.findTop10ByCategoryIdAndStatusNotAndIdLessThanOrderByIdDesc(reqBody.categoryId(), PostStatus.DELETED,
reqBody.lastId());
}
}
Expand Down