feat(posts): 优化帖子评论统计性能

- 在 Post 模型中添加 commentCount 和 lastReplyAt 字段
- 在 CommentService 中实现更新帖子评论统计的方法
- 在 PostMapper 中使用 Post 模型中的评论统计字段
- 新增数据库迁移脚本,添加评论统计字段和索引
- 更新相关测试用例
This commit is contained in:
sivdead
2025-09-12 11:08:59 +08:00
parent d2ce203236
commit 1a21ba8935
7 changed files with 178 additions and 2 deletions

View File

@@ -67,7 +67,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.setCommentCount(post.getCommentCount());
dto.setStatus(post.getStatus());
dto.setPinnedAt(post.getPinnedAt());
dto.setRssExcluded(post.getRssExcluded() == null || post.getRssExcluded());
@@ -82,7 +82,7 @@ public class PostMapper {
List<User> participants = commentService.getParticipants(post.getId(), 5);
dto.setParticipants(participants.stream().map(userMapper::toAuthorDto).collect(Collectors.toList()));
LocalDateTime last = commentService.getLastCommentTime(post.getId());
LocalDateTime last = post.getLastReplyAt();
dto.setLastReplyAt(last != null ? last : post.getCreatedAt());
dto.setReward(0);
dto.setSubscribed(false);