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"
|
||||
|
||||
@@ -28,21 +29,20 @@ func (l *UpdatePostLikesLogic) UpdatePostLikes(in *pb.UpdatePostLikesReq) (*pb.U
|
||||
if in.PostId == nil || in.UserId == nil {
|
||||
return nil, errors.New("postId and userId are required")
|
||||
}
|
||||
postID := in.GetPostId()
|
||||
userID := in.GetUserId()
|
||||
|
||||
store := l.svcCtx.Store
|
||||
store.Mu.Lock()
|
||||
defer store.Mu.Unlock()
|
||||
|
||||
if _, ok := store.Posts[postID]; !ok {
|
||||
return nil, errors.New("post not found")
|
||||
_, err := l.svcCtx.CommunityModelRO.PostLikes.Query().
|
||||
Where(postlikes.PostIDEQ(in.GetPostId()), postlikes.UserIDEQ(in.GetUserId())).
|
||||
First(l.ctx)
|
||||
if err != nil {
|
||||
return nil, errors.New("post like not found")
|
||||
}
|
||||
key := postLikeKey(postID, userID)
|
||||
store.PostLikes[key] = &pb.PostLikes{
|
||||
PostId: postID,
|
||||
UserId: userID,
|
||||
CreatedAt: nowUnix(in.GetCreatedAt()),
|
||||
|
||||
_, err = l.svcCtx.CommunityModelRW.PostLikes.Update().
|
||||
Where(postlikes.PostIDEQ(in.GetPostId()), postlikes.UserIDEQ(in.GetUserId())).
|
||||
Save(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("updatePostLikes err: %v", err)
|
||||
return nil, errors.New("update post like failed")
|
||||
}
|
||||
|
||||
return &pb.UpdatePostLikesResp{}, nil
|
||||
|
||||
Reference in New Issue
Block a user