作者 tangxvhui

调试 评论统计数量

... ... @@ -152,11 +152,23 @@ 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.ArticleCommentRepository.Insert(ctx, c, &newComment)
if err != nil {
return err
}
// 增加文章评论计数
err = l.svcCtx.ArticleRepository.IncreaseCountComment(ctx, c, 1, articleInfo.Id)
if err != nil {
return err
}
//增加段落评论计数
if newComment.SectionId > 0 {
err = l.svcCtx.ArticleSectionRepository.IncreaseCountComment(ctx, c, 1, newComment.SectionId)
if err != nil {
return err
}
}
//增加评论回复计数
if pComment != nil {
err = l.svcCtx.ArticleCommentRepository.IncreaseCountReply(l.ctx, c, 1, pComment.Id)
... ... @@ -171,11 +183,6 @@ func (l *MiniCreateArticleCommentLogic) MiniCreateArticleComment(req *types.Mini
return err
}
}
//保存评论
_, err = l.svcCtx.ArticleCommentRepository.Insert(ctx, c, &newComment)
if err != nil {
return err
}
return nil
}, true)
... ...
... ... @@ -60,6 +60,18 @@ func (l *MiniDeleteArticleCommentLogic) MiniDeleteArticleComment(req *types.Mini
return err
}
}
//减少加段落评论计数
if commetInfo.SectionId > 0 {
err = l.svcCtx.ArticleSectionRepository.IncreaseCountComment(ctx, c, -1, commetInfo.SectionId)
if err != nil {
return err
}
}
// 减少文章的评论数
err = l.svcCtx.ArticleRepository.IncreaseCountComment(ctx, c, -1, commetInfo.ArticleId)
if err != nil {
return err
}
return nil
}, true)
... ...
... ... @@ -178,6 +178,26 @@ func (repository *ArticleSectionRepository) DomainModelToModel(from *domain.Arti
return to, nil
}
// 评论数量变动
func (repository *ArticleSectionRepository) IncreaseCountComment(ctx context.Context, conn transaction.Conn, incr int, sectionId int64) error {
var (
err error
m *models.ArticleSection
tx = conn.DB()
)
m = &models.ArticleSection{Id: sectionId}
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Updates(map[string]interface{}{
"total_comment": gorm.Expr("total_comment+?", incr),
})
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return err
}
return nil
}
func NewArticleSectionRepository(cache *cache.CachedRepository) domain.ArticleSectionRepository {
return &ArticleSectionRepository{CachedRepository: cache}
}
... ...
... ... @@ -27,6 +27,7 @@ type ArticleSectionRepository interface {
Delete(ctx context.Context, conn transaction.Conn, dm *ArticleSection) (*ArticleSection, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ArticleSection, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleSection, error)
IncreaseCountComment(ctx context.Context, conn transaction.Conn, incr int, sectionId int64) error //评论数量变动
}
// 排序文章分段列表
... ...