From 9237991fa79f9b90a3f14f8b3c42dab25d029f05 Mon Sep 17 00:00:00 2001 From: RanXom Date: Fri, 17 Jul 2026 16:42:21 +0530 Subject: feat(gallery): introduce gallery query --- internal/gallery/query.go | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 internal/gallery/query.go (limited to 'internal/gallery') diff --git a/internal/gallery/query.go b/internal/gallery/query.go new file mode 100644 index 0000000..fd11762 --- /dev/null +++ b/internal/gallery/query.go @@ -0,0 +1,9 @@ +package gallery + +// Query describes how photos should be selected. +// +// The zero value returns the entire gallery. +type Query struct { + Limit int + Offset int +} -- cgit v1.2.3 From f056150175e9cfdeab8c34bb52ff298107d9ecc3 Mon Sep 17 00:00:00 2001 From: RanXom Date: Fri, 17 Jul 2026 18:28:24 +0530 Subject: feat(gallery): support paginated photo queries --- internal/gallery/query_photos.go | 18 +++++ internal/gallery/query_photos_test.go | 125 ++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 internal/gallery/query_photos.go create mode 100644 internal/gallery/query_photos_test.go (limited to 'internal/gallery') diff --git a/internal/gallery/query_photos.go b/internal/gallery/query_photos.go new file mode 100644 index 0000000..12fa877 --- /dev/null +++ b/internal/gallery/query_photos.go @@ -0,0 +1,18 @@ +package gallery + +func QueryPhotos( + photos []Photo, + query Query, +) []Photo { + if query.Offset >= len(photos) { + return []Photo{} + } + + photos = photos[query.Offset:] + + if query.Limit == 0 || query.Limit >= len(photos) { + return photos + } + + return photos[:query.Limit] +} diff --git a/internal/gallery/query_photos_test.go b/internal/gallery/query_photos_test.go new file mode 100644 index 0000000..45e7149 --- /dev/null +++ b/internal/gallery/query_photos_test.go @@ -0,0 +1,125 @@ +package gallery + +import ( + "strconv" + "testing" +) + +func makePhotos(n int) []Photo { + photos := make([]Photo, n) + + for i := range n { + photos[i] = Photo{ + ID: strconv.Itoa(i), + } + } + + return photos +} + +func assertIDs( + t *testing.T, + photos []Photo, + want ...string, +) { + t.Helper() + + if len(photos) != len(want) { + t.Fatalf( + "expected %d photos, got %d", + len(want), + len(photos), + ) + } + + for i := range want { + if photos[i].ID != want[i] { + t.Fatalf( + "photo %d: expected %q, got %q", + i, + want[i], + photos[i].ID, + ) + } + } +} + +func TestQueryPhotos(t *testing.T) { + photos := makePhotos(10) + + t.Run("default", func(t *testing.T) { + got := QueryPhotos(photos, Query{}) + + assertIDs( + t, + got, + "0", "1", "2", "3", "4", + "5", "6", "7", "8", "9", + ) + }) + + t.Run("limit", func(t *testing.T) { + got := QueryPhotos( + photos, + Query{ + Limit: 3, + }, + ) + + assertIDs(t, got, "0", "1", "2") + }) + + t.Run("offset", func(t *testing.T) { + got := QueryPhotos( + photos, + Query{ + Offset: 5, + }, + ) + + assertIDs( + t, + got, + "5", "6", "7", "8", "9", + ) + }) + + t.Run("offset and limit", func(t *testing.T) { + got := QueryPhotos( + photos, + Query{ + Offset: 5, + Limit: 2, + }, + ) + + assertIDs(t, got, "5", "6") + }) + + t.Run("offset beyond end", func(t *testing.T) { + got := QueryPhotos( + photos, + Query{ + Offset: 100, + }, + ) + + assertIDs(t, got) + }) + + t.Run("limit larger than gallery", func(t *testing.T) { + got := QueryPhotos( + photos, + Query{ + Limit: 100, + }, + ) + + assertIDs( + t, + got, + "0", "1", "2", "3", "4", + "5", "6", "7", "8", "9", + ) + }) +} -- cgit v1.2.3 From 41ad7da084544b3b8eea5b009cc3cab652988b8d Mon Sep 17 00:00:00 2001 From: RanXom Date: Fri, 17 Jul 2026 22:05:17 +0530 Subject: feat(gallery): add sorting options to queries --- internal/gallery/query.go | 18 ++++++++++++++++++ internal/gallery/query_photos_test.go | 17 +++++++++++++++++ 2 files changed, 35 insertions(+) (limited to 'internal/gallery') diff --git a/internal/gallery/query.go b/internal/gallery/query.go index fd11762..f528906 100644 --- a/internal/gallery/query.go +++ b/internal/gallery/query.go @@ -1,9 +1,27 @@ package gallery +type SortField string + +const ( + SortByDateTime SortField = "dateTaken" + SortByPath SortField = "path" +) + +// SortOrder identifies the sort direction +type SortOrder string + +const ( + SortAsc SortOrder = "asc" + SortDesc SortOrder = "desc" +) + // Query describes how photos should be selected. // // The zero value returns the entire gallery. type Query struct { Limit int Offset int + + Sort SortField + Order SortOrder } diff --git a/internal/gallery/query_photos_test.go b/internal/gallery/query_photos_test.go index 45e7149..d5a25d0 100644 --- a/internal/gallery/query_photos_test.go +++ b/internal/gallery/query_photos_test.go @@ -122,4 +122,21 @@ func TestQueryPhotos(t *testing.T) { "5", "6", "7", "8", "9", ) }) + + t.Run("ignores sorting for now", func(t *testing.T) { + got := QueryPhotos( + photos, + Query{ + Sort: SortByPath, + Order: SortAsc, + }, + ) + + assertIDs( + t, + got, + "0", "1", "2", "3", "4", + "5", "6", "7", "8", "9", + ) + }) } -- cgit v1.2.3 From eb6a8c0306c44d5317c8d321ae24537bac9434ed Mon Sep 17 00:00:00 2001 From: RanXom Date: Sat, 18 Jul 2026 22:40:52 +0530 Subject: feat(gallery): support sorted photo queries --- internal/gallery/query_photos.go | 6 +++++ internal/gallery/query_photos_test.go | 48 +++++++++++++++++++++++++++++++++++ internal/gallery/query_sort.go | 40 +++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 internal/gallery/query_sort.go (limited to 'internal/gallery') diff --git a/internal/gallery/query_photos.go b/internal/gallery/query_photos.go index 12fa877..e024090 100644 --- a/internal/gallery/query_photos.go +++ b/internal/gallery/query_photos.go @@ -4,6 +4,12 @@ func QueryPhotos( photos []Photo, query Query, ) []Photo { + if query.Sort != "" { + photos = append([]Photo(nil), photos...) + + sortQueryPhotos(photos, query) + } + if query.Offset >= len(photos) { return []Photo{} } diff --git a/internal/gallery/query_photos_test.go b/internal/gallery/query_photos_test.go index d5a25d0..d2700e5 100644 --- a/internal/gallery/query_photos_test.go +++ b/internal/gallery/query_photos_test.go @@ -3,6 +3,8 @@ package gallery import ( "strconv" "testing" + + "github.com/RanXom/galleryd/internal/scanner" ) func makePhotos(n int) []Photo { @@ -140,3 +142,49 @@ func TestQueryPhotos(t *testing.T) { ) }) } + +func TestQueryPhotosSortByPathAscending(t *testing.T) { + photos := []Photo{ + {ID: "1", File: scanner.File{RelativePath: "c.jpg"}}, + {ID: "2", File: scanner.File{RelativePath: "a.jpg"}}, + {ID: "3", File: scanner.File{RelativePath: "b.jpg"}}, + } + + got := QueryPhotos(photos, Query{ + Sort: SortByPath, + Order: SortAsc, + }) + + assertIDs(t, got, "2", "3", "1") +} + +func TestQueryPhotosSortByPathDescending(t *testing.T) { + photos := []Photo{ + {ID: "1", File: scanner.File{RelativePath: "a.jpg"}}, + {ID: "2", File: scanner.File{RelativePath: "b.jpg"}}, + {ID: "3", File: scanner.File{RelativePath: "c.jpg"}}, + } + + got := QueryPhotos(photos, Query{ + Sort: SortByPath, + Order: SortDesc, + }) + + assertIDs(t, got, "3", "2", "1") +} + +func TestQueryPhotosSortThenPaginate(t *testing.T) { + photos := []Photo{ + {ID: "1", File: scanner.File{RelativePath: "c.jpg"}}, + {ID: "2", File: scanner.File{RelativePath: "a.jpg"}}, + {ID: "3", File: scanner.File{RelativePath: "b.jpg"}}, + } + + got := QueryPhotos(photos, Query{ + Sort: SortByPath, + Order: SortAsc, + Limit: 1, + }) + + assertIDs(t, got, "2") +} diff --git a/internal/gallery/query_sort.go b/internal/gallery/query_sort.go new file mode 100644 index 0000000..7ad811b --- /dev/null +++ b/internal/gallery/query_sort.go @@ -0,0 +1,40 @@ +package gallery + +import "sort" + +func sortQueryPhotos( + photos []Photo, + query Query, +) { + if query.Sort == "" { + return + } + + sort.SliceStable(photos, func(i, j int) bool { + switch query.Sort { + case SortByPath: + if query.Order == SortDesc { + return photos[i].RelativePath > photos[j].RelativePath + } + + return photos[i].RelativePath < photos[j].RelativePath + case SortByDateTime: + if query.Order == SortAsc { + if !photos[i].DateTaken.Equal(photos[j].DateTaken) { + return photos[i].DateTaken.Before(photos[j].DateTaken) + } + + return photos[i].RelativePath < photos[j].RelativePath + } + + if !photos[i].DateTaken.Equal(photos[j].DateTaken) { + return photos[i].DateTaken.After(photos[j].DateTaken) + } + + return photos[i].RelativePath < photos[j].RelativePath + + default: + return false + } + }) +} -- cgit v1.2.3 From 3a5d1edb2735cfadfbcc7ca0ed79e12ad9fbf7b9 Mon Sep 17 00:00:00 2001 From: RanXom Date: Wed, 22 Jul 2026 17:26:11 +0530 Subject: test(gallery): cover date sorting --- internal/gallery/query_photos_test.go | 62 +++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'internal/gallery') diff --git a/internal/gallery/query_photos_test.go b/internal/gallery/query_photos_test.go index d2700e5..9b190da 100644 --- a/internal/gallery/query_photos_test.go +++ b/internal/gallery/query_photos_test.go @@ -3,7 +3,9 @@ package gallery import ( "strconv" "testing" + "time" + "github.com/RanXom/galleryd/internal/metadata" "github.com/RanXom/galleryd/internal/scanner" ) @@ -188,3 +190,63 @@ func TestQueryPhotosSortThenPaginate(t *testing.T) { assertIDs(t, got, "2") } + +func TestQueryPhotosSortByDateAscending(t *testing.T) { + photos := []Photo{ + { + ID: "new", + Metadata: metadata.Metadata{ + DateTaken: time.Date(2025, 1, 3, 0, 0, 0, 0, time.UTC), + }, + }, + { + ID: "old", + Metadata: metadata.Metadata{ + DateTaken: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC), + }, + }, + { + ID: "mid", + Metadata: metadata.Metadata{ + DateTaken: time.Date(2025, 1, 2, 0, 0, 0, 0, time.UTC), + }, + }, + } + + got := QueryPhotos(photos, Query{ + Sort: SortByDateTime, + Order: SortAsc, + }) + + assertIDs(t, got, "old", "mid", "new") +} + +func TestQueryPhotosSortByDateDescending(t *testing.T) { + photos := []Photo{ + { + ID: "old", + Metadata: metadata.Metadata{ + DateTaken: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC), + }, + }, + { + ID: "mid", + Metadata: metadata.Metadata{ + DateTaken: time.Date(2025, 1, 2, 0, 0, 0, 0, time.UTC), + }, + }, + { + ID: "new", + Metadata: metadata.Metadata{ + DateTaken: time.Date(2025, 1, 3, 0, 0, 0, 0, time.UTC), + }, + }, + } + + got := QueryPhotos(photos, Query{ + Sort: SortByDateTime, + Order: SortDesc, + }) + + assertIDs(t, got, "new", "mid", "old") +} -- cgit v1.2.3 From ddd8a37a0eb5623960e4528802263c7cec23af87 Mon Sep 17 00:00:00 2001 From: RanXom Date: Wed, 22 Jul 2026 17:31:35 +0530 Subject: refactor(gallery/api): rename SortByDateTime to SortByDateTaken --- internal/gallery/query.go | 4 ++-- internal/gallery/query_photos_test.go | 4 ++-- internal/gallery/query_sort.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) (limited to 'internal/gallery') diff --git a/internal/gallery/query.go b/internal/gallery/query.go index f528906..e2003d5 100644 --- a/internal/gallery/query.go +++ b/internal/gallery/query.go @@ -3,8 +3,8 @@ package gallery type SortField string const ( - SortByDateTime SortField = "dateTaken" - SortByPath SortField = "path" + SortByDateTaken SortField = "dateTaken" + SortByPath SortField = "path" ) // SortOrder identifies the sort direction diff --git a/internal/gallery/query_photos_test.go b/internal/gallery/query_photos_test.go index 9b190da..c9bb986 100644 --- a/internal/gallery/query_photos_test.go +++ b/internal/gallery/query_photos_test.go @@ -214,7 +214,7 @@ func TestQueryPhotosSortByDateAscending(t *testing.T) { } got := QueryPhotos(photos, Query{ - Sort: SortByDateTime, + Sort: SortByDateTaken, Order: SortAsc, }) @@ -244,7 +244,7 @@ func TestQueryPhotosSortByDateDescending(t *testing.T) { } got := QueryPhotos(photos, Query{ - Sort: SortByDateTime, + Sort: SortByDateTaken, Order: SortDesc, }) diff --git a/internal/gallery/query_sort.go b/internal/gallery/query_sort.go index 7ad811b..1a3a63c 100644 --- a/internal/gallery/query_sort.go +++ b/internal/gallery/query_sort.go @@ -18,7 +18,7 @@ func sortQueryPhotos( } return photos[i].RelativePath < photos[j].RelativePath - case SortByDateTime: + case SortByDateTaken: if query.Order == SortAsc { if !photos[i].DateTaken.Equal(photos[j].DateTaken) { return photos[i].DateTaken.Before(photos[j].DateTaken) -- cgit v1.2.3