feat: show correct reply counts

This commit is contained in:
Tim
2025-08-05 12:44:50 +08:00
parent 2e8bc012fa
commit c65dfbcbf9
5 changed files with 16 additions and 5 deletions

View File

@@ -19,6 +19,7 @@ public class PostSummaryDto {
private CategoryDto category;
private List<TagDto> tags;
private long views;
private long commentCount;
private PostStatus status;
private LocalDateTime pinnedAt;
private LocalDateTime lastReplyAt;

View File

@@ -58,6 +58,7 @@ public class PostMapper {
dto.setCategory(categoryMapper.toDto(post.getCategory()));
dto.setTags(post.getTags().stream().map(tagMapper::toDto).collect(Collectors.toList()));
dto.setViews(post.getViews());
dto.setCommentCount(commentService.countComments(post.getId()));
dto.setStatus(post.getStatus());
dto.setPinnedAt(post.getPinnedAt());

View File

@@ -27,4 +27,7 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {
@org.springframework.data.jpa.repository.Query("SELECT MAX(c.createdAt) FROM Comment c WHERE c.author.id = :userId")
java.time.LocalDateTime findLastCommentTimeOfUserByUserId(@org.springframework.data.repository.query.Param("userId") Long userId);
@org.springframework.data.jpa.repository.Query("SELECT COUNT(c) FROM Comment c WHERE c.post.id = :postId")
long countByPostId(@org.springframework.data.repository.query.Param("postId") Long postId);
}

View File

@@ -186,6 +186,13 @@ public class CommentService {
return time;
}
public long countComments(Long postId) {
log.debug("countComments called for post {}", postId);
long count = commentRepository.countByPostId(postId);
log.debug("countComments for post {} is {}", postId, count);
return count;
}
@Transactional
public void deleteComment(String username, Long id) {
log.debug("deleteComment called by user {} for comment {}", username, id);