2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2019-12-04 15:14:04 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-12-17 18:24:55 +01:00
|
|
|
type PhotoAlbums []PhotoAlbum
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// PhotoAlbum represents the many_to_many relation between Photo and Album
|
2019-12-04 15:14:04 +01:00
|
|
|
type PhotoAlbum struct {
|
2020-12-17 18:24:55 +01:00
|
|
|
PhotoUID string `gorm:"type:VARBINARY(42);primary_key;auto_increment:false" json:"PhotoUID" yaml:"UID"`
|
|
|
|
AlbumUID string `gorm:"type:VARBINARY(42);primary_key;auto_increment:false;index" json:"AlbumUID" yaml:"-"`
|
|
|
|
Order int `json:"Order" yaml:"Order,omitempty"`
|
|
|
|
Hidden bool `json:"Hidden" yaml:"Hidden,omitempty"`
|
2020-12-27 13:11:08 +01:00
|
|
|
Missing bool `json:"Missing" yaml:"Missing,omitempty"`
|
2020-12-17 18:24:55 +01:00
|
|
|
CreatedAt time.Time `json:"CreatedAt" yaml:"CreatedAt,omitempty"`
|
2020-12-18 09:11:42 +01:00
|
|
|
UpdatedAt time.Time `json:"UpdatedAt" yaml:"-"`
|
2020-12-17 18:24:55 +01:00
|
|
|
Photo *Photo `gorm:"PRELOAD:false" yaml:"-"`
|
|
|
|
Album *Album `gorm:"PRELOAD:true" yaml:"-"`
|
2019-12-04 15:14:04 +01:00
|
|
|
}
|
|
|
|
|
2021-08-15 20:57:26 +02:00
|
|
|
// TableName returns the entity database table name.
|
2019-12-04 15:14:04 +01:00
|
|
|
func (PhotoAlbum) TableName() string {
|
|
|
|
return "photos_albums"
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
// NewPhotoAlbum registers an photo and album association using UID
|
|
|
|
func NewPhotoAlbum(photoUID, albumUID string) *PhotoAlbum {
|
2019-12-04 15:14:04 +01:00
|
|
|
result := &PhotoAlbum{
|
2020-05-23 20:58:58 +02:00
|
|
|
PhotoUID: photoUID,
|
|
|
|
AlbumUID: albumUID,
|
2019-12-04 15:14:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
// Create inserts a new row to the database.
|
|
|
|
func (m *PhotoAlbum) Create() error {
|
|
|
|
return Db().Create(m).Error
|
|
|
|
}
|
|
|
|
|
2020-05-30 01:41:47 +02:00
|
|
|
// Save updates or inserts a row.
|
|
|
|
func (m *PhotoAlbum) Save() error {
|
|
|
|
return Db().Save(m).Error
|
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
// FirstOrCreatePhotoAlbum returns the existing row, inserts a new row or nil in case of errors.
|
|
|
|
func FirstOrCreatePhotoAlbum(m *PhotoAlbum) *PhotoAlbum {
|
|
|
|
result := PhotoAlbum{}
|
|
|
|
|
|
|
|
if err := Db().Where("photo_uid = ? AND album_uid = ?", m.PhotoUID, m.AlbumUID).First(&result).Error; err == nil {
|
|
|
|
return &result
|
|
|
|
} else if err := m.Create(); err != nil {
|
|
|
|
log.Errorf("photo-album: %s", err)
|
|
|
|
return nil
|
2019-12-19 09:37:10 +01:00
|
|
|
}
|
2019-12-04 15:14:04 +01:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|