Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/修改commnet的删除bug #45

Merged
merged 6 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 73 additions & 8 deletions biz/adaptor/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"github.com/CloudStriver/platform/biz/application/service"
"github.com/CloudStriver/platform/biz/infrastructure/config"
"github.com/CloudStriver/platform/biz/infrastructure/consts"
"github.com/CloudStriver/service-idl-gen-go/kitex_gen/platform"
"github.com/zeromicro/go-zero/core/mr"
)
Expand Down Expand Up @@ -69,11 +68,36 @@ func (c *PlatformServerImpl) CreateComment(ctx context.Context, req *platform.Cr
if res, err = c.CommentService.CreateComment(ctx, req); err != nil {
return res, err
}

var getSubjectResp *platform.GetCommentSubjectResp
if getSubjectResp, err = c.SubjectService.GetCommentSubject(ctx, &platform.GetCommentSubjectReq{SubjectId: req.SubjectId}); err != nil {
return res, err
}

_ = mr.Finish(func() error {
c.CommentService.UpdateCount(ctx, req.RootId, req.SubjectId, req.FatherId, consts.Increment)
if req.RootId != req.SubjectId {
if req.FatherId != req.SubjectId {
var getRootComment *platform.GetCommentResp
if getRootComment, err = c.CommentService.GetComment(ctx, &platform.GetCommentReq{CommentId: req.RootId}); err != nil {
return err
}
// 二级评论 + 三级评论
c.CommentService.UpdateCount(ctx, req.RootId, getRootComment.Count+1)
}
}
return nil
}, func() error {
c.SubjectService.UpdateCount(ctx, req.RootId, req.SubjectId, req.FatherId, consts.Increment)
if req.RootId == req.SubjectId {
// 一级评论
if req.FatherId == req.SubjectId {
c.SubjectService.UpdateCount(ctx, req.SubjectId, getSubjectResp.RootCount+1, getSubjectResp.AllCount+1)
}
} else {
// 二级评论 + 三级评论
if req.FatherId != req.SubjectId {
c.SubjectService.UpdateCount(ctx, req.SubjectId, getSubjectResp.RootCount, getSubjectResp.AllCount+1)
}
}
return nil
})
return res, nil
Expand All @@ -84,18 +108,59 @@ func (c *PlatformServerImpl) UpdateComment(ctx context.Context, req *platform.Up
}

func (c *PlatformServerImpl) DeleteComment(ctx context.Context, req *platform.DeleteCommentReq) (res *platform.DeleteCommentResp, err error) {
var data *platform.GetCommentResp
if data, err = c.CommentService.GetComment(ctx, &platform.GetCommentReq{CommentId: req.CommentId}); err != nil {
var (
getCommentResp *platform.GetCommentResp
getSubjectResp *platform.GetCommentSubjectResp
)

if getCommentResp, err = c.CommentService.GetComment(ctx, &platform.GetCommentReq{CommentId: req.CommentId}); err != nil {
return res, err
}
if res, err = c.CommentService.DeleteComment(ctx, req); err != nil {

if getSubjectResp, err = c.SubjectService.GetCommentSubject(ctx, &platform.GetCommentSubjectReq{SubjectId: getCommentResp.SubjectId}); err != nil {
return res, err
}

if getCommentResp.RootId == getCommentResp.SubjectId {
// 一级评论
if getCommentResp.FatherId == getCommentResp.SubjectId {
if res, err = c.CommentService.DeleteComment(ctx, req.CommentId, getCommentResp.Type, true); err != nil {
return res, err
}
}
} else {
// 二级评论 + 三级评论
if getCommentResp.FatherId != getCommentResp.SubjectId {
if res, err = c.CommentService.DeleteComment(ctx, req.CommentId, getCommentResp.Type, false); err != nil {
return res, err
}
}
}

_ = mr.Finish(func() error {
c.CommentService.UpdateCount(ctx, data.RootId, data.SubjectId, data.FatherId, consts.Decrement)
if getCommentResp.RootId != getCommentResp.SubjectId {
if getCommentResp.FatherId != getCommentResp.SubjectId {
var getRootComment *platform.GetCommentResp
if getRootComment, err = c.CommentService.GetComment(ctx, &platform.GetCommentReq{CommentId: getCommentResp.RootId}); err != nil {
return err
}
// 二级评论 + 三级评论
c.CommentService.UpdateCount(ctx, getCommentResp.RootId, getRootComment.Count-1)
}
}
return nil
}, func() error {
c.SubjectService.UpdateCount(ctx, data.RootId, data.SubjectId, data.FatherId, consts.Decrement)
if getCommentResp.RootId == getCommentResp.SubjectId {
// 一级评论
if getCommentResp.FatherId == getCommentResp.SubjectId {
c.SubjectService.UpdateCount(ctx, getCommentResp.SubjectId, getSubjectResp.RootCount-1, getSubjectResp.AllCount-getCommentResp.Count-1)
}
} else {
// 二级评论 + 三级评论
if getCommentResp.FatherId != getCommentResp.SubjectId {
c.SubjectService.UpdateCount(ctx, getCommentResp.SubjectId, getSubjectResp.RootCount, getSubjectResp.AllCount-1)
}
}
return nil
})
return res, nil
Expand Down
99 changes: 84 additions & 15 deletions biz/application/service/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,42 @@ package service

import (
"context"
"github.com/CloudStriver/cloudmind-mq/app/util/message"
"github.com/CloudStriver/go-pkg/utils/pagination"
"github.com/CloudStriver/go-pkg/utils/pconvertor"
"github.com/CloudStriver/go-pkg/utils/util/log"
"github.com/CloudStriver/platform/biz/infrastructure/consts"
"github.com/CloudStriver/platform/biz/infrastructure/convertor"
"github.com/CloudStriver/platform/biz/infrastructure/kq"
commentMapper "github.com/CloudStriver/platform/biz/infrastructure/mapper/comment"
subjectMapper "github.com/CloudStriver/platform/biz/infrastructure/mapper/subject"
"github.com/CloudStriver/platform/biz/infrastructure/sort"
"github.com/CloudStriver/service-idl-gen-go/kitex_gen/platform"
"github.com/bytedance/sonic"
"github.com/google/wire"
"github.com/samber/lo"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"math"
)

type ICommentService interface {
UpdateCount(ctx context.Context, rootId, subjectId, fatherId string, count int64)
UpdateCount(ctx context.Context, rootId string, count int64)
GetComment(ctx context.Context, req *platform.GetCommentReq) (resp *platform.GetCommentResp, err error)
GetCommentList(ctx context.Context, req *platform.GetCommentListReq) (resp *platform.GetCommentListResp, err error)
GetCommentBlocks(ctx context.Context, req *platform.GetCommentBlocksReq) (resp *platform.GetCommentBlocksResp, err error)
CreateComment(ctx context.Context, req *platform.CreateCommentReq) (resp *platform.CreateCommentResp, err error)
UpdateComment(ctx context.Context, req *platform.UpdateCommentReq) (resp *platform.UpdateCommentResp, err error)
DeleteComment(ctx context.Context, req *platform.DeleteCommentReq) (resp *platform.DeleteCommentResp, err error)
DeleteComment(ctx context.Context, commentId string, commentType int64, level bool) (resp *platform.DeleteCommentResp, err error)
DeleteCommentByIds(ctx context.Context, req *platform.DeleteCommentByIdsReq) (resp *platform.DeleteCommentByIdsResp, err error)
SetCommentAttrs(ctx context.Context, req *platform.SetCommentAttrsReq, res *platform.GetCommentSubjectResp) (resp *platform.SetCommentAttrsResp, err error)
}

type CommentService struct {
CommentMongoMapper commentMapper.IMongoMapper
SubjectMongoMapper subjectMapper.IMongoMapper
CommentMongoMapper commentMapper.IMongoMapper
SubjectMongoMapper subjectMapper.IMongoMapper
DeleteCommentRelationKq *kq.DeleteCommentRelationKq
}

var CommentSet = wire.NewSet(
Expand Down Expand Up @@ -67,6 +74,7 @@ func (s *CommentService) GetComment(ctx context.Context, req *platform.GetCommen
AtUserId: data.AtUserId,
Content: data.Content,
Meta: data.Meta,
Type: data.Type,
CreateTime: data.CreateAt.UnixMilli(),
}
return resp, nil
Expand Down Expand Up @@ -174,20 +182,16 @@ func (s *CommentService) CreateComment(ctx context.Context, req *platform.Create
Count: lo.ToPtr(int64(0)),
State: int64(platform.State_Normal),
Attrs: int64(platform.Attrs_None),
Type: req.Type,
}); err != nil {
log.CtxError(ctx, "创建评论 失败[%v]\n", err)
return resp, err
}
return resp, nil
}

func (s *CommentService) UpdateCount(ctx context.Context, rootId, subjectId, fatherId string, count int64) {
if rootId != subjectId {
if fatherId != subjectId {
// 二级评论 + 三级评论
s.CommentMongoMapper.UpdateCount(ctx, rootId, count)
}
}
func (s *CommentService) UpdateCount(ctx context.Context, rootId string, count int64) {
s.CommentMongoMapper.UpdateCount(ctx, rootId, count)
}

func (s *CommentService) UpdateComment(ctx context.Context, req *platform.UpdateCommentReq) (resp *platform.UpdateCommentResp, err error) {
Expand All @@ -208,12 +212,77 @@ func (s *CommentService) UpdateComment(ctx context.Context, req *platform.Update
return resp, nil
}

func (s *CommentService) DeleteComment(ctx context.Context, req *platform.DeleteCommentReq) (resp *platform.DeleteCommentResp, err error) {
func (s *CommentService) DeleteComment(ctx context.Context, commentId string, commentType int64, level bool) (resp *platform.DeleteCommentResp, err error) {
resp = new(platform.DeleteCommentResp)
if _, err = s.CommentMongoMapper.Delete(ctx, req.CommentId); err != nil {
log.CtxError(ctx, "删除评论 失败[%v]\n", err)
return resp, err

if level {
var (
ids []string
comments []*commentMapper.Comment
)
if err = s.CommentMongoMapper.GetConn().Find(ctx, &comments, bson.M{consts.RootId: commentId}); err != nil {
return resp, err
}

ids = lo.Map(comments, func(comment *commentMapper.Comment, _ int) string {
return comment.ID.Hex()
})

tx := s.CommentMongoMapper.StartClient()
if err = tx.UseSession(ctx, func(sessionContext mongo.SessionContext) error {
var err1 error
if err1 = sessionContext.StartTransaction(); err1 != nil {
return err1
}
if _, err1 = s.CommentMongoMapper.Delete(sessionContext, commentId); err1 != nil {
if rbErr := sessionContext.AbortTransaction(sessionContext); rbErr != nil {
log.CtxError(sessionContext, "删除评论: 产生错误[%v]: 回滚异常[%v]\n", err1, rbErr)
}
return err1
}

if _, err1 = s.CommentMongoMapper.DeleteMany(sessionContext, ids); err1 != nil {
if rbErr := sessionContext.AbortTransaction(sessionContext); rbErr != nil {
log.CtxError(sessionContext, "删除子评论 产生错误[%v]: 回滚异常[%v]\n", err1, rbErr)
}
return err1
}

if err1 = sessionContext.CommitTransaction(sessionContext); err1 != nil {
log.CtxError(sessionContext, "删除评论: 提交事务异常[%v]\n", err1)
return err1
}

return nil
}); err != nil {
return resp, err
}

for _, v := range comments {
data, _ := sonic.Marshal(&message.DeleteCommentRelationsMessage{
FromType: v.Type,
FromId: v.ID.Hex(),
})
if err2 := s.DeleteCommentRelationKq.Push(pconvertor.Bytes2String(data)); err2 != nil {
return resp, err2
}
}

} else {
if _, err = s.CommentMongoMapper.Delete(ctx, commentId); err != nil {
log.CtxError(ctx, "删除评论 失败[%v]\n", err)
return resp, err
}
}

data, _ := sonic.Marshal(&message.DeleteCommentRelationsMessage{
FromType: commentType,
FromId: commentId,
})
if err2 := s.DeleteCommentRelationKq.Push(pconvertor.Bytes2String(data)); err2 != nil {
return resp, err2
}

return resp, nil
}

Expand Down
11 changes: 6 additions & 5 deletions biz/application/service/relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"github.com/CloudStriver/go-pkg/utils/pagination"
"github.com/CloudStriver/go-pkg/utils/pagination/mongop"
"github.com/CloudStriver/go-pkg/utils/pconvertor"
"github.com/CloudStriver/go-pkg/utils/util/log"
"github.com/CloudStriver/platform/biz/infrastructure/config"
Expand Down Expand Up @@ -71,7 +72,7 @@ func (s *RelationServiceImpl) DeleteNode(ctx context.Context, req *platform.Dele
OnlyFromId: lo.ToPtr(req.NodeId),
}); err1 != nil {
if rbErr := sessionContext.AbortTransaction(sessionContext); rbErr != nil {
log.CtxError(sessionContext, "保存文件中产生错误[%v]: 回滚异常[%v]\n", err1, rbErr)
log.CtxError(sessionContext, "删除关系节点:产生错误[%v]: 回滚异常[%v]\n", err1, rbErr)
}
return err1
}
Expand All @@ -81,13 +82,13 @@ func (s *RelationServiceImpl) DeleteNode(ctx context.Context, req *platform.Dele
OnlyToId: lo.ToPtr(req.NodeId),
}); err1 != nil {
if rbErr := sessionContext.AbortTransaction(sessionContext); rbErr != nil {
log.CtxError(sessionContext, "保存文件中产生错误[%v]: 回滚异常[%v]\n", err1, rbErr)
log.CtxError(sessionContext, "删除关系节点:产生错误[%v]: 回滚异常[%v]\n", err1, rbErr)
}
return err1
}

if err1 = sessionContext.CommitTransaction(sessionContext); err1 != nil {
log.CtxError(sessionContext, "保存文件: 提交事务异常[%v]\n", err1)
log.CtxError(sessionContext, "删除关系节点: 提交事务异常[%v]\n", err1)
return err1
}
return nil
Expand Down Expand Up @@ -161,7 +162,7 @@ func (s *RelationServiceImpl) GetRelations(ctx context.Context, req *platform.Ge
OnlyFromId: lo.ToPtr(o.FromFilterOptions.FromId),
OnlyToType: lo.ToPtr(o.FromFilterOptions.ToType),
OnlyRelationType: lo.ToPtr(req.RelationType),
}, p, sort.TimeCursorType); err != nil {
}, p, mongop.IdCursorType); err != nil {
return resp, err
}

Expand All @@ -178,7 +179,7 @@ func (s *RelationServiceImpl) GetRelations(ctx context.Context, req *platform.Ge
OnlyToId: lo.ToPtr(o.ToFilterOptions.ToId),
OnlyToType: lo.ToPtr(o.ToFilterOptions.ToType),
OnlyRelationType: lo.ToPtr(req.RelationType),
}, p, sort.TimeCursorType); err != nil {
}, p, mongop.IdCursorType); err != nil {
return resp, err
}

Expand Down
Loading
Loading