ListComments 列出评论的实现
(ctx context.Context, postId, minID, limit int64)
| 82 | |
| 83 | // ListComments 列出评论的实现 |
| 84 | func (c *commentService) ListComments(ctx context.Context, postId, minID, limit int64) ([]domain.Comment, error) { |
| 85 | // 获取评论列表 |
| 86 | comments, err := c.repo.ListComments(ctx, postId, minID, limit) |
| 87 | if err != nil { |
| 88 | return nil, fmt.Errorf("获取评论列表失败: %w", err) |
| 89 | } |
| 90 | |
| 91 | // 初始化返回的评论列表 |
| 92 | domainComments := make([]domain.Comment, 0, len(comments)) |
| 93 | var eg errgroup.Group |
| 94 | |
| 95 | // 并发处理每个评论 |
| 96 | for i, comment := range comments { |
| 97 | domainComments = append(domainComments, comment) |
| 98 | |
| 99 | i := i // 创建副本避免闭包问题 |
| 100 | commentId := comment.Id |
| 101 | eg.Go(func() error { |
| 102 | // 获取最新的三条子评论 |
| 103 | subComments, err := c.repo.GetMoreCommentsReply(ctx, commentId, 0, 3) |
| 104 | if err != nil { |
| 105 | return fmt.Errorf("获取子评论失败: %w", err) |
| 106 | } |
| 107 | |
| 108 | domainComments[i].Children = subComments |
| 109 | return nil |
| 110 | }) |
| 111 | } |
| 112 | |
| 113 | if err := eg.Wait(); err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | |
| 117 | return domainComments, nil |
| 118 | } |
| 119 | |
| 120 | func (c *commentService) GetTopCommentsReply(ctx context.Context, postId int64) (domain.Comment, error) { |
| 121 | return c.repo.GetTopCommentsReply(ctx, postId) |
nothing calls this directly
no test coverage detected