Backend: Move UpdatePhotoCounts() to entity package

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer 2020-05-11 11:01:28 +02:00
parent 71c849c23b
commit 3c47a85ea5
10 changed files with 31 additions and 38 deletions

View file

@ -48,7 +48,7 @@ func BatchPhotosArchive(router *gin.RouterGroup, conf *config.Config) {
return return
} }
if err := query.UpdatePhotoCounts(); err != nil { if err := entity.UpdatePhotoCounts(); err != nil {
log.Errorf("photos: %s", err) log.Errorf("photos: %s", err)
} }
@ -95,7 +95,7 @@ func BatchPhotosRestore(router *gin.RouterGroup, conf *config.Config) {
return return
} }
if err := query.UpdatePhotoCounts(); err != nil { if err := entity.UpdatePhotoCounts(); err != nil {
log.Errorf("photos: %s", err) log.Errorf("photos: %s", err)
} }
@ -175,7 +175,7 @@ func BatchPhotosPrivate(router *gin.RouterGroup, conf *config.Config) {
return return
} }
if err := query.UpdatePhotoCounts(); err != nil { if err := entity.UpdatePhotoCounts(); err != nil {
log.Errorf("photos: %s", err) log.Errorf("photos: %s", err)
} }

View file

@ -76,12 +76,6 @@ func UpdatePhoto(router *gin.RouterGroup, conf *config.Config) {
return return
} }
log.Info("photo: updating related entity counts")
if err := query.UpdatePhotoCounts(); err != nil {
log.Errorf("photo: %s", err)
}
PublishPhotoEvent(EntityUpdated, uuid, c) PublishPhotoEvent(EntityUpdated, uuid, c)
event.Success("photo saved") event.Success("photo saved")

View file

@ -71,12 +71,6 @@ func AddPhotoLabel(router *gin.RouterGroup, conf *config.Config) {
return 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) PublishPhotoEvent(EntityUpdated, c.Param("uuid"), c)
event.Success("label updated") event.Success("label updated")
@ -137,12 +131,6 @@ func RemovePhotoLabel(router *gin.RouterGroup, conf *config.Config) {
return 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) PublishPhotoEvent(EntityUpdated, c.Param("uuid"), c)
event.Success("label removed") event.Success("label removed")

View file

@ -106,7 +106,15 @@ func SavePhotoForm(model Photo, form form.Photo, geoApi string) error {
model.EditedAt = &edited model.EditedAt = &edited
model.PhotoQuality = model.QualityScore() 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. // Save stored the entity in the database.
@ -130,7 +138,15 @@ func (m *Photo) Save() error {
m.PhotoQuality = m.QualityScore() 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 // ClassifyLabels returns all associated labels as classify.Labels

View file

@ -1,9 +1,11 @@
package query package entity
import "github.com/jinzhu/gorm" import "github.com/jinzhu/gorm"
// UpdatePhotoCounts updates photos count in related tables as needed. // UpdatePhotoCounts updates photos count in related tables as needed.
func UpdatePhotoCounts() error { func UpdatePhotoCounts() error {
log.Info("index: updating photo counts")
if err := Db().Table("places"). if err := Db().Table("places").
UpdateColumn("photo_count", gorm.Expr("(SELECT COUNT(*) FROM photos ph "+ UpdateColumn("photo_count", gorm.Expr("(SELECT COUNT(*) FROM photos ph "+
"WHERE places.id = ph.place_id "+ "WHERE places.id = ph.place_id "+

View file

@ -1,4 +1,4 @@
package query package entity
import ( import (
"testing" "testing"

View file

@ -14,7 +14,6 @@ import (
"github.com/photoprism/photoprism/internal/entity" "github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/event" "github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/internal/mutex" "github.com/photoprism/photoprism/internal/mutex"
"github.com/photoprism/photoprism/internal/query"
"github.com/photoprism/photoprism/pkg/fs" "github.com/photoprism/photoprism/pkg/fs"
"github.com/photoprism/photoprism/pkg/txt" "github.com/photoprism/photoprism/pkg/txt"
) )
@ -196,9 +195,7 @@ func (imp *Import) Start(opt ImportOptions) map[string]bool {
} }
if len(done) > 0 { if len(done) > 0 {
log.Info("import: updating photo counts") if err := entity.UpdatePhotoCounts(); err != nil {
if err := query.UpdatePhotoCounts(); err != nil {
log.Errorf("import: %s", err) log.Errorf("import: %s", err)
} }
} }

View file

@ -11,6 +11,7 @@ import (
"github.com/karrick/godirwalk" "github.com/karrick/godirwalk"
"github.com/photoprism/photoprism/internal/classify" "github.com/photoprism/photoprism/internal/classify"
"github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/event" "github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/internal/mutex" "github.com/photoprism/photoprism/internal/mutex"
"github.com/photoprism/photoprism/internal/nsfw" "github.com/photoprism/photoprism/internal/nsfw"
@ -172,9 +173,7 @@ func (ind *Index) Start(opt IndexOptions) map[string]bool {
} }
if len(done) > 0 { if len(done) > 0 {
log.Info("index: updating photo counts") if err := entity.UpdatePhotoCounts(); err != nil {
if err := query.UpdatePhotoCounts(); err != nil {
log.Errorf("index: %s", err) log.Errorf("index: %s", err)
} }
} }

View file

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/event" "github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/internal/mutex" "github.com/photoprism/photoprism/internal/mutex"
"github.com/photoprism/photoprism/internal/query" "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 return purgedFiles, purgedPhotos, err
} }
log.Info("purge: updating photo counts") if err := entity.UpdatePhotoCounts(); err != nil {
if err := query.UpdatePhotoCounts(); err != nil {
log.Errorf("purge: %s", err) log.Errorf("purge: %s", err)
} }

View file

@ -180,9 +180,7 @@ func (s *Sync) download(a entity.Account) (complete bool, err error) {
} }
if len(done) > 0 { if len(done) > 0 {
log.Info("sync: updating photo counts") if err := entity.UpdatePhotoCounts(); err != nil {
if err := query.UpdatePhotoCounts(); err != nil {
log.Errorf("sync: %s", err) log.Errorf("sync: %s", err)
} }
} }