Files
VLoop/backend/internal/video/video_repo.go

98 lines
2.5 KiB
Go
Raw Normal View History

2025-12-07 16:45:00 +08:00
package video
import (
"context"
2025-12-24 20:48:36 +08:00
"errors"
2025-12-07 16:45:00 +08:00
"gorm.io/gorm"
)
type VideoRepository struct {
db *gorm.DB
}
func NewVideoRepository(db *gorm.DB) *VideoRepository {
return &VideoRepository{db: db}
}
func (vr *VideoRepository) CreateVideo(ctx context.Context, video *Video) error {
if err := vr.db.WithContext(ctx).Create(video).Error; err != nil {
return err
}
return nil
}
func (vr *VideoRepository) DeleteVideo(ctx context.Context, id uint) error {
if err := vr.db.WithContext(ctx).Delete(&Video{}, id).Error; err != nil {
return err
}
return nil
}
2025-12-07 16:45:00 +08:00
func (vr *VideoRepository) ListByAuthorID(ctx context.Context, authorID int64) ([]Video, error) {
var videos []Video
if err := vr.db.WithContext(ctx).
Where("author_id = ?", authorID).
Order("create_time desc").
Offset(0).
Find(&videos).Error; err != nil {
return nil, err
}
return videos, nil
}
func (vr *VideoRepository) GetByID(ctx context.Context, id uint) (*Video, error) {
var video Video
if err := vr.db.WithContext(ctx).First(&video, id).Error; err != nil {
2025-12-24 20:48:36 +08:00
return (*Video)(nil), err
2025-12-07 16:45:00 +08:00
}
return &video, nil
}
func (vr *VideoRepository) UpdateLikesCount(ctx context.Context, id uint, likesCount int64) error {
if err := vr.db.WithContext(ctx).Model(&Video{}).
Where("id = ?", id).
Update("likes_count", likesCount).Error; err != nil {
return err
}
return nil
}
func (vr *VideoRepository) IsExist(ctx context.Context, id uint) (bool, error) {
var video Video
if err := vr.db.WithContext(ctx).First(&video, id).Error; err != nil {
2025-12-24 20:48:36 +08:00
if errors.Is(err, gorm.ErrRecordNotFound) {
return false, nil
}
return false, err
}
return true, nil
}
func (vr *VideoRepository) UpdatePopularity(ctx context.Context, id uint, change int64) error {
if err := vr.db.WithContext(ctx).Model(&Video{}).
Where("id = ?", id).
Update("popularity", gorm.Expr("popularity + ?", change)).Error; err != nil {
return err
}
return nil
}
2025-12-30 01:26:38 +08:00
func (vr *VideoRepository) ChangeLikesCount(ctx context.Context, id uint, change int64) error {
if err := vr.db.WithContext(ctx).Model(&Video{}).
Where("id = ?", id).
UpdateColumn("likes_count", gorm.Expr("GREATEST(likes_count + ?, 0)", change)).Error; err != nil {
return err
}
return nil
}
func (vr *VideoRepository) ChangePopularity(ctx context.Context, id uint, change int64) error {
if err := vr.db.WithContext(ctx).Model(&Video{}).
Where("id = ?", id).
UpdateColumn("popularity", gorm.Expr("GREATEST(popularity + ?, 0)", change)).Error; err != nil {
return err
}
return nil
}