2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2019-12-04 15:14:04 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
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-06-09 19:40:32 +02:00
|
|
|
PhotoUID string `gorm:"type:varbinary(42);primary_key;auto_increment:false"`
|
|
|
|
AlbumUID string `gorm:"type:varbinary(42);primary_key;auto_increment:false;index"`
|
2019-12-04 15:14:04 +01:00
|
|
|
Order int
|
2020-05-09 21:00:02 +02:00
|
|
|
Hidden bool
|
2019-12-04 15:14:04 +01:00
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
2020-05-22 16:29:12 +02:00
|
|
|
Photo *Photo `gorm:"PRELOAD:false"`
|
|
|
|
Album *Album `gorm:"PRELOAD:true"`
|
2019-12-04 15:14:04 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// TableName returns PhotoAlbum table identifier "photos_albums"
|
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
|
|
|
|
}
|