photoprism/internal/entity/keyword.go
François d'Yvoire 55693fab35
Some more comment improvements (#257)
* Improve comment in classify package

* improve comment in config package

* improve entity package comments

* grammar error in comments
2020-02-21 01:14:45 +01:00

38 lines
789 B
Go

package entity
import (
"strings"
"github.com/jinzhu/gorm"
"github.com/photoprism/photoprism/internal/mutex"
)
// Keyword used for full text search
type Keyword struct {
ID uint `gorm:"primary_key"`
Keyword string `gorm:"type:varchar(64);index;"`
Skip bool
}
// NewKeyword registers a new keyword in database
func NewKeyword(keyword string) *Keyword {
keyword = strings.ToLower(strings.TrimSpace(keyword))
result := &Keyword{
Keyword: keyword,
}
return result
}
// FirstOrCreate checks wether the keyword already exist in the database
func (m *Keyword) FirstOrCreate(db *gorm.DB) *Keyword {
mutex.Db.Lock()
defer mutex.Db.Unlock()
if err := db.FirstOrCreate(m, "keyword = ?", m.Keyword).Error; err != nil {
log.Errorf("keyword: %s", err)
}
return m
}