2020-05-18 22:18:58 +02:00
|
|
|
package entity
|
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
2020-07-06 19:15:57 +02:00
|
|
|
|
2020-05-18 22:18:58 +02:00
|
|
|
// Details stores additional metadata fields for each photo to improve search performance.
|
|
|
|
type Details struct {
|
2020-07-06 19:15:57 +02:00
|
|
|
PhotoID uint `gorm:"primary_key;auto_increment:false" yaml:"-"`
|
2020-09-13 17:51:43 +02:00
|
|
|
Keywords string `gorm:"type:TEXT;" json:"Keywords" yaml:"Keywords"`
|
|
|
|
Notes string `gorm:"type:TEXT;" json:"Notes" yaml:"Notes,omitempty"`
|
|
|
|
Subject string `gorm:"type:VARCHAR(255);" json:"Subject" yaml:"Subject,omitempty"`
|
|
|
|
Artist string `gorm:"type:VARCHAR(255);" json:"Artist" yaml:"Artist,omitempty"`
|
|
|
|
Copyright string `gorm:"type:VARCHAR(255);" json:"Copyright" yaml:"Copyright,omitempty"`
|
|
|
|
License string `gorm:"type:VARCHAR(255);" json:"License" yaml:"License,omitempty"`
|
2020-07-06 19:15:57 +02:00
|
|
|
CreatedAt time.Time `yaml:"-"`
|
|
|
|
UpdatedAt time.Time `yaml:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDetails creates new photo details.
|
|
|
|
func NewDetails(photo Photo) Details {
|
|
|
|
return Details{PhotoID: photo.ID}
|
2020-05-18 22:18:58 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
// Create inserts a new row to the database.
|
|
|
|
func (m *Details) Create() error {
|
2020-07-07 10:51:55 +02:00
|
|
|
if m.PhotoID == 0 {
|
|
|
|
return fmt.Errorf("details: photo id must not be empty (create)")
|
|
|
|
}
|
|
|
|
|
2020-07-07 12:59:47 +02:00
|
|
|
return UnscopedDb().Create(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save updates existing photo details or inserts a new row.
|
|
|
|
func (m *Details) Save() error {
|
|
|
|
if m.PhotoID == 0 {
|
|
|
|
return fmt.Errorf("details: photo id must not be empty (save)")
|
|
|
|
}
|
|
|
|
|
|
|
|
return UnscopedDb().Save(m).Error
|
2020-05-26 11:00:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FirstOrCreateDetails returns the existing row, inserts a new row or nil in case of errors.
|
|
|
|
func FirstOrCreateDetails(m *Details) *Details {
|
|
|
|
result := Details{}
|
|
|
|
|
2020-07-09 17:45:56 +02:00
|
|
|
if err := m.Create(); err == nil {
|
|
|
|
return m
|
|
|
|
} else if err := Db().Where("photo_id = ?", m.PhotoID).First(&result).Error; err == nil {
|
2020-07-06 19:15:57 +02:00
|
|
|
if m.CreatedAt.IsZero() {
|
|
|
|
m.CreatedAt = Timestamp()
|
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
return &result
|
2020-07-09 17:45:56 +02:00
|
|
|
} else {
|
|
|
|
log.Errorf("details: %s (first or create %d)", err, m.PhotoID)
|
2020-05-26 11:00:39 +02:00
|
|
|
}
|
|
|
|
|
2020-07-09 17:45:56 +02:00
|
|
|
return nil
|
2020-05-18 22:18:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NoKeywords checks if the photo has no Keywords
|
|
|
|
func (m *Details) NoKeywords() bool {
|
|
|
|
return m.Keywords == ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoSubject checks if the photo has no Subject
|
|
|
|
func (m *Details) NoSubject() bool {
|
|
|
|
return m.Subject == ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoNotes checks if the photo has no Notes
|
|
|
|
func (m *Details) NoNotes() bool {
|
|
|
|
return m.Notes == ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoArtist checks if the photo has no Artist
|
|
|
|
func (m *Details) NoArtist() bool {
|
|
|
|
return m.Artist == ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoCopyright checks if the photo has no Copyright
|
|
|
|
func (m *Details) NoCopyright() bool {
|
|
|
|
return m.Copyright == ""
|
|
|
|
}
|