diff options
author | Runxi Yu <me@runxiyu.org> | 2025-02-14 08:56:07 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-02-14 08:56:07 +0800 |
commit | 1660282ccc8c2ef8949c0416e206dc175a977722 (patch) | |
tree | 1e7e29a8efb0032b4d1b7c74ca99cb4265879482 | |
parent | http_*: Refactor to reduce duplication (diff) | |
download | forge-1660282ccc8c2ef8949c0416e206dc175a977722.tar.gz forge-1660282ccc8c2ef8949c0416e206dc175a977722.tar.zst forge-1660282ccc8c2ef8949c0416e206dc175a977722.zip |
{database,http_handle_*index}.go: Reduce query_name_desc_list duplication
Diffstat (limited to '')
-rw-r--r-- | database.go | 18 | ||||
-rw-r--r-- | http_handle_group_index.go | 14 | ||||
-rw-r--r-- | http_handle_index.go | 20 |
3 files changed, 20 insertions, 32 deletions
diff --git a/database.go b/database.go index a583211..22afad2 100644 --- a/database.go +++ b/database.go @@ -26,3 +26,21 @@ func query_list[T any](ctx context.Context, query string, args ...any) ([]T, err return result, nil } + +func query_name_desc_list(ctx context.Context, query string, args ...any) ([]name_desc_t, error) { + rows, err := database.Query(ctx, query, args...) + if err != nil { + return nil, err + } + defer rows.Close() + + result := []name_desc_t{} + for rows.Next() { + var name, description string + if err := rows.Scan(&name, &description); err != nil { + return nil, err + } + result = append(result, name_desc_t{name, description}) + } + return result, rows.Err() +} diff --git a/http_handle_group_index.go b/http_handle_group_index.go index 3098076..670c128 100644 --- a/http_handle_group_index.go +++ b/http_handle_group_index.go @@ -6,23 +6,11 @@ import ( func handle_group_repos(w http.ResponseWriter, r *http.Request, params map[string]any) { group_name := params["group_name"] - - rows, err := database.Query(r.Context(), "SELECT r.name, COALESCE(r.description, '') FROM repos r JOIN groups g ON r.group_id = g.id WHERE g.name = $1;", group_name) + repos, err := query_name_desc_list(r.Context(), "SELECT r.name, COALESCE(r.description, '') FROM repos r JOIN groups g ON r.group_id = g.id WHERE g.name = $1;", group_name) if err != nil { http.Error(w, "Error getting groups: "+err.Error(), http.StatusInternalServerError) return } - defer rows.Close() - - repos := []name_desc_t{} - for rows.Next() { - var repoName, repoDescription string - if err := rows.Scan(&repoName, &repoDescription); err != nil { - http.Error(w, "Error scanning repo: "+err.Error(), http.StatusInternalServerError) - return - } - repos = append(repos, name_desc_t{repoName, repoDescription}) - } params["repos"] = repos err = templates.ExecuteTemplate(w, "group_repos", params) diff --git a/http_handle_index.go b/http_handle_index.go index 41a55e8..ee92c49 100644 --- a/http_handle_index.go +++ b/http_handle_index.go @@ -5,30 +5,12 @@ import ( ) func handle_index(w http.ResponseWriter, r *http.Request, params map[string]any) { - rows, err := database.Query(r.Context(), "SELECT name, COALESCE(description, '') FROM groups") + groups, err := query_name_desc_list(r.Context(), "SELECT name, COALESCE(description, '') FROM groups") if err != nil { http.Error(w, "Error querying groups: "+err.Error(), http.StatusInternalServerError) return } - defer rows.Close() - - groups := []name_desc_t{} - for rows.Next() { - var groupName, groupDescription string - if err := rows.Scan(&groupName, &groupDescription); err != nil { - http.Error(w, "Error scanning group: "+err.Error(), http.StatusInternalServerError) - return - } - groups = append(groups, name_desc_t{groupName, groupDescription}) - } - - if err := rows.Err(); err != nil { - http.Error(w, "Error iterating over rows: "+err.Error(), http.StatusInternalServerError) - return - } - params["groups"] = groups - err = templates.ExecuteTemplate(w, "index", params) if err != nil { http.Error(w, "Error rendering template: "+err.Error(), http.StatusInternalServerError) |