feat: community RPC 从内存存储迁移到 ent 数据库
This commit is contained in:
@@ -3,11 +3,12 @@ package logic
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"juwan-backend/app/community/rpc/internal/models/posts"
|
||||
"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,63 +39,39 @@ func (l *SearchPostsLogic) SearchPosts(in *pb.SearchPostsReq) (*pb.SearchPostsRe
|
||||
offset = 0
|
||||
}
|
||||
|
||||
store := l.svcCtx.Store
|
||||
store.Mu.RLock()
|
||||
defer store.Mu.RUnlock()
|
||||
query := l.svcCtx.CommunityModelRO.Posts.Query().
|
||||
Where(posts.DeletedAtIsNil())
|
||||
|
||||
filtered := make([]*pb.Posts, 0, len(store.Posts))
|
||||
for _, p := range store.Posts {
|
||||
if p.GetDeletedAt() > 0 {
|
||||
continue
|
||||
}
|
||||
if in.GetId() > 0 && p.GetId() != in.GetId() {
|
||||
continue
|
||||
}
|
||||
if in.AuthorId != nil && p.GetAuthorId() != in.GetAuthorId() {
|
||||
continue
|
||||
}
|
||||
if in.AuthorRole != nil && p.GetAuthorRole() != in.GetAuthorRole() {
|
||||
continue
|
||||
}
|
||||
if in.Title != nil && !strings.Contains(strings.ToLower(p.GetTitle()), strings.ToLower(in.GetTitle())) {
|
||||
continue
|
||||
}
|
||||
if in.Content != nil && !strings.Contains(strings.ToLower(p.GetContent()), strings.ToLower(in.GetContent())) {
|
||||
continue
|
||||
}
|
||||
if len(in.GetTags()) > 0 {
|
||||
match := false
|
||||
for _, t := range in.GetTags() {
|
||||
for _, pt := range p.GetTags() {
|
||||
if t == pt {
|
||||
match = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if match {
|
||||
break
|
||||
}
|
||||
}
|
||||
if !match {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
cp := *p
|
||||
cp.Images = append([]string(nil), p.Images...)
|
||||
cp.Tags = append([]string(nil), p.Tags...)
|
||||
filtered = append(filtered, &cp)
|
||||
if in.GetId() > 0 {
|
||||
query = query.Where(posts.IDEQ(in.GetId()))
|
||||
}
|
||||
if in.AuthorId != nil {
|
||||
query = query.Where(posts.AuthorIDEQ(in.GetAuthorId()))
|
||||
}
|
||||
if in.AuthorRole != nil {
|
||||
query = query.Where(posts.AuthorRole(in.GetAuthorRole()))
|
||||
}
|
||||
if in.Title != nil {
|
||||
query = query.Where(posts.TitleContainsFold(in.GetTitle()))
|
||||
}
|
||||
if in.Content != nil {
|
||||
query = query.Where(posts.ContentContainsFold(in.GetContent()))
|
||||
}
|
||||
|
||||
sortPostsDesc(filtered)
|
||||
|
||||
if offset >= int64(len(filtered)) {
|
||||
return &pb.SearchPostsResp{Posts: []*pb.Posts{}}, nil
|
||||
}
|
||||
end := offset + limit
|
||||
if end > int64(len(filtered)) {
|
||||
end = int64(len(filtered))
|
||||
list, err := query.
|
||||
Order(posts.ByPinned(sql.OrderDesc()), posts.ByCreatedAt(sql.OrderDesc()), posts.ByID(sql.OrderDesc())).
|
||||
Offset(int(offset)).
|
||||
Limit(int(limit)).
|
||||
All(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("searchPosts err: %v", err)
|
||||
return nil, errors.New("search posts failed")
|
||||
}
|
||||
|
||||
return &pb.SearchPostsResp{Posts: filtered[offset:end]}, nil
|
||||
out := make([]*pb.Posts, len(list))
|
||||
for i, p := range list {
|
||||
out[i] = entPostToPb(p)
|
||||
}
|
||||
|
||||
return &pb.SearchPostsResp{Posts: out}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user