2020-01-05 14:18:40 +01:00
|
|
|
package query
|
2019-12-11 07:37:39 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-12-11 16:55:18 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2019-12-11 07:37:39 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2020-01-13 11:07:09 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/capture"
|
2019-12-11 07:37:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// AlbumResult contains found albums
|
|
|
|
type AlbumResult struct {
|
|
|
|
ID uint
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
DeletedAt time.Time
|
|
|
|
AlbumUUID string
|
|
|
|
AlbumSlug string
|
|
|
|
AlbumName string
|
|
|
|
AlbumCount int
|
|
|
|
AlbumFavorite bool
|
|
|
|
AlbumDescription string
|
|
|
|
AlbumNotes string
|
|
|
|
}
|
|
|
|
|
2020-03-28 15:29:17 +01:00
|
|
|
// AlbumByUUID returns a Album based on the UUID.
|
2020-03-28 17:17:41 +01:00
|
|
|
func (q *Query) AlbumByUUID(albumUUID string) (album entity.Album, err error) {
|
|
|
|
if err := q.db.Where("album_uuid = ?", albumUUID).First(&album).Error; err != nil {
|
2019-12-11 07:37:39 +01:00
|
|
|
return album, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return album, nil
|
|
|
|
}
|
|
|
|
|
2020-03-28 15:29:17 +01:00
|
|
|
// AlbumThumbByUUID returns a album preview file based on the uuid.
|
2020-03-28 17:17:41 +01:00
|
|
|
func (q *Query) AlbumThumbByUUID(albumUUID string) (file entity.File, err error) {
|
|
|
|
// q.db.LogMode(true)
|
2019-12-11 07:37:39 +01:00
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
if err := q.db.Where("files.file_primary AND files.deleted_at IS NULL").
|
2019-12-11 07:37:39 +01:00
|
|
|
Joins("JOIN albums ON albums.album_uuid = ?", albumUUID).
|
|
|
|
Joins("JOIN photos_albums pa ON pa.album_uuid = albums.album_uuid AND pa.photo_uuid = files.photo_uuid").
|
|
|
|
First(&file).Error; err != nil {
|
|
|
|
return file, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Albums searches albums based on their name.
|
2020-03-28 17:17:41 +01:00
|
|
|
func (q *Query) Albums(f form.AlbumSearch) (results []AlbumResult, err error) {
|
2019-12-11 07:37:39 +01:00
|
|
|
if err := f.ParseQueryString(); err != nil {
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
|
2020-01-30 18:19:26 +01:00
|
|
|
defer log.Debug(capture.Time(time.Now(), fmt.Sprintf("albums: %+v", f)))
|
2019-12-11 07:37:39 +01:00
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
s := q.db.NewScope(nil).DB()
|
2019-12-11 07:37:39 +01:00
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Table("albums").
|
2019-12-11 07:37:39 +01:00
|
|
|
Select(`albums.*, COUNT(photos_albums.album_uuid) AS album_count`).
|
|
|
|
Joins("LEFT JOIN photos_albums ON photos_albums.album_uuid = albums.album_uuid").
|
|
|
|
Where("albums.deleted_at IS NULL").
|
|
|
|
Group("albums.id")
|
|
|
|
|
2020-01-30 18:19:26 +01:00
|
|
|
if f.ID != "" {
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Where("albums.album_uuid = ?", f.ID)
|
2020-01-30 18:19:26 +01:00
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
if result := s.Scan(&results); result.Error != nil {
|
2020-01-30 18:19:26 +01:00
|
|
|
return results, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:37:39 +01:00
|
|
|
if f.Query != "" {
|
|
|
|
likeString := "%" + strings.ToLower(f.Query) + "%"
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Where("LOWER(albums.album_name) LIKE ?", likeString)
|
2019-12-11 07:37:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if f.Favorites {
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Where("albums.album_favorite = 1")
|
2019-12-11 07:37:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch f.Order {
|
|
|
|
case "slug":
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Order("albums.album_favorite DESC, album_slug ASC")
|
2019-12-11 07:37:39 +01:00
|
|
|
default:
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Order("albums.album_favorite DESC, album_count DESC, albums.created_at DESC")
|
2019-12-11 07:37:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if f.Count > 0 && f.Count <= 1000 {
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Limit(f.Count).Offset(f.Offset)
|
2019-12-11 07:37:39 +01:00
|
|
|
} else {
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Limit(100).Offset(0)
|
2019-12-11 07:37:39 +01:00
|
|
|
}
|
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
if result := s.Scan(&results); result.Error != nil {
|
2019-12-11 07:37:39 +01:00
|
|
|
return results, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|