From 3c47a85ea544623acf9a52926192ac4d6e47c17d Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Mon, 11 May 2020 11:01:28 +0200 Subject: [PATCH] Backend: Move UpdatePhotoCounts() to entity package Signed-off-by: Michael Mayer --- internal/api/batch.go | 6 +++--- internal/api/photo.go | 6 ------ internal/api/photo_label.go | 12 ----------- internal/entity/photo.go | 20 +++++++++++++++++-- internal/{query => entity}/photo_counts.go | 4 +++- .../{query => entity}/photo_counts_test.go | 2 +- internal/photoprism/import.go | 5 +---- internal/photoprism/index.go | 5 ++--- internal/photoprism/purge.go | 5 ++--- internal/workers/sync_download.go | 4 +--- 10 files changed, 31 insertions(+), 38 deletions(-) rename internal/{query => entity}/photo_counts.go (97%) rename internal/{query => entity}/photo_counts_test.go (89%) diff --git a/internal/api/batch.go b/internal/api/batch.go index f078c0995..d36a1403d 100644 --- a/internal/api/batch.go +++ b/internal/api/batch.go @@ -48,7 +48,7 @@ func BatchPhotosArchive(router *gin.RouterGroup, conf *config.Config) { return } - if err := query.UpdatePhotoCounts(); err != nil { + if err := entity.UpdatePhotoCounts(); err != nil { log.Errorf("photos: %s", err) } @@ -95,7 +95,7 @@ func BatchPhotosRestore(router *gin.RouterGroup, conf *config.Config) { return } - if err := query.UpdatePhotoCounts(); err != nil { + if err := entity.UpdatePhotoCounts(); err != nil { log.Errorf("photos: %s", err) } @@ -175,7 +175,7 @@ func BatchPhotosPrivate(router *gin.RouterGroup, conf *config.Config) { return } - if err := query.UpdatePhotoCounts(); err != nil { + if err := entity.UpdatePhotoCounts(); err != nil { log.Errorf("photos: %s", err) } diff --git a/internal/api/photo.go b/internal/api/photo.go index 289cc3867..a117c5b0a 100644 --- a/internal/api/photo.go +++ b/internal/api/photo.go @@ -76,12 +76,6 @@ func UpdatePhoto(router *gin.RouterGroup, conf *config.Config) { return } - log.Info("photo: updating related entity counts") - - if err := query.UpdatePhotoCounts(); err != nil { - log.Errorf("photo: %s", err) - } - PublishPhotoEvent(EntityUpdated, uuid, c) event.Success("photo saved") diff --git a/internal/api/photo_label.go b/internal/api/photo_label.go index 1fd9197b4..00e86d364 100644 --- a/internal/api/photo_label.go +++ b/internal/api/photo_label.go @@ -71,12 +71,6 @@ func AddPhotoLabel(router *gin.RouterGroup, conf *config.Config) { return } - log.Info("photo: updating related entity counts") - - if err := query.UpdatePhotoCounts(); err != nil { - log.Errorf("photo: %s", err) - } - PublishPhotoEvent(EntityUpdated, c.Param("uuid"), c) event.Success("label updated") @@ -137,12 +131,6 @@ func RemovePhotoLabel(router *gin.RouterGroup, conf *config.Config) { return } - log.Info("photo: updating related entity counts") - - if err := query.UpdatePhotoCounts(); err != nil { - log.Errorf("photo: %s", err) - } - PublishPhotoEvent(EntityUpdated, c.Param("uuid"), c) event.Success("label removed") diff --git a/internal/entity/photo.go b/internal/entity/photo.go index a0af033cc..e16f9fdcc 100644 --- a/internal/entity/photo.go +++ b/internal/entity/photo.go @@ -106,7 +106,15 @@ func SavePhotoForm(model Photo, form form.Photo, geoApi string) error { model.EditedAt = &edited model.PhotoQuality = model.QualityScore() - return db.Unscoped().Save(&model).Error + if err := db.Unscoped().Save(&model).Error; err != nil { + return err + } + + if err := UpdatePhotoCounts(); err != nil { + log.Errorf("photo: %s", err) + } + + return nil } // Save stored the entity in the database. @@ -130,7 +138,15 @@ func (m *Photo) Save() error { m.PhotoQuality = m.QualityScore() - return db.Unscoped().Save(m).Error + if err := db.Unscoped().Save(m).Error; err != nil { + return err + } + + if err := UpdatePhotoCounts(); err != nil { + log.Errorf("photo: %s", err) + } + + return nil } // ClassifyLabels returns all associated labels as classify.Labels diff --git a/internal/query/photo_counts.go b/internal/entity/photo_counts.go similarity index 97% rename from internal/query/photo_counts.go rename to internal/entity/photo_counts.go index 9684bfbab..2cb70863e 100644 --- a/internal/query/photo_counts.go +++ b/internal/entity/photo_counts.go @@ -1,9 +1,11 @@ -package query +package entity import "github.com/jinzhu/gorm" // UpdatePhotoCounts updates photos count in related tables as needed. func UpdatePhotoCounts() error { + log.Info("index: updating photo counts") + if err := Db().Table("places"). UpdateColumn("photo_count", gorm.Expr("(SELECT COUNT(*) FROM photos ph "+ "WHERE places.id = ph.place_id "+ diff --git a/internal/query/photo_counts_test.go b/internal/entity/photo_counts_test.go similarity index 89% rename from internal/query/photo_counts_test.go rename to internal/entity/photo_counts_test.go index 064ad6bfe..c7c890045 100644 --- a/internal/query/photo_counts_test.go +++ b/internal/entity/photo_counts_test.go @@ -1,4 +1,4 @@ -package query +package entity import ( "testing" diff --git a/internal/photoprism/import.go b/internal/photoprism/import.go index 041740ead..6a6fd25db 100644 --- a/internal/photoprism/import.go +++ b/internal/photoprism/import.go @@ -14,7 +14,6 @@ import ( "github.com/photoprism/photoprism/internal/entity" "github.com/photoprism/photoprism/internal/event" "github.com/photoprism/photoprism/internal/mutex" - "github.com/photoprism/photoprism/internal/query" "github.com/photoprism/photoprism/pkg/fs" "github.com/photoprism/photoprism/pkg/txt" ) @@ -196,9 +195,7 @@ func (imp *Import) Start(opt ImportOptions) map[string]bool { } if len(done) > 0 { - log.Info("import: updating photo counts") - - if err := query.UpdatePhotoCounts(); err != nil { + if err := entity.UpdatePhotoCounts(); err != nil { log.Errorf("import: %s", err) } } diff --git a/internal/photoprism/index.go b/internal/photoprism/index.go index 1caccc657..549640ba5 100644 --- a/internal/photoprism/index.go +++ b/internal/photoprism/index.go @@ -11,6 +11,7 @@ import ( "github.com/karrick/godirwalk" "github.com/photoprism/photoprism/internal/classify" "github.com/photoprism/photoprism/internal/config" + "github.com/photoprism/photoprism/internal/entity" "github.com/photoprism/photoprism/internal/event" "github.com/photoprism/photoprism/internal/mutex" "github.com/photoprism/photoprism/internal/nsfw" @@ -172,9 +173,7 @@ func (ind *Index) Start(opt IndexOptions) map[string]bool { } if len(done) > 0 { - log.Info("index: updating photo counts") - - if err := query.UpdatePhotoCounts(); err != nil { + if err := entity.UpdatePhotoCounts(); err != nil { log.Errorf("index: %s", err) } } diff --git a/internal/photoprism/purge.go b/internal/photoprism/purge.go index 0459c854c..f09c8977b 100644 --- a/internal/photoprism/purge.go +++ b/internal/photoprism/purge.go @@ -8,6 +8,7 @@ import ( "time" "github.com/photoprism/photoprism/internal/config" + "github.com/photoprism/photoprism/internal/entity" "github.com/photoprism/photoprism/internal/event" "github.com/photoprism/photoprism/internal/mutex" "github.com/photoprism/photoprism/internal/query" @@ -172,9 +173,7 @@ func (prg *Purge) Start(opt PurgeOptions) (purgedFiles map[string]bool, purgedPh return purgedFiles, purgedPhotos, err } - log.Info("purge: updating photo counts") - - if err := query.UpdatePhotoCounts(); err != nil { + if err := entity.UpdatePhotoCounts(); err != nil { log.Errorf("purge: %s", err) } diff --git a/internal/workers/sync_download.go b/internal/workers/sync_download.go index 13c2f2b18..91a421877 100644 --- a/internal/workers/sync_download.go +++ b/internal/workers/sync_download.go @@ -180,9 +180,7 @@ func (s *Sync) download(a entity.Account) (complete bool, err error) { } if len(done) > 0 { - log.Info("sync: updating photo counts") - - if err := query.UpdatePhotoCounts(); err != nil { + if err := entity.UpdatePhotoCounts(); err != nil { log.Errorf("sync: %s", err) } }