104 lines
3.1 KiB
Go
104 lines
3.1 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"kontor-api-gin/internal/helpers"
|
|
"kontor-api-gin/internal/models"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type MediaVideoRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewMediaVideoRepository(db *gorm.DB) *MediaVideoRepository {
|
|
repo := &MediaVideoRepository{db: db}
|
|
return repo
|
|
}
|
|
|
|
func (repository *MediaVideoRepository) FindAll(context context.Context) ([]models.MediaVideo, error) {
|
|
var videos []models.MediaVideo
|
|
|
|
result := repository.db.Find(&videos)
|
|
slog.Info("MediaVideoRepository#FindAll", "count", result.RowsAffected)
|
|
|
|
return videos, nil
|
|
}
|
|
|
|
func (repository *MediaVideoRepository) GetById(context context.Context, id string) (*models.MediaVideo, error) {
|
|
var video = models.MediaVideo{ID: id}
|
|
slog.Info("MediaVideoRepository#GetById", "id", id)
|
|
result := repository.db.First(&video)
|
|
if result.Error != nil {
|
|
slog.Info("MediaVideoRepository#GetById", "error", result.Error.Error())
|
|
}
|
|
return &video, result.Error
|
|
}
|
|
|
|
func (repository *MediaVideoRepository) Add(context context.Context, url string) (*models.MediaVideo, error) {
|
|
slog.Info("MediaVideoRepository#Add", "url", url)
|
|
webLink := helpers.StripUrl(url, "v")
|
|
var video models.MediaVideo
|
|
search_result := repository.db.Where("url = ?", webLink).First(&video)
|
|
if search_result.RowsAffected > 0 {
|
|
slog.Info("search for existing url", "result", search_result, "video", video)
|
|
return &video, nil
|
|
}
|
|
if errors.Is(search_result.Error, gorm.ErrRecordNotFound) {
|
|
slog.Info("MediaVideoRepository#Add URL not found", "url", webLink)
|
|
video = models.MediaVideo{
|
|
ID: uuid.NewString(),
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
DeletedAt: nil,
|
|
Version: 0,
|
|
WebLink: webLink,
|
|
ShouldDownload: false,
|
|
Review: true,
|
|
Title: "",
|
|
FileName: "",
|
|
}
|
|
add_result := repository.db.Create(&video)
|
|
if add_result.Error != nil {
|
|
slog.Info("add new MediaVideo", "error", add_result.Error)
|
|
return nil, add_result.Error
|
|
}
|
|
slog.Info("add new MediaVideo", "music", video)
|
|
return &video, nil
|
|
}
|
|
if search_result.Error != nil {
|
|
return nil, search_result.Error
|
|
}
|
|
return nil, fmt.Errorf("MediaVideoRepository#Add unknown error")
|
|
}
|
|
|
|
func (repository *MediaVideoRepository) Update(context context.Context, update models.MediaVideoDTO) (*models.MediaVideo, error) {
|
|
slog.Info("MediaVideoRepository#Update", "music", update)
|
|
var video = models.MediaVideo{ID: update.ID}
|
|
slog.Info("MediaVideoRepository#Update", "id", update.ID)
|
|
result := repository.db.First(&video)
|
|
if result.Error != nil {
|
|
slog.Info("MediaVideoRepository#Update", "error", result.Error.Error())
|
|
return nil, result.Error
|
|
}
|
|
video.Update(update)
|
|
repository.db.Save(&video)
|
|
return &video, nil
|
|
}
|
|
|
|
func (repository *MediaVideoRepository) Delete(context context.Context, id string) error {
|
|
var actor = models.MediaVideo{ID: id}
|
|
slog.Info("MediaVideoRepository#Delete", "id", id)
|
|
result := repository.db.Delete(&actor)
|
|
if result.Error != nil {
|
|
slog.Info("MediaVideoRepository#Delete", "error", result.Error.Error())
|
|
}
|
|
return result.Error
|
|
}
|