feat:添加热榜功能并添加了增加热度的方法。

This commit is contained in:
Leon
2025-12-26 22:49:17 +08:00
parent 14a905e194
commit 328fae5f07
12 changed files with 327 additions and 10 deletions

View File

@@ -68,3 +68,36 @@ func (repo *FeedRepository) ListByFollowing(ctx context.Context, limit int, view
}
return videos, nil
}
func (repo *FeedRepository) ListByPopularity(ctx context.Context, limit int, popularityBefore int64, timeBefore time.Time, idBefore uint) ([]*video.Video, error) {
var videos []*video.Video
query := repo.db.WithContext(ctx).Model(&video.Video{}).
Order("popularity DESC, create_time DESC, id DESC")
// 只有当游标完整提供时才加过滤popularity 允许为 0
if !timeBefore.IsZero() && idBefore > 0 {
query = query.Where(
"(popularity < ?) OR (popularity = ? AND create_time < ?) OR (popularity = ? AND create_time = ? AND id < ?)",
popularityBefore,
popularityBefore, timeBefore,
popularityBefore, timeBefore, idBefore,
)
}
if err := query.Limit(limit).Find(&videos).Error; err != nil {
return nil, err
}
return videos, nil
}
func (repo *FeedRepository) GetByIDs(ctx context.Context, ids []uint) ([]*video.Video, error) {
var videos []*video.Video
if len(ids) == 0 {
return videos, nil
}
if err := repo.db.WithContext(ctx).Model(&video.Video{}).
Where("id IN ?", ids).Find(&videos).Error; err != nil {
return nil, err
}
return videos, nil
}