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:
@@ -64,6 +64,8 @@ type PostService interface {
|
||||
|
||||
// 其他
|
||||
IncrementViews(ctx context.Context, postID, userID string) error
|
||||
// RecordShare 记录分享(仅已发布帖子计数 +1),返回最新 shares_count
|
||||
RecordShare(ctx context.Context, postID, userID string) (sharesCount int, err error)
|
||||
|
||||
// 日志服务设置
|
||||
SetLogService(logService *LogService)
|
||||
@@ -492,6 +494,19 @@ func (s *postServiceImpl) IncrementViews(ctx context.Context, postID, userID str
|
||||
return nil
|
||||
}
|
||||
|
||||
// RecordShare 记录分享:仅已发布帖子增加 shares_count,并失效帖子详情与列表缓存
|
||||
func (s *postServiceImpl) RecordShare(ctx context.Context, postID, userID string) (int, error) {
|
||||
n, err := s.postRepo.IncrementShares(postID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if s.cache != nil {
|
||||
cache.InvalidatePostDetail(s.cache, postID)
|
||||
cache.InvalidatePostList(s.cache)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// GetFavorites 获取收藏列表
|
||||
func (s *postServiceImpl) GetFavorites(ctx context.Context, userID string, page, pageSize int) ([]*model.Post, int64, error) {
|
||||
return s.postRepo.GetFavorites(userID, page, pageSize)
|
||||
|
||||
Reference in New Issue
Block a user