fix: order zero-booking services after popular ones (NULLS LAST)
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.
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user