fix version 1
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"bsm/full/module/base/cms/internal/impl"
|
||||
@@ -203,6 +204,30 @@ func ModifyPost(identity string, post *CmsPost, accessory, category, tags []stri
|
||||
})
|
||||
}
|
||||
|
||||
// GetPostForAuth 按 identity 查询文章(不更新点击量),用于写操作的归属校验
|
||||
func GetPostForAuth(identity string) (*CmsPost, error) {
|
||||
post := new(CmsPost)
|
||||
if err := impl.DBService.Where("identity = ?", identity).Take(post).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return post, nil
|
||||
}
|
||||
|
||||
// GetCommentForAuth 按 identity 查询评论,用于写操作的归属校验
|
||||
func GetCommentForAuth(identity string) (*CmsComment, error) {
|
||||
comment := new(CmsComment)
|
||||
if err := impl.DBService.Where("identity = ?", identity).Take(comment).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return comment, nil
|
||||
}
|
||||
|
||||
// DeletePost 删除文章
|
||||
func DeletePost(identity, authorIdentity string) (err error) {
|
||||
return impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
@@ -265,10 +290,13 @@ func GetPost(key, val string) (*CmsPost, error) {
|
||||
// - error: 错误信息
|
||||
func IncrOrDescPostField(identity, column string, desc bool) (err error) {
|
||||
expr := "%s + ?"
|
||||
tx := impl.DBService.Model(&CmsPost{}).Where("identity = ?", identity)
|
||||
if desc {
|
||||
expr = "%s - ?"
|
||||
// 减少时限定计数大于 0,避免出现负值
|
||||
tx = tx.Where(column+" > ?", 0)
|
||||
}
|
||||
if err := impl.DBService.Model(&CmsPost{}).Where("identity = ?", identity).UpdateColumn(column, gorm.Expr(fmt.Sprintf(expr, column), 1)).Error; err != nil {
|
||||
if err := tx.UpdateColumn(column, gorm.Expr(fmt.Sprintf(expr, column), 1)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -308,12 +336,17 @@ func DeleteComment(identity string) (err error) {
|
||||
return err
|
||||
}
|
||||
err = impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
// 删除评论,更新文章评论量
|
||||
// 统计将被级联删除的子评论数量,保证文章评论计数与实际评论数一致
|
||||
var childCnt int64
|
||||
if err := tx.Model(&CmsComment{}).Where("parent_id = ?", comment.ID).Count(&childCnt).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// 删除评论,更新文章评论量(减去本评论及其被级联删除的子评论总数)
|
||||
if err := tx.Where("identity = ?", identity).Delete(&CmsComment{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Model(&CmsPost{}).Where("identity = ?", comment.PostIdentity).
|
||||
UpdateColumn("comment_hits", gorm.Expr("comment_hits - ?", 1)).Error; err != nil {
|
||||
UpdateColumn("comment_hits", gorm.Expr("comment_hits - ?", childCnt+1)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if comment.ParentId != 0 {
|
||||
@@ -343,13 +376,17 @@ func DeleteComment(identity string) (err error) {
|
||||
// 返回:
|
||||
// - error: 错误信息
|
||||
//
|
||||
// TODO: 记录点赞对象避免重复点赞
|
||||
// 说明:现有表结构未记录「谁点过赞」,无法做完整去重;
|
||||
// 完整去重需新增「点赞记录表/字段」,此处仅在计数层做下限保护,避免被刷成负数。
|
||||
func IncrOrDescCommentField(identity, column string, desc bool) (err error) {
|
||||
expr := "%s + ?"
|
||||
tx := impl.DBService.Model(&CmsComment{}).Where("identity = ?", identity)
|
||||
if desc {
|
||||
expr = "%s - ?"
|
||||
// 减少时限定计数大于 0,避免出现负值
|
||||
tx = tx.Where(column+" > ?", 0)
|
||||
}
|
||||
if err = impl.DBService.Model(&CmsComment{}).Where("identity = ?", identity).UpdateColumn(column, gorm.Expr(fmt.Sprintf(expr, column), 1)).Error; err != nil {
|
||||
if err = tx.UpdateColumn(column, gorm.Expr(fmt.Sprintf(expr, column), 1)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user