2020-01-05 14:18:40 +01:00
package query
2019-12-11 07:37:39 +01:00
import (
2020-01-29 16:49:42 +01:00
"github.com/jinzhu/gorm"
2019-12-11 16:55:18 +01:00
"github.com/photoprism/photoprism/internal/entity"
2019-12-11 07:37:39 +01:00
)
2020-03-28 15:29:17 +01:00
// PhotoByID returns a Photo based on the ID.
2020-05-08 15:41:01 +02:00
func PhotoByID ( photoID uint64 ) ( photo entity . Photo , err error ) {
if err := UnscopedDb ( ) . Where ( "id = ?" , photoID ) .
2020-04-16 20:57:00 +02:00
Preload ( "Links" ) .
2020-05-18 22:18:58 +02:00
Preload ( "Camera" ) .
Preload ( "Lens" ) .
Preload ( "Details" ) .
2020-04-16 20:57:00 +02:00
Preload ( "Location" ) .
Preload ( "Location.Place" ) .
Preload ( "Labels" , func ( db * gorm . DB ) * gorm . DB {
2020-04-19 01:13:55 +02:00
return db . Order ( "photos_labels.uncertainty ASC, photos_labels.label_id DESC" )
2020-04-16 20:57:00 +02:00
} ) .
Preload ( "Labels.Label" ) .
First ( & photo ) . Error ; err != nil {
2019-12-11 07:37:39 +01:00
return photo , err
}
return photo , nil
}
2020-03-28 15:29:17 +01:00
// PhotoByUUID returns a Photo based on the UUID.
2020-05-08 15:41:01 +02:00
func PhotoByUUID ( photoUUID string ) ( photo entity . Photo , err error ) {
if err := UnscopedDb ( ) . Where ( "photo_uuid = ?" , photoUUID ) .
2020-04-16 20:57:00 +02:00
Preload ( "Links" ) .
2020-05-18 22:18:58 +02:00
Preload ( "Camera" ) .
Preload ( "Lens" ) .
Preload ( "Details" ) .
2020-04-16 20:57:00 +02:00
Preload ( "Location" ) .
Preload ( "Location.Place" ) .
Preload ( "Labels" , func ( db * gorm . DB ) * gorm . DB {
2020-04-19 01:13:55 +02:00
return db . Order ( "photos_labels.uncertainty ASC, photos_labels.label_id DESC" )
2020-04-16 20:57:00 +02:00
} ) .
Preload ( "Labels.Label" ) .
First ( & photo ) . Error ; err != nil {
2019-12-11 07:37:39 +01:00
return photo , err
}
return photo , nil
}
2019-12-11 19:11:44 +01:00
// PreloadPhotoByUUID returns a Photo based on the UUID with all dependencies preloaded.
2020-05-08 15:41:01 +02:00
func PreloadPhotoByUUID ( photoUUID string ) ( photo entity . Photo , err error ) {
if err := UnscopedDb ( ) . Where ( "photo_uuid = ?" , photoUUID ) .
2020-01-29 16:49:42 +01:00
Preload ( "Labels" , func ( db * gorm . DB ) * gorm . DB {
2020-04-19 01:13:55 +02:00
return db . Order ( "photos_labels.uncertainty ASC, photos_labels.label_id DESC" )
2020-01-29 16:49:42 +01:00
} ) .
2020-01-29 15:28:20 +01:00
Preload ( "Labels.Label" ) .
Preload ( "Camera" ) .
Preload ( "Lens" ) .
2020-04-08 13:24:06 +02:00
Preload ( "Links" ) .
2020-04-16 20:57:00 +02:00
Preload ( "Location" ) .
Preload ( "Location.Place" ) .
2020-05-18 22:18:58 +02:00
Preload ( "Details" ) .
2020-01-29 15:28:20 +01:00
First ( & photo ) . Error ; err != nil {
2019-12-11 19:11:44 +01:00
return photo , err
}
2020-04-30 20:07:03 +02:00
photo . PreloadMany ( )
2019-12-11 19:11:44 +01:00
return photo , nil
}
2020-05-07 19:42:04 +02:00
// MissingPhotos returns photo entities without existing files.
2020-05-08 15:41:01 +02:00
func MissingPhotos ( limit int , offset int ) ( entities [ ] entity . Photo , err error ) {
err = Db ( ) .
2020-05-07 20:33:11 +02:00
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" ) .
Group ( "photos.id" ) .
Limit ( limit ) . Offset ( offset ) . Find ( & entities ) . Error
2020-05-07 19:42:04 +02:00
return entities , err
}
2020-05-08 12:01:22 +02:00
// ResetPhotosQuality resets the quality of photos without primary file to -1.
2020-05-08 15:41:01 +02:00
func ResetPhotosQuality ( ) error {
return Db ( ) . Table ( "photos" ) .
2020-05-08 12:01:22 +02:00
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
}