Backend: Move UpdatePhotoCounts() to entity package
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
parent
71c849c23b
commit
3c47a85ea5
10 changed files with 31 additions and 38 deletions
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 "+
|
|
@ -1,4 +1,4 @@
|
|||
package query
|
||||
package entity
|
||||
|
||||
import (
|
||||
"testing"
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue