feat: community RPC 从内存存储迁移到 ent 数据库
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"juwan-backend/app/community/rpc/internal/models/commentlikes"
|
||||
"juwan-backend/app/community/rpc/internal/svc"
|
||||
"juwan-backend/app/community/rpc/pb"
|
||||
|
||||
@@ -37,29 +38,27 @@ func (l *SearchCommentLikesLogic) SearchCommentLikes(in *pb.SearchCommentLikesRe
|
||||
offset = 0
|
||||
}
|
||||
|
||||
store := l.svcCtx.Store
|
||||
store.Mu.RLock()
|
||||
defer store.Mu.RUnlock()
|
||||
|
||||
filtered := make([]*pb.CommentLikes, 0, len(store.CommentLikes))
|
||||
for _, like := range store.CommentLikes {
|
||||
if in.GetCommentId() > 0 && like.GetCommentId() != in.GetCommentId() {
|
||||
continue
|
||||
}
|
||||
if in.GetUserId() > 0 && like.GetUserId() != in.GetUserId() {
|
||||
continue
|
||||
}
|
||||
cp := *like
|
||||
filtered = append(filtered, &cp)
|
||||
query := l.svcCtx.CommunityModelRO.CommentLikes.Query()
|
||||
if in.GetCommentId() > 0 {
|
||||
query = query.Where(commentlikes.CommentIDEQ(in.GetCommentId()))
|
||||
}
|
||||
if in.GetUserId() > 0 {
|
||||
query = query.Where(commentlikes.UserIDEQ(in.GetUserId()))
|
||||
}
|
||||
|
||||
if offset >= int64(len(filtered)) {
|
||||
return &pb.SearchCommentLikesResp{CommentLikes: []*pb.CommentLikes{}}, 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("searchCommentLikes err: %v", err)
|
||||
return nil, errors.New("search comment likes failed")
|
||||
}
|
||||
|
||||
return &pb.SearchCommentLikesResp{CommentLikes: filtered[offset:end]}, nil
|
||||
out := make([]*pb.CommentLikes, len(list))
|
||||
for i, like := range list {
|
||||
out[i] = entCommentLikeToPb(like)
|
||||
}
|
||||
|
||||
return &pb.SearchCommentLikesResp{CommentLikes: out}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user