77 lines
2.3 KiB
Go
77 lines
2.3 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"kontor-api-gin/internal/models"
|
|
"log/slog"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type MediaActorRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func (repository *MediaActorRepository) Import(dtos models.MediaActorDTOList) {
|
|
slog.Info("MediaActorRepository#Import")
|
|
for index, dto := range dtos {
|
|
slog.Info("importing", "index", index, "video", dto)
|
|
model := dto.ConvertDTOToModel()
|
|
existing_item := models.MediaActor{ID: dto.ID}
|
|
result_existing := repository.db.First(&existing_item)
|
|
if result_existing.Error != nil {
|
|
slog.Info("check existing id: not found - inserting")
|
|
result := repository.db.Create(&model)
|
|
if result.Error != nil {
|
|
slog.Info("error during import", "error", result.Error.Error())
|
|
}
|
|
} else {
|
|
slog.Info("check existing id: found - updating")
|
|
result := repository.db.Save(&model)
|
|
if result.Error != nil {
|
|
slog.Info("error during updating", "error", result.Error.Error())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func NewMediaActorRepository(db *gorm.DB) *MediaActorRepository {
|
|
repo := &MediaActorRepository{db: db}
|
|
return repo
|
|
}
|
|
|
|
func (repository *MediaActorRepository) FindAll(context context.Context) ([]models.MediaActor, error) {
|
|
var actors []models.MediaActor
|
|
|
|
result := repository.db.Find(&actors)
|
|
slog.Info("MediaActorRepository#FindAll", "count", result.RowsAffected)
|
|
|
|
return actors, nil
|
|
}
|
|
|
|
func (repository *MediaActorRepository) GetById(context context.Context, id string) (*models.MediaActor, error) {
|
|
var actor = models.MediaActor{ID: id}
|
|
slog.Info("MediaActorRepository#GetById", "id", id)
|
|
result := repository.db.First(&actor)
|
|
if result.Error != nil {
|
|
slog.Info("SportRepository#GetById", "error", result.Error.Error())
|
|
}
|
|
return &actor, result.Error
|
|
}
|
|
|
|
func (repository *MediaActorRepository) Add(context context.Context, url string) (*models.MediaActor, error) {
|
|
slog.Info("MediaActorRepository#Add", "url", url)
|
|
return nil, fmt.Errorf("unimplemented method MediaFileRepository#Add")
|
|
}
|
|
|
|
func (repository *MediaActorRepository) Delete(context context.Context, id string) error {
|
|
var actor = models.MediaActor{ID: id}
|
|
slog.Info("MediaActorRepository#Delete", "id", id)
|
|
result := repository.db.Delete(&actor)
|
|
if result.Error != nil {
|
|
slog.Info("SportRepository#Delete", "error", result.Error.Error())
|
|
}
|
|
return result.Error
|
|
}
|