photoprism/internal/entity/details.go
Michael Mayer eeef16f07e Backend: Refactor photo details entity #379 #357
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-07-06 19:15:57 +02:00

70 lines
2 KiB
Go

package entity
import "time"
// Details stores additional metadata fields for each photo to improve search performance.
type Details struct {
PhotoID uint `gorm:"primary_key;auto_increment:false" yaml:"-"`
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"`
CreatedAt time.Time `yaml:"-"`
UpdatedAt time.Time `yaml:"-"`
}
// NewDetails creates new photo details.
func NewDetails(photo Photo) Details {
return Details{PhotoID: photo.ID}
}
// Create inserts a new row to the database.
func (m *Details) Create() error {
return Db().Create(m).Error
}
// FirstOrCreateDetails returns the existing row, inserts a new row or nil in case of errors.
func FirstOrCreateDetails(m *Details) *Details {
result := Details{}
if err := Db().Where("photo_id = ?", m.PhotoID).First(&result).Error; err == nil {
if m.CreatedAt.IsZero() {
m.CreatedAt = Timestamp()
}
return &result
} else if err := m.Create(); err != nil {
log.Errorf("details: %s", err)
return nil
}
return m
}
// 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 == ""
}