package logic import ( "context" "errors" "juwan-backend/app/community/rpc/internal/svc" "juwan-backend/app/community/rpc/pb" "github.com/zeromicro/go-zero/core/logx" ) type AddCommentsLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } func NewAddCommentsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddCommentsLogic { return &AddCommentsLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } // -----------------------comments----------------------- func (l *AddCommentsLogic) AddComments(in *pb.AddCommentsReq) (*pb.AddCommentsResp, error) { if in.GetPostId() <= 0 { return nil, errors.New("postId is required") } if in.GetAuthorId() <= 0 { return nil, errors.New("authorId is required") } if in.GetContent() == "" { return nil, errors.New("content is required") } store := l.svcCtx.Store store.Mu.Lock() defer store.Mu.Unlock() post, ok := store.Posts[in.GetPostId()] if !ok || post.GetDeletedAt() > 0 { return nil, errors.New("post not found") } now := nowUnix(in.GetCreatedAt()) comment := &pb.Comments{ Id: store.NextComment(), PostId: in.GetPostId(), AuthorId: in.GetAuthorId(), Content: in.GetContent(), LikeCount: 0, CreatedAt: now, } store.Comments[comment.Id] = comment post.CommentCount++ post.UpdatedAt = now return &pb.AddCommentsResp{}, nil }