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"
|
2020-01-08 19:51:21 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
2019-06-04 18:26:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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;"`
|
2020-01-06 02:14:17 +01:00
|
|
|
LabelSlug string `gorm:"type:varbinary(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 {
|
2020-01-12 12:32:24 +01:00
|
|
|
if err := scope.SetColumn("LabelUUID", rnd.PPID('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 {
|
2020-01-08 19:51:21 +01:00
|
|
|
mutex.Db.Lock()
|
|
|
|
defer mutex.Db.Unlock()
|
|
|
|
|
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)
|
|
|
|
}
|