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 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