diff options
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/api/query.go | 3 | ||||
| -rw-r--r-- | internal/api/query_test.go | 39 | ||||
| -rw-r--r-- | internal/gallery/query.go | 3 | ||||
| -rw-r--r-- | internal/gallery/query_filter.go | 62 | ||||
| -rw-r--r-- | internal/gallery/query_photos.go | 2 | ||||
| -rw-r--r-- | internal/gallery/query_photos_test.go | 159 |
6 files changed, 268 insertions, 0 deletions
diff --git a/internal/api/query.go b/internal/api/query.go index 3468163..d5428f0 100644 --- a/internal/api/query.go +++ b/internal/api/query.go @@ -62,5 +62,8 @@ func parseQuery(r *http.Request) (gallery.Query, error) { } } + query.Extension = values.Get("extension") + query.Search = values.Get("q") + return query, nil } diff --git a/internal/api/query_test.go b/internal/api/query_test.go index cbfe89a..a1effe7 100644 --- a/internal/api/query_test.go +++ b/internal/api/query_test.go @@ -1,6 +1,7 @@ package api import ( + "net/http" "net/http/httptest" "testing" @@ -209,3 +210,41 @@ func TestParseQueryOrderWithoutSort(t *testing.T) { ) } } + +func TestParseQueryExtension(t *testing.T) { + req := httptest.NewRequest( + http.MethodGet, + "/api/photos?extension=jpg", + nil, + ) + + got, err := parseQuery(req) + if err != nil { + t.Fatalf("parseQuery() error = %v", err) + } + + if got.Extension != "jpg" { + t.Fatalf("expected extension %q, got %q", "jpg", got.Extension) + } +} + +func TestParseQuerySearch(t *testing.T) { + req := httptest.NewRequest( + "http.MethodGet", + "/api/photos?q=vacation", + nil, + ) + + got, err := parseQuery(req) + if err != nil { + t.Fatalf("parseQuery() error = %v", err) + } + + if got.Search != "vacation" { + t.Fatalf( + "expected search %q, got %q", + "vacation", + got.Search, + ) + } +} diff --git a/internal/gallery/query.go b/internal/gallery/query.go index e2003d5..346c25f 100644 --- a/internal/gallery/query.go +++ b/internal/gallery/query.go @@ -24,4 +24,7 @@ type Query struct { Sort SortField Order SortOrder + + Extension string + Search string } diff --git a/internal/gallery/query_filter.go b/internal/gallery/query_filter.go new file mode 100644 index 0000000..14d9d59 --- /dev/null +++ b/internal/gallery/query_filter.go @@ -0,0 +1,62 @@ +package gallery + +import ( + "path/filepath" + "strings" +) + +func filterQueryPhotos( + photos []Photo, + query Query, +) []Photo { + filtered := make([]Photo, 0, len(photos)) + + for _, photo := range photos { + if !matchesExtension(photo, query.Extension) { + continue + } + + 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.go b/internal/gallery/query_photos.go index e024090..7b25250 100644 --- a/internal/gallery/query_photos.go +++ b/internal/gallery/query_photos.go @@ -4,6 +4,8 @@ func QueryPhotos( photos []Photo, query Query, ) []Photo { + photos = filterQueryPhotos(photos, query) + if query.Sort != "" { photos = append([]Photo(nil), photos...) diff --git a/internal/gallery/query_photos_test.go b/internal/gallery/query_photos_test.go index c9bb986..f38c237 100644 --- a/internal/gallery/query_photos_test.go +++ b/internal/gallery/query_photos_test.go @@ -250,3 +250,162 @@ func TestQueryPhotosSortByDateDescending(t *testing.T) { assertIDs(t, got, "new", "mid", "old") } + +func TestQueryPhotosFilterByExtension(t *testing.T) { + photos := []Photo{ + { + ID: "jpg", + File: scanner.File{ + RelativePath: "photo.jpg", + }, + }, + { + ID: "png", + File: scanner.File{ + RelativePath: "photo.png", + }, + }, + { + ID: "jpg2", + File: scanner.File{ + RelativePath: "anotherphoto.jpg", + }, + }, + } + + got := QueryPhotos(photos, Query{ + Extension: "jpg", + }) + + assertIDs(t, got, "jpg", "jpg2") +} + +func TestQueryPhotosFilterThenSortThenPaginate(t *testing.T) { + photos := []Photo{ + { + ID: "1", + File: scanner.File{ + RelativePath: "a.png", + }, + }, + { + ID: "2", + File: scanner.File{ + RelativePath: "c.jpg", + }, + }, + { + ID: "3", + File: scanner.File{ + RelativePath: "a.jpg", + }, + }, + } + + got := QueryPhotos(photos, Query{ + Extension: "jpg", + Sort: SortByPath, + Order: SortAsc, + Limit: 1, + }) + + assertIDs(t, got, "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, "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") +} + +func TestQueryPhotosFilterSearchSortThenPaginate(t *testing.T) { + photos := []Photo{ + { + ID: "1", + File: scanner.File{ + RelativePath: "photos/work/report.jpg", + }, + }, + { + ID: "2", + File: scanner.File{ + RelativePath: "photos/vacation/zebra.jpg", + }, + }, + { + ID: "3", + File: scanner.File{ + RelativePath: "photos/vacation/apple.jpg", + }, + }, + { + ID: "4", + File: scanner.File{ + RelativePath: "photos/vacation/beach.png", + }, + }, + { + ID: "5", + File: scanner.File{ + RelativePath: "photos/vacation/mountains.jpg", + }, + }, + } + + got := QueryPhotos(photos, Query{ + Extension: "jpg", + Search: "vacation", + Sort: SortByPath, + Order: SortAsc, + Offset: 1, + Limit: 1, + }) + + assertIDs(t, got, "5") +} |