2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2019-12-08 15:05:35 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
2020-01-08 19:51:21 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
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"`
|
|
|
|
Keyword string `gorm:"type:varchar(64);index;"`
|
|
|
|
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 {
|
|
|
|
keyword = strings.ToLower(strings.TrimSpace(keyword))
|
|
|
|
|
|
|
|
result := &Keyword{
|
|
|
|
Keyword: keyword,
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// FirstOrCreate checks wether the keyword already exist in the database
|
2019-12-08 15:05:35 +01:00
|
|
|
func (m *Keyword) FirstOrCreate(db *gorm.DB) *Keyword {
|
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, "keyword = ?", m.Keyword).Error; err != nil {
|
|
|
|
log.Errorf("keyword: %s", err)
|
|
|
|
}
|
2019-12-08 15:05:35 +01:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|