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
@@ -3,11 +3,12 @@ package logic
import (
"context"
"errors"
"strings"
"juwan-backend/app/community/rpc/internal/models/comments"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"entgo.io/ent/dialect/sql"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -38,43 +39,36 @@ func (l *SearchCommentsLogic) SearchComments(in *pb.SearchCommentsReq) (*pb.Sear
offset = 0
}
store := l.svcCtx.Store
store.Mu.RLock()
defer store.Mu.RUnlock()
query := l.svcCtx.CommunityModelRO.Comments.Query().
Where(comments.DeletedAtIsNil())
filtered := make([]*pb.Comments, 0, len(store.Comments))
for _, c := range store.Comments {
if c.GetDeletedAt() > 0 {
continue
}
if in.GetId() > 0 && c.GetId() != in.GetId() {
continue
}
if in.GetPostId() > 0 && c.GetPostId() != in.GetPostId() {
continue
}
if in.GetAuthorId() > 0 && c.GetAuthorId() != in.GetAuthorId() {
continue
}
if in.Content != nil && !strings.Contains(strings.ToLower(c.GetContent()), strings.ToLower(in.GetContent())) {
continue
}
if in.LikeCount != nil && c.GetLikeCount() != in.GetLikeCount() {
continue
}
cc := *c
filtered = append(filtered, &cc)
if in.GetId() > 0 {
query = query.Where(comments.IDEQ(in.GetId()))
}
if in.GetPostId() > 0 {
query = query.Where(comments.PostIDEQ(in.GetPostId()))
}
if in.GetAuthorId() > 0 {
query = query.Where(comments.AuthorIDEQ(in.GetAuthorId()))
}
if in.Content != nil {
query = query.Where(comments.ContentContainsFold(in.GetContent()))
}
sortCommentsAsc(filtered)
if offset >= int64(len(filtered)) {
return &pb.SearchCommentsResp{Comments: []*pb.Comments{}}, nil
}
end := offset + limit
if end > int64(len(filtered)) {
end = int64(len(filtered))
list, err := query.
Order(comments.ByCreatedAt(sql.OrderAsc()), comments.ByID(sql.OrderAsc())).
Offset(int(offset)).
Limit(int(limit)).
All(l.ctx)
if err != nil {
logx.Errorf("searchComments err: %v", err)
return nil, errors.New("search comments failed")
}
return &pb.SearchCommentsResp{Comments: filtered[offset:end]}, nil
out := make([]*pb.Comments, len(list))
for i, c := range list {
out[i] = entCommentToPb(c)
}
return &pb.SearchCommentsResp{Comments: out}, nil
}