aboutsummaryrefslogtreecommitdiff
path: root/http_handle_index.go
blob: e36332d701d953efe3a4f3c1cbbc565be27f7306 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main

import (
	"net/http"
)

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")
	if err != nil {
		http.Error(w, "Error querying groups: "+err.Error(), http.StatusInternalServerError)
		return
	}
	defer rows.Close()

	groups := []struct {
		Name        string
		Description string
	}{}
	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, struct {
			Name        string
			Description string
		}{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)
		return
	}
}