2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2019-12-08 15:05:35 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2020-04-26 14:31:33 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-12-08 15:05:35 +01:00
|
|
|
)
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Keyword used for full text search
|
2019-12-08 15:05:35 +01:00
|
|
|
type Keyword struct {
|
|
|
|
ID uint `gorm:"primary_key"`
|
2020-09-13 17:51:43 +02:00
|
|
|
Keyword string `gorm:"type:VARCHAR(64);index;"`
|
2019-12-08 15:05:35 +01:00
|
|
|
Skip bool
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NewKeyword registers a new keyword in database
|
2019-12-08 15:05:35 +01:00
|
|
|
func NewKeyword(keyword string) *Keyword {
|
2020-04-26 14:31:33 +02:00
|
|
|
keyword = strings.ToLower(txt.Clip(keyword, txt.ClipKeyword))
|
2019-12-08 15:05:35 +01:00
|
|
|
|
|
|
|
result := &Keyword{
|
|
|
|
Keyword: keyword,
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
// Updates multiple columns in the database.
|
|
|
|
func (m *Keyword) Updates(values interface{}) error {
|
|
|
|
return UnscopedDb().Model(m).UpdateColumns(values).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates a column in the database.
|
|
|
|
func (m *Keyword) Update(attr string, value interface{}) error {
|
|
|
|
return UnscopedDb().Model(m).UpdateColumn(attr, value).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save updates the existing or inserts a new row.
|
|
|
|
func (m *Keyword) Save() error {
|
|
|
|
return Db().Save(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create inserts a new row to the database.
|
|
|
|
func (m *Keyword) Create() error {
|
|
|
|
return Db().Create(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// FirstOrCreateKeyword returns the existing row, inserts a new row or nil in case of errors.
|
|
|
|
func FirstOrCreateKeyword(m *Keyword) *Keyword {
|
|
|
|
result := Keyword{}
|
|
|
|
|
|
|
|
if err := Db().Where("keyword = ?", m.Keyword).First(&result).Error; err == nil {
|
|
|
|
return &result
|
2020-07-07 20:44:33 +02:00
|
|
|
} else if createErr := m.Create(); createErr == nil {
|
|
|
|
return m
|
|
|
|
} else if err := Db().Where("keyword = ?", m.Keyword).First(&result).Error; err == nil {
|
|
|
|
return &result
|
|
|
|
} else {
|
|
|
|
log.Errorf("keyword: %s (first or create %s)", createErr, m.Keyword)
|
2019-12-19 09:37:10 +01:00
|
|
|
}
|
2019-12-08 15:05:35 +01:00
|
|
|
|
2020-07-07 20:44:33 +02:00
|
|
|
return nil
|
2019-12-08 15:05:35 +01:00
|
|
|
}
|