mirror of https://github.com/halo-dev/halo
feat: refactor PostTagDirective.
parent
537d28ba1c
commit
192890b5f2
|
@ -60,10 +60,18 @@ public class PostTagDirective implements TemplateDirectiveModel {
|
|||
Integer categoryId = Integer.parseInt(params.get("categoryId").toString());
|
||||
env.setVariable("posts", builder.build().wrap(postCategoryService.listPostBy(categoryId, PostStatus.PUBLISHED)));
|
||||
break;
|
||||
case "listByCategorySlug":
|
||||
String categorySlug = params.get("categorySlug").toString();
|
||||
env.setVariable("posts", builder.build().wrap(postCategoryService.listPostBy(categorySlug, PostStatus.PUBLISHED)));
|
||||
break;
|
||||
case "listByTagId":
|
||||
Integer tagId = Integer.parseInt(params.get("tagId").toString());
|
||||
env.setVariable("posts", builder.build().wrap(postTagService.listPostsBy(tagId, PostStatus.PUBLISHED)));
|
||||
break;
|
||||
case "listByTagSlug":
|
||||
String tagSlug = params.get("tagSlug").toString();
|
||||
env.setVariable("posts", builder.build().wrap(postTagService.listPostsBy(tagSlug, PostStatus.PUBLISHED)));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -64,6 +64,16 @@ public interface PostCategoryService extends CrudService<PostCategory, Integer>
|
|||
@NonNull
|
||||
List<Post> listPostBy(@NonNull Integer categoryId, @NonNull PostStatus status);
|
||||
|
||||
/**
|
||||
* Lists post by category slug and post status.
|
||||
*
|
||||
* @param slug category slug must not be null
|
||||
* @param status post status
|
||||
* @return a list of post
|
||||
*/
|
||||
@NonNull
|
||||
List<Post> listPostBy(@NonNull String slug, @NonNull PostStatus status);
|
||||
|
||||
/**
|
||||
* Pages post by category slug name.
|
||||
*
|
||||
|
|
|
@ -73,6 +73,16 @@ public interface PostTagService extends CrudService<PostTag, Integer> {
|
|||
@NonNull
|
||||
List<Post> listPostsBy(@NonNull Integer tagId, @NonNull PostStatus status);
|
||||
|
||||
/**
|
||||
* Lists posts by tag slug and post status.
|
||||
*
|
||||
* @param slug tag slug must not be null
|
||||
* @param status post status
|
||||
* @return a list of post
|
||||
*/
|
||||
@NonNull
|
||||
List<Post> listPostsBy(@NonNull String slug, @NonNull PostStatus status);
|
||||
|
||||
/**
|
||||
* Pages posts by tag id.
|
||||
*
|
||||
|
|
|
@ -6,6 +6,7 @@ import org.springframework.data.domain.Sort;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import run.halo.app.exception.NotFoundException;
|
||||
import run.halo.app.model.dto.CategoryWithPostCountDTO;
|
||||
import run.halo.app.model.entity.Category;
|
||||
import run.halo.app.model.entity.Post;
|
||||
|
@ -98,7 +99,7 @@ public class PostCategoryServiceImpl extends AbstractCrudService<PostCategory, I
|
|||
@Override
|
||||
public List<Post> listPostBy(Integer categoryId, PostStatus status) {
|
||||
Assert.notNull(categoryId, "Category id must not be null");
|
||||
Assert.notNull(categoryId, "Post status must not be null");
|
||||
Assert.notNull(status, "Post status must not be null");
|
||||
|
||||
// Find all post ids
|
||||
Set<Integer> postIds = postCategoryRepository.findAllPostIdsByCategoryId(categoryId, status);
|
||||
|
@ -106,6 +107,18 @@ public class PostCategoryServiceImpl extends AbstractCrudService<PostCategory, I
|
|||
return postRepository.findAllById(postIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Post> listPostBy(String slug, PostStatus status) {
|
||||
Assert.notNull(slug, "Category slug must not be null");
|
||||
Assert.notNull(status, "Post status must not be null");
|
||||
|
||||
Category category = categoryRepository.getBySlugName(slug).orElseThrow(() -> new NotFoundException("查询不到该分类的信息").setErrorData(slug));
|
||||
|
||||
Set<Integer> postsIds = postCategoryRepository.findAllPostIdsByCategoryId(category.getId(), status);
|
||||
|
||||
return postRepository.findAllById(postsIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<Post> pagePostBy(Integer categoryId, Pageable pageable) {
|
||||
Assert.notNull(categoryId, "Category id must not be null");
|
||||
|
@ -120,7 +133,7 @@ public class PostCategoryServiceImpl extends AbstractCrudService<PostCategory, I
|
|||
@Override
|
||||
public Page<Post> pagePostBy(Integer categoryId, PostStatus status, Pageable pageable) {
|
||||
Assert.notNull(categoryId, "Category id must not be null");
|
||||
Assert.notNull(categoryId, "Post status must not be null");
|
||||
Assert.notNull(status, "Post status must not be null");
|
||||
Assert.notNull(pageable, "Page info must not be null");
|
||||
|
||||
// Find all post ids
|
||||
|
|
|
@ -6,6 +6,7 @@ import org.springframework.data.domain.Sort;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import run.halo.app.exception.NotFoundException;
|
||||
import run.halo.app.model.dto.TagWithPostCountDTO;
|
||||
import run.halo.app.model.entity.Post;
|
||||
import run.halo.app.model.entity.PostTag;
|
||||
|
@ -118,7 +119,7 @@ public class PostTagServiceImpl extends AbstractCrudService<PostTag, Integer> im
|
|||
@Override
|
||||
public List<Post> listPostsBy(Integer tagId, PostStatus status) {
|
||||
Assert.notNull(tagId, "Tag id must not be null");
|
||||
Assert.notNull(tagId, "Post status must not be null");
|
||||
Assert.notNull(status, "Post status must not be null");
|
||||
|
||||
// Find all post ids
|
||||
Set<Integer> postIds = postTagRepository.findAllPostIdsByTagId(tagId, status);
|
||||
|
@ -126,6 +127,18 @@ public class PostTagServiceImpl extends AbstractCrudService<PostTag, Integer> im
|
|||
return postRepository.findAllById(postIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Post> listPostsBy(String slug, PostStatus status) {
|
||||
Assert.notNull(slug, "Tag slug must not be null");
|
||||
Assert.notNull(status, "Post status must not be null");
|
||||
|
||||
Tag tag = tagRepository.getBySlugName(slug).orElseThrow(() -> new NotFoundException("查询不到该标签的信息").setErrorData(slug));
|
||||
|
||||
Set<Integer> postIds = postTagRepository.findAllPostIdsByTagId(tag.getId(), status);
|
||||
|
||||
return postRepository.findAllById(postIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<Post> pagePostsBy(Integer tagId, Pageable pageable) {
|
||||
Assert.notNull(tagId, "Tag id must not be null");
|
||||
|
@ -140,7 +153,7 @@ public class PostTagServiceImpl extends AbstractCrudService<PostTag, Integer> im
|
|||
@Override
|
||||
public Page<Post> pagePostsBy(Integer tagId, PostStatus status, Pageable pageable) {
|
||||
Assert.notNull(tagId, "Tag id must not be null");
|
||||
Assert.notNull(tagId, "Post status must not be null");
|
||||
Assert.notNull(status, "Post status must not be null");
|
||||
Assert.notNull(pageable, "Page info must not be null");
|
||||
|
||||
// Find all post ids
|
||||
|
|
Loading…
Reference in New Issue