2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2018-09-24 19:07:43 +02:00
|
|
|
|
|
|
|
import (
|
2020-01-06 04:24:49 +01:00
|
|
|
"strings"
|
2019-12-27 05:18:52 +01:00
|
|
|
"time"
|
|
|
|
|
2018-09-24 19:07:43 +02:00
|
|
|
"github.com/gosimple/slug"
|
|
|
|
)
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Lens represents camera lens (as extracted from UpdateExif metadata)
|
2018-09-24 19:07:43 +02:00
|
|
|
type Lens struct {
|
2020-05-23 20:58:58 +02:00
|
|
|
ID uint `gorm:"primary_key" json:"ID" yaml:"ID"`
|
|
|
|
LensSlug string `gorm:"type:varbinary(255);unique_index;" json:"Slug" yaml:"Slug,omitempty"`
|
|
|
|
LensModel string `json:"Model" yaml:"Model"`
|
|
|
|
LensMake string `json:"Make" yaml:"Make"`
|
|
|
|
LensType string `json:"Type" yaml:"Type,omitempty"`
|
|
|
|
LensDescription string `gorm:"type:text;" json:"Description,omitempty" yaml:"Description,omitempty"`
|
|
|
|
LensNotes string `gorm:"type:text;" json:"Notes,omitempty" yaml:"Notes,omitempty"`
|
|
|
|
CreatedAt time.Time `json:"-" yaml:"-"`
|
|
|
|
UpdatedAt time.Time `json:"-" yaml:"-"`
|
|
|
|
DeletedAt *time.Time `sql:"index" json:"-" yaml:"-"`
|
2018-09-24 19:07:43 +02:00
|
|
|
}
|
|
|
|
|
2020-04-25 14:22:47 +02:00
|
|
|
var UnknownLens = Lens{
|
|
|
|
LensModel: "Unknown",
|
|
|
|
LensMake: "",
|
|
|
|
LensSlug: "zz",
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateUnknownLens initializes the database with an unknown lens if not exists
|
2020-04-30 20:07:03 +02:00
|
|
|
func CreateUnknownLens() {
|
2020-05-24 22:16:06 +02:00
|
|
|
FirstOrCreateLens(&UnknownLens)
|
2020-04-25 14:22:47 +02:00
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// TableName returns Lens table identifier "lens"
|
2018-09-24 19:07:43 +02:00
|
|
|
func (Lens) TableName() string {
|
|
|
|
return "lenses"
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NewLens creates a new lens in database
|
2018-09-24 19:07:43 +02:00
|
|
|
func NewLens(modelName string, makeName string) *Lens {
|
2020-01-06 04:24:49 +01:00
|
|
|
modelName = strings.TrimSpace(modelName)
|
|
|
|
makeName = strings.TrimSpace(makeName)
|
2020-04-25 14:22:47 +02:00
|
|
|
lensSlug := slug.MakeLang(modelName, "en")
|
2020-01-06 04:24:49 +01:00
|
|
|
|
2018-09-24 19:07:43 +02:00
|
|
|
if modelName == "" {
|
2020-04-25 14:22:47 +02:00
|
|
|
return &UnknownLens
|
2018-09-24 19:07:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
result := &Lens{
|
|
|
|
LensModel: modelName,
|
|
|
|
LensMake: makeName,
|
|
|
|
LensSlug: lensSlug,
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-05-24 22:16:06 +02:00
|
|
|
// Create inserts a new row to the database.
|
|
|
|
func (m *Lens) Create() error {
|
2020-05-26 11:00:39 +02:00
|
|
|
return Db().Create(m).Error
|
2020-05-24 22:16:06 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
// FirstOrCreateLens returns the existing row, inserts a new row or nil in case of errors.
|
2020-05-24 22:16:06 +02:00
|
|
|
func FirstOrCreateLens(m *Lens) *Lens {
|
|
|
|
result := Lens{}
|
|
|
|
|
|
|
|
if err := Db().Where("lens_slug = ?", m.LensSlug).First(&result).Error; err == nil {
|
|
|
|
return &result
|
|
|
|
} else if err := m.Create(); err != nil {
|
2019-12-19 09:37:10 +01:00
|
|
|
log.Errorf("lens: %s", err)
|
2020-05-24 22:16:06 +02:00
|
|
|
return nil
|
2019-12-19 09:37:10 +01:00
|
|
|
}
|
2018-09-24 19:07:43 +02:00
|
|
|
|
2019-06-04 18:26:35 +02:00
|
|
|
return m
|
2018-09-24 19:07:43 +02:00
|
|
|
}
|