photoprism/internal/models/tag.go
2018-11-06 19:02:03 +01:00

39 lines
624 B
Go

package models
import (
"strings"
"github.com/gosimple/slug"
"github.com/jinzhu/gorm"
)
// Photo tagß
type Tag struct {
gorm.Model
TagSlug string `gorm:"type:varchar(100);unique_index"`
TagLabel string `gorm:"type:varchar(100);unique_index"`
}
// Create a new tag
func NewTag(label string) *Tag {
if label == "" {
label = "unknown"
}
tagLabel := strings.ToLower(label)
tagSlug := slug.MakeLang(tagLabel, "en")
result := &Tag{
TagLabel: tagLabel,
TagSlug: tagSlug,
}
return result
}
func (t *Tag) FirstOrCreate(db *gorm.DB) *Tag {
db.FirstOrCreate(t, "tag_label = ?", t.TagLabel)
return t
}