diff options
| -rw-r--r-- | internal/gallery/query_filter.go | 52 | ||||
| -rw-r--r-- | internal/gallery/query_photos_test.go | 48 |
2 files changed, 87 insertions, 13 deletions
diff --git a/internal/gallery/query_filter.go b/internal/gallery/query_filter.go index dda73c4..14d9d59 100644 --- a/internal/gallery/query_filter.go +++ b/internal/gallery/query_filter.go @@ -9,22 +9,54 @@ func filterQueryPhotos( photos []Photo, query Query, ) []Photo { - if query.Extension == "" { - return photos - } - filtered := make([]Photo, 0, len(photos)) for _, photo := range photos { - ext := strings.TrimPrefix(filepath.Ext(photo.RelativePath), ".") + if !matchesExtension(photo, query.Extension) { + continue + } - if strings.EqualFold( - strings.TrimPrefix(ext, "."), - strings.TrimPrefix(query.Extension, "."), - ) { - filtered = append(filtered, photo) + if !matchesSearch(photo, query.Search) { + continue } + + filtered = append(filtered, photo) } return filtered } + +func matchesExtension( + photo Photo, + extension string, +) bool { + if extension == "" { + return true + } + + ext := strings.TrimPrefix( + filepath.Ext(photo.RelativePath), + ".", + ) + + extension = strings.TrimPrefix( + extension, + ".", + ) + + return strings.EqualFold(ext, extension) +} + +func matchesSearch( + photo Photo, + search string, +) bool { + if search == "" { + return true + } + + return strings.Contains( + strings.ToLower(photo.RelativePath), + strings.ToLower(search), + ) +} diff --git a/internal/gallery/query_photos_test.go b/internal/gallery/query_photos_test.go index b60cfea..c5d9376 100644 --- a/internal/gallery/query_photos_test.go +++ b/internal/gallery/query_photos_test.go @@ -312,12 +312,54 @@ func TestQueryPhotosFilterThenSortThenPaginate(t *testing.T) { assertIDs(t, got, "3") } -func TestQueryPhotosIgnoreSearchForNow(t *testing.T) { - photos := makePhotos(3) +func TestQueryPhotosSearchByRelativePath(t *testing.T) { + photos := []Photo{ + { + ID: "vacation", + File: scanner.File{ + RelativePath: "photos/vacation/beach.jpg", + }, + }, + { + ID: "work", + File: scanner.File{ + RelativePath: "photos/work/meeting.jpg", + }, + }, + { + ID: "vacation2", + File: scanner.File{ + RelativePath: "photos/vacation/mountains.jpg", + }, + }, + } got := QueryPhotos(photos, Query{ Search: "vacation", }) - assertIDs(t, got, "0", "1", "2") + assertIDs(t, got, "vacation", "vacation2") +} + +func TestQueryPhotosSearchIsCaseInsensitive(t *testing.T) { + photos := []Photo{ + { + ID: "1", + File: scanner.File{ + RelativePath: "Photos/Vacation/Beach.jpg", + }, + }, + { + ID: "2", + File: scanner.File{ + RelativePath: "photos/work/meeting.jpg", + }, + }, + } + + got := QueryPhotos(photos, Query{ + Search: "VACATION", + }) + + assertIDs(t, got, "1") } |