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 {
|
2019-12-27 05:18:52 +01:00
|
|
|
PhotoUUID string `gorm:"type:varbinary(36);primary_key;auto_increment:false"`
|
|
|
|
AlbumUUID string `gorm:"type:varbinary(36);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
|
|
|
|
Photo *Photo
|
|
|
|
Album *Album
|
|
|
|
}
|
|
|
|
|
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-02-21 01:14:45 +01:00
|
|
|
// NewPhotoAlbum registers an photo and album association using UUID
|
2019-12-04 15:14:04 +01:00
|
|
|
func NewPhotoAlbum(photoUUID, albumUUID string) *PhotoAlbum {
|
|
|
|
result := &PhotoAlbum{
|
|
|
|
PhotoUUID: photoUUID,
|
|
|
|
AlbumUUID: albumUUID,
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
// FirstOrCreate checks if the PhotoAlbum relation already exist in the database before the creation
|
|
|
|
func (m *PhotoAlbum) FirstOrCreate() *PhotoAlbum {
|
|
|
|
if err := Db().FirstOrCreate(m, "photo_uuid = ? AND album_uuid = ?", m.PhotoUUID, m.AlbumUUID).Error; err != nil {
|
2019-12-19 09:37:10 +01:00
|
|
|
log.Errorf("photo album: %s", err)
|
|
|
|
}
|
2019-12-04 15:14:04 +01:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|