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 @@ -68,9 +68,9 @@ ResponseEntity<BaseResponse<InfiniteResponse<PostDetailResponse>>> getPostByCate
type = "string",
allowableValues = {"FREE", "REPAIR", "INFO"},
example = "FREE"))
@RequestParam
@RequestParam(required = false)
String category,
@Parameter(description = "마지막으로 조회한 게시글 식별자(첫 조회 시 생략)", example = "3")
@Parameter(description = "마지막으로 조회한 게시글 식별자(첫 조회 시 생략, 최신순이라서 식별자 값 감소)", example = "3")
@RequestParam(required = false)
Long lastPostId,
@Parameter(description = "한 번에 조회할 게시글 개수", example = "3") @RequestParam(defaultValue = "3")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ public interface PostRepository extends JpaRepository<Post, Long> {

Page<Post> findByPostCategory(PostCategory category, Pageable pageable);

Page<Post> findByPostCategoryContainingAndIdLessThan(
String category, Long lastPostId, Pageable pageable);

Page<Post> findAllByUser_Id(Long userId, Pageable pageable);

Page<Post> findAllByUser_IdAndIdLessThan(Long userId, Long lastPostId, Pageable pageable);
Page<Post> findByIdLessThan(Long id, Pageable pageable);

Page<Post> findByPostCategoryAndIdLessThan(PostCategory category, Long id, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,28 @@ public InfiniteResponse<PostDetailResponse> getPostsByCategory(
Pageable pageable = PageRequest.of(0, size + 1, Sort.by(Sort.Direction.DESC, "id"));
List<Post> posts;

PostCategory postCategory;
try {
postCategory = PostCategory.valueOf(category);
} catch (IllegalArgumentException e) {
throw new CustomException(PostErrorCode.INVALID_CATEGORY);
}

if (lastPostId == null) {
posts = postRepository.findByPostCategory(postCategory, pageable).getContent();
if (category == null || category.isBlank()) {
if (lastPostId == null) {
posts = postRepository.findAll(pageable).getContent();
} else {
posts = postRepository.findByIdLessThan(lastPostId, pageable).getContent();
}
} else {
posts =
postRepository
.findByPostCategoryContainingAndIdLessThan(category, lastPostId, pageable)
.getContent();
PostCategory postCategory;
try {
postCategory = PostCategory.valueOf(category);
} catch (IllegalArgumentException e) {
throw new CustomException(PostErrorCode.INVALID_CATEGORY);
}

if (lastPostId == null) {
posts = postRepository.findByPostCategory(postCategory, pageable).getContent();
} else {
posts =
postRepository
.findByPostCategoryAndIdLessThan(postCategory, lastPostId, pageable)
.getContent();
}
}

boolean hasNext = posts.size() > size;
Expand Down Expand Up @@ -204,7 +212,7 @@ public InfiniteResponse<PostDetailResponse> getPostsByCategory(
user))
.toList();

Long newLastCursor = posts.isEmpty() ? null : posts.getLast().getId();
Long newLastCursor = posts.isEmpty() ? null : posts.get(posts.size() - 1).getId();

log.info(
"[POST CATEGORY LIST] category={}, lastPostId={}, size={}", category, lastPostId, size);
Expand Down