-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbom-parishes.go
More file actions
55 lines (49 loc) · 1.39 KB
/
bom-parishes.go
File metadata and controls
55 lines (49 loc) · 1.39 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
package apiary
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
)
// Parish describes a parish name, canonical name, and unique ID.
type Parish struct {
ParishID int `json:"id"`
Name string `json:"name"`
CanonicalName string `json:"canonical_name"`
BillSubunit NullString `json:"subunit"`
FoundationYear NullString `json:"foundation_year"`
Notes NullString `json:"notes"`
}
// ParishesHandler returns a list of unique parish IDs and names.
func (s *Server) ParishesHandler() http.HandlerFunc {
query := `
SELECT id, parish_name, canonical_name, bills_subunit, foundation_year, notes
FROM bom.parishes
ORDER BY canonical_name;
`
return func(w http.ResponseWriter, r *http.Request) {
results := make([]Parish, 0)
var row Parish
rows, err := s.DB.Query(context.TODO(), query)
if err != nil {
log.Println(err)
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&row.ParishID, &row.Name, &row.CanonicalName, &row.BillSubunit, &row.FoundationYear, &row.Notes)
if err != nil {
log.Println(err)
}
results = append(results, row)
}
err = rows.Err()
if err != nil {
log.Println(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
response, _ := json.Marshal(results)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(response))
}
}