From 2e4c8722caec6d798ea8f5e0631d0471e0480721 Mon Sep 17 00:00:00 2001 From: Jon Seager Date: Mon, 2 Feb 2026 14:28:45 +0000 Subject: [PATCH] refactor(bags): align order_clause with other repositories - Use method access (sort_key(), sort_direction()) instead of field access for consistency with roasters.rs and roasts.rs - Add secondary sort clauses (e.g., created_at DESC) as tiebreakers to ensure deterministic pagination results --- src/infrastructure/repositories/bags.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/infrastructure/repositories/bags.rs b/src/infrastructure/repositories/bags.rs index f7ea1db..82a1842 100644 --- a/src/infrastructure/repositories/bags.rs +++ b/src/infrastructure/repositories/bags.rs @@ -31,20 +31,18 @@ impl SqlBagRepository { } fn order_clause(request: &ListRequest) -> String { - let sort_column = match request.sort_key { - BagSortKey::RoastDate => "b.roast_date", - BagSortKey::CreatedAt => "b.created_at", - BagSortKey::Roaster => "rr.name", - BagSortKey::Roast => "r.name", - BagSortKey::FinishedAt => "b.finished_at", - }; - - let direction = match request.sort_direction { + let dir_sql = match request.sort_direction() { SortDirection::Asc => "ASC", SortDirection::Desc => "DESC", }; - format!("{} {}", sort_column, direction) + match request.sort_key() { + BagSortKey::RoastDate => format!("b.roast_date {dir_sql}, b.created_at DESC"), + BagSortKey::CreatedAt => format!("b.created_at {dir_sql}, b.id DESC"), + BagSortKey::Roaster => format!("LOWER(rr.name) {dir_sql}, b.created_at DESC"), + BagSortKey::Roast => format!("LOWER(r.name) {dir_sql}, b.created_at DESC"), + BagSortKey::FinishedAt => format!("b.finished_at {dir_sql}, b.created_at DESC"), + } } fn to_domain(record: BagRecord) -> Bag {