Skip to content

Commit

Permalink
Fix ordering of records on the subscribers page
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Jul 5, 2020
1 parent db032d3 commit e7da8fa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
3 changes: 2 additions & 1 deletion queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
Expand Down
12 changes: 8 additions & 4 deletions subscribers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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)))
}
Expand Down

0 comments on commit e7da8fa

Please sign in to comment.