From 5645cb1d0ec85ccd73db6557906c2472162ba553 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Wed, 9 Dec 2020 00:44:33 +0100 Subject: [PATCH] Albums: Update folder album slug when path changes Signed-off-by: Michael Mayer --- internal/entity/album.go | 11 +++++++---- internal/entity/album_test.go | 4 ++-- internal/photoprism/moments.go | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/internal/entity/album.go b/internal/entity/album.go index f61ef2848..777f1210c 100644 --- a/internal/entity/album.go +++ b/internal/entity/album.go @@ -119,8 +119,8 @@ func NewAlbum(albumTitle, albumType string) *Album { } // NewFolderAlbum creates a new folder album. -func NewFolderAlbum(albumTitle, albumSlug, albumPath, albumFilter string) *Album { - if albumTitle == "" || albumSlug == "" || albumFilter == "" { +func NewFolderAlbum(albumTitle, albumPath, albumFilter string) *Album { + if albumTitle == "" || albumPath == "" || albumFilter == "" { return nil } @@ -130,7 +130,7 @@ func NewFolderAlbum(albumTitle, albumSlug, albumPath, albumFilter string) *Album AlbumOrder: SortOrderAdded, AlbumType: AlbumFolder, AlbumTitle: albumTitle, - AlbumSlug: albumSlug, + AlbumSlug: slug.Make(albumPath), AlbumPath: albumPath, AlbumFilter: albumFilter, CreatedAt: now, @@ -322,7 +322,10 @@ func (m *Album) Update(attr string, value interface{}) error { // UpdatePath sets a unique path for an albums. func (m *Album) UpdatePath(albumPath string) error { - if err := m.Update("AlbumPath", albumPath); err != nil { + if err := UnscopedDb().Model(m).UpdateColumns(map[string]interface{}{ + "AlbumPath": albumPath, + "AlbumSlug": slug.Make(albumPath), + }).Error; err != nil { return err } else if err := UnscopedDb().Exec("UPDATE albums SET album_path = NULL WHERE album_path = ? AND id <> ?", albumPath, m.ID).Error; err != nil { return err diff --git a/internal/entity/album_test.go b/internal/entity/album_test.go index df49efec3..794fe8470 100644 --- a/internal/entity/album_test.go +++ b/internal/entity/album_test.go @@ -165,7 +165,7 @@ func TestAddPhotoToAlbums(t *testing.T) { func TestNewFolderAlbum(t *testing.T) { t.Run("name Christmas 2018", func(t *testing.T) { - album := NewFolderAlbum("Dogs", "dogs", "dogs", "label:dog") + album := NewFolderAlbum("Dogs", "dogs", "label:dog") assert.Equal(t, "Dogs", album.AlbumTitle) assert.Equal(t, "dogs", album.AlbumSlug) assert.Equal(t, AlbumFolder, album.AlbumType) @@ -173,7 +173,7 @@ func TestNewFolderAlbum(t *testing.T) { assert.Equal(t, "label:dog", album.AlbumFilter) }) t.Run("title empty", func(t *testing.T) { - album := NewFolderAlbum("", "dogs", "dogs", "label:dog") + album := NewFolderAlbum("", "dogs", "label:dog") assert.Nil(t, album) }) } diff --git a/internal/photoprism/moments.go b/internal/photoprism/moments.go index a5e7db028..3ee4ba423 100644 --- a/internal/photoprism/moments.go +++ b/internal/photoprism/moments.go @@ -81,7 +81,7 @@ func (m *Moments) Start() (err error) { } else { log.Tracef("moments: %s already exists (%s)", txt.Quote(a.AlbumTitle), a.AlbumFilter) } - } else if a := entity.NewFolderAlbum(mom.Title(), mom.Slug(), mom.Path, f.Serialize()); a != nil { + } else if a := entity.NewFolderAlbum(mom.Title(), mom.Path, f.Serialize()); a != nil { a.AlbumYear = mom.FolderYear a.AlbumMonth = mom.FolderMonth a.AlbumDay = mom.FolderDay