feat: community RPC 从内存存储迁移到 ent 数据库

This commit is contained in:
zetaloop
2026-04-24 08:16:31 +08:00
parent 5ad579f03c
commit 6cc14479c5
69 changed files with 14396 additions and 501 deletions
@@ -4,6 +4,8 @@ import (
"context"
"errors"
"juwan-backend/app/community/rpc/internal/models/postlikes"
"juwan-backend/app/community/rpc/internal/models/posts"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
@@ -29,36 +31,33 @@ func (l *DelPostLikesLogic) DelPostLikes(in *pb.DelPostLikesReq) (*pb.DelPostLik
return nil, errors.New("id is required")
}
store := l.svcCtx.Store
store.Mu.Lock()
defer store.Mu.Unlock()
if in.UserId != nil && in.GetUserId() > 0 {
key := postLikeKey(in.GetId(), in.GetUserId())
if _, ok := store.PostLikes[key]; ok {
delete(store.PostLikes, key)
if post, ok := store.Posts[in.GetId()]; ok && post.LikeCount > 0 {
post.LikeCount--
}
n, err := l.svcCtx.CommunityModelRW.PostLikes.Delete().
Where(postlikes.PostIDEQ(in.GetId()), postlikes.UserIDEQ(in.GetUserId())).
Exec(l.ctx)
if err != nil {
logx.Errorf("delPostLikes err: %v", err)
}
if n > 0 {
l.svcCtx.CommunityModelRW.Posts.Update().
Where(posts.IDEQ(in.GetId()), posts.LikeCountGT(0)).
AddLikeCount(-1).
Exec(l.ctx)
}
return &pb.DelPostLikesResp{}, nil
}
removed := int64(0)
for key, like := range store.PostLikes {
if like.GetPostId() == in.GetId() {
delete(store.PostLikes, key)
removed++
}
n, err := l.svcCtx.CommunityModelRW.PostLikes.Delete().
Where(postlikes.PostIDEQ(in.GetId())).
Exec(l.ctx)
if err != nil {
logx.Errorf("delPostLikes err: %v", err)
}
if removed > 0 {
if post, ok := store.Posts[in.GetId()]; ok {
if post.LikeCount >= removed {
post.LikeCount -= removed
} else {
post.LikeCount = 0
}
}
if n > 0 {
l.svcCtx.CommunityModelRW.Posts.Update().
Where(posts.IDEQ(in.GetId()), posts.LikeCountGTE(n)).
AddLikeCount(-n).
Exec(l.ctx)
}
return &pb.DelPostLikesResp{}, nil