mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-20 22:11:01 +08:00
feat(posts): 优化帖子评论统计性能
- 在 Post 模型中添加 commentCount 和 lastReplyAt 字段 - 在 CommentService 中实现更新帖子评论统计的方法 - 在 PostMapper 中使用 Post 模型中的评论统计字段 - 新增数据库迁移脚本,添加评论统计字段和索引 - 更新相关测试用例
This commit is contained in:
@@ -69,6 +69,10 @@ public class CommentService {
|
||||
comment.setContent(content);
|
||||
comment = commentRepository.save(comment);
|
||||
log.debug("Comment {} saved for post {}", comment.getId(), postId);
|
||||
|
||||
// Update post comment statistics
|
||||
updatePostCommentStats(post);
|
||||
|
||||
imageUploader.addReferences(imageUploader.extractUrls(content));
|
||||
if (!author.getId().equals(post.getAuthor().getId())) {
|
||||
notificationService.createNotification(post.getAuthor(), NotificationType.COMMENT_REPLY, post, comment,
|
||||
@@ -118,6 +122,10 @@ public class CommentService {
|
||||
comment.setContent(content);
|
||||
comment = commentRepository.save(comment);
|
||||
log.debug("Reply {} saved for parent {}", comment.getId(), parentId);
|
||||
|
||||
// Update post comment statistics
|
||||
updatePostCommentStats(parent.getPost());
|
||||
|
||||
imageUploader.addReferences(imageUploader.extractUrls(content));
|
||||
if (!author.getId().equals(parent.getAuthor().getId())) {
|
||||
notificationService.createNotification(parent.getAuthor(), NotificationType.COMMENT_REPLY, parent.getPost(),
|
||||
@@ -263,9 +271,13 @@ public class CommentService {
|
||||
imageUploader.removeReferences(imageUploader.extractUrls(comment.getContent()));
|
||||
|
||||
// 逻辑删除评论
|
||||
Post post = comment.getPost();
|
||||
commentRepository.delete(comment);
|
||||
// 删除积分历史
|
||||
pointHistoryRepository.deleteAll(pointHistories);
|
||||
|
||||
// Update post comment statistics
|
||||
updatePostCommentStats(post);
|
||||
|
||||
// 重新计算受影响用户的积分
|
||||
if (!usersToRecalculate.isEmpty()) {
|
||||
@@ -311,4 +323,19 @@ public class CommentService {
|
||||
int replies = commentRepository.findByParentOrderByCreatedAtAsc(comment).size();
|
||||
return reactions + replies;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update post comment statistics (comment count and last reply time)
|
||||
*/
|
||||
private void updatePostCommentStats(Post post) {
|
||||
long commentCount = commentRepository.countByPostId(post.getId());
|
||||
LocalDateTime lastReplyAt = commentRepository.findLastCommentTime(post);
|
||||
|
||||
post.setCommentCount(commentCount);
|
||||
post.setLastReplyAt(lastReplyAt);
|
||||
postRepository.save(post);
|
||||
|
||||
log.debug("Updated post {} stats: commentCount={}, lastReplyAt={}",
|
||||
post.getId(), commentCount, lastReplyAt);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user