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 CategoryDto category;
private List<TagDto> tags; private List<TagDto> tags;
private long views; private long views;
private long commentCount;
private PostStatus status; private PostStatus status;
private LocalDateTime pinnedAt; private LocalDateTime pinnedAt;
private LocalDateTime lastReplyAt; private LocalDateTime lastReplyAt;

View File

@@ -58,6 +58,7 @@ public class PostMapper {
dto.setCategory(categoryMapper.toDto(post.getCategory())); dto.setCategory(categoryMapper.toDto(post.getCategory()));
dto.setTags(post.getTags().stream().map(tagMapper::toDto).collect(Collectors.toList())); dto.setTags(post.getTags().stream().map(tagMapper::toDto).collect(Collectors.toList()));
dto.setViews(post.getViews()); dto.setViews(post.getViews());
dto.setCommentCount(commentService.countComments(post.getId()));
dto.setStatus(post.getStatus()); dto.setStatus(post.getStatus());
dto.setPinnedAt(post.getPinnedAt()); 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") @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); 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; 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 @Transactional
public void deleteComment(String username, Long id) { public void deleteComment(String username, Long id) {
log.debug("deleteComment called by user {} for comment {}", username, id); log.debug("deleteComment called by user {} for comment {}", username, id);

View File

@@ -175,8 +175,7 @@ export default {
}) })
}) })
const countComments = (list) => // Backend now returns comment counts directly
list.reduce((sum, c) => sum + 1 + countComments(c.replies || []), 0)
const loadOptions = async () => { const loadOptions = async () => {
if (selectedCategory.value && !isNaN(selectedCategory.value)) { if (selectedCategory.value && !isNaN(selectedCategory.value)) {
@@ -267,7 +266,7 @@ export default {
category: p.category, category: p.category,
tags: p.tags || [], tags: p.tags || [],
members: (p.participants || []).map(m => ({ id: m.id, avatar: m.avatar })), members: (p.participants || []).map(m => ({ id: m.id, avatar: m.avatar })),
comments: countComments(p.comments || []), comments: p.commentCount,
views: p.views, views: p.views,
time: TimeManager.format(p.createdAt), time: TimeManager.format(p.createdAt),
pinned: !!p.pinnedAt pinned: !!p.pinnedAt
@@ -304,7 +303,7 @@ export default {
category: p.category, category: p.category,
tags: p.tags || [], tags: p.tags || [],
members: (p.participants || []).map(m => ({ id: m.id, avatar: m.avatar })), members: (p.participants || []).map(m => ({ id: m.id, avatar: m.avatar })),
comments: countComments(p.comments || []), comments: p.commentCount,
views: p.views, views: p.views,
time: TimeManager.format(p.createdAt), time: TimeManager.format(p.createdAt),
pinned: !!p.pinnedAt pinned: !!p.pinnedAt
@@ -341,7 +340,7 @@ export default {
category: p.category, category: p.category,
tags: p.tags || [], tags: p.tags || [],
members: (p.participants || []).map(m => ({ id: m.id, avatar: m.avatar })), members: (p.participants || []).map(m => ({ id: m.id, avatar: m.avatar })),
comments: countComments(p.comments || []), comments: p.commentCount,
views: p.views, views: p.views,
time: TimeManager.format(p.lastReplyAt || p.createdAt), time: TimeManager.format(p.lastReplyAt || p.createdAt),
pinned: !!p.pinnedAt pinned: !!p.pinnedAt