-
Notifications
You must be signed in to change notification settings - Fork 1
/
bom-causes.go
252 lines (220 loc) · 6.4 KB
/
bom-causes.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package apiary
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"github.com/jackc/pgx/v4"
)
type DeathsAPIParameters struct {
StartYear int
EndYear int
Death []string
Sort string
}
// DeathCauses returns a list of causes of death with a count of deaths for each
// cause and related metadata.
type DeathCauses struct {
Death string `json:"death"`
Count NullInt64 `json:"count"`
DescriptiveText NullString `json:"descriptive_text"`
WeekID string `json:"week_id"`
WeekNo NullInt64 `json:"week_no"`
StartDay NullInt64 `json:"start_day"`
StartMonth NullString `json:"start_month"`
EndDay NullInt64 `json:"end_day"`
EndMonth NullString `json:"end_month"`
Year NullInt64 `json:"year"`
SplitYear NullString `json:"split_year"`
TotalRecords int `json:"totalrecords"`
}
// Causes describes a cause of death.
type Causes struct {
Name string `json:"name"`
}
// DeathCausesHandler returns a JSON array of causes of death. The list of causes
// depends on whether a user has provided a comma-separated list of causes. If
// no list is provided, it returns the entire list of causes.
func (s *Server) DeathCausesHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
startYear := r.URL.Query().Get("start-year")
endYear := r.URL.Query().Get("end-year")
causes := r.URL.Query().Get("id")
limit := r.URL.Query().Get("limit")
offset := r.URL.Query().Get("offset")
apiParams := DeathsAPIParameters{
StartYear: 1648,
EndYear: 1750,
Death: []string{},
Sort: "year, week_no, death",
}
// If a start year is provided, update the API parameters
if startYear != "" {
startYearInt, err := strconv.Atoi(startYear)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
log.Println("start year is not an integer", err)
return
}
apiParams.StartYear = startYearInt
}
// If an end year is provided, update the API parameters
if endYear != "" {
endYearInt, err := strconv.Atoi(endYear)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
log.Println("end year is not an integer", err)
return
}
apiParams.EndYear = endYearInt
}
// if a cause string is provided, update the API parameters
if causes != "" {
causesList := strings.Split(causes, ",")
var causesStr []string
for _, p := range causesList {
causeStr := strings.TrimSpace(p)
if causeStr == "" {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
log.Println("cause is an empty string")
return
}
causesStr = append(causesStr, causeStr)
}
apiParams.Death = causesStr
}
query := `
SELECT
c.death,
c.count,
c.descriptive_text,
c.week_id,
w.week_no,
w.start_day,
w.start_month,
w.end_day,
w.end_month,
y.year,
w.split_year,
COUNT(*) OVER() AS totalrecords
FROM
bom.causes_of_death c
JOIN
bom.week w ON w.joinid = c.week_id
JOIN
bom.year y ON y.year = w.year
WHERE
y.year::int >= $1
AND y.year::int <= $2
AND count IS NOT NULL
`
if len(apiParams.Death) > 0 {
query += " AND c.death = ANY($3)"
}
query += " ORDER BY " + apiParams.Sort
if limit != "" {
limitInt, err := strconv.Atoi(limit)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
log.Println("limit is not an integer", err)
return
}
query += " LIMIT " + strconv.Itoa(limitInt)
}
// If an offset is provided, add it to the query
if offset != "" {
offsetInt, err := strconv.Atoi(offset)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
log.Println("offset is not an integer", err)
return
}
query += " OFFSET " + strconv.Itoa(offsetInt)
}
results := make([]DeathCauses, 0)
var row DeathCauses
var rows pgx.Rows
var err error
if len(apiParams.Death) > 0 {
rows, err = s.DB.Query(context.TODO(), query, apiParams.StartYear, apiParams.EndYear, apiParams.Death)
} else {
rows, err = s.DB.Query(context.TODO(), query, apiParams.StartYear, apiParams.EndYear)
}
if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Fatal("Error preparing statement", err)
return
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(
&row.Death,
&row.Count,
&row.DescriptiveText,
&row.WeekID,
&row.WeekNo,
&row.StartDay,
&row.StartMonth,
&row.EndDay,
&row.EndMonth,
&row.Year,
&row.SplitYear,
&row.TotalRecords,
)
if err != nil {
log.Printf("Error scanning row: %v", err)
log.Printf("Types: death=%T, count=%T, descriptiveText=%T, weekID=%T, weekNo=%T, startDay=%T, startMonth=%T, endDay=%T, endMonth=%T, year=%T, splitYear=%T, totalRecords=%T",
row.Death, row.Count, row.DescriptiveText, row.WeekID, row.WeekNo, row.StartDay, row.StartMonth, row.EndDay, row.EndMonth, row.Year, row.SplitYear, row.TotalRecords)
continue
}
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))
}
}
func (s *Server) ListCausesHandler() http.HandlerFunc {
// Query to get a unique list of causes of death
query := `
SELECT DISTINCT
death
FROM
bom.causes_of_death
ORDER BY
death ASC
`
return func(w http.ResponseWriter, r *http.Request) {
results := make([]Causes, 0)
var row Causes
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)
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))
}
}