diff --git a/src/main/java/run/halo/app/model/freemarker/tag/PostTagDirective.java b/src/main/java/run/halo/app/model/freemarker/tag/PostTagDirective.java index df8a636d2..375fba429 100644 --- a/src/main/java/run/halo/app/model/freemarker/tag/PostTagDirective.java +++ b/src/main/java/run/halo/app/model/freemarker/tag/PostTagDirective.java @@ -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; } diff --git a/src/main/java/run/halo/app/service/PostCategoryService.java b/src/main/java/run/halo/app/service/PostCategoryService.java index 383666108..1e94f4fa8 100644 --- a/src/main/java/run/halo/app/service/PostCategoryService.java +++ b/src/main/java/run/halo/app/service/PostCategoryService.java @@ -64,6 +64,16 @@ public interface PostCategoryService extends CrudService @NonNull List 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 listPostBy(@NonNull String slug, @NonNull PostStatus status); + /** * Pages post by category slug name. * diff --git a/src/main/java/run/halo/app/service/PostTagService.java b/src/main/java/run/halo/app/service/PostTagService.java index 42c56a848..6868205db 100644 --- a/src/main/java/run/halo/app/service/PostTagService.java +++ b/src/main/java/run/halo/app/service/PostTagService.java @@ -73,6 +73,16 @@ public interface PostTagService extends CrudService { @NonNull List 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 listPostsBy(@NonNull String slug, @NonNull PostStatus status); + /** * Pages posts by tag id. * diff --git a/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java b/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java index 0f82f4b58..0cc8cc3bd 100644 --- a/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java @@ -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 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 postIds = postCategoryRepository.findAllPostIdsByCategoryId(categoryId, status); @@ -106,6 +107,18 @@ public class PostCategoryServiceImpl extends AbstractCrudService 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 postsIds = postCategoryRepository.findAllPostIdsByCategoryId(category.getId(), status); + + return postRepository.findAllById(postsIds); + } + @Override public Page pagePostBy(Integer categoryId, Pageable pageable) { Assert.notNull(categoryId, "Category id must not be null"); @@ -120,7 +133,7 @@ public class PostCategoryServiceImpl extends AbstractCrudService 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 diff --git a/src/main/java/run/halo/app/service/impl/PostTagServiceImpl.java b/src/main/java/run/halo/app/service/impl/PostTagServiceImpl.java index 3bd5d3186..1f993f6ef 100644 --- a/src/main/java/run/halo/app/service/impl/PostTagServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostTagServiceImpl.java @@ -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 im @Override public List 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 postIds = postTagRepository.findAllPostIdsByTagId(tagId, status); @@ -126,6 +127,18 @@ public class PostTagServiceImpl extends AbstractCrudService im return postRepository.findAllById(postIds); } + @Override + public List 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 postIds = postTagRepository.findAllPostIdsByTagId(tag.getId(), status); + + return postRepository.findAllById(postIds); + } + @Override public Page pagePostsBy(Integer tagId, Pageable pageable) { Assert.notNull(tagId, "Tag id must not be null"); @@ -140,7 +153,7 @@ public class PostTagServiceImpl extends AbstractCrudService im @Override public Page 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