Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby authored Mar 28, 2021
1 parent deff63f commit 299a9a3
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ public interface BaseCommentRepository<COMMENT extends BaseComment>
@NonNull
List<CommentCountProjection> countByPostIds(@NonNull Collection<Integer> postIds);

/**
* Counts comment count by comment status and post id collection.
*
* @param status status must not be null
* @param postIds post id collection must not be null
* @return a list of comment count
*/
@Query(
"select new run.halo.app.model.projection.CommentCountProjection(count(comment.id), "
+ "comment.postId) "
+ "from BaseComment comment "
+ "where comment.status = ?1 "
+ "and comment.postId in ?2 "
+ "group by comment.postId")
@NonNull
List<CommentCountProjection> countByStatusAndPostIds(@NonNull CommentStatus status,
@NonNull Collection<Integer> postIds);

/**
* Count comments by post id.
*
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/run/halo/app/service/base/BaseCommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ Page<BaseCommentWithParentVO> pageWithParentVoBy(@NonNull Integer postId,
@NonNull
Map<Integer, Long> countByPostIds(@Nullable Collection<Integer> postIds);

/**
* Counts by comment status and post id collection.
*
* @param status status
* @param postIds post id collection
* @return a count map, key: post id, value: comment count
*/
Map<Integer, Long> countByStatusAndPostIds(@NonNull CommentStatus status,
@NonNull Collection<Integer> postIds);

/**
* Count comments by post id.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,21 @@ public Map<Integer, Long> countByPostIds(Collection<Integer> postIds) {
CommentCountProjection::getCount);
}

@Override
public Map<Integer, Long> countByStatusAndPostIds(@NonNull CommentStatus status,
@NonNull Collection<Integer> postIds) {
if (CollectionUtils.isEmpty(postIds)) {
return Collections.emptyMap();
}

// Get all comment counts
List<CommentCountProjection> commentCountProjections =
baseCommentRepository.countByStatusAndPostIds(status, postIds);

return ServiceUtils.convertToMap(commentCountProjections, CommentCountProjection::getPostId,
CommentCountProjection::getCount);
}

@Override
public long countByPostId(Integer postId) {
Assert.notNull(postId, "Post id must not be null");
Expand Down Expand Up @@ -444,7 +459,7 @@ public BaseCommentDTO convertTo(COMMENT comment) {
protected Specification<COMMENT> buildSpecByQuery(@NonNull CommentQuery commentQuery) {
Assert.notNull(commentQuery, "Comment query must not be null");

return (Specification<COMMENT>) (root, query, criteriaBuilder) -> {
return (root, query, criteriaBuilder) -> {
List<Predicate> predicates = new LinkedList<>();

if (commentQuery.getStatus() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import run.halo.app.model.dto.JournalWithCmtCountDTO;
import run.halo.app.model.entity.Journal;
import run.halo.app.model.entity.JournalComment;
import run.halo.app.model.enums.CommentStatus;
import run.halo.app.model.enums.JournalType;
import run.halo.app.model.params.JournalParam;
import run.halo.app.model.params.JournalQuery;
Expand Down Expand Up @@ -123,7 +124,7 @@ public List<JournalWithCmtCountDTO> convertToCmtCountDto(List<Journal> journals)

// Get comment count map
Map<Integer, Long> journalCommentCountMap =
journalCommentService.countByPostIds(journalIds);
journalCommentService.countByStatusAndPostIds(CommentStatus.PUBLISHED, journalIds);

return journals.stream()
.map(journal -> {
Expand Down Expand Up @@ -182,7 +183,7 @@ public void increaseLike(long likes, Integer id) {
private Specification<Journal> buildSpecByQuery(@NonNull JournalQuery journalQuery) {
Assert.notNull(journalQuery, "Journal query must not be null");

return (Specification<Journal>) (root, query, criteriaBuilder) -> {
return (root, query, criteriaBuilder) -> {
List<Predicate> predicates = new LinkedList<>();

if (journalQuery.getType() != null) {
Expand Down
25 changes: 14 additions & 11 deletions src/main/java/run/halo/app/service/impl/PostServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import run.halo.app.model.entity.PostMeta;
import run.halo.app.model.entity.PostTag;
import run.halo.app.model.entity.Tag;
import run.halo.app.model.enums.CommentStatus;
import run.halo.app.model.enums.LogType;
import run.halo.app.model.enums.PostPermalinkType;
import run.halo.app.model.enums.PostStatus;
Expand Down Expand Up @@ -583,7 +584,8 @@ public Page<PostListVO> convertToListVo(Page<Post> postPage, boolean queryEncryp
.listCategoryListMap(postIds, queryEncryptCategory);

// Get comment count
Map<Integer, Long> commentCountMap = postCommentService.countByPostIds(postIds);
Map<Integer, Long> commentCountMap = postCommentService.countByStatusAndPostIds(
CommentStatus.PUBLISHED, postIds);

// Get post meta list map
Map<Integer, List<PostMeta>> postMetaListMap = postMetaService.listPostMetaAsMap(postIds);
Expand Down Expand Up @@ -646,7 +648,8 @@ public List<PostListVO> convertToListVo(List<Post> posts, boolean queryEncryptCa
.listCategoryListMap(postIds, queryEncryptCategory);

// Get comment count
Map<Integer, Long> commentCountMap = postCommentService.countByPostIds(postIds);
Map<Integer, Long> commentCountMap =
postCommentService.countByStatusAndPostIds(CommentStatus.PUBLISHED, postIds);

// Get post meta list map
Map<Integer, List<PostMeta>> postMetaListMap = postMetaService.listPostMetaAsMap(postIds);
Expand Down Expand Up @@ -782,7 +785,7 @@ private PostDetailVO convertTo(@NonNull Post post, @Nullable List<Tag> tags,
private Specification<Post> buildSpecByQuery(@NonNull PostQuery postQuery) {
Assert.notNull(postQuery, "Post query must not be null");

return (Specification<Post>) (root, query, criteriaBuilder) -> {
return (root, query, criteriaBuilder) -> {
List<Predicate> predicates = new LinkedList<>();

if (postQuery.getStatus() != null) {
Expand Down Expand Up @@ -823,15 +826,15 @@ private PostDetailVO createOrUpdate(@NonNull Post post, Set<Integer> tagIds,

// Create or update post
Boolean needEncrypt = Optional.ofNullable(categoryIds)
.filter(CollectionUtil::isNotEmpty)
.map(categoryIdSet -> {
for (Integer categoryId : categoryIdSet) {
if (categoryService.categoryHasEncrypt(categoryId)) {
return true;
}
.filter(CollectionUtil::isNotEmpty)
.map(categoryIdSet -> {
for (Integer categoryId : categoryIdSet) {
if (categoryService.categoryHasEncrypt(categoryId)) {
return true;
}
return false;
}).orElse(Boolean.FALSE);
}
return false;
}).orElse(Boolean.FALSE);

// if password is not empty or parent category has encrypt, change status to intimate
if (post.getStatus() != PostStatus.DRAFT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import run.halo.app.model.entity.Sheet;
import run.halo.app.model.entity.SheetComment;
import run.halo.app.model.entity.SheetMeta;
import run.halo.app.model.enums.CommentStatus;
import run.halo.app.model.enums.LogType;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.enums.SheetPermalinkType;
Expand Down Expand Up @@ -276,7 +277,8 @@ public Page<SheetListVO> convertToListVo(Page<Sheet> sheetPage) {
Set<Integer> sheetIds = ServiceUtils.fetchProperty(sheets, Sheet::getId);

// key: sheet id, value: comment count
Map<Integer, Long> sheetCommentCountMap = sheetCommentService.countByPostIds(sheetIds);
Map<Integer, Long> sheetCommentCountMap = sheetCommentService.countByStatusAndPostIds(
CommentStatus.PUBLISHED, sheetIds);

return sheetPage.map(sheet -> {
SheetListVO sheetListVO = new SheetListVO().convertFrom(sheet);
Expand Down

0 comments on commit 299a9a3

Please sign in to comment.