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/commentlikes"
"juwan-backend/app/community/rpc/internal/models/comments"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
@@ -29,36 +31,33 @@ func (l *DelCommentLikesLogic) DelCommentLikes(in *pb.DelCommentLikesReq) (*pb.D
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 := commentLikeKey(in.GetId(), in.GetUserId())
if _, ok := store.CommentLikes[key]; ok {
delete(store.CommentLikes, key)
if comment, ok := store.Comments[in.GetId()]; ok && comment.LikeCount > 0 {
comment.LikeCount--
}
n, err := l.svcCtx.CommunityModelRW.CommentLikes.Delete().
Where(commentlikes.CommentIDEQ(in.GetId()), commentlikes.UserIDEQ(in.GetUserId())).
Exec(l.ctx)
if err != nil {
logx.Errorf("delCommentLikes err: %v", err)
}
if n > 0 {
l.svcCtx.CommunityModelRW.Comments.Update().
Where(comments.IDEQ(in.GetId()), comments.LikeCountGT(0)).
AddLikeCount(-1).
Exec(l.ctx)
}
return &pb.DelCommentLikesResp{}, nil
}
removed := int64(0)
for key, like := range store.CommentLikes {
if like.GetCommentId() == in.GetId() {
delete(store.CommentLikes, key)
removed++
}
n, err := l.svcCtx.CommunityModelRW.CommentLikes.Delete().
Where(commentlikes.CommentIDEQ(in.GetId())).
Exec(l.ctx)
if err != nil {
logx.Errorf("delCommentLikes err: %v", err)
}
if removed > 0 {
if comment, ok := store.Comments[in.GetId()]; ok {
if comment.LikeCount >= removed {
comment.LikeCount -= removed
} else {
comment.LikeCount = 0
}
}
if n > 0 {
l.svcCtx.CommunityModelRW.Comments.Update().
Where(comments.IDEQ(in.GetId()), comments.LikeCountGTE(n)).
AddLikeCount(-n).
Exec(l.ctx)
}
return &pb.DelCommentLikesResp{}, nil