From 5fe93e048c3f529c80d2d1adb1e49eafebc1af3c Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Sat, 12 Oct 2024 00:26:17 +0200 Subject: Sort categories on loading --- main.go | 2 +- model/categories.sql.go | 16 +++++++++------- sql/categories.sql | 5 +++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 29c85da..05ee948 100644 --- a/main.go +++ b/main.go @@ -96,7 +96,7 @@ func recurPage() http.HandlerFunc { func categoriesPage() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { uid := userId(r) - cats, _ := Q.GetCategories(r.Context(), uid) + cats, _ := Q.GetCategoriesOrdered(r.Context(), uid) showTemplate(w, "categories", cats) } } diff --git a/model/categories.sql.go b/model/categories.sql.go index 74eb8a9..7df0cb3 100644 --- a/model/categories.sql.go +++ b/model/categories.sql.go @@ -9,31 +9,33 @@ import ( "context" ) -const getCategories = `-- name: GetCategories :many +const getCategoriesOrdered = `-- name: GetCategoriesOrdered :many SELECT id, name FROM categories WHERE user_id = $1 + ORDER BY name ASC ` -type GetCategoriesRow struct { +type GetCategoriesOrderedRow struct { ID int32 Name string } -// GetCategories +// GetCategoriesOrdered // // SELECT id, name // FROM categories // WHERE user_id = $1 -func (q *Queries) GetCategories(ctx context.Context, userID int32) ([]GetCategoriesRow, error) { - rows, err := q.db.Query(ctx, getCategories, userID) +// ORDER BY name ASC +func (q *Queries) GetCategoriesOrdered(ctx context.Context, userID int32) ([]GetCategoriesOrderedRow, error) { + rows, err := q.db.Query(ctx, getCategoriesOrdered, userID) if err != nil { return nil, err } defer rows.Close() - var items []GetCategoriesRow + var items []GetCategoriesOrderedRow for rows.Next() { - var i GetCategoriesRow + var i GetCategoriesOrderedRow if err := rows.Scan(&i.ID, &i.Name); err != nil { return nil, err } diff --git a/sql/categories.sql b/sql/categories.sql index 2c8e5c7..e7d9579 100644 --- a/sql/categories.sql +++ b/sql/categories.sql @@ -1,4 +1,5 @@ --- name: GetCategories :many +-- name: GetCategoriesOrdered :many SELECT id, name FROM categories - WHERE user_id = $1; \ No newline at end of file + WHERE user_id = $1 + ORDER BY name ASC; \ No newline at end of file -- cgit v1.2.3-70-g09d2