photoprism/internal/entity/photo_album.go
Michael Mayer 84b17194af Backend: Refactor person entity #98 #144
Database column types are now UPPERCASE so that developers can easily recognize them. After upgrading to GORM 2.0, we can use generic types instead.

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-09-13 17:51:43 +02:00

56 lines
1.4 KiB
Go

package entity
import (
"time"
)
// PhotoAlbum represents the many_to_many relation between Photo and Album
type PhotoAlbum struct {
PhotoUID string `gorm:"type:VARBINARY(42);primary_key;auto_increment:false"`
AlbumUID string `gorm:"type:VARBINARY(42);primary_key;auto_increment:false;index"`
Order int
Hidden bool
CreatedAt time.Time
UpdatedAt time.Time
Photo *Photo `gorm:"PRELOAD:false"`
Album *Album `gorm:"PRELOAD:true"`
}
// TableName returns PhotoAlbum table identifier "photos_albums"
func (PhotoAlbum) TableName() string {
return "photos_albums"
}
// NewPhotoAlbum registers an photo and album association using UID
func NewPhotoAlbum(photoUID, albumUID string) *PhotoAlbum {
result := &PhotoAlbum{
PhotoUID: photoUID,
AlbumUID: albumUID,
}
return result
}
// Create inserts a new row to the database.
func (m *PhotoAlbum) Create() error {
return Db().Create(m).Error
}
// Save updates or inserts a row.
func (m *PhotoAlbum) Save() error {
return Db().Save(m).Error
}
// 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
}
return m
}