feat(post): implement share recording functionality
- 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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user