mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-05-28 13:37:36 +08:00
Implement AND logic for category and tag filters
This commit is contained in:
@@ -64,11 +64,19 @@ public class PostController {
|
|||||||
if (categoryId != null) {
|
if (categoryId != null) {
|
||||||
ids = java.util.List.of(categoryId);
|
ids = java.util.List.of(categoryId);
|
||||||
}
|
}
|
||||||
if ((tagId != null || (tagIds != null && !tagIds.isEmpty()))) {
|
List<Long> tids = tagIds;
|
||||||
List<Long> tids = tagIds;
|
if (tagId != null) {
|
||||||
if (tagId != null) {
|
tids = java.util.List.of(tagId);
|
||||||
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)
|
return postService.listPostsByTags(tids, page, pageSize)
|
||||||
.stream().map(this::toDto).collect(Collectors.toList());
|
.stream().map(this::toDto).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ public interface PostRepository extends JpaRepository<Post, Long> {
|
|||||||
List<Post> findByCategoryInAndStatus(List<Category> categories, PostStatus status, Pageable pageable);
|
List<Post> findByCategoryInAndStatus(List<Category> categories, PostStatus status, Pageable pageable);
|
||||||
List<Post> findDistinctByTagsInAndStatus(List<Tag> tags, PostStatus status);
|
List<Post> findDistinctByTagsInAndStatus(List<Tag> tags, PostStatus status);
|
||||||
List<Post> findDistinctByTagsInAndStatus(List<Tag> tags, PostStatus status, Pageable pageable);
|
List<Post> findDistinctByTagsInAndStatus(List<Tag> tags, PostStatus status, Pageable pageable);
|
||||||
|
List<Post> findDistinctByCategoryInAndTagsInAndStatus(List<Category> categories, List<Tag> tags, PostStatus status);
|
||||||
|
List<Post> findDistinctByCategoryInAndTagsInAndStatus(List<Category> categories, List<Tag> tags, PostStatus status, Pageable pageable);
|
||||||
List<Post> findByTitleContainingIgnoreCaseOrContentContainingIgnoreCaseAndStatus(String titleKeyword, String contentKeyword, PostStatus status);
|
List<Post> findByTitleContainingIgnoreCaseOrContentContainingIgnoreCaseAndStatus(String titleKeyword, String contentKeyword, PostStatus status);
|
||||||
List<Post> findByContentContainingIgnoreCaseAndStatus(String keyword, PostStatus status);
|
List<Post> findByContentContainingIgnoreCaseAndStatus(String keyword, PostStatus status);
|
||||||
List<Post> findByTitleContainingIgnoreCaseAndStatus(String keyword, PostStatus status);
|
List<Post> findByTitleContainingIgnoreCaseAndStatus(String keyword, PostStatus status);
|
||||||
|
|||||||
@@ -168,6 +168,31 @@ public class PostService {
|
|||||||
return postRepository.findDistinctByTagsInAndStatus(tags, PostStatus.PUBLISHED);
|
return postRepository.findDistinctByTagsInAndStatus(tags, PostStatus.PUBLISHED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Post> listPostsByCategoriesAndTags(java.util.List<Long> categoryIds,
|
||||||
|
java.util.List<Long> 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<Category> categories = categoryRepository.findAllById(categoryIds);
|
||||||
|
java.util.List<com.openisle.model.Tag> 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<Post> listPendingPosts() {
|
public List<Post> listPendingPosts() {
|
||||||
return postRepository.findByStatus(PostStatus.PENDING);
|
return postRepository.findByStatus(PostStatus.PENDING);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -190,7 +190,7 @@ class PostControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void listPostsByTags() throws Exception {
|
void listPostsByCategoriesAndTags() throws Exception {
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setUsername("alice");
|
user.setUsername("alice");
|
||||||
Category cat = new Category();
|
Category cat = new Category();
|
||||||
@@ -206,7 +206,7 @@ class PostControllerTest {
|
|||||||
post.setCategory(cat);
|
post.setCategory(cat);
|
||||||
post.setTags(java.util.Set.of(tag));
|
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));
|
.thenReturn(List.of(post));
|
||||||
|
|
||||||
mockMvc.perform(get("/api/posts")
|
mockMvc.perform(get("/api/posts")
|
||||||
@@ -217,7 +217,7 @@ class PostControllerTest {
|
|||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(jsonPath("$[0].id").value(2));
|
.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());
|
verify(postService, never()).listPostsByCategories(any(), any(), any());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user