feat: community RPC 从内存存储迁移到 ent 数据库
This commit is contained in:
@@ -4,8 +4,11 @@ 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"
|
||||
"juwan-backend/app/snowflake/rpc/snowflake"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -30,26 +33,39 @@ func (l *AddCommentLikesLogic) AddCommentLikes(in *pb.AddCommentLikesReq) (*pb.A
|
||||
return nil, errors.New("commentId and userId are required")
|
||||
}
|
||||
|
||||
store := l.svcCtx.Store
|
||||
store.Mu.Lock()
|
||||
defer store.Mu.Unlock()
|
||||
|
||||
comment, ok := store.Comments[in.GetCommentId()]
|
||||
if !ok || comment.GetDeletedAt() > 0 {
|
||||
exists, err := l.svcCtx.CommunityModelRO.Comments.Query().
|
||||
Where(comments.IDEQ(in.GetCommentId()), comments.DeletedAtIsNil()).
|
||||
Exist(l.ctx)
|
||||
if err != nil || !exists {
|
||||
return nil, errors.New("comment not found")
|
||||
}
|
||||
|
||||
key := commentLikeKey(in.GetCommentId(), in.GetUserId())
|
||||
if _, exists := store.CommentLikes[key]; exists {
|
||||
dup, _ := l.svcCtx.CommunityModelRO.CommentLikes.Query().
|
||||
Where(commentlikes.CommentIDEQ(in.GetCommentId()), commentlikes.UserIDEQ(in.GetUserId())).
|
||||
Exist(l.ctx)
|
||||
if dup {
|
||||
return &pb.AddCommentLikesResp{}, nil
|
||||
}
|
||||
|
||||
store.CommentLikes[key] = &pb.CommentLikes{
|
||||
CommentId: in.GetCommentId(),
|
||||
UserId: in.GetUserId(),
|
||||
CreatedAt: nowUnix(in.GetCreatedAt()),
|
||||
idResp, err := l.svcCtx.Snowflake.NextId(l.ctx, &snowflake.NextIdReq{})
|
||||
if err != nil {
|
||||
return nil, errors.New("create comment like id failed")
|
||||
}
|
||||
comment.LikeCount++
|
||||
|
||||
_, err = l.svcCtx.CommunityModelRW.CommentLikes.Create().
|
||||
SetID(idResp.Id).
|
||||
SetCommentID(in.GetCommentId()).
|
||||
SetUserID(in.GetUserId()).
|
||||
Save(l.ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("addCommentLikes err: %v", err)
|
||||
return nil, errors.New("add comment like failed")
|
||||
}
|
||||
|
||||
l.svcCtx.CommunityModelRW.Comments.Update().
|
||||
Where(comments.IDEQ(in.GetCommentId())).
|
||||
AddLikeCount(1).
|
||||
Exec(l.ctx)
|
||||
|
||||
return &pb.AddCommentLikesResp{}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user