feat: community RPC 从内存存储迁移到 ent 数据库
This commit is contained in:
@@ -4,8 +4,10 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"juwan-backend/app/community/rpc/internal/models/posts"
|
||||
"juwan-backend/app/community/rpc/internal/svc"
|
||||
"juwan-backend/app/community/rpc/pb"
|
||||
"juwan-backend/app/snowflake/rpc/snowflake"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -36,27 +38,33 @@ func (l *AddCommentsLogic) AddComments(in *pb.AddCommentsReq) (*pb.AddCommentsRe
|
||||
return nil, errors.New("content is required")
|
||||
}
|
||||
|
||||
store := l.svcCtx.Store
|
||||
store.Mu.Lock()
|
||||
defer store.Mu.Unlock()
|
||||
|
||||
post, ok := store.Posts[in.GetPostId()]
|
||||
if !ok || post.GetDeletedAt() > 0 {
|
||||
exists, err := l.svcCtx.CommunityModelRO.Posts.Query().
|
||||
Where(posts.IDEQ(in.GetPostId()), posts.DeletedAtIsNil()).
|
||||
Exist(l.ctx)
|
||||
if err != nil || !exists {
|
||||
return nil, errors.New("post not found")
|
||||
}
|
||||
|
||||
now := nowUnix(in.GetCreatedAt())
|
||||
comment := &pb.Comments{
|
||||
Id: store.NextComment(),
|
||||
PostId: in.GetPostId(),
|
||||
AuthorId: in.GetAuthorId(),
|
||||
Content: in.GetContent(),
|
||||
LikeCount: 0,
|
||||
CreatedAt: now,
|
||||
idResp, err := l.svcCtx.Snowflake.NextId(l.ctx, &snowflake.NextIdReq{})
|
||||
if err != nil {
|
||||
return nil, errors.New("create comment id failed")
|
||||
}
|
||||
store.Comments[comment.Id] = comment
|
||||
post.CommentCount++
|
||||
post.UpdatedAt = now
|
||||
|
||||
_, err = l.svcCtx.CommunityModelRW.Comments.Create().
|
||||
SetID(idResp.Id).
|
||||
SetPostID(in.GetPostId()).
|
||||
SetAuthorID(in.GetAuthorId()).
|
||||
SetContent(in.GetContent()).
|
||||
Save(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("addComments err: %v", err)
|
||||
return nil, errors.New("add comment failed")
|
||||
}
|
||||
|
||||
l.svcCtx.CommunityModelRW.Posts.Update().
|
||||
Where(posts.IDEQ(in.GetPostId())).
|
||||
AddCommentCount(1).
|
||||
Exec(l.ctx)
|
||||
|
||||
return &pb.AddCommentsResp{}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user