From fdd658687b31c3b2e6eb7931dd5116d795230ed1 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Thu, 18 Nov 2021 12:54:26 +0100 Subject: [PATCH] Folders: Include Path in Search #1737 --- internal/search/albums.go | 41 +++++++++++++++++++++++++------ internal/search/albums_results.go | 1 + 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/internal/search/albums.go b/internal/search/albums.go index 3bb3aa7e6..dd1ac9f01 100644 --- a/internal/search/albums.go +++ b/internal/search/albums.go @@ -14,6 +14,9 @@ func Albums(f form.AlbumSearch) (results AlbumResults, err error) { return results, err } + // Clip and normalize search query. + f.Query = txt.NormalizeQuery(f.Query) + // Base query. s := UnscopedDb().Table("albums"). Select("albums.*, cp.photo_count, cl.link_count, CASE WHEN albums.album_year = 0 THEN 0 ELSE 1 END AS has_year"). @@ -29,6 +32,35 @@ func Albums(f form.AlbumSearch) (results AlbumResults, err error) { s = s.Limit(MaxResults).Offset(f.Offset) } + // Filter by storage path? + if f.Query != "" && f.Type == entity.AlbumFolder { + f.Order = entity.SortOrderPath + + p := f.Query + + if strings.HasPrefix(p, "/") { + p = p[1:] + } + + if strings.HasSuffix(p, "/") { + s = s.Where("albums.album_path = ?", p[:len(p)-1]) + } else { + p = p + "*" + + where, values := OrLike("albums.album_path", p) + + if w, v := OrLike("albums.album_title", p); len(v) > 0 { + where = where + " OR " + w + values = append(values, v...) + } + + s = s.Where(where, values...) + } + } else if f.Query != "" { + likeString := "%" + f.Query + "%" + s = s.Where("albums.album_title LIKE ? OR albums.album_location LIKE ?", likeString, likeString) + } + // Set sort order. switch f.Order { case entity.SortOrderCount: @@ -44,11 +76,11 @@ func Albums(f form.AlbumSearch) (results AlbumResults, err error) { case entity.SortOrderMoment: s = s.Order("albums.album_favorite DESC, has_year, albums.album_year DESC, albums.album_month DESC, albums.album_title ASC, albums.album_uid DESC") case entity.SortOrderPlace: - s = s.Order("albums.album_favorite DESC, albums.album_country, albums.album_title, albums.album_year DESC, albums.album_month ASC, albums.album_day ASC, albums.album_uid DESC") + s = s.Order("albums.album_favorite DESC, albums.album_country, albums.album_state, albums.album_title, albums.album_year DESC, albums.album_month ASC, albums.album_day ASC, albums.album_uid DESC") case entity.SortOrderName: s = s.Order("albums.album_title ASC, albums.album_uid DESC") case entity.SortOrderPath: - s = s.Order("albums.album_favorite DESC, albums.album_path DESC, albums.album_uid DESC") + s = s.Order("albums.album_path, albums.album_uid DESC") case entity.SortOrderCategory: s = s.Order("albums.album_category, albums.album_title, albums.album_uid DESC") case entity.SortOrderSlug: @@ -67,11 +99,6 @@ func Albums(f form.AlbumSearch) (results AlbumResults, err error) { return results, nil } - if f.Query != "" { - likeString := "%" + f.Query + "%" - s = s.Where("albums.album_title LIKE ? OR albums.album_location LIKE ?", likeString, likeString) - } - if f.Type != "" { s = s.Where("albums.album_type IN (?)", strings.Split(f.Type, txt.Or)) } diff --git a/internal/search/albums_results.go b/internal/search/albums_results.go index 318a025da..a8b2e7e59 100644 --- a/internal/search/albums_results.go +++ b/internal/search/albums_results.go @@ -23,6 +23,7 @@ type Album struct { AlbumOrder string `json:"Order"` AlbumTemplate string `json:"Template"` AlbumPath string `json:"Path"` + AlbumState string `json:"State"` AlbumCountry string `json:"Country"` AlbumYear int `json:"Year"` AlbumMonth int `json:"Month"`