Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: getPostBy() does not return posts in subcategories #2405

Merged
merged 2 commits into from
Sep 13, 2022
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 @@ -123,7 +123,11 @@ public List<Post> listPostBy(Integer categoryId) {
Assert.notNull(categoryId, "Category id must not be null");

// Find all post ids
Set<Integer> postIds = postCategoryRepository.findAllPostIdsByCategoryId(categoryId);
Set<Integer> postIds = categoryService.listAllByParentId(categoryId)
.stream()
.map(item -> postCategoryRepository.findAllPostIdsByCategoryId(item.getId()))
.flatMap(Set::stream)
.collect(Collectors.toSet());

return postRepository.findAllById(postIds);
}
Expand All @@ -134,8 +138,11 @@ public List<Post> listPostBy(Integer categoryId, PostStatus status) {
Assert.notNull(status, "Post status must not be null");

// Find all post ids
Set<Integer> postIds =
postCategoryRepository.findAllPostIdsByCategoryId(categoryId, status);
Set<Integer> postIds = categoryService.listAllByParentId(categoryId)
.stream()
.map(item -> postCategoryRepository.findAllPostIdsByCategoryId(item.getId(), status))
.flatMap(Set::stream)
.collect(Collectors.toSet());

return postRepository.findAllById(postIds);
}
Expand All @@ -146,8 +153,11 @@ public List<Post> listPostBy(Integer categoryId, Set<PostStatus> status) {
Assert.notNull(status, "Post status must not be null");

// Find all post ids
Set<Integer> postIds = postCategoryRepository
.findAllPostIdsByCategoryId(categoryId, status);
Set<Integer> postIds = categoryService.listAllByParentId(categoryId)
.stream()
.map(item -> postCategoryRepository.findAllPostIdsByCategoryId(item.getId(), status))
.flatMap(Set::stream)
.collect(Collectors.toSet());

return postRepository.findAllById(postIds);
}
Expand All @@ -163,10 +173,13 @@ public List<Post> listPostBy(String slug, Set<PostStatus> status) {
throw new NotFoundException("查询不到该分类的信息").setErrorData(slug);
}

Set<Integer> postsIds = postCategoryRepository
.findAllPostIdsByCategoryId(category.getId(), status);
Set<Integer> postIds = categoryService.listAllByParentId(category.getId())
.stream()
.map(item -> postCategoryRepository.findAllPostIdsByCategoryId(item.getId(), status))
.flatMap(Set::stream)
.collect(Collectors.toSet());

return postRepository.findAllById(postsIds);
return postRepository.findAllById(postIds);
}

@Override
Expand All @@ -180,10 +193,13 @@ public List<Post> listPostBy(String slug, PostStatus status) {
throw new NotFoundException("查询不到该分类的信息").setErrorData(slug);
}

Set<Integer> postsIds =
postCategoryRepository.findAllPostIdsByCategoryId(category.getId(), status);
Set<Integer> postIds = categoryService.listAllByParentId(category.getId())
.stream()
.map(item -> postCategoryRepository.findAllPostIdsByCategoryId(item.getId(), status))
.flatMap(Set::stream)
.collect(Collectors.toSet());

return postRepository.findAllById(postsIds);
return postRepository.findAllById(postIds);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package run.halo.app.service.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import java.util.Set;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import run.halo.app.model.entity.Category;
import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.PostCategory;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.service.CategoryService;

@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
class PostCategoryServiceImplTest {

@Autowired
private CategoryService categoryService;

@Autowired
private PostCategoryServiceImpl postCategoryService;

@Autowired
private PostServiceImpl postService;

@BeforeAll
public void setUp() {
Category category1 = new Category();
category1.setId(1);
category1.setName("分类-1");
category1.setSlug("category-1");

Category category2 = new Category();
category2.setId(2);
category2.setName("分类-1-1");
category2.setSlug("category-1-1");
category2.setParentId(1);

Category category3 = new Category();
category3.setId(3);
category3.setName("分类-1-1-1");
category3.setSlug("category-1-1-1");
category3.setParentId(2);

Category category4 = new Category();
category4.setId(4);
category4.setName("分类-1-1-2");
category4.setSlug("category-1-1-2");
category4.setParentId(2);

categoryService.create(category1);
categoryService.create(category2);
categoryService.create(category3);
categoryService.create(category4);

Post post1 = new Post();
post1.setId(1);
post1.setTitle("post1");
post1.setSlug("post1");

Post post2 = new Post();
post2.setId(2);
post2.setTitle("post2");
post2.setSlug("post2");
post2.setStatus(PostStatus.PUBLISHED);

Post post3 = new Post();
post3.setId(3);
post3.setTitle("post3");
post3.setSlug("post3");
post3.setStatus(PostStatus.PUBLISHED);

Post post4 = new Post();
post4.setId(4);
post4.setTitle("post4");
post4.setSlug("post4");

postService.create(post1);
postService.create(post2);
postService.create(post3);
postService.create(post4);

PostCategory postCategory1 = new PostCategory();
postCategory1.setCategoryId(1);
postCategory1.setPostId(1);

PostCategory postCategory2 = new PostCategory();
postCategory2.setCategoryId(2);
postCategory2.setPostId(2);

PostCategory postCategory3 = new PostCategory();
postCategory3.setCategoryId(3);
postCategory3.setPostId(3);

PostCategory postCategory4 = new PostCategory();
postCategory4.setCategoryId(4);
postCategory4.setPostId(4);

postCategoryService.create(postCategory1);
postCategoryService.create(postCategory2);
postCategoryService.create(postCategory3);
postCategoryService.create(postCategory4);
}

@Test
void listPostByCategoryIdTest() {
List<Post> posts = postCategoryService.listPostBy(1);
assertEquals(List.of(postService.getById(1), postService.getById(2), postService.getById(3),
postService.getById(4)), posts);
}

@Test
void listPostByCategoryIdAndStatusTest() {
List<Post> posts = postCategoryService.listPostBy(1, PostStatus.PUBLISHED);
assertEquals(List.of(postService.getById(2), postService.getById(3)), posts);
}

@Test
void listPostByCategoryIdAndStatusesTest() {
List<Post> posts =
postCategoryService.listPostBy(1, Set.of(PostStatus.PUBLISHED, PostStatus.DRAFT));
assertEquals(List.of(postService.getById(1), postService.getById(2), postService.getById(3),
postService.getById(4)), posts);
}

@Test
void listPostBySlugIdAndStatusTest() {
List<Post> posts = postCategoryService.listPostBy("category-1", PostStatus.PUBLISHED);
assertEquals(List.of(postService.getById(2), postService.getById(3)), posts);
}

@Test
void listPostBySlugIdAndStatusesTest() {
List<Post> posts = postCategoryService.listPostBy("category-1",
Set.of(PostStatus.PUBLISHED, PostStatus.DRAFT));
assertEquals(List.of(postService.getById(1), postService.getById(2), postService.getById(3),
postService.getById(4)), posts);
}
}