photoprism/internal/entity/photo_keyword.go
Michael Mayer dd42d2b823 Entities: Refactor FirstOrCreate
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-05-26 11:00:39 +02:00

41 lines
1.1 KiB
Go

package entity
// PhotoKeyword represents the many-to-many relation between Photo and Keyword
type PhotoKeyword struct {
PhotoID uint `gorm:"primary_key;auto_increment:false"`
KeywordID uint `gorm:"primary_key;auto_increment:false;index"`
}
// TableName returns PhotoKeyword table identifier "photos_keywords"
func (PhotoKeyword) TableName() string {
return "photos_keywords"
}
// NewPhotoKeyword registers a new PhotoKeyword relation
func NewPhotoKeyword(photoID, keywordID uint) *PhotoKeyword {
result := &PhotoKeyword{
PhotoID: photoID,
KeywordID: keywordID,
}
return result
}
// Create inserts a new row to the database.
func (m *PhotoKeyword) Create() error {
return Db().Create(m).Error
}
// FirstOrCreatePhotoKeyword returns the existing row, inserts a new row or nil in case of errors.
func FirstOrCreatePhotoKeyword(m *PhotoKeyword) *PhotoKeyword {
result := PhotoKeyword{}
if err := Db().Where("photo_id = ? AND keyword_id = ?", m.PhotoID, m.KeywordID).First(&result).Error; err == nil {
return &result
} else if err := m.Create(); err != nil {
log.Errorf("photo-keyword: %s", err)
return nil
}
return m
}