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 AddPostsLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } func NewAddPostsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddPostsLogic { return &AddPostsLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } // -----------------------posts----------------------- func (l *AddPostsLogic) AddPosts(in *pb.AddPostsReq) (*pb.AddPostsResp, error) { if in.GetAuthorId() <= 0 { return nil, errors.New("authorId is required") } if in.GetTitle() == "" { return nil, errors.New("title is required") } if in.GetContent() == "" { return nil, errors.New("content is required") } store := l.svcCtx.Store store.Mu.Lock() defer store.Mu.Unlock() now := nowUnix(in.GetCreatedAt()) post := &pb.Posts{ Id: store.NextPost(), AuthorId: in.GetAuthorId(), AuthorRole: in.GetAuthorRole(), Title: in.GetTitle(), Content: in.GetContent(), Images: append([]string(nil), in.GetImages()...), Tags: append([]string(nil), in.GetTags()...), LinkedOrderId: in.GetLinkedOrderId(), QuotedPostId: in.GetQuotedPostId(), LikeCount: 0, CommentCount: 0, Pinned: in.GetPinned(), SearchText: in.GetTitle() + " " + in.GetContent(), CreatedAt: now, UpdatedAt: now, } if post.AuthorRole == "" { post.AuthorRole = "consumer" } store.Posts[post.Id] = post return &pb.AddPostsResp{}, nil }