2020-02-04 05:18:22 +01:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PhotoSelection returns all selected photos.
|
2020-03-28 17:17:41 +01:00
|
|
|
func (q *Query) PhotoSelection(f form.Selection) (results []entity.Photo, err error) {
|
2020-02-04 05:18:22 +01:00
|
|
|
if f.Empty() {
|
|
|
|
return results, errors.New("no photos selected")
|
|
|
|
}
|
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
s := q.db.NewScope(nil).DB()
|
2020-02-04 05:18:22 +01:00
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Table("photos").
|
2020-02-04 05:18:22 +01:00
|
|
|
Select("photos.*").
|
|
|
|
Joins("LEFT JOIN photos_labels ON photos_labels.photo_id = photos.id").
|
|
|
|
Joins("LEFT JOIN labels ON photos_labels.label_id = labels.id AND labels.deleted_at IS NULL").
|
|
|
|
Where("photos.deleted_at IS NULL").
|
|
|
|
Group("photos.id")
|
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Where("photos.photo_uuid IN (?) OR labels.label_uuid IN (?)", f.Photos, f.Labels)
|
2020-02-04 05:18:22 +01:00
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
if result := s.Scan(&results); result.Error != nil {
|
2020-02-04 05:18:22 +01:00
|
|
|
return results, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|