photoprism/internal/query/photo.go
Michael Mayer 5648c9616e Backend: Refactor location data tables and entities
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-07-12 08:27:05 +02:00

111 lines
3.3 KiB
Go

package query
import (
"time"
"github.com/jinzhu/gorm"
"github.com/photoprism/photoprism/internal/entity"
)
// PhotoByID returns a Photo based on the ID.
func PhotoByID(photoID uint64) (photo entity.Photo, err error) {
if err := UnscopedDb().Where("id = ?", photoID).
Preload("Labels", func(db *gorm.DB) *gorm.DB {
return db.Order("photos_labels.uncertainty ASC, photos_labels.label_id DESC")
}).
Preload("Labels.Label").
Preload("Camera").
Preload("Lens").
Preload("Details").
Preload("Place").
Preload("Cell").
Preload("Cell.Place").
First(&photo).Error; err != nil {
return photo, err
}
return photo, nil
}
// PhotoByUID returns a Photo based on the UID.
func PhotoByUID(photoUID string) (photo entity.Photo, err error) {
if err := UnscopedDb().Where("photo_uid = ?", photoUID).
Preload("Labels", func(db *gorm.DB) *gorm.DB {
return db.Order("photos_labels.uncertainty ASC, photos_labels.label_id DESC")
}).
Preload("Labels.Label").
Preload("Camera").
Preload("Lens").
Preload("Details").
Preload("Place").
Preload("Cell").
Preload("Cell.Place").
First(&photo).Error; err != nil {
return photo, err
}
return photo, nil
}
// PhotoPreloadByUID returns a Photo based on the UID with all dependencies preloaded.
func PhotoPreloadByUID(photoUID string) (photo entity.Photo, err error) {
if err := UnscopedDb().Where("photo_uid = ?", photoUID).
Preload("Labels", func(db *gorm.DB) *gorm.DB {
return db.Order("photos_labels.uncertainty ASC, photos_labels.label_id DESC")
}).
Preload("Labels.Label").
Preload("Camera").
Preload("Lens").
Preload("Details").
Preload("Place").
Preload("Cell").
Preload("Cell.Place").
First(&photo).Error; err != nil {
return photo, err
}
photo.PreloadMany()
return photo, nil
}
// PhotosMissing returns photo entities without existing files.
func PhotosMissing(limit int, offset int) (entities entity.Photos, err error) {
err = Db().
Select("photos.*").
Joins("JOIN files a ON photos.id = a.photo_id ").
Joins("LEFT JOIN files b ON a.photo_id = b.photo_id AND a.id != b.id AND b.file_missing = 0").
Where("a.file_missing = 1 AND b.id IS NULL").
Where("photos.photo_type <> ?", entity.TypeText).
Group("photos.id").
Limit(limit).Offset(offset).Find(&entities).Error
return entities, err
}
// ResetPhotoQuality resets the quality of photos without primary file to -1.
func ResetPhotoQuality() error {
return Db().Table("photos").
Where("id IN (SELECT photos.id FROM photos LEFT JOIN files ON photos.id = files.photo_id AND files.file_primary = 1 WHERE files.id IS NULL GROUP BY photos.id)").
Update("photo_quality", -1).Error
}
// PhotosCheck returns photos selected for maintenance.
func PhotosCheck(limit int, offset int) (entities entity.Photos, err error) {
err = Db().
Preload("Labels", func(db *gorm.DB) *gorm.DB {
return db.Order("photos_labels.uncertainty ASC, photos_labels.label_id DESC")
}).
Preload("Labels.Label").
Preload("Camera").
Preload("Lens").
Preload("Details").
Preload("Place").
Preload("Cell").
Preload("Cell.Place").
Where("checked_at IS NULL OR checked_at < ?", time.Now().Add(-1*time.Hour*24*3)).
Where("updated_at < ?", time.Now().Add(-1*time.Minute*10)).
Limit(limit).Offset(offset).Find(&entities).Error
return entities, err
}