2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2019-12-08 15:05:35 +01:00
|
|
|
|
2020-01-08 19:51:21 +01:00
|
|
|
import (
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
|
|
|
)
|
2019-12-08 15:05:35 +01:00
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// PhotoKeyword represents the many-to-many relation between Photo and Keyword
|
2019-12-08 15:05:35 +01:00
|
|
|
type PhotoKeyword struct {
|
|
|
|
PhotoID uint `gorm:"primary_key;auto_increment:false"`
|
|
|
|
KeywordID uint `gorm:"primary_key;auto_increment:false;index"`
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// TableName returns PhotoKeyword table identifier "photos_keywords"
|
2019-12-08 15:05:35 +01:00
|
|
|
func (PhotoKeyword) TableName() string {
|
|
|
|
return "photos_keywords"
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NewPhotoKeyword registers a new PhotoKeyword relation
|
2019-12-08 15:05:35 +01:00
|
|
|
func NewPhotoKeyword(photoID, keywordID uint) *PhotoKeyword {
|
|
|
|
result := &PhotoKeyword{
|
|
|
|
PhotoID: photoID,
|
|
|
|
KeywordID: keywordID,
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// FirstOrCreate checks wether the PhotoKeywords relation already exist in the database before the creation
|
2019-12-08 15:05:35 +01:00
|
|
|
func (m *PhotoKeyword) FirstOrCreate(db *gorm.DB) *PhotoKeyword {
|
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, "photo_id = ? AND keyword_id = ?", m.PhotoID, m.KeywordID).Error; err != nil {
|
|
|
|
log.Errorf("photo keyword: %s", err)
|
|
|
|
}
|
2019-12-08 15:05:35 +01:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|