From d9e61ac75668422912ea5f916b06c89914cd7821 Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Tue, 1 Jul 2025 16:03:07 +0800 Subject: [PATCH] Add post listing filters and pagination --- .../openisle/controller/PostController.java | 12 ++++++++-- .../openisle/repository/PostRepository.java | 4 ++++ .../com/openisle/service/PostService.java | 24 ++++++++++++++++++- .../controller/PostControllerTest.java | 3 ++- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/openisle/controller/PostController.java b/src/main/java/com/openisle/controller/PostController.java index 3985b26a9..2cb44c16b 100644 --- a/src/main/java/com/openisle/controller/PostController.java +++ b/src/main/java/com/openisle/controller/PostController.java @@ -49,8 +49,16 @@ public class PostController { } @GetMapping - public List listPosts() { - return postService.listPosts().stream().map(this::toDto).collect(Collectors.toList()); + public List listPosts(@RequestParam(value = "categoryId", required = false) Long categoryId, + @RequestParam(value = "categoryIds", required = false) List categoryIds, + @RequestParam(value = "page", required = false) Integer page, + @RequestParam(value = "pageSize", required = false) Integer pageSize) { + List ids = categoryIds; + if (categoryId != null) { + ids = java.util.List.of(categoryId); + } + return postService.listPostsByCategories(ids, page, pageSize) + .stream().map(this::toDto).collect(Collectors.toList()); } private PostDto toDto(Post post) { diff --git a/src/main/java/com/openisle/repository/PostRepository.java b/src/main/java/com/openisle/repository/PostRepository.java index e328d7b72..00c2b158c 100644 --- a/src/main/java/com/openisle/repository/PostRepository.java +++ b/src/main/java/com/openisle/repository/PostRepository.java @@ -3,6 +3,7 @@ package com.openisle.repository; import com.openisle.model.Post; import com.openisle.model.PostStatus; import com.openisle.model.User; +import com.openisle.model.Category; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; @@ -10,5 +11,8 @@ import java.util.List; public interface PostRepository extends JpaRepository { List findByStatus(PostStatus status); + List findByStatus(PostStatus status, Pageable pageable); List findByAuthorAndStatusOrderByCreatedAtDesc(User author, PostStatus status, Pageable pageable); + List findByCategoryInAndStatus(List categories, PostStatus status); + List findByCategoryInAndStatus(List categories, PostStatus status, Pageable pageable); } diff --git a/src/main/java/com/openisle/service/PostService.java b/src/main/java/com/openisle/service/PostService.java index 15f1b3020..738c2e4de 100644 --- a/src/main/java/com/openisle/service/PostService.java +++ b/src/main/java/com/openisle/service/PostService.java @@ -58,7 +58,29 @@ public class PostService { } public List listPosts() { - return postRepository.findByStatus(PostStatus.PUBLISHED); + return listPostsByCategories(null, null, null); + } + + public List listPostsByCategories(java.util.List categoryIds, + Integer page, + Integer pageSize) { + Pageable pageable = null; + if (page != null && pageSize != null) { + pageable = PageRequest.of(page, pageSize); + } + + if (categoryIds == null || categoryIds.isEmpty()) { + if (pageable != null) { + return postRepository.findByStatus(PostStatus.PUBLISHED, pageable); + } + return postRepository.findByStatus(PostStatus.PUBLISHED); + } + + java.util.List categories = categoryRepository.findAllById(categoryIds); + if (pageable != null) { + return postRepository.findByCategoryInAndStatus(categories, PostStatus.PUBLISHED, pageable); + } + return postRepository.findByCategoryInAndStatus(categories, PostStatus.PUBLISHED); } public List getRecentPostsByUser(String username, int limit) { diff --git a/src/test/java/com/openisle/controller/PostControllerTest.java b/src/test/java/com/openisle/controller/PostControllerTest.java index d2155896e..8824987b3 100644 --- a/src/test/java/com/openisle/controller/PostControllerTest.java +++ b/src/test/java/com/openisle/controller/PostControllerTest.java @@ -81,7 +81,8 @@ class PostControllerTest { post.setCreatedAt(LocalDateTime.now()); post.setAuthor(user); post.setCategory(cat); - Mockito.when(postService.listPosts()).thenReturn(List.of(post)); + Mockito.when(postService.listPostsByCategories(Mockito.isNull(), Mockito.isNull(), Mockito.isNull())) + .thenReturn(List.of(post)); mockMvc.perform(get("/api/posts")) .andExpect(status().isOk())