feat(feed): add ListLatest
This commit is contained in:
30
internal/feed/repo.go
Normal file
30
internal/feed/repo.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package feed
|
||||
|
||||
import (
|
||||
"context"
|
||||
"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
|
||||
}
|
||||
41
internal/feed/service.go
Normal file
41
internal/feed/service.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package feed
|
||||
|
||||
import (
|
||||
"context"
|
||||
"feedsystem_video_go/internal/video"
|
||||
"time"
|
||||
)
|
||||
|
||||
type FeedService struct {
|
||||
repo *FeedRepository
|
||||
}
|
||||
|
||||
type FeedResponse struct {
|
||||
VideoList []video.Video `json:"video_list"`
|
||||
NextTime time.Time `json:"next_time"`
|
||||
HasMore bool `json:"has_more"`
|
||||
}
|
||||
|
||||
func NewFeedService(repo *FeedRepository) *FeedService {
|
||||
return &FeedService{repo: repo}
|
||||
}
|
||||
|
||||
func (f *FeedService) ListLatest(ctx context.Context, limit int, latestBefore time.Time) (FeedResponse, error) {
|
||||
videos, err := f.repo.ListLatest(ctx, limit, latestBefore)
|
||||
if err != nil {
|
||||
return FeedResponse{}, err
|
||||
}
|
||||
var nextTime time.Time
|
||||
if len(videos) > 0 {
|
||||
nextTime = videos[len(videos)-1].CreateTime
|
||||
} else {
|
||||
nextTime = time.Time{}
|
||||
}
|
||||
hasMore := len(videos) == limit
|
||||
resp := FeedResponse{
|
||||
VideoList: videos,
|
||||
NextTime: nextTime,
|
||||
HasMore: hasMore,
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
Reference in New Issue
Block a user