From 88b6d55d2385925478d85b0b6bf210e5c760f135 Mon Sep 17 00:00:00 2001 From: johnniang Date: Tue, 26 Mar 2019 14:02:19 +0800 Subject: [PATCH] Fix tag count fetch feature --- .../projection/TagPostCountProjection.java | 28 +++++++++++++++++++ .../halo/repository/PostTagRepository.java | 20 +++++++++++++ .../halo/service/impl/PostTagServiceImpl.java | 10 ++++++- 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/main/java/cc/ryanc/halo/model/projection/TagPostCountProjection.java diff --git a/src/main/java/cc/ryanc/halo/model/projection/TagPostCountProjection.java b/src/main/java/cc/ryanc/halo/model/projection/TagPostCountProjection.java new file mode 100644 index 000000000..71a889f3b --- /dev/null +++ b/src/main/java/cc/ryanc/halo/model/projection/TagPostCountProjection.java @@ -0,0 +1,28 @@ +package cc.ryanc.halo.model.projection; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * Tag post count projection. + * + * @author johnniang + * @date 3/26/19 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class TagPostCountProjection { + + /** + * Post count. + */ + private Long count; + + /** + * Tag id + */ + private Integer tagId; + +} diff --git a/src/main/java/cc/ryanc/halo/repository/PostTagRepository.java b/src/main/java/cc/ryanc/halo/repository/PostTagRepository.java index 8323f28db..a8b69c59d 100644 --- a/src/main/java/cc/ryanc/halo/repository/PostTagRepository.java +++ b/src/main/java/cc/ryanc/halo/repository/PostTagRepository.java @@ -1,6 +1,7 @@ package cc.ryanc.halo.repository; import cc.ryanc.halo.model.entity.PostTag; +import cc.ryanc.halo.model.projection.TagPostCountProjection; import cc.ryanc.halo.repository.base.BaseRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.lang.NonNull; @@ -80,4 +81,23 @@ public interface PostTagRepository extends BaseRepository { */ @NonNull List deleteByTagId(@NonNull Integer tagId); + + /** + * Finds post count by tag id collection. + * + * @param tagIds tag id collection must not be null + * @return a list of tag post count projection + */ + @Query("select new cc.ryanc.halo.model.projection.TagPostCountProjection(count(pt.postId), pt.tagId) from PostTag pt where pt.tagId in ?1 group by pt.tagId") + @NonNull + List findPostCountByTagIds(@NonNull Iterable tagIds); + + /** + * Finds post count of tag. + * + * @return a list of tag post count projection + */ + @Query("select new cc.ryanc.halo.model.projection.TagPostCountProjection(count(pt.postId), pt.tagId) from PostTag pt group by pt.tagId") + @NonNull + List findPostCount(); } diff --git a/src/main/java/cc/ryanc/halo/service/impl/PostTagServiceImpl.java b/src/main/java/cc/ryanc/halo/service/impl/PostTagServiceImpl.java index af0d3f18e..00c3abbe9 100644 --- a/src/main/java/cc/ryanc/halo/service/impl/PostTagServiceImpl.java +++ b/src/main/java/cc/ryanc/halo/service/impl/PostTagServiceImpl.java @@ -4,6 +4,7 @@ import cc.ryanc.halo.model.dto.TagWithCountOutputDTO; import cc.ryanc.halo.model.entity.Post; import cc.ryanc.halo.model.entity.PostTag; import cc.ryanc.halo.model.entity.Tag; +import cc.ryanc.halo.model.projection.TagPostCountProjection; import cc.ryanc.halo.repository.PostRepository; import cc.ryanc.halo.repository.PostTagRepository; import cc.ryanc.halo.repository.TagRepository; @@ -59,9 +60,16 @@ public class PostTagServiceImpl extends AbstractCrudService im // Find all tags List tags = tagRepository.findAll(sort); + // Find all post count + Map tagPostCountMap = ServiceUtils.convertToMap(postTagRepository.findPostCount(), TagPostCountProjection::getTagId, TagPostCountProjection::getCount); + // Find post count return tags.stream().map( - tag -> new TagWithCountOutputDTO().convertFrom(tag) + tag -> { + TagWithCountOutputDTO tagWithCountOutputDTO = new TagWithCountOutputDTO().convertFrom(tag); + tagWithCountOutputDTO.setPostCount(tagPostCountMap.getOrDefault(tag.getId(), 0L)); + return tagWithCountOutputDTO; + } ).collect(Collectors.toList()); }