Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

Fix ordering of records on the subscribers page

Kailash Nadhe7da8fa66854
diff --git a/queries.sql b/queries.sql
index 7b124fcb..6542525d 100644
--- a/queries.sql
+++ b/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
diff --git a/subscribers.go b/subscribers.go
index 71e90e46..c76d8d9a 100644
--- a/subscribers.go
+++ b/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)))
}