mirror of https://github.com/halo-dev/halo
Fix tag count fetch feature
parent
157689b0c2
commit
88b6d55d23
|
@ -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;
|
||||
|
||||
}
|
|
@ -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<PostTag, Integer> {
|
|||
*/
|
||||
@NonNull
|
||||
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();
|
||||
}
|
||||
|
|
|
@ -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<PostTag, Integer> im
|
|||
// Find all tags
|
||||
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
|
||||
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());
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue