CreateComment 创建评论的实现
(ctx context.Context, comment domain.Comment)
| 35 | |
| 36 | // CreateComment 创建评论的实现 |
| 37 | func (c *commentService) CreateComment(ctx context.Context, comment domain.Comment) error { |
| 38 | // 参数校验 |
| 39 | if comment.Content == "" { |
| 40 | return fmt.Errorf("评论内容不能为空") |
| 41 | } |
| 42 | |
| 43 | // 创建评论 |
| 44 | commentId, err := c.repo.CreateComment(ctx, comment) |
| 45 | if err != nil || commentId == 0 { |
| 46 | return fmt.Errorf("发布评论失败: %w", err) |
| 47 | } |
| 48 | |
| 49 | ctx, cancel := context.WithTimeout(ctx, time.Second*5) |
| 50 | defer cancel() |
| 51 | |
| 52 | general.WithAsyncCancel(ctx, cancel, func() error { |
| 53 | // 异步发送审核事件 |
| 54 | go func() { |
| 55 | event := check.CheckEvent{ |
| 56 | BizId: 2, // 表示审核业务类型为评论 |
| 57 | PostId: uint(commentId), |
| 58 | Content: comment.Content, |
| 59 | Uid: comment.UserId, |
| 60 | } |
| 61 | |
| 62 | if err := c.checkProducer.ProduceCheckEvent(event); err != nil { |
| 63 | log.Printf("发送评论审核事件失败: %v", err) |
| 64 | return |
| 65 | } |
| 66 | }() |
| 67 | return nil |
| 68 | })() |
| 69 | |
| 70 | return nil |
| 71 | } |
| 72 | |
| 73 | // DeleteComment 删除评论的实现 |
| 74 | func (c *commentService) DeleteComment(ctx context.Context, commentId int64) error { |
nothing calls this directly
no test coverage detected