2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
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-05-26 11:00:39 +02:00
|
|
|
// 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
|
2019-12-19 09:37:10 +01:00
|
|
|
}
|
2019-12-08 15:05:35 +01:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|