104 lines
3.5 KiB
Go
104 lines
3.5 KiB
Go
package models
|
|
|
|
import (
|
|
"fmt"
|
|
"kontor-api-gin/internal/helpers"
|
|
"log/slog"
|
|
"time"
|
|
)
|
|
|
|
type MediaLoFi struct {
|
|
ID string `gorm:"type:string;primary_key;" json:"id"`
|
|
CreatedAt time.Time `gorm:"column:created_date" json:"created_date"`
|
|
UpdatedAt time.Time `gorm:"column:last_modified_date" json:"last_modified_date"`
|
|
DeletedAt *time.Time `gorm:"column:deleted_date" json:"deleted_date"`
|
|
Version int `gorm:"default:0" json:"version"`
|
|
WebLink string `gorm:"column:url" json:"url"`
|
|
Review bool `gorm:"column:review" json:"review"`
|
|
ShouldDownload bool `gorm:"column:should_download" json:"should_download"`
|
|
Title string `gorm:"size:300;column:title" json:"title"`
|
|
FileName string `gorm:"column:file_name" json:"file_name"`
|
|
}
|
|
|
|
func (media_lofi *MediaLoFi) Update(update MediaLoFiDTO) {
|
|
media_lofi.CreatedAt = update.CreatedAt.Time
|
|
media_lofi.UpdatedAt = time.Now()
|
|
media_lofi.Version = media_lofi.Version + 1
|
|
media_lofi.WebLink = update.WebLink
|
|
media_lofi.Review = update.Review
|
|
media_lofi.ShouldDownload = update.ShouldDownload
|
|
media_lofi.Title = update.Title
|
|
media_lofi.FileName = update.FileName
|
|
}
|
|
|
|
func (media_lofi *MediaLoFi) TableName() string {
|
|
return "media_lofi"
|
|
}
|
|
|
|
type MediaLofiAdd struct {
|
|
Url string `json:"url"`
|
|
}
|
|
|
|
type MediaLoFiDTO struct {
|
|
ID string `json:"id" binding:"required"`
|
|
CreatedAt helpers.KontorTime `json:"created_date"`
|
|
UpdatedAt helpers.KontorTime `json:"last_modified_date"`
|
|
DeletedAt *time.Time `json:"deleted_date,omitempty"`
|
|
Version int `json:"version"`
|
|
FileName string `json:"file_name"`
|
|
Review bool `json:"review"`
|
|
ShouldDownload bool `json:"should_download"`
|
|
Title string `json:"title"`
|
|
WebLink string `json:"url"`
|
|
}
|
|
|
|
type MediaLoFiDTOList []MediaLoFiDTO
|
|
|
|
func (source *MediaLoFiDTOList) CompareIds(target *MediaLoFiDTOList) (missing, obsolete []string) {
|
|
sources := make(map[string]MediaLoFiDTO, len(*source))
|
|
targets := make(map[string]MediaLoFiDTO, len(*target))
|
|
for _, source := range *source {
|
|
sources[source.ID] = source
|
|
}
|
|
for _, target := range *target {
|
|
targets[target.ID] = target
|
|
}
|
|
// iterate source IDs
|
|
for id := range sources {
|
|
if _, ok := targets[id]; ok {
|
|
slog.Debug(fmt.Sprintf("ID %s found in both lists", id))
|
|
} else {
|
|
missing = append(missing, id)
|
|
}
|
|
}
|
|
for id := range targets {
|
|
if _, ok := sources[id]; ok {
|
|
slog.Debug(fmt.Sprintf("ID %s found in both lists", id))
|
|
} else {
|
|
obsolete = append(obsolete, id)
|
|
}
|
|
}
|
|
slog.Info("CompareIds", "missing", len(missing), "obsolete", len(obsolete))
|
|
slog.Debug("CompareIds", "missing", missing, "obsolete", obsolete)
|
|
return missing, obsolete
|
|
}
|
|
|
|
func ConvertMediaLoFisToDtos(media_lofis []MediaLoFi) []MediaLoFiDTO {
|
|
dtos := make([]MediaLoFiDTO, len(media_lofis))
|
|
for i, media_lofi := range media_lofis {
|
|
dtos[i] = MediaLoFiDTO{
|
|
ID: media_lofi.ID,
|
|
CreatedAt: helpers.KontorTime{Time: media_lofi.CreatedAt},
|
|
UpdatedAt: helpers.KontorTime{Time: media_lofi.UpdatedAt},
|
|
DeletedAt: media_lofi.DeletedAt,
|
|
Version: media_lofi.Version,
|
|
WebLink: media_lofi.WebLink,
|
|
Review: media_lofi.Review,
|
|
ShouldDownload: media_lofi.ShouldDownload,
|
|
Title: media_lofi.Title,
|
|
FileName: media_lofi.FileName,
|
|
}
|
|
}
|
|
return dtos
|
|
}
|