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 {
|
2022-10-02 11:38:30 +02: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:"-"`
|
2020-12-17 18:24:55 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
// TableName returns the entity table name.
|
2019-12-04 15:14:04 +01:00
|
|
|
func (PhotoAlbum) TableName() string {
|
|
|
|
return "photos_albums"
|
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
// NewPhotoAlbum creates a new photo and album mapping with UIDs.
|
|
|
|
func NewPhotoAlbum(photoUid, albumUid string) *PhotoAlbum {
|
2019-12-04 15:14:04 +01:00
|
|
|
result := &PhotoAlbum{
|
2022-10-02 11:38:30 +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
|
|
|
|
}
|