feat: community RPC 从内存存储迁移到 ent 数据库
This commit is contained in:
@@ -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,18 +30,12 @@ func (l *GetPostsByIdLogic) GetPostsById(in *pb.GetPostsByIdReq) (*pb.GetPostsBy
|
||||
return nil, errors.New("id is required")
|
||||
}
|
||||
|
||||
store := l.svcCtx.Store
|
||||
store.Mu.RLock()
|
||||
defer store.Mu.RUnlock()
|
||||
|
||||
post, ok := store.Posts[in.GetId()]
|
||||
if !ok || post.GetDeletedAt() > 0 {
|
||||
post, err := l.svcCtx.CommunityModelRO.Posts.Query().
|
||||
Where(posts.IDEQ(in.GetId()), posts.DeletedAtIsNil()).
|
||||
First(l.ctx)
|
||||
if err != nil {
|
||||
return nil, errors.New("post not found")
|
||||
}
|
||||
|
||||
out := *post
|
||||
out.Images = append([]string(nil), post.Images...)
|
||||
out.Tags = append([]string(nil), post.Tags...)
|
||||
|
||||
return &pb.GetPostsByIdResp{Posts: &out}, nil
|
||||
return &pb.GetPostsByIdResp{Posts: entPostToPb(post)}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user