194 lines
5.5 KiB
Go
194 lines
5.5 KiB
Go
package models
|
|
|
|
import (
|
|
"fmt"
|
|
"kontor-api-echo/internal/config"
|
|
"kontor-api-echo/internal/helpers"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
type MediaLoFi struct {
|
|
bun.BaseModel `bun:"table:media_lofi"`
|
|
|
|
ID string `bun:"id,pk"`
|
|
CreatedAt time.Time `bun:"created_date,nullzero,notnull,default:current_timestamp"`
|
|
UpdatedAt time.Time `bun:"last_modified_date,nullzero,notnull,default:current_timestamp"`
|
|
DeletedAt *time.Time `bun:"deleted_date,nullzero"`
|
|
Version int `bun:"version,default:0"`
|
|
WebLink string `bun:"url,unique:url"`
|
|
Review bool `bun:"review"`
|
|
ShouldDownload bool `bun:"should_download"`
|
|
Title string `bun:"title"`
|
|
FileName string `bun:"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) ConvertMediaLoFiToDTO() MediaLoFiDTO {
|
|
dto := 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 dto
|
|
}
|
|
|
|
type MediaLofiAdd struct {
|
|
Url string `json:"url"`
|
|
}
|
|
|
|
type MediaLoFiForm struct {
|
|
Title string `form:"title" binding:"required"`
|
|
Url string `form:"url" binding:"required"`
|
|
}
|
|
|
|
func (form *MediaLoFiForm) ConvertFormToMediaLofiDTO() MediaLoFiDTO {
|
|
media_lofi := MediaLoFiDTO{}
|
|
return media_lofi
|
|
}
|
|
|
|
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"`
|
|
}
|
|
|
|
func (dto MediaLoFiDTO) ConvertDTOToModel() MediaLoFi {
|
|
model := MediaLoFi{
|
|
ID: dto.ID,
|
|
CreatedAt: dto.CreatedAt.Time,
|
|
UpdatedAt: dto.UpdatedAt.Time,
|
|
DeletedAt: dto.DeletedAt,
|
|
Version: dto.Version,
|
|
FileName: dto.FileName,
|
|
Review: dto.Review,
|
|
ShouldDownload: dto.ShouldDownload,
|
|
Title: dto.Title,
|
|
WebLink: dto.WebLink,
|
|
}
|
|
return model
|
|
}
|
|
|
|
func (source MediaLoFiDTO) Equals(target MediaLoFiDTO) bool {
|
|
if config.Verbose > 1 {
|
|
slog.Info("MediaLoFiDTO#Equals", "id", source.ID)
|
|
}
|
|
equals := true
|
|
if source.Title != target.Title {
|
|
if config.Verbose > 1 {
|
|
slog.Info("MediaLoFiDTO#Equals: Title changed")
|
|
}
|
|
equals = false
|
|
}
|
|
if source.WebLink != target.WebLink {
|
|
if config.Verbose > 1 {
|
|
slog.Info("MediaLoFiDTO#Equals: WebLink changed")
|
|
}
|
|
equals = false
|
|
}
|
|
if source.Version != target.Version {
|
|
if config.Verbose > 1 {
|
|
slog.Info("MediaLoFiDTO#Equals: Version changed")
|
|
}
|
|
equals = false
|
|
}
|
|
if source.FileName != target.FileName {
|
|
if config.Verbose > 1 {
|
|
slog.Info("MediaLoFiDTO#Equals: FileName changed")
|
|
}
|
|
equals = false
|
|
}
|
|
if source.Review != target.Review {
|
|
if config.Verbose > 1 {
|
|
slog.Info("MediaLoFiDTO#Equals: Review changed")
|
|
}
|
|
equals = false
|
|
}
|
|
if source.ShouldDownload != target.ShouldDownload {
|
|
if config.Verbose > 1 {
|
|
slog.Info("MediaLoFiDTO#Equals: ShouldDownload changed")
|
|
}
|
|
equals = false
|
|
}
|
|
return equals
|
|
}
|
|
|
|
type MediaLoFiDTOList []MediaLoFiDTO
|
|
|
|
func (source MediaLoFiDTOList) GetMediaLofi(lofi_id string) *MediaLoFiDTO {
|
|
for _, lofi := range source {
|
|
if lofi.ID == lofi_id {
|
|
return &lofi
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (source *MediaLoFiDTOList) CompareIds(target *MediaLoFiDTOList) (missing, obsolete, changed []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 {
|
|
if config.Verbose > 1 {
|
|
slog.Info(fmt.Sprintf("ID %s found in both lists", id))
|
|
}
|
|
if !sources[id].Equals(targets[id]) {
|
|
changed = append(changed, 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), "changed", len(changed))
|
|
slog.Debug("CompareIds", "missing", missing, "obsolete", obsolete)
|
|
return missing, obsolete, changed
|
|
}
|
|
|
|
func ConvertMediaLoFisToDtos(media_lofis []MediaLoFi) MediaLoFiDTOList {
|
|
dtos := make(MediaLoFiDTOList, len(media_lofis))
|
|
for i, media_lofi := range media_lofis {
|
|
dtos[i] = media_lofi.ConvertMediaLoFiToDTO()
|
|
}
|
|
return dtos
|
|
}
|