2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2019-06-04 18:26:35 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2019-12-27 05:18:52 +01:00
|
|
|
"time"
|
2019-06-04 18:26:35 +02:00
|
|
|
|
|
|
|
"github.com/gosimple/slug"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Labels for photo, album and location categorization
|
|
|
|
type Label struct {
|
2019-12-27 05:18:52 +01:00
|
|
|
ID uint `gorm:"primary_key"`
|
|
|
|
LabelUUID string `gorm:"type:varbinary(36);unique_index;"`
|
2019-06-04 18:26:35 +02:00
|
|
|
LabelSlug string `gorm:"type:varchar(128);index;"`
|
2019-07-15 23:11:29 +02:00
|
|
|
LabelName string `gorm:"type:varchar(128);"`
|
2019-06-04 18:26:35 +02:00
|
|
|
LabelPriority int
|
2019-06-09 04:37:02 +02:00
|
|
|
LabelFavorite bool
|
2019-06-04 18:26:35 +02:00
|
|
|
LabelDescription string `gorm:"type:text;"`
|
|
|
|
LabelNotes string `gorm:"type:text;"`
|
2019-06-09 04:37:02 +02:00
|
|
|
LabelCategories []*Label `gorm:"many2many:categories;association_jointable_foreignkey:category_id"`
|
2019-12-27 05:18:52 +01:00
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
DeletedAt *time.Time `sql:"index"`
|
|
|
|
New bool `gorm:"-"`
|
2019-06-04 18:26:35 +02:00
|
|
|
}
|
|
|
|
|
2019-12-16 23:33:52 +01:00
|
|
|
func (m *Label) BeforeCreate(scope *gorm.Scope) error {
|
2019-12-27 05:18:52 +01:00
|
|
|
if err := scope.SetColumn("LabelUUID", ID('l')); err != nil {
|
2019-12-19 09:37:10 +01:00
|
|
|
log.Errorf("label: %s", err)
|
2019-12-16 23:33:52 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-04 18:26:35 +02:00
|
|
|
func NewLabel(labelName string, labelPriority int) *Label {
|
|
|
|
labelName = strings.TrimSpace(labelName)
|
|
|
|
|
|
|
|
if labelName == "" {
|
|
|
|
labelName = "Unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
labelSlug := slug.Make(labelName)
|
|
|
|
|
|
|
|
result := &Label{
|
2019-06-09 04:37:02 +02:00
|
|
|
LabelName: labelName,
|
|
|
|
LabelSlug: labelSlug,
|
2019-06-04 18:26:35 +02:00
|
|
|
LabelPriority: labelPriority,
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Label) FirstOrCreate(db *gorm.DB) *Label {
|
2019-12-19 09:37:10 +01:00
|
|
|
if err := db.FirstOrCreate(m, "label_slug = ?", m.LabelSlug).Error; err != nil {
|
|
|
|
log.Errorf("label: %s", err)
|
|
|
|
}
|
2019-06-04 18:26:35 +02:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
2019-12-11 04:12:54 +01:00
|
|
|
|
|
|
|
func (m *Label) AfterCreate(scope *gorm.Scope) error {
|
|
|
|
return scope.SetColumn("New", true)
|
|
|
|
}
|