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-18 22:18:58 +02:00
|
|
|
// FirstOrCreate checks if the Keywords relation already exist in the database before the creation
|
2020-04-30 20:07:03 +02:00
|
|
|
func (m *PhotoKeyword) FirstOrCreate() *PhotoKeyword {
|
|
|
|
if err := Db().FirstOrCreate(m, "photo_id = ? AND keyword_id = ?", m.PhotoID, m.KeywordID).Error; err != nil {
|
2019-12-19 09:37:10 +01:00
|
|
|
log.Errorf("photo keyword: %s", err)
|
|
|
|
}
|
2019-12-08 15:05:35 +01:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|