40 lines
913 B
Go
40 lines
913 B
Go
package categories
|
|
|
|
import (
|
|
"encoding/json"
|
|
"gis/app"
|
|
"net/http"
|
|
)
|
|
|
|
func listCategoriesRoute(application *app.App) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
rows, err := application.Db.Query(application.Ctx, "SELECT id, name FROM categories")
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
defer rows.Close()
|
|
|
|
cats := make([]Category, 0)
|
|
for rows.Next() {
|
|
var c Category
|
|
if err := rows.Scan(&c.ID, &c.Name); err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
cats = append(cats, c)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if err := json.NewEncoder(w).Encode(map[string][]Category{"data": cats}); err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
}
|