diff options
author | Runxi Yu <me@runxiyu.org> | 2025-03-06 20:32:14 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-03-06 20:32:29 +0800 |
commit | 63d776344aab5c766c77462d762a74fb48b0c650 (patch) | |
tree | cb9a76e23a40979b80edeaff160f9594ddb6a85d | |
parent | group/index: Add group description (diff) | |
download | forge-63d776344aab5c766c77462d762a74fb48b0c650.tar.gz forge-63d776344aab5c766c77462d762a74fb48b0c650.tar.zst forge-63d776344aab5c766c77462d762a74fb48b0c650.zip |
group/index: Reuse ID from first SQL query to avoid triple recursion
-rw-r--r-- | http_handle_group_index.go | 69 |
1 files changed, 10 insertions, 59 deletions
diff --git a/http_handle_group_index.go b/http_handle_group_index.go index e3f94fd..438729b 100644 --- a/http_handle_group_index.go +++ b/http_handle_group_index.go @@ -16,6 +16,7 @@ func handle_group_index(w http.ResponseWriter, r *http.Request, params map[strin var repos []name_desc_t var subgroups []name_desc_t var err error + var group_id int var group_description string group_path = params["group_path"].([]string) @@ -44,13 +45,13 @@ func handle_group_index(w http.ResponseWriter, r *http.Request, params map[strin WHERE g.name = ($1::text[])[group_path_cte.depth + 1] AND group_path_cte.depth + 1 <= cardinality($1::text[]) ) - SELECT COALESCE(g.description, '') + SELECT c.id, COALESCE(g.description, '') FROM group_path_cte c JOIN groups g ON g.id = c.id WHERE c.depth = cardinality($1::text[]) `, pgtype.FlatArray[string](group_path), - ).Scan(&group_description) + ).Scan(&group_id, &group_description) if err == pgx.ErrNoRows { http.Error(w, "Group not found", http.StatusNotFound) @@ -63,35 +64,10 @@ func handle_group_index(w http.ResponseWriter, r *http.Request, params map[strin // Repos var rows pgx.Rows rows, err = database.Query(r.Context(), ` - WITH RECURSIVE group_path_cte AS ( - SELECT - id, - parent_group, - name, - 1 AS depth - FROM groups - WHERE name = ($1::text[])[1] - AND parent_group IS NULL - - UNION ALL - - SELECT - g.id, - g.parent_group, - g.name, - group_path_cte.depth + 1 - FROM groups g - JOIN group_path_cte ON g.parent_group = group_path_cte.id - WHERE g.name = ($1::text[])[group_path_cte.depth + 1] - AND group_path_cte.depth + 1 <= cardinality($1::text[]) - ) - SELECT r.name, COALESCE(r.description, '') - FROM group_path_cte c - JOIN repos r ON r.group_id = c.id - WHERE c.depth = cardinality($1::text[]) - `, - pgtype.FlatArray[string](group_path), - ) + SELECT name, COALESCE(description, '') + FROM repos + WHERE group_id = $1 + `, group_id) if err != nil { http.Error(w, "Error getting repos: "+err.Error(), http.StatusInternalServerError) return @@ -113,35 +89,10 @@ func handle_group_index(w http.ResponseWriter, r *http.Request, params map[strin // Subgroups rows, err = database.Query(r.Context(), ` - WITH RECURSIVE group_path_cte AS ( - SELECT - id, - parent_group, - name, - 1 AS depth + SELECT name, COALESCE(description, '') FROM groups - WHERE name = ($1::text[])[1] - AND parent_group IS NULL - - UNION ALL - - SELECT - g.id, - g.parent_group, - g.name, - group_path_cte.depth + 1 - FROM groups g - JOIN group_path_cte ON g.parent_group = group_path_cte.id - WHERE g.name = ($1::text[])[group_path_cte.depth + 1] - AND group_path_cte.depth + 1 <= cardinality($1::text[]) - ) - SELECT g.name, COALESCE(g.description, '') - FROM group_path_cte c - JOIN groups g ON g.parent_group = c.id - WHERE c.depth = cardinality($1::text[]) - `, - pgtype.FlatArray[string](group_path), - ) + WHERE parent_group = $1 + `, group_id) if err != nil { http.Error(w, "Error getting subgroups: "+err.Error(), http.StatusInternalServerError) return |