feat(post): implement share recording functionality
All checks were successful
Build Backend / build (push) Successful in 13m16s
Build Backend / build-docker (push) Successful in 1m4s

- Added RecordShare method in PostHandler to handle sharing posts and increment share count.
- Introduced IncrementShares method in PostRepository to update shares_count for published posts.
- Updated PostService to include RecordShare, ensuring cache invalidation for post details and lists.
- Integrated new share route in the router for handling share requests.
This commit is contained in:
lafay
2026-03-24 05:21:36 +08:00
parent 176cd20847
commit 15f0f1605e
4 changed files with 79 additions and 0 deletions

View File

@@ -413,6 +413,32 @@ func (r *PostRepository) IncrementViews(postID string) error {
}).Error
}
// IncrementShares 已发布帖子分享次数 +1返回新的 shares_count非已发布或不存在返回 gorm.ErrRecordNotFound
func (r *PostRepository) IncrementShares(postID string) (int, error) {
var newCount int
err := r.db.Transaction(func(tx *gorm.DB) error {
res := tx.Model(&model.Post{}).
Where("id = ? AND status = ?", postID, model.PostStatusPublished).
Updates(map[string]interface{}{
"shares_count": gorm.Expr("shares_count + 1"),
"updated_at": gorm.Expr("updated_at"),
})
if res.Error != nil {
return res.Error
}
if res.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
var p model.Post
if err := tx.Select("shares_count").Where("id = ?", postID).First(&p).Error; err != nil {
return err
}
newCount = p.SharesCount
return nil
})
return newCount, err
}
// Search 搜索帖子
func (r *PostRepository) Search(keyword string, page, pageSize int) ([]*model.Post, int64, error) {
var posts []*model.Post