-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbom-christenings.go
231 lines (205 loc) · 5.03 KB
/
bom-christenings.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package apiary
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"github.com/jackc/pgx/v4"
)
// ChristeningsByYear describes a christening's description, total count, week number,
// week ID, and year.
type ChristeningsByYear struct {
Christening string `json:"christening"`
TotalCount NullInt64 `json:"count"`
WeekNo NullInt64 `json:"week_no"`
StartDay NullInt64 `json:"start_day"`
StartMonth string `json:"start_month"`
EndDay NullInt64 `json:"end_day"`
EndMonth string `json:"end_month"`
Year int `json:"year"`
SplitYear string `json:"split_year"`
TotalRecords int `json:"totalrecords"`
}
// Christenings describes a christening location.
type Christenings struct {
Name string `json:"name"`
ID int `json:"id"`
}
// ChristeningsHandler returns the christenings for a given range of years. It expects a start year and
// end year as query parameters.
func (s *Server) ChristeningsHandler() http.HandlerFunc {
queryLocation := `
SELECT
c.christening,
c.count,
c.week_number,
c.start_day,
c.start_month,
c.end_day,
c.end_month,
y.year,
COUNT(*) OVER() AS totalrecords
FROM
bom.christenings c
JOIN
bom.year y ON y.year = c.year
JOIN
bom.christening_locations l ON l.name = c.christening
WHERE
y.year >= $1::int
AND y.year < $2::int
AND (
$3::int[] IS NULL
OR l.id = ANY($3::int[])
)
ORDER BY
year ASC,
week_number ASC
LIMIT $4
OFFSET $5;
`
query := `
SELECT
c.christening,
c.count,
c.week_number,
c.start_day,
c.start_month,
c.end_day,
c.end_month,
y.year,
COUNT(*) OVER() AS totalrecords
FROM
bom.christenings c
JOIN
bom.year y ON y.year = c.year
WHERE
y.year >= $1::int
AND y.year < $2::int
ORDER BY
year ASC,
week_number ASC
LIMIT $3
OFFSET $4;
`
return func(w http.ResponseWriter, r *http.Request) {
startYear := r.URL.Query().Get("start-year")
endYear := r.URL.Query().Get("end-year")
location := r.URL.Query().Get("id")
limit := r.URL.Query().Get("limit")
offset := r.URL.Query().Get("offset")
if startYear == "" || endYear == "" {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
startYearInt, err := strconv.Atoi(startYear)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
endYearInt, err := strconv.Atoi(endYear)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
if limit == "" {
limit = "25"
}
if offset == "" {
offset = "0"
}
// location needs to be a postgres array
location = fmt.Sprintf("{%s}", strings.TrimSpace(location))
limitInt, err := strconv.Atoi(limit)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
offsetInt, err := strconv.Atoi(offset)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
results := make([]ChristeningsByYear, 0)
var row ChristeningsByYear
var rows pgx.Rows
switch {
case location == "{}":
rows, err = s.DB.Query(context.TODO(), query, startYearInt, endYearInt, limitInt, offsetInt)
case location != "{}":
rows, err = s.DB.Query(context.TODO(), queryLocation, startYearInt, endYearInt, location, limitInt, offsetInt)
default:
rows, err = s.DB.Query(context.TODO(), query, startYearInt, endYearInt, limitInt, offsetInt)
}
if err != nil {
log.Println(err)
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(
&row.Christening,
&row.TotalCount,
&row.WeekNo,
&row.StartDay,
&row.StartMonth,
&row.EndDay,
&row.EndMonth,
&row.Year,
&row.TotalRecords,
)
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)
return
}
response, _ := json.Marshal(results)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(response))
}
}
// ListChristeningsHandler returns a list of parish names and ids where
// christenings have been recorded.
func (s *Server) ListChristeningsHandler() http.HandlerFunc {
query := `
SELECT DISTINCT
name,
id
FROM
bom.christening_locations
ORDER BY
name ASC
`
return func(w http.ResponseWriter, r *http.Request) {
results := make([]Christenings, 0)
var row Christenings
rows, err := s.DB.Query(context.TODO(), query)
if err != nil {
log.Println(err)
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&row.Name, &row.ID)
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))
}
}