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) {
|
func PopularServicesHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
query := `
|
query := `
|
||||||
SELECT s.id, s.name, s.description, s.price, s.duration_minutes,
|
SELECT s.id, s.name, s.description, s.price, s.duration_minutes,
|
||||||
s.minimum_age_required, COALESCE(pt.notice_duration_hours, 0),
|
s.minimum_age_required, COALESCE(pt.notice_duration_hours, 0)
|
||||||
COALESCE(booking_counts.cnt, 0)
|
|
||||||
FROM services s
|
FROM services s
|
||||||
LEFT JOIN patch_tests pt ON s.id = ANY(pt.service_ids)
|
LEFT JOIN patch_tests pt ON s.id = ANY(pt.service_ids)
|
||||||
LEFT JOIN (
|
LEFT JOIN (
|
||||||
@@ -279,7 +278,7 @@ func PopularServicesHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
GROUP BY bsvc.service_id
|
GROUP BY bsvc.service_id
|
||||||
) booking_counts ON s.id = booking_counts.service_id
|
) booking_counts ON s.id = booking_counts.service_id
|
||||||
WHERE s.is_active = TRUE
|
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)
|
rows, err := db.Conn.Query(r.Context(), query)
|
||||||
@@ -293,7 +292,6 @@ func PopularServicesHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var service ServiceResponse
|
var service ServiceResponse
|
||||||
var bookingCount int
|
|
||||||
|
|
||||||
err := rows.Scan(
|
err := rows.Scan(
|
||||||
&service.ID,
|
&service.ID,
|
||||||
@@ -303,7 +301,6 @@ func PopularServicesHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
&service.DurationMinutes,
|
&service.DurationMinutes,
|
||||||
&service.MinimumAgeRequired,
|
&service.MinimumAgeRequired,
|
||||||
&service.PatchTestDurationHours,
|
&service.PatchTestDurationHours,
|
||||||
&bookingCount,
|
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Failed to read service data: "+err.Error(), http.StatusInternalServerError)
|
http.Error(w, "Failed to read service data: "+err.Error(), http.StatusInternalServerError)
|
||||||
|
|||||||
Reference in New Issue
Block a user