Fixed the problem of not adding post status when querying posts by category or tag.

pull/235/head
ruibaby 2019-07-09 20:25:14 +08:00
parent 31f4ec3afe
commit e67061cf74
12 changed files with 139 additions and 8 deletions

View File

@ -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<Post> postPage = postCategoryService.pagePostBy(category.getId(), pageable);
Page<Post> postPage = postCategoryService.pagePostBy(category.getId(), PostStatus.PUBLISHED, pageable);
Page<PostListVO> posts = postService.convertToListVo(postPage);
final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);

View File

@ -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

View File

@ -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<Post> postPage = postTagService.pagePostsBy(tag.getId(), pageable);
Page<Post> postPage = postTagService.pagePostsBy(tag.getId(), PostStatus.PUBLISHED, pageable);
Page<PostListVO> posts = postService.convertToListVo(postPage);
final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3);

View File

@ -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<Post> postPage = postCategoryService.pagePostBy(category.getId(), pageable);
Page<Post> postPage = postCategoryService.pagePostBy(category.getId(), PostStatus.PUBLISHED, pageable);
return postService.convertToSimple(postPage);
}
}

View File

@ -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<Post> postPage = postTagService.pagePostsBy(tag.getId(), pageable);
Page<Post> postPage = postTagService.pagePostsBy(tag.getId(), PostStatus.PUBLISHED, pageable);
return postService.convertToSimple(postPage);
}
}

View File

@ -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;

View File

@ -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<PostCategory, Integer> {
@ -37,6 +40,17 @@ public interface PostCategoryRepository extends BaseRepository<PostCategory, Int
@Query("select postCategory.postId from PostCategory postCategory where postCategory.categoryId = ?1")
Set<Integer> 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<Integer> findAllPostIdsByCategoryId(@NonNull Integer categoryId, @NonNull PostStatus status);
/**
* Finds all post categories by post id in.
*

View File

@ -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<PostTag, Integer> {
@ -55,6 +58,17 @@ public interface PostTagRepository extends BaseRepository<PostTag, Integer> {
@NonNull
Set<Integer> 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<Integer> findAllPostIdsByTagId(@NonNull Integer tagId, @NonNull PostStatus status);
/**
* Finds all tags by post id in.
*

View File

@ -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<PostCategory, Integer> {
@ -52,6 +54,16 @@ public interface PostCategoryService extends CrudService<PostCategory, Integer>
@NonNull
List<Post> 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<Post> listPostBy(@NonNull Integer categoryId, @NonNull PostStatus status);
/**
* Pages post by category slug name.
*
@ -62,6 +74,17 @@ public interface PostCategoryService extends CrudService<PostCategory, Integer>
@NonNull
Page<Post> 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<Post> pagePostBy(@NonNull Integer categoryId, @NonNull PostStatus status, Pageable pageable);
/**
* Merges or creates post categories by post id and category id set if absent.
*

View File

@ -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<PostTag, Integer> {
@ -61,6 +63,16 @@ public interface PostTagService extends CrudService<PostTag, Integer> {
@NonNull
List<Post> 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<Post> listPostsBy(@NonNull Integer tagId, @NonNull PostStatus status);
/**
* Pages posts by tag id.
*
@ -70,6 +82,16 @@ public interface PostTagService extends CrudService<PostTag, Integer> {
*/
Page<Post> 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<Post> pagePostsBy(@NonNull Integer tagId, @NonNull PostStatus status, Pageable pageable);
/**
* Merges or creates post tags by post id and tag id set if absent.
*

View File

@ -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<PostCategory, I
return postRepository.findAllById(postIds);
}
@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");
// Find all post ids
Set<Integer> postIds = postCategoryRepository.findAllPostIdsByCategoryId(categoryId, status);
return postRepository.findAllById(postIds);
}
@Override
public Page<Post> pagePostBy(Integer categoryId, Pageable pageable) {
Assert.notNull(categoryId, "Category id must not be null");
@ -104,6 +117,18 @@ public class PostCategoryServiceImpl extends AbstractCrudService<PostCategory, I
return postRepository.findAllByIdIn(postIds, pageable);
}
@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(pageable, "Page info must not be null");
// Find all post ids
Set<Integer> postIds = postCategoryRepository.findAllPostIdsByCategoryId(categoryId, status);
return postRepository.findAllByIdIn(postIds, pageable);
}
@Override
public List<PostCategory> mergeOrCreateByIfAbsent(Integer postId, Set<Integer> categoryIds) {
Assert.notNull(postId, "Post id must not be null");

View File

@ -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<PostTag, Integer> im
return postRepository.findAllById(postIds);
}
@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");
// Find all post ids
Set<Integer> postIds = postTagRepository.findAllPostIdsByTagId(tagId, status);
return postRepository.findAllById(postIds);
}
@Override
public Page<Post> pagePostsBy(Integer tagId, Pageable pageable) {
Assert.notNull(tagId, "Tag id must not be null");
@ -124,6 +137,18 @@ public class PostTagServiceImpl extends AbstractCrudService<PostTag, Integer> im
return postRepository.findAllByIdIn(postIds, pageable);
}
@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(pageable, "Page info must not be null");
// Find all post ids
Set<Integer> postIds = postTagRepository.findAllPostIdsByTagId(tagId, status);
return postRepository.findAllByIdIn(postIds, pageable);
}
@Override
public List<PostTag> mergeOrCreateByIfAbsent(Integer postId, Set<Integer> tagIds) {
Assert.notNull(postId, "Post id must not be null");