From e67061cf74516846e09d53961220cb62541e4af1 Mon Sep 17 00:00:00 2001 From: ruibaby Date: Tue, 9 Jul 2019 20:25:14 +0800 Subject: [PATCH] Fixed the problem of not adding post status when querying posts by category or tag. --- .../content/ContentCategoryController.java | 5 +++- .../content/ContentSearchController.java | 2 +- .../content/ContentTagController.java | 5 ++-- .../content/api/CategoryController.java | 3 ++- .../controller/content/api/TagController.java | 4 ++- .../freemarker/tag/PostTagDirective.java | 5 ++-- .../repository/PostCategoryRepository.java | 14 +++++++++++ .../app/repository/PostTagRepository.java | 14 +++++++++++ .../halo/app/service/PostCategoryService.java | 23 +++++++++++++++++ .../run/halo/app/service/PostTagService.java | 22 ++++++++++++++++ .../service/impl/PostCategoryServiceImpl.java | 25 +++++++++++++++++++ .../app/service/impl/PostTagServiceImpl.java | 25 +++++++++++++++++++ 12 files changed, 139 insertions(+), 8 deletions(-) diff --git a/src/main/java/run/halo/app/controller/content/ContentCategoryController.java b/src/main/java/run/halo/app/controller/content/ContentCategoryController.java index e713fd34a..97956eebc 100644 --- a/src/main/java/run/halo/app/controller/content/ContentCategoryController.java +++ b/src/main/java/run/halo/app/controller/content/ContentCategoryController.java @@ -13,12 +13,15 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import run.halo.app.model.entity.Category; import run.halo.app.model.entity.Post; +import run.halo.app.model.enums.PostStatus; import run.halo.app.model.vo.PostListVO; import run.halo.app.service.*; import static org.springframework.data.domain.Sort.Direction.DESC; /** + * Category controller. + * * @author ryanwang * @date : 2019/3/20 */ @@ -88,7 +91,7 @@ public class ContentCategoryController { final Category category = categoryService.getBySlugNameOfNonNull(slugName); final Pageable pageable = PageRequest.of(page - 1, optionService.getPostPageSize(), sort); - Page postPage = postCategoryService.pagePostBy(category.getId(), pageable); + Page postPage = postCategoryService.pagePostBy(category.getId(), PostStatus.PUBLISHED, pageable); Page posts = postService.convertToListVo(postPage); final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3); diff --git a/src/main/java/run/halo/app/controller/content/ContentSearchController.java b/src/main/java/run/halo/app/controller/content/ContentSearchController.java index 93bf732b3..4da44d503 100644 --- a/src/main/java/run/halo/app/controller/content/ContentSearchController.java +++ b/src/main/java/run/halo/app/controller/content/ContentSearchController.java @@ -22,7 +22,7 @@ import run.halo.app.service.ThemeService; import static org.springframework.data.domain.Sort.Direction.DESC; /** - * Search Controller + * Search controller. * * @author ryanwang * @date : 2019-04-21 diff --git a/src/main/java/run/halo/app/controller/content/ContentTagController.java b/src/main/java/run/halo/app/controller/content/ContentTagController.java index eae71c455..ee9a09066 100644 --- a/src/main/java/run/halo/app/controller/content/ContentTagController.java +++ b/src/main/java/run/halo/app/controller/content/ContentTagController.java @@ -13,13 +13,14 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import run.halo.app.model.entity.Post; import run.halo.app.model.entity.Tag; +import run.halo.app.model.enums.PostStatus; import run.halo.app.model.vo.PostListVO; import run.halo.app.service.*; import static org.springframework.data.domain.Sort.Direction.DESC; /** - * Tag Controller + * Tag controller. * * @author ryanwang * @date : 2019-03-21 @@ -91,7 +92,7 @@ public class ContentTagController { final Tag tag = tagService.getBySlugNameOfNonNull(slugName); final Pageable pageable = PageRequest.of(page - 1, optionService.getPostPageSize(), sort); - Page postPage = postTagService.pagePostsBy(tag.getId(), pageable); + Page postPage = postTagService.pagePostsBy(tag.getId(), PostStatus.PUBLISHED, pageable); Page posts = postService.convertToListVo(postPage); final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3); diff --git a/src/main/java/run/halo/app/controller/content/api/CategoryController.java b/src/main/java/run/halo/app/controller/content/api/CategoryController.java index be62bbdeb..2974ccd9e 100644 --- a/src/main/java/run/halo/app/controller/content/api/CategoryController.java +++ b/src/main/java/run/halo/app/controller/content/api/CategoryController.java @@ -11,6 +11,7 @@ import run.halo.app.model.dto.CategoryDTO; import run.halo.app.model.dto.post.BasePostSimpleDTO; import run.halo.app.model.entity.Category; import run.halo.app.model.entity.Post; +import run.halo.app.model.enums.PostStatus; import run.halo.app.service.CategoryService; import run.halo.app.service.PostCategoryService; import run.halo.app.service.PostService; @@ -60,7 +61,7 @@ public class CategoryController { // Get category by slug name Category category = categoryService.getBySlugNameOfNonNull(slugName); - Page postPage = postCategoryService.pagePostBy(category.getId(), pageable); + Page postPage = postCategoryService.pagePostBy(category.getId(), PostStatus.PUBLISHED, pageable); return postService.convertToSimple(postPage); } } diff --git a/src/main/java/run/halo/app/controller/content/api/TagController.java b/src/main/java/run/halo/app/controller/content/api/TagController.java index 81506d103..ecf7e9ebb 100644 --- a/src/main/java/run/halo/app/controller/content/api/TagController.java +++ b/src/main/java/run/halo/app/controller/content/api/TagController.java @@ -12,6 +12,7 @@ import run.halo.app.model.dto.TagDTO; import run.halo.app.model.dto.post.BasePostSimpleDTO; import run.halo.app.model.entity.Post; import run.halo.app.model.entity.Tag; +import run.halo.app.model.enums.PostStatus; import run.halo.app.service.PostService; import run.halo.app.service.PostTagService; import run.halo.app.service.TagService; @@ -24,6 +25,7 @@ import static org.springframework.data.domain.Sort.Direction.DESC; * Portal tag controller. * * @author johnniang + * @author ryanwang * @date 4/2/19 */ @RestController("ApiContentTagController") @@ -63,7 +65,7 @@ public class TagController { Tag tag = tagService.getBySlugNameOfNonNull(slugName); // Get posts, convert and return - Page postPage = postTagService.pagePostsBy(tag.getId(), pageable); + Page postPage = postTagService.pagePostsBy(tag.getId(), PostStatus.PUBLISHED, pageable); return postService.convertToSimple(postPage); } } 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 f392755e5..df8a636d2 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 @@ -3,6 +3,7 @@ package run.halo.app.model.freemarker.tag; import freemarker.core.Environment; import freemarker.template.*; import org.springframework.stereotype.Component; +import run.halo.app.model.enums.PostStatus; import run.halo.app.model.support.HaloConst; import run.halo.app.service.PostCategoryService; import run.halo.app.service.PostService; @@ -57,11 +58,11 @@ public class PostTagDirective implements TemplateDirectiveModel { break; case "listByCategoryId": Integer categoryId = Integer.parseInt(params.get("categoryId").toString()); - env.setVariable("posts", builder.build().wrap(postCategoryService.listPostBy(categoryId))); + env.setVariable("posts", builder.build().wrap(postCategoryService.listPostBy(categoryId, PostStatus.PUBLISHED))); break; case "listByTagId": Integer tagId = Integer.parseInt(params.get("tagId").toString()); - env.setVariable("posts", builder.build().wrap(postTagService.listPostsBy(tagId))); + env.setVariable("posts", builder.build().wrap(postTagService.listPostsBy(tagId, PostStatus.PUBLISHED))); break; default: break; diff --git a/src/main/java/run/halo/app/repository/PostCategoryRepository.java b/src/main/java/run/halo/app/repository/PostCategoryRepository.java index 2055c666c..c3da9cefd 100644 --- a/src/main/java/run/halo/app/repository/PostCategoryRepository.java +++ b/src/main/java/run/halo/app/repository/PostCategoryRepository.java @@ -3,6 +3,7 @@ package run.halo.app.repository; import org.springframework.data.jpa.repository.Query; import org.springframework.lang.NonNull; import run.halo.app.model.entity.PostCategory; +import run.halo.app.model.enums.PostStatus; import run.halo.app.model.projection.CategoryPostCountProjection; import run.halo.app.repository.base.BaseRepository; @@ -14,6 +15,8 @@ import java.util.Set; * Post category repository. * * @author johnniang + * @author ryanwang + * @date 2019-03-19 */ public interface PostCategoryRepository extends BaseRepository { @@ -37,6 +40,17 @@ public interface PostCategoryRepository extends BaseRepository findAllPostIdsByCategoryId(@NonNull Integer categoryId); + /** + * Finds all post ids by category id and post status. + * + * @param categoryId category id must not be null + * @param status post status must not be null + * @return a set of post id + */ + @NonNull + @Query("select postCategory.postId from PostCategory postCategory, Post post where postCategory.categoryId = ?1 and post.id = postCategory.postId and post.status = ?2") + Set findAllPostIdsByCategoryId(@NonNull Integer categoryId, @NonNull PostStatus status); + /** * Finds all post categories by post id in. * diff --git a/src/main/java/run/halo/app/repository/PostTagRepository.java b/src/main/java/run/halo/app/repository/PostTagRepository.java index e3452308b..1c5451ecd 100644 --- a/src/main/java/run/halo/app/repository/PostTagRepository.java +++ b/src/main/java/run/halo/app/repository/PostTagRepository.java @@ -3,6 +3,7 @@ package run.halo.app.repository; import org.springframework.data.jpa.repository.Query; import org.springframework.lang.NonNull; import run.halo.app.model.entity.PostTag; +import run.halo.app.model.enums.PostStatus; import run.halo.app.model.projection.TagPostPostCountProjection; import run.halo.app.repository.base.BaseRepository; @@ -14,6 +15,8 @@ import java.util.Set; * Post tag repository. * * @author johnniang + * @author ryanwang + * @date 2019-03-19 */ public interface PostTagRepository extends BaseRepository { @@ -55,6 +58,17 @@ public interface PostTagRepository extends BaseRepository { @NonNull Set findAllPostIdsByTagId(@NonNull Integer tagId); + /** + * Finds all post id by tag id and post status. + * + * @param tagId tag id must not be null + * @param status post status + * @return a set of post id + */ + @Query("select postTag.postId from PostTag postTag,Post post where postTag.tagId = ?1 and post.id = postTag.postId and post.status = ?2") + @NonNull + Set findAllPostIdsByTagId(@NonNull Integer tagId, @NonNull PostStatus status); + /** * Finds all tags by post id in. * diff --git a/src/main/java/run/halo/app/service/PostCategoryService.java b/src/main/java/run/halo/app/service/PostCategoryService.java index 0f7908eca..bca6cb80c 100644 --- a/src/main/java/run/halo/app/service/PostCategoryService.java +++ b/src/main/java/run/halo/app/service/PostCategoryService.java @@ -10,6 +10,7 @@ import run.halo.app.model.dto.CategoryWithPostCountDTO; 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.base.CrudService; import java.util.Collection; @@ -21,6 +22,7 @@ import java.util.Set; * Post category service interface. * * @author johnniang + * @author ryanwang * @date 2019-03-19 */ public interface PostCategoryService extends CrudService { @@ -52,6 +54,16 @@ public interface PostCategoryService extends CrudService @NonNull List listPostBy(@NonNull Integer categoryId); + /** + * Lists post by category id and post status. + * + * @param categoryId category id must not be null + * @param status post status + * @return a list of post + */ + @NonNull + List listPostBy(@NonNull Integer categoryId, @NonNull PostStatus status); + /** * Pages post by category slug name. * @@ -62,6 +74,17 @@ public interface PostCategoryService extends CrudService @NonNull Page pagePostBy(@NonNull Integer categoryId, Pageable pageable); + /** + * Pages post by category slug name and post status. + * + * @param categoryId category id must not be null + * @param status post status + * @param pageable pageable + * @return page of post + */ + @NonNull + Page pagePostBy(@NonNull Integer categoryId, @NonNull PostStatus status, Pageable pageable); + /** * Merges or creates post categories by post id and category id set if absent. * diff --git a/src/main/java/run/halo/app/service/PostTagService.java b/src/main/java/run/halo/app/service/PostTagService.java index 8cfb8e770..42c56a848 100644 --- a/src/main/java/run/halo/app/service/PostTagService.java +++ b/src/main/java/run/halo/app/service/PostTagService.java @@ -10,6 +10,7 @@ import run.halo.app.model.dto.TagWithPostCountDTO; import run.halo.app.model.entity.Post; import run.halo.app.model.entity.PostTag; import run.halo.app.model.entity.Tag; +import run.halo.app.model.enums.PostStatus; import run.halo.app.service.base.CrudService; import java.util.Collection; @@ -21,6 +22,7 @@ import java.util.Set; * Post tag service interface. * * @author johnniang + * @author ryanwang * @date 2019-03-19 */ public interface PostTagService extends CrudService { @@ -61,6 +63,16 @@ public interface PostTagService extends CrudService { @NonNull List listPostsBy(@NonNull Integer tagId); + /** + * Lists posts by tag id and post status. + * + * @param tagId tag id must not be null + * @param status post status + * @return a list of post + */ + @NonNull + List listPostsBy(@NonNull Integer tagId, @NonNull PostStatus status); + /** * Pages posts by tag id. * @@ -70,6 +82,16 @@ public interface PostTagService extends CrudService { */ Page pagePostsBy(@NonNull Integer tagId, Pageable pageable); + /** + * Pages posts by tag id and post status. + * + * @param tagId must not be null + * @param status post status + * @param pageable must not be null + * @return a page of post + */ + Page pagePostsBy(@NonNull Integer tagId, @NonNull PostStatus status, Pageable pageable); + /** * Merges or creates post tags by post id and tag id set if absent. * 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 a60f6b2bf..cd79ee8e0 100644 --- a/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostCategoryServiceImpl.java @@ -10,6 +10,7 @@ import run.halo.app.model.dto.CategoryWithPostCountDTO; 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.model.projection.CategoryPostCountProjection; import run.halo.app.repository.CategoryRepository; import run.halo.app.repository.PostCategoryRepository; @@ -25,6 +26,7 @@ import java.util.stream.Collectors; * Post category service implementation. * * @author johnniang + * @author ryanwang * @date 2019-03-19 */ @Service @@ -93,6 +95,17 @@ 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"); + + // Find all post ids + Set postIds = postCategoryRepository.findAllPostIdsByCategoryId(categoryId, status); + + return postRepository.findAllById(postIds); + } + @Override public Page pagePostBy(Integer categoryId, Pageable pageable) { Assert.notNull(categoryId, "Category id must not be null"); @@ -104,6 +117,18 @@ 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(pageable, "Page info must not be null"); + + // Find all post ids + Set postIds = postCategoryRepository.findAllPostIdsByCategoryId(categoryId, status); + + return postRepository.findAllByIdIn(postIds, pageable); + } + @Override public List mergeOrCreateByIfAbsent(Integer postId, Set categoryIds) { Assert.notNull(postId, "Post id must not be null"); 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 2effa4284..3bd5d3186 100644 --- a/src/main/java/run/halo/app/service/impl/PostTagServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostTagServiceImpl.java @@ -10,6 +10,7 @@ import run.halo.app.model.dto.TagWithPostCountDTO; import run.halo.app.model.entity.Post; import run.halo.app.model.entity.PostTag; import run.halo.app.model.entity.Tag; +import run.halo.app.model.enums.PostStatus; import run.halo.app.model.projection.TagPostPostCountProjection; import run.halo.app.repository.PostRepository; import run.halo.app.repository.PostTagRepository; @@ -25,6 +26,7 @@ import java.util.stream.Collectors; * Post tag service implementation. * * @author johnniang + * @author ryanwang * @date 2019-03-19 */ @Service @@ -113,6 +115,17 @@ public class PostTagServiceImpl extends AbstractCrudService im return postRepository.findAllById(postIds); } + @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"); + + // Find all post ids + Set postIds = postTagRepository.findAllPostIdsByTagId(tagId, status); + + return postRepository.findAllById(postIds); + } + @Override public Page pagePostsBy(Integer tagId, Pageable pageable) { Assert.notNull(tagId, "Tag id must not be null"); @@ -124,6 +137,18 @@ public class PostTagServiceImpl extends AbstractCrudService im return postRepository.findAllByIdIn(postIds, pageable); } + @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(pageable, "Page info must not be null"); + + // Find all post ids + Set postIds = postTagRepository.findAllPostIdsByTagId(tagId, status); + + return postRepository.findAllByIdIn(postIds, pageable); + } + @Override public List mergeOrCreateByIfAbsent(Integer postId, Set tagIds) { Assert.notNull(postId, "Post id must not be null");