Files
VLoop/internal/feed/repo.go

68 lines
1.8 KiB
Go

package feed
import (
"context"
"feedsystem_video_go/internal/social"
"feedsystem_video_go/internal/video"
"time"
"gorm.io/gorm"
)
type FeedRepository struct {
db *gorm.DB
}
func NewFeedRepository(db *gorm.DB) *FeedRepository {
return &FeedRepository{db: db}
}
func (repo *FeedRepository) ListLatest(ctx context.Context, limit int, latestBefore time.Time) ([]*video.Video, error) {
var videos []*video.Video
query := repo.db.WithContext(ctx).Model(&video.Video{}).
Order("create_time DESC")
if !latestBefore.IsZero() {
query = query.Where("create_time < ?", latestBefore)
}
if err := query.Limit(limit).Find(&videos).Error; err != nil {
return nil, err
}
return videos, nil
}
func (repo *FeedRepository) ListLikesCountWithCursor(ctx context.Context, limit int, cursor *LikesCountCursor) ([]*video.Video, error) {
var videos []*video.Video
query := repo.db.WithContext(ctx).Model(&video.Video{}).
Order("likes_count DESC, id DESC")
if cursor != nil {
query = query.Where(
"(likes_count < ?) OR (likes_count = ? AND id < ?)",
cursor.LikesCount,
cursor.LikesCount, cursor.ID,
)
}
if err := query.Limit(limit).Find(&videos).Error; err != nil {
return nil, err
}
return videos, nil
}
func (repo *FeedRepository) ListByFollowing(ctx context.Context, limit int, viewerAccountID uint) ([]*video.Video, error) {
var videos []*video.Video
query := repo.db.WithContext(ctx).Model(&video.Video{}).
Order("create_time DESC")
if viewerAccountID > 0 {
followingSubQuery := repo.db.WithContext(ctx).
Model(&social.Social{}).
Select("vlogger_id").
Where("follower_id = ?", viewerAccountID)
query = query.Where("author_id IN (?)", followingSubQuery)
}
if err := query.Limit(limit).Find(&videos).Error; err != nil {
return nil, err
}
return videos, nil
}