Explorar o código

Fix ordering of records on the subscribers page

Kailash Nadh %!s(int64=5) %!d(string=hai) anos
pai
achega
e7da8fa668
Modificáronse 2 ficheiros con 10 adicións e 5 borrados
  1. 2 1
      queries.sql
  2. 8 4
      subscribers.go

+ 2 - 1
queries.sql

@@ -224,6 +224,7 @@ SELECT (SELECT email FROM prof) as email,
 -- there's a COUNT() OVER() that still returns the total result count
 -- for pagination in the frontend, albeit being a field that'll repeat
 -- with every resultant row.
+-- %s = arbitrary expression, %s = order by field, %s = order direction
 SELECT COUNT(*) OVER () AS total, subscribers.* FROM subscribers
     LEFT JOIN subscriber_lists
     ON (
@@ -233,7 +234,7 @@ SELECT COUNT(*) OVER () AS total, subscribers.* FROM subscribers
     )
     WHERE subscriber_lists.list_id = ALL($1::INT[])
     %s
-    ORDER BY $2 DESC OFFSET $3 LIMIT $4;
+    ORDER BY %s %s OFFSET $2 LIMIT $3;
 
 -- name: query-subscribers-template
 -- raw: true

+ 8 - 4
subscribers.go

@@ -101,12 +101,16 @@ func handleQuerySubscribers(c echo.Context) error {
 	}
 
 	// There's an arbitrary query condition from the frontend.
-	cond := ""
+	var (
+		cond  = ""
+		ordBy = "updated_at"
+		ord   = "DESC"
+	)
 	if query != "" {
 		cond = " AND " + query
 	}
 
-	stmt := fmt.Sprintf(app.queries.QuerySubscribers, cond)
+	stmt := fmt.Sprintf(app.queries.QuerySubscribers, cond, ordBy, ord)
 
 	// Create a readonly transaction to prevent mutations.
 	tx, err := app.db.BeginTxx(context.Background(), &sql.TxOptions{ReadOnly: true})
@@ -117,8 +121,8 @@ func handleQuerySubscribers(c echo.Context) error {
 	}
 	defer tx.Rollback()
 
-	// Run the query.
-	if err := tx.Select(&out.Results, stmt, listIDs, "updaated_at", pg.Offset, pg.Limit); err != nil {
+	// Run the query. stmt is the raw SQL query.
+	if err := tx.Select(&out.Results, stmt, listIDs, pg.Offset, pg.Limit); err != nil {
 		return echo.NewHTTPError(http.StatusInternalServerError,
 			fmt.Sprintf("Error querying subscribers: %v", pqErrMsg(err)))
 	}