Fix tag count fetch feature

pull/137/head
johnniang 2019-03-26 14:02:19 +08:00
parent 157689b0c2
commit 88b6d55d23
3 changed files with 57 additions and 1 deletions

View File

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

View File

@ -1,6 +1,7 @@
package cc.ryanc.halo.repository; package cc.ryanc.halo.repository;
import cc.ryanc.halo.model.entity.PostTag; import cc.ryanc.halo.model.entity.PostTag;
import cc.ryanc.halo.model.projection.TagPostCountProjection;
import cc.ryanc.halo.repository.base.BaseRepository; import cc.ryanc.halo.repository.base.BaseRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
@ -80,4 +81,23 @@ public interface PostTagRepository extends BaseRepository<PostTag, Integer> {
*/ */
@NonNull @NonNull
List<PostTag> deleteByTagId(@NonNull Integer tagId); List<PostTag> 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<TagPostCountProjection> findPostCountByTagIds(@NonNull Iterable<Integer> 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<TagPostCountProjection> findPostCount();
} }

View File

@ -4,6 +4,7 @@ import cc.ryanc.halo.model.dto.TagWithCountOutputDTO;
import cc.ryanc.halo.model.entity.Post; import cc.ryanc.halo.model.entity.Post;
import cc.ryanc.halo.model.entity.PostTag; import cc.ryanc.halo.model.entity.PostTag;
import cc.ryanc.halo.model.entity.Tag; 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.PostRepository;
import cc.ryanc.halo.repository.PostTagRepository; import cc.ryanc.halo.repository.PostTagRepository;
import cc.ryanc.halo.repository.TagRepository; import cc.ryanc.halo.repository.TagRepository;
@ -59,9 +60,16 @@ public class PostTagServiceImpl extends AbstractCrudService<PostTag, Integer> im
// Find all tags // Find all tags
List<Tag> tags = tagRepository.findAll(sort); List<Tag> tags = tagRepository.findAll(sort);
// Find all post count
Map<Integer, Long> tagPostCountMap = ServiceUtils.convertToMap(postTagRepository.findPostCount(), TagPostCountProjection::getTagId, TagPostCountProjection::getCount);
// Find post count // Find post count
return tags.stream().map( return tags.stream().map(
tag -> new TagWithCountOutputDTO().<TagWithCountOutputDTO>convertFrom(tag) tag -> {
TagWithCountOutputDTO tagWithCountOutputDTO = new TagWithCountOutputDTO().convertFrom(tag);
tagWithCountOutputDTO.setPostCount(tagPostCountMap.getOrDefault(tag.getId(), 0L));
return tagWithCountOutputDTO;
}
).collect(Collectors.toList()); ).collect(Collectors.toList());
} }