feat: community RPC 从内存存储迁移到 ent 数据库
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"juwan-backend/app/community/rpc/internal/svc"
|
||||
"juwan-backend/app/community/rpc/pb"
|
||||
"juwan-backend/app/snowflake/rpc/snowflake"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -36,32 +37,38 @@ func (l *AddPostsLogic) AddPosts(in *pb.AddPostsReq) (*pb.AddPostsResp, error) {
|
||||
return nil, errors.New("content is required")
|
||||
}
|
||||
|
||||
store := l.svcCtx.Store
|
||||
store.Mu.Lock()
|
||||
defer store.Mu.Unlock()
|
||||
idResp, err := l.svcCtx.Snowflake.NextId(l.ctx, &snowflake.NextIdReq{})
|
||||
if err != nil {
|
||||
return nil, errors.New("create post id failed")
|
||||
}
|
||||
|
||||
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,
|
||||
authorRole := in.GetAuthorRole()
|
||||
if authorRole == "" {
|
||||
authorRole = "consumer"
|
||||
}
|
||||
if post.AuthorRole == "" {
|
||||
post.AuthorRole = "consumer"
|
||||
|
||||
creator := l.svcCtx.CommunityModelRW.Posts.Create().
|
||||
SetID(idResp.Id).
|
||||
SetAuthorID(in.GetAuthorId()).
|
||||
SetAuthorRole(authorRole).
|
||||
SetTitle(in.GetTitle()).
|
||||
SetContent(in.GetContent()).
|
||||
SetImages(toTextArray(in.GetImages())).
|
||||
SetTags(toTextArray(in.GetTags())).
|
||||
SetPinned(in.GetPinned())
|
||||
|
||||
if in.GetLinkedOrderId() != 0 {
|
||||
creator = creator.SetLinkedOrderID(in.GetLinkedOrderId())
|
||||
}
|
||||
if in.GetQuotedPostId() != 0 {
|
||||
creator = creator.SetQuotedPostID(in.GetQuotedPostId())
|
||||
}
|
||||
|
||||
_, err = creator.Save(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("addPosts err: %v", err)
|
||||
return nil, errors.New("add post failed")
|
||||
}
|
||||
store.Posts[post.Id] = post
|
||||
|
||||
return &pb.AddPostsResp{}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user