2020-05-10 19:43:49 +02:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PhotoLabel returns a photo label entity if exists.
|
|
|
|
func PhotoLabel(photoID, labelID uint) (label entity.PhotoLabel, err error) {
|
|
|
|
if err := Db().Where("photo_id = ? AND label_id = ?", photoID, labelID).Preload("Photo").Preload("Label").First(&label).Error; err != nil {
|
|
|
|
return label, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return label, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LabelBySlug returns a Label based on the slug name.
|
|
|
|
func LabelBySlug(labelSlug string) (label entity.Label, err error) {
|
2020-06-22 15:16:26 +02:00
|
|
|
if err := Db().Where("label_slug = ? OR custom_slug = ?", labelSlug, labelSlug).First(&label).Error; err != nil {
|
2020-05-10 19:43:49 +02:00
|
|
|
return label, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return label, nil
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
// LabelByUID returns a Label based on the label UID.
|
|
|
|
func LabelByUID(labelUID string) (label entity.Label, err error) {
|
2020-06-22 15:16:26 +02:00
|
|
|
if err := Db().Where("label_uid = ?", labelUID).First(&label).Error; err != nil {
|
2020-05-10 19:43:49 +02:00
|
|
|
return label, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return label, nil
|
|
|
|
}
|
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
// LabelThumbBySlug returns a label cover file based on the slug name.
|
2020-05-10 19:43:49 +02:00
|
|
|
func LabelThumbBySlug(labelSlug string) (file entity.File, err error) {
|
2020-05-25 19:10:44 +02:00
|
|
|
if err := Db().Where("files.file_primary AND files.file_type = 'jpg' AND files.deleted_at IS NULL").
|
2020-05-10 19:43:49 +02:00
|
|
|
Joins("JOIN labels ON labels.label_slug = ?", labelSlug).
|
|
|
|
Joins("JOIN photos_labels ON photos_labels.label_id = labels.id AND photos_labels.photo_id = files.photo_id AND photos_labels.uncertainty < 100").
|
|
|
|
Joins("JOIN photos ON photos.id = files.photo_id AND photos.photo_private = 0 AND photos.deleted_at IS NULL").
|
|
|
|
Order("photos.photo_quality DESC, photos_labels.uncertainty ASC").
|
|
|
|
First(&file).Error; err != nil {
|
|
|
|
return file, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
// LabelThumbByUID returns a label cover file based on the label UID.
|
2020-05-23 20:58:58 +02:00
|
|
|
func LabelThumbByUID(labelUID string) (file entity.File, err error) {
|
2020-05-10 19:43:49 +02:00
|
|
|
// Search matching label
|
|
|
|
err = Db().Where("files.file_primary AND files.deleted_at IS NULL").
|
2020-05-23 20:58:58 +02:00
|
|
|
Joins("JOIN labels ON labels.label_uid = ?", labelUID).
|
2020-05-10 19:43:49 +02:00
|
|
|
Joins("JOIN photos_labels ON photos_labels.label_id = labels.id AND photos_labels.photo_id = files.photo_id AND photos_labels.uncertainty < 100").
|
|
|
|
Joins("JOIN photos ON photos.id = files.photo_id AND photos.photo_private = 0 AND photos.deleted_at IS NULL").
|
|
|
|
Order("photos.photo_quality DESC, photos_labels.uncertainty ASC").
|
|
|
|
First(&file).Error
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If failed, search for category instead
|
|
|
|
err = Db().Where("files.file_primary AND files.deleted_at IS NULL").
|
|
|
|
Joins("JOIN photos_labels ON photos_labels.photo_id = files.photo_id AND photos_labels.uncertainty < 100").
|
|
|
|
Joins("JOIN categories c ON photos_labels.label_id = c.label_id").
|
2020-05-23 20:58:58 +02:00
|
|
|
Joins("JOIN labels ON c.category_id = labels.id AND labels.label_uid= ?", labelUID).
|
2020-05-10 19:43:49 +02:00
|
|
|
Joins("JOIN photos ON photos.id = files.photo_id AND photos.photo_private = 0 AND photos.deleted_at IS NULL").
|
|
|
|
Order("photos.photo_quality DESC, photos_labels.uncertainty ASC").
|
|
|
|
First(&file).Error
|
|
|
|
|
|
|
|
return file, err
|
|
|
|
}
|