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
@@ -3,7 +3,10 @@ package logic
import (
"context"
"errors"
"time"
"juwan-backend/app/community/rpc/internal/models/comments"
"juwan-backend/app/community/rpc/internal/models/posts"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
@@ -29,22 +32,26 @@ func (l *DelCommentsLogic) DelComments(in *pb.DelCommentsReq) (*pb.DelCommentsRe
return nil, errors.New("id is required")
}
store := l.svcCtx.Store
store.Mu.Lock()
defer store.Mu.Unlock()
comment, ok := store.Comments[in.GetId()]
if !ok {
comment, err := l.svcCtx.CommunityModelRO.Comments.Query().
Where(comments.IDEQ(in.GetId()), comments.DeletedAtIsNil()).
First(l.ctx)
if err != nil {
return &pb.DelCommentsResp{}, nil
}
if comment.GetDeletedAt() == 0 {
now := nowUnix(0)
comment.DeletedAt = now
if post, ok := store.Posts[comment.GetPostId()]; ok && post.GetDeletedAt() == 0 && post.CommentCount > 0 {
post.CommentCount--
post.UpdatedAt = now
}
now := time.Now()
_, err = l.svcCtx.CommunityModelRW.Comments.Update().
Where(comments.IDEQ(in.GetId()), comments.DeletedAtIsNil()).
SetDeletedAt(now).
Save(l.ctx)
if err != nil {
logx.Errorf("delComments err: %v", err)
}
l.svcCtx.CommunityModelRW.Posts.Update().
Where(posts.IDEQ(comment.PostID), posts.DeletedAtIsNil(), posts.CommentCountGT(0)).
AddCommentCount(-1).
Exec(l.ctx)
return &pb.DelCommentsResp{}, nil
}