feat: community RPC 从内存存储迁移到 ent 数据库

This commit is contained in:
zetaloop
2026-04-24 08:16:31 +08:00
parent 5ad579f03c
commit 6cc14479c5
69 changed files with 14396 additions and 501 deletions
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"juwan-backend/app/community/rpc/internal/models/posts"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
@@ -29,58 +30,51 @@ func (l *UpdatePostsLogic) UpdatePosts(in *pb.UpdatePostsReq) (*pb.UpdatePostsRe
return nil, errors.New("id is required")
}
store := l.svcCtx.Store
store.Mu.Lock()
defer store.Mu.Unlock()
updater := l.svcCtx.CommunityModelRW.Posts.Update().
Where(posts.IDEQ(in.GetId()), posts.DeletedAtIsNil())
post, ok := store.Posts[in.GetId()]
if !ok || post.GetDeletedAt() > 0 {
if in.AuthorId != nil {
updater = updater.SetAuthorID(in.GetAuthorId())
}
if in.AuthorRole != nil {
updater = updater.SetAuthorRole(in.GetAuthorRole())
}
if in.Title != nil {
updater = updater.SetTitle(in.GetTitle())
}
if in.Content != nil {
updater = updater.SetContent(in.GetContent())
}
if len(in.Images) > 0 {
updater = updater.SetImages(toTextArray(in.GetImages()))
}
if len(in.Tags) > 0 {
updater = updater.SetTags(toTextArray(in.GetTags()))
}
if in.LinkedOrderId != nil {
updater = updater.SetLinkedOrderID(in.GetLinkedOrderId())
}
if in.QuotedPostId != nil {
updater = updater.SetQuotedPostID(in.GetQuotedPostId())
}
if in.LikeCount != nil {
updater = updater.SetLikeCount(int(in.GetLikeCount()))
}
if in.CommentCount != nil {
updater = updater.SetCommentCount(int(in.GetCommentCount()))
}
if in.Pinned != nil {
updater = updater.SetPinned(in.GetPinned())
}
n, err := updater.Save(l.ctx)
if err != nil {
logx.Errorf("updatePosts err: %v", err)
return nil, errors.New("update post failed")
}
if n == 0 {
return nil, errors.New("post not found")
}
if in.AuthorId != nil {
post.AuthorId = in.GetAuthorId()
}
if in.AuthorRole != nil {
post.AuthorRole = in.GetAuthorRole()
}
if in.Title != nil {
post.Title = in.GetTitle()
}
if in.Content != nil {
post.Content = in.GetContent()
}
if len(in.Images) > 0 {
post.Images = append([]string(nil), in.GetImages()...)
}
if len(in.Tags) > 0 {
post.Tags = append([]string(nil), in.GetTags()...)
}
if in.LinkedOrderId != nil {
post.LinkedOrderId = in.GetLinkedOrderId()
}
if in.QuotedPostId != nil {
post.QuotedPostId = in.GetQuotedPostId()
}
if in.LikeCount != nil {
post.LikeCount = in.GetLikeCount()
}
if in.CommentCount != nil {
post.CommentCount = in.GetCommentCount()
}
if in.Pinned != nil {
post.Pinned = in.GetPinned()
}
if in.SearchText != nil {
post.SearchText = in.GetSearchText()
}
if in.DeletedAt != nil {
post.DeletedAt = in.GetDeletedAt()
}
if in.CreatedAt != nil {
post.CreatedAt = in.GetCreatedAt()
}
post.UpdatedAt = nowUnix(in.GetUpdatedAt())
return &pb.UpdatePostsResp{}, nil
}