From aad364bbd14722837b63cdac7ec97c49c86c1f5f Mon Sep 17 00:00:00 2001 From: Stephen Adamson Date: Thu, 30 Jul 2026 10:57:12 +0100 Subject: [PATCH] fix: order zero-booking services after popular ones (NULLS LAST) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PostgreSQL ORDER BY ... DESC puts NULLs first by default, so services with no bookings appeared at the top instead of the bottom. Also removes the booking count column from SELECT entirely — the sort is done purely in the ORDER BY. --- backend/handlers/services/services.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/backend/handlers/services/services.go b/backend/handlers/services/services.go index ba1ed39..b6649e6 100644 --- a/backend/handlers/services/services.go +++ b/backend/handlers/services/services.go @@ -267,8 +267,7 @@ func DeleteServiceHandler(w http.ResponseWriter, r *http.Request) { func PopularServicesHandler(w http.ResponseWriter, r *http.Request) { query := ` SELECT s.id, s.name, s.description, s.price, s.duration_minutes, - s.minimum_age_required, COALESCE(pt.notice_duration_hours, 0), - COALESCE(booking_counts.cnt, 0) + s.minimum_age_required, COALESCE(pt.notice_duration_hours, 0) FROM services s LEFT JOIN patch_tests pt ON s.id = ANY(pt.service_ids) LEFT JOIN ( @@ -279,7 +278,7 @@ func PopularServicesHandler(w http.ResponseWriter, r *http.Request) { GROUP BY bsvc.service_id ) booking_counts ON s.id = booking_counts.service_id WHERE s.is_active = TRUE - ORDER BY booking_counts.cnt DESC, s.price DESC, s.name + ORDER BY booking_counts.cnt DESC NULLS LAST, s.price DESC, s.name ` rows, err := db.Conn.Query(r.Context(), query) @@ -293,7 +292,6 @@ func PopularServicesHandler(w http.ResponseWriter, r *http.Request) { for rows.Next() { var service ServiceResponse - var bookingCount int err := rows.Scan( &service.ID, @@ -303,7 +301,6 @@ func PopularServicesHandler(w http.ResponseWriter, r *http.Request) { &service.DurationMinutes, &service.MinimumAgeRequired, &service.PatchTestDurationHours, - &bookingCount, ) if err != nil { http.Error(w, "Failed to read service data: "+err.Error(), http.StatusInternalServerError)