2018-09-16 19:09:40 +02:00
|
|
|
package models
|
2018-07-18 15:17:56 +02:00
|
|
|
|
|
|
|
import (
|
2018-10-31 07:14:33 +01:00
|
|
|
"strings"
|
|
|
|
|
2018-09-13 07:05:13 +02:00
|
|
|
"github.com/gosimple/slug"
|
2018-07-18 15:17:56 +02:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
|
|
|
|
2018-11-06 19:02:03 +01:00
|
|
|
// Photo tagß
|
2018-07-18 15:17:56 +02:00
|
|
|
type Tag struct {
|
|
|
|
gorm.Model
|
2018-09-13 07:05:13 +02:00
|
|
|
TagSlug string `gorm:"type:varchar(100);unique_index"`
|
2018-09-19 11:16:18 +02:00
|
|
|
TagLabel string `gorm:"type:varchar(100);unique_index"`
|
2018-09-13 07:05:13 +02:00
|
|
|
}
|
|
|
|
|
2018-11-06 19:02:03 +01:00
|
|
|
// Create a new tag
|
2018-09-13 07:05:13 +02:00
|
|
|
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
|
2018-07-18 15:17:56 +02:00
|
|
|
}
|