diff options
| author | RanXom <saiyedshizain@gmail.com> | 2026-07-17 18:28:24 +0530 |
|---|---|---|
| committer | RanXom <saiyedshizain@gmail.com> | 2026-07-17 18:28:24 +0530 |
| commit | f056150175e9cfdeab8c34bb52ff298107d9ecc3 (patch) | |
| tree | 5d6b60a714f9d423845fd29b4a924dc0b22ab4e4 /internal | |
| parent | 79037d80de00d8f8d14fc9b3a113a243f8199751 (diff) | |
| download | galleryd-f056150175e9cfdeab8c34bb52ff298107d9ecc3.tar.xz galleryd-f056150175e9cfdeab8c34bb52ff298107d9ecc3.zip | |
feat(gallery): support paginated photo queries
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/gallery/query_photos.go | 18 | ||||
| -rw-r--r-- | internal/gallery/query_photos_test.go | 125 |
2 files changed, 143 insertions, 0 deletions
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", + ) + }) +} |