-
Notifications
You must be signed in to change notification settings - Fork 1
/
presbyterians.go
59 lines (51 loc) · 1.29 KB
/
presbyterians.go
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
56
57
58
59
package apiary
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
)
// PresbyteriansByYear holds aggregate data on Presbyterian membership and churches.
type PresbyteriansByYear struct {
Year int `json:"year"`
Members int `json:"members"`
Churches int `json:"churches"`
}
// PresbyteriansHandler returns the aggregate data on Presbyterian memberhsip and churches.
func (s *Server) PresbyteriansHandler() http.HandlerFunc {
query := `
SELECT
year,
SUM(members) as members,
SUM(churches) as churches
FROM presbyterians_weber
WHERE members IS NOT NULL
GROUP BY year
ORDER BY year;
`
return func(w http.ResponseWriter, r *http.Request) {
results := make([]PresbyteriansByYear, 0)
var row PresbyteriansByYear
rows, err := s.DB.Query(context.TODO(), query)
if err != nil {
log.Println(err)
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&row.Year, &row.Members, &row.Churches)
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))
}
}