作者 tangxvhui

调整评论回复数量计数

... ... @@ -149,11 +149,18 @@ func (l *MiniCreateArticleCommentLogic) MiniCreateArticleComment(req *types.Mini
}
//保存数据
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error {
// 增加评论计数
// 增加文章评论计数
err = l.svcCtx.ArticleRepository.IncreaseCountComment(ctx, c, 1, articleInfo)
if err != nil {
return err
}
//增加评论回复计数
if pComment != nil {
err = l.svcCtx.ArticleCommentRepository.IncreaseCountReply(l.ctx, c, 1, pComment)
if err != nil {
return err
}
}
//保存评论
_, err = l.svcCtx.ArticleCommentRepository.Insert(ctx, c, &newComment)
if err != nil {
... ...
... ... @@ -218,6 +218,27 @@ func (repository *ArticleCommentRepository) IncreaseCountUserLove(ctx context.Co
}
// 点赞数量变动
func (repository *ArticleCommentRepository) IncreaseCountReply(ctx context.Context, conn transaction.Conn, incr int, dm *domain.ArticleComment) error {
var (
err error
m *models.ArticleComment
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return err
}
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Update("count_reply", gorm.Expr("count_reply+?", incr))
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return err
}
return nil
}
func NewArticleCommentRepository(cache *cache.CachedRepository) domain.ArticleCommentRepository {
return &ArticleCommentRepository{CachedRepository: cache}
}
... ...
... ... @@ -48,4 +48,5 @@ type ArticleCommentRepository interface {
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ArticleComment, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleComment, error)
IncreaseCountUserLove(ctx context.Context, conn transaction.Conn, incr int, dm *ArticleComment) error //点赞数量变动
IncreaseCountReply(ctx context.Context, conn transaction.Conn, incr int, dm *ArticleComment) error // 评论回复数量变动
}
... ...