feat: community RPC 从内存存储迁移到 ent 数据库
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"juwan-backend/app/community/rpc/internal/models/postlikes"
|
||||
"juwan-backend/app/community/rpc/internal/svc"
|
||||
"juwan-backend/app/community/rpc/pb"
|
||||
|
||||
@@ -37,29 +38,27 @@ func (l *SearchPostLikesLogic) SearchPostLikes(in *pb.SearchPostLikesReq) (*pb.S
|
||||
offset = 0
|
||||
}
|
||||
|
||||
store := l.svcCtx.Store
|
||||
store.Mu.RLock()
|
||||
defer store.Mu.RUnlock()
|
||||
|
||||
filtered := make([]*pb.PostLikes, 0, len(store.PostLikes))
|
||||
for _, like := range store.PostLikes {
|
||||
if in.PostId != nil && like.GetPostId() != in.GetPostId() {
|
||||
continue
|
||||
}
|
||||
if in.UserId != nil && like.GetUserId() != in.GetUserId() {
|
||||
continue
|
||||
}
|
||||
cp := *like
|
||||
filtered = append(filtered, &cp)
|
||||
query := l.svcCtx.CommunityModelRO.PostLikes.Query()
|
||||
if in.PostId != nil {
|
||||
query = query.Where(postlikes.PostIDEQ(in.GetPostId()))
|
||||
}
|
||||
if in.UserId != nil {
|
||||
query = query.Where(postlikes.UserIDEQ(in.GetUserId()))
|
||||
}
|
||||
|
||||
if offset >= int64(len(filtered)) {
|
||||
return &pb.SearchPostLikesResp{PostLikes: []*pb.PostLikes{}}, nil
|
||||
}
|
||||
end := offset + limit
|
||||
if end > int64(len(filtered)) {
|
||||
end = int64(len(filtered))
|
||||
list, err := query.
|
||||
Offset(int(offset)).
|
||||
Limit(int(limit)).
|
||||
All(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("searchPostLikes err: %v", err)
|
||||
return nil, errors.New("search post likes failed")
|
||||
}
|
||||
|
||||
return &pb.SearchPostLikesResp{PostLikes: filtered[offset:end]}, nil
|
||||
out := make([]*pb.PostLikes, len(list))
|
||||
for i, like := range list {
|
||||
out[i] = entPostLikeToPb(like)
|
||||
}
|
||||
|
||||
return &pb.SearchPostLikesResp{PostLikes: out}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user