diff --git a/src/main/java/com/openisle/controller/PostController.java b/src/main/java/com/openisle/controller/PostController.java index 43a15e6de..026ebd4f6 100644 --- a/src/main/java/com/openisle/controller/PostController.java +++ b/src/main/java/com/openisle/controller/PostController.java @@ -64,11 +64,19 @@ public class PostController { if (categoryId != null) { ids = java.util.List.of(categoryId); } - if ((tagId != null || (tagIds != null && !tagIds.isEmpty()))) { - List tids = tagIds; - if (tagId != null) { - tids = java.util.List.of(tagId); - } + List tids = tagIds; + if (tagId != null) { + tids = java.util.List.of(tagId); + } + + boolean hasCategories = ids != null && !ids.isEmpty(); + boolean hasTags = tids != null && !tids.isEmpty(); + + if (hasCategories && hasTags) { + return postService.listPostsByCategoriesAndTags(ids, tids, page, pageSize) + .stream().map(this::toDto).collect(Collectors.toList()); + } + if (hasTags) { return postService.listPostsByTags(tids, page, pageSize) .stream().map(this::toDto).collect(Collectors.toList()); } diff --git a/src/main/java/com/openisle/repository/PostRepository.java b/src/main/java/com/openisle/repository/PostRepository.java index 2dd8fd1c3..55a213dbf 100644 --- a/src/main/java/com/openisle/repository/PostRepository.java +++ b/src/main/java/com/openisle/repository/PostRepository.java @@ -21,6 +21,8 @@ public interface PostRepository extends JpaRepository { List findByCategoryInAndStatus(List categories, PostStatus status, Pageable pageable); List findDistinctByTagsInAndStatus(List tags, PostStatus status); List findDistinctByTagsInAndStatus(List tags, PostStatus status, Pageable pageable); + List findDistinctByCategoryInAndTagsInAndStatus(List categories, List tags, PostStatus status); + List findDistinctByCategoryInAndTagsInAndStatus(List categories, List tags, PostStatus status, Pageable pageable); List findByTitleContainingIgnoreCaseOrContentContainingIgnoreCaseAndStatus(String titleKeyword, String contentKeyword, PostStatus status); List findByContentContainingIgnoreCaseAndStatus(String keyword, PostStatus status); List findByTitleContainingIgnoreCaseAndStatus(String keyword, PostStatus status); diff --git a/src/main/java/com/openisle/service/PostService.java b/src/main/java/com/openisle/service/PostService.java index d8fbf085c..7efec2926 100644 --- a/src/main/java/com/openisle/service/PostService.java +++ b/src/main/java/com/openisle/service/PostService.java @@ -168,6 +168,31 @@ public class PostService { return postRepository.findDistinctByTagsInAndStatus(tags, PostStatus.PUBLISHED); } + public List listPostsByCategoriesAndTags(java.util.List categoryIds, + java.util.List tagIds, + Integer page, + Integer pageSize) { + if (categoryIds == null || categoryIds.isEmpty() || tagIds == null || tagIds.isEmpty()) { + return java.util.List.of(); + } + + Pageable pageable = null; + if (page != null && pageSize != null) { + pageable = PageRequest.of(page, pageSize); + } + + java.util.List categories = categoryRepository.findAllById(categoryIds); + java.util.List tags = tagRepository.findAllById(tagIds); + if (categories.isEmpty() || tags.isEmpty()) { + return java.util.List.of(); + } + + if (pageable != null) { + return postRepository.findDistinctByCategoryInAndTagsInAndStatus(categories, tags, PostStatus.PUBLISHED, pageable); + } + return postRepository.findDistinctByCategoryInAndTagsInAndStatus(categories, tags, PostStatus.PUBLISHED); + } + public List listPendingPosts() { return postRepository.findByStatus(PostStatus.PENDING); } diff --git a/src/test/java/com/openisle/controller/PostControllerTest.java b/src/test/java/com/openisle/controller/PostControllerTest.java index d7ab4c76c..2ecad2598 100644 --- a/src/test/java/com/openisle/controller/PostControllerTest.java +++ b/src/test/java/com/openisle/controller/PostControllerTest.java @@ -190,7 +190,7 @@ class PostControllerTest { } @Test - void listPostsByTags() throws Exception { + void listPostsByCategoriesAndTags() throws Exception { User user = new User(); user.setUsername("alice"); Category cat = new Category(); @@ -206,7 +206,7 @@ class PostControllerTest { post.setCategory(cat); post.setTags(java.util.Set.of(tag)); - Mockito.when(postService.listPostsByTags(eq(java.util.List.of(1L, 2L)), eq(0), eq(5))) + Mockito.when(postService.listPostsByCategoriesAndTags(eq(java.util.List.of(1L)), eq(java.util.List.of(1L, 2L)), eq(0), eq(5))) .thenReturn(List.of(post)); mockMvc.perform(get("/api/posts") @@ -217,7 +217,7 @@ class PostControllerTest { .andExpect(status().isOk()) .andExpect(jsonPath("$[0].id").value(2)); - verify(postService).listPostsByTags(eq(java.util.List.of(1L, 2L)), eq(0), eq(5)); + verify(postService).listPostsByCategoriesAndTags(eq(java.util.List.of(1L)), eq(java.util.List.of(1L, 2L)), eq(0), eq(5)); verify(postService, never()).listPostsByCategories(any(), any(), any()); } }