update kontor-echo
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"kontor-api-echo/internal/models"
|
||||
"log/slog"
|
||||
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type MediaActorRepository struct {
|
||||
db *bun.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 *bun.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
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"kontor-api-echo/internal/models"
|
||||
"log/slog"
|
||||
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type MediaActorFileRepository struct {
|
||||
db *bun.DB
|
||||
}
|
||||
|
||||
func (repository *MediaActorFileRepository) Import(dtos models.MediaActorFileDTOList) {
|
||||
slog.Info("MediaActorFileRepository#Import")
|
||||
for index, dto := range dtos {
|
||||
slog.Info("importing", "index", index, "video", dto)
|
||||
model := dto.ConvertDTOToModel()
|
||||
existing_item := models.MediaActorFile{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 NewMediaActorFileRepository(db *bun.DB) *MediaActorFileRepository {
|
||||
repo := &MediaActorFileRepository{db: db}
|
||||
return repo
|
||||
}
|
||||
|
||||
func (repository *MediaActorFileRepository) FindAll(context context.Context) ([]models.MediaActorFile, error) {
|
||||
var actor_files []models.MediaActorFile
|
||||
|
||||
result := repository.db.Find(&actor_files)
|
||||
slog.Info("MediaActorFileRepository#FindAll", "count", result.RowsAffected)
|
||||
|
||||
return actor_files, nil
|
||||
}
|
||||
|
||||
func (repository *MediaActorFileRepository) GetById(context context.Context, id string) (*models.MediaActorFile, error) {
|
||||
var actorfile = models.MediaActorFile{ID: id}
|
||||
slog.Info("MediaActorFileRepository#GetById", "id", id)
|
||||
result := repository.db.First(&actorfile)
|
||||
if result.Error != nil {
|
||||
slog.Info("MediaActorFileRepository#GetById", "error", result.Error.Error())
|
||||
return nil, result.Error
|
||||
}
|
||||
return &actorfile, result.Error
|
||||
}
|
||||
|
||||
func (repository *MediaActorFileRepository) Add(context context.Context, media_actor_id, media_file_id string) (*models.MediaActorFile, error) {
|
||||
slog.Info("MediaActorFileRepository#Add", "actor_id", media_actor_id, "file_id", media_file_id)
|
||||
return nil, fmt.Errorf("unimplemented method MediaActorFileRepository#Add")
|
||||
}
|
||||
|
||||
func (repository *MediaActorFileRepository) Delete(context context.Context, id string) error {
|
||||
var file = models.MediaActorFile{ID: id}
|
||||
slog.Info("MediaActorFileRepository#Delete", "id", id)
|
||||
result := repository.db.Delete(&file)
|
||||
if result.Error != nil {
|
||||
slog.Info("MediaActorFileRepository#Delete", "error", result.Error.Error())
|
||||
}
|
||||
return result.Error
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"kontor-api-echo/internal/models"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type MediaArticleRepository struct {
|
||||
db *bun.DB
|
||||
}
|
||||
|
||||
func (repository *MediaArticleRepository) Import(dtos models.MediaArticleDTOList) {
|
||||
slog.Info("MediaArticleRepository#Import")
|
||||
for index, dto := range dtos {
|
||||
slog.Info("importing", "index", index, "profile", dto)
|
||||
model := dto.ConvertDTOToModel()
|
||||
existing_item := models.MediaArticle{ID: dto.ID}
|
||||
err := repository.db.NewSelect().Model(&existing_item).Where("id = ?", dto.ID).Scan(context.Background())
|
||||
if err != nil {
|
||||
slog.Info("check existing id: not found - inserting")
|
||||
_, err := repository.db.NewInsert().Model(&model).Exec(context.Background())
|
||||
if err != nil {
|
||||
slog.Info("error during import", "error", err.Error())
|
||||
}
|
||||
} else {
|
||||
slog.Info("check existing id: found - updating")
|
||||
_, err := repository.db.NewUpdate().Model(&model).
|
||||
Set("updated_at = ? ", time.Now).
|
||||
Where("id = ?", model.ID).
|
||||
Exec(context.Background())
|
||||
if err != nil {
|
||||
slog.Info("error during updating", "error", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewMediaArticleRepository(db *bun.DB) *MediaArticleRepository {
|
||||
repo := &MediaArticleRepository{db: db}
|
||||
return repo
|
||||
}
|
||||
|
||||
func (repository *MediaArticleRepository) FindAll(context context.Context) ([]models.MediaArticle, error) {
|
||||
var articles []models.MediaArticle
|
||||
|
||||
err := repository.db.NewSelect().Model(&articles).Scan(context)
|
||||
if err != nil {
|
||||
slog.Info(err.Error())
|
||||
return nil, echo.ErrInternalServerError
|
||||
}
|
||||
slog.Info("MediaArticleRepository#FindAll", "count", len(articles))
|
||||
|
||||
return articles, nil
|
||||
}
|
||||
|
||||
func (repository *MediaArticleRepository) GetById(context context.Context, id string) (*models.MediaArticle, error) {
|
||||
var article = models.MediaArticle{ID: id}
|
||||
slog.Info("MediaArticleRepository#GetById", "id", id)
|
||||
err := repository.db.NewSelect().Model(&article).Where("id = ?", id).Scan(context)
|
||||
if err != nil {
|
||||
slog.Info("MediaArticleRepository#GetById", "error", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
return &article, err
|
||||
}
|
||||
|
||||
func (repository *MediaArticleRepository) Add(context context.Context, url string) (*models.MediaArticle, error) {
|
||||
slog.Info("MediaArticleRepository#Add", "url", url)
|
||||
return nil, fmt.Errorf("unimplemented method MediaArticleRepository#Add")
|
||||
}
|
||||
|
||||
func (repository *MediaArticleRepository) Delete(context context.Context, id string) error {
|
||||
slog.Info("MediaArticleRepository#Delete", "id", id)
|
||||
_, err := repository.db.NewDelete().Model((*models.MediaArticle)(nil)).Where("id = ?", id).Exec(context)
|
||||
if err != nil {
|
||||
slog.Info("MediaArticleRepository#Delete", "error", err.Error())
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"kontor-api-echo/internal/models"
|
||||
"log/slog"
|
||||
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type MediaFileRepository struct {
|
||||
db *bun.DB
|
||||
}
|
||||
|
||||
func (repository *MediaFileRepository) Import(dtos models.MediaFileDTOList) {
|
||||
slog.Info("MediaFileRepository#Import")
|
||||
for index, dto := range dtos {
|
||||
slog.Info("importing", "index", index, "video", dto)
|
||||
model := dto.ConvertDTOToModel()
|
||||
existing_item := models.MediaFile{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 NewMediaFileRepository(db *bun.DB) *MediaFileRepository {
|
||||
repo := &MediaFileRepository{db: db}
|
||||
return repo
|
||||
}
|
||||
|
||||
func (repository *MediaFileRepository) FindAll(context context.Context) ([]models.MediaFile, error) {
|
||||
var files []models.MediaFile
|
||||
|
||||
result := repository.db.Find(&files)
|
||||
slog.Info("MediaFileRepository#FindAll", "count", result.RowsAffected)
|
||||
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (repository *MediaFileRepository) GetById(context context.Context, id string) (*models.MediaFile, error) {
|
||||
var file = models.MediaFile{ID: id}
|
||||
slog.Info("MediaFileRepository#GetById", "id", id)
|
||||
result := repository.db.First(&file)
|
||||
if result.Error != nil {
|
||||
slog.Info("MediaFileRepository#GetById", "error", result.Error.Error())
|
||||
return nil, result.Error
|
||||
}
|
||||
return &file, result.Error
|
||||
}
|
||||
|
||||
func (repository *MediaFileRepository) Add(context context.Context, url string) (*models.MediaFile, error) {
|
||||
slog.Info("MediaFileRepository#Add", "url", url)
|
||||
return nil, fmt.Errorf("unimplemented method MediaFileRepository#Add")
|
||||
}
|
||||
|
||||
func (repository *MediaFileRepository) Delete(context context.Context, id string) error {
|
||||
var file = models.MediaFile{ID: id}
|
||||
slog.Info("MediaFileRepository#Delete", "id", id)
|
||||
result := repository.db.Delete(&file)
|
||||
if result.Error != nil {
|
||||
slog.Info("MediaFileRepository#Delete", "error", result.Error.Error())
|
||||
}
|
||||
return result.Error
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"kontor-api-echo/internal/helpers"
|
||||
"kontor-api-echo/internal/models"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type MediaLoFiRepository struct {
|
||||
db *bun.DB
|
||||
}
|
||||
|
||||
func (repository *MediaLoFiRepository) Import(dtos models.MediaLoFiDTOList) {
|
||||
slog.Info("MediaLoFiRepository#Import")
|
||||
for index, dto := range dtos {
|
||||
slog.Info("importing", "index", index, "video", dto)
|
||||
model := dto.ConvertDTOToModel()
|
||||
existing_item := models.MediaLoFi{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 NewMediaLoFiRepository(db *bun.DB) *MediaLoFiRepository {
|
||||
repo := &MediaLoFiRepository{db: db}
|
||||
return repo
|
||||
}
|
||||
|
||||
func (repository *MediaLoFiRepository) FindAll(context context.Context) ([]models.MediaLoFi, error) {
|
||||
var lofis []models.MediaLoFi
|
||||
|
||||
result := repository.db.Find(&lofis)
|
||||
slog.Info("MediaLoFiRepository#FindAll", "count", result.RowsAffected)
|
||||
|
||||
return lofis, nil
|
||||
}
|
||||
|
||||
func (repository *MediaLoFiRepository) GetById(context context.Context, id string) (*models.MediaLoFi, error) {
|
||||
var lofi = models.MediaLoFi{ID: id}
|
||||
slog.Info("MediaLoFiRepository#GetById", "id", id)
|
||||
result := repository.db.First(&lofi)
|
||||
if result.Error != nil {
|
||||
slog.Info("MediaLoFiRepository#GetById", "error", result.Error.Error())
|
||||
}
|
||||
return &lofi, result.Error
|
||||
}
|
||||
|
||||
func (repository *MediaLoFiRepository) Add(context context.Context, url string) (*models.MediaLoFi, error) {
|
||||
slog.Info("MediaLoFiRepository#Add", "url", url)
|
||||
webLink := helpers.StripUrl(url, "v")
|
||||
var lofi models.MediaLoFi
|
||||
search_result := repository.db.Where("url = ?", webLink).First(&lofi)
|
||||
if search_result.RowsAffected > 0 {
|
||||
slog.Info("search for existing url", "result", search_result, "lofi", lofi)
|
||||
return &lofi, nil
|
||||
}
|
||||
if errors.Is(search_result.Error, sql.ErrNoRows) {
|
||||
slog.Info("MediaLoFiRepository#Add URL not found", "url", webLink)
|
||||
lofi = models.MediaLoFi{
|
||||
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(&lofi)
|
||||
if add_result.Error != nil {
|
||||
slog.Info("add new MediaLofi", "error", add_result.Error)
|
||||
return nil, add_result.Error
|
||||
}
|
||||
slog.Info("add new MediaLofi", "lofi", lofi)
|
||||
return &lofi, nil
|
||||
}
|
||||
if search_result.Error != nil {
|
||||
return nil, search_result.Error
|
||||
}
|
||||
return nil, fmt.Errorf("MediaLoFiRepository#Add unknown error")
|
||||
}
|
||||
|
||||
func (repository *MediaLoFiRepository) Update(context context.Context, update models.MediaLoFiDTO) (*models.MediaLoFi, error) {
|
||||
slog.Info("MediaLoFiRepository#Update", "lofi", update)
|
||||
var lofi = models.MediaLoFi{ID: update.ID}
|
||||
slog.Info("MediaLoFiRepository#Update", "id", update.ID)
|
||||
result := repository.db.First(&lofi)
|
||||
if result.Error != nil {
|
||||
slog.Info("MediaLoFiRepository#Update", "error", result.Error.Error())
|
||||
return nil, result.Error
|
||||
}
|
||||
lofi.Update(update)
|
||||
repository.db.Save(&lofi)
|
||||
return &lofi, nil
|
||||
}
|
||||
|
||||
func (repository *MediaLoFiRepository) Delete(context context.Context, id string) error {
|
||||
var actor = models.MediaLoFi{ID: id}
|
||||
slog.Info("MediaLoFiRepository#Delete", "id", id)
|
||||
result := repository.db.Delete(&actor)
|
||||
if result.Error != nil {
|
||||
slog.Info("MediaLoFiRepository#Delete", "error", result.Error.Error())
|
||||
}
|
||||
return result.Error
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"kontor-api-echo/internal/helpers"
|
||||
"kontor-api-echo/internal/models"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type MediaMusicRepository struct {
|
||||
db *bun.DB
|
||||
}
|
||||
|
||||
func (repository *MediaMusicRepository) Import(dtos models.MediaMusicDTOList) {
|
||||
slog.Info("MediaMusicRepository#Import")
|
||||
for index, dto := range dtos {
|
||||
slog.Info("importing", "index", index, "video", dto)
|
||||
model := dto.ConvertDTOToModel()
|
||||
existing_item := models.MediaMusic{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 NewMediaMusicRepository(db *bun.DB) *MediaMusicRepository {
|
||||
repo := &MediaMusicRepository{db: db}
|
||||
return repo
|
||||
}
|
||||
|
||||
func (repository *MediaMusicRepository) FindAll(context context.Context) ([]models.MediaMusic, error) {
|
||||
var musics []models.MediaMusic
|
||||
|
||||
result := repository.db.Find(&musics)
|
||||
slog.Info("MediaMusicRepository#FindAll", "count", result.RowsAffected)
|
||||
|
||||
return musics, nil
|
||||
}
|
||||
|
||||
func (repository *MediaMusicRepository) GetById(context context.Context, id string) (*models.MediaMusic, error) {
|
||||
var music = models.MediaMusic{ID: id}
|
||||
slog.Info("MediaMusicRepository#GetById", "id", id)
|
||||
result := repository.db.First(&music)
|
||||
if result.Error != nil {
|
||||
slog.Info("MediaMusicRepository#GetById", "error", result.Error.Error())
|
||||
}
|
||||
return &music, result.Error
|
||||
}
|
||||
|
||||
func (repository *MediaMusicRepository) Add(context context.Context, url string) (*models.MediaMusic, error) {
|
||||
slog.Info("MediaMusicRepository#Add", "url", url)
|
||||
webLink := helpers.StripUrl(url, "v")
|
||||
var music models.MediaMusic
|
||||
search_result := repository.db.Where("url = ?", webLink).First(&music)
|
||||
if search_result.RowsAffected > 0 {
|
||||
slog.Info("search for existing url", "result", search_result, "music", music)
|
||||
return &music, nil
|
||||
}
|
||||
if errors.Is(search_result.Error, sql.ErrNoRows) {
|
||||
slog.Info("MediaMusicRepository#Add URL not found", "url", webLink)
|
||||
music = models.MediaMusic{
|
||||
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(&music)
|
||||
if add_result.Error != nil {
|
||||
slog.Info("add new MediaMusic", "error", add_result.Error)
|
||||
return nil, add_result.Error
|
||||
}
|
||||
slog.Info("add new MediaMusic", "music", music)
|
||||
return &music, nil
|
||||
}
|
||||
if search_result.Error != nil {
|
||||
return nil, search_result.Error
|
||||
}
|
||||
return nil, fmt.Errorf("MediaMusicRepository#Add unknown error")
|
||||
}
|
||||
|
||||
func (repository *MediaMusicRepository) Update(context context.Context, update models.MediaMusicDTO) (*models.MediaMusic, error) {
|
||||
slog.Info("MediaMusicRepository#Update", "music", update)
|
||||
var music = models.MediaMusic{ID: update.ID}
|
||||
slog.Info("MediaMusicRepository#Update", "id", update.ID)
|
||||
result := repository.db.First(&music)
|
||||
if result.Error != nil {
|
||||
slog.Info("MediaMusicRepository#Update", "error", result.Error.Error())
|
||||
return nil, result.Error
|
||||
}
|
||||
music.Update(update)
|
||||
repository.db.Save(&music)
|
||||
return &music, nil
|
||||
}
|
||||
|
||||
func (repository *MediaMusicRepository) Delete(context context.Context, id string) error {
|
||||
var actor = models.MediaMusic{ID: id}
|
||||
slog.Info("MediaMusicRepository#Delete", "id", id)
|
||||
result := repository.db.Delete(&actor)
|
||||
if result.Error != nil {
|
||||
slog.Info("MediaMusicRepository#Delete", "error", result.Error.Error())
|
||||
}
|
||||
return result.Error
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"kontor-api-echo/internal/helpers"
|
||||
"kontor-api-echo/internal/models"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type MediaVideoRepository struct {
|
||||
db *bun.DB
|
||||
}
|
||||
|
||||
func (repository *MediaVideoRepository) Import(dtos models.MediaVideoDTOList) {
|
||||
slog.Info("MediaVideoRepository#Import")
|
||||
for index, dto := range dtos {
|
||||
slog.Info("importing", "index", index, "video", dto)
|
||||
model := dto.ConvertDTOToModel()
|
||||
existing_item := models.MediaVideo{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 NewMediaVideoRepository(db *bun.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, sql.ErrNoRows) {
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user