-
Notifications
You must be signed in to change notification settings - Fork 1
/
apb-verse-quotations.go
70 lines (58 loc) · 1.71 KB
/
apb-verse-quotations.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
package apiary
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
)
// VerseQuotation is a single instance of a quotation
type VerseQuotation struct {
Reference string `json:"reference"`
DocID string `json:"docID"`
Date string `json:"date"`
Probability float32 `json:"probability"`
Title string `json:"title"`
State string `json:"state"`
}
// APBVerseQuotationsHandler returns the instances of quotations for a verse.
func (s *Server) APBVerseQuotationsHandler() http.HandlerFunc {
query := `
SELECT q.reference_id, q.doc_id, q.date::text, q.probability,
n.title_clean, places.state
FROM apb.quotations q
LEFT JOIN apb.chronam_pages p ON q.doc_id = p.doc_id
LEFT JOIN apb.chronam_newspapers n ON p.lccn = n.lccn
LEFT JOIN (SELECT DISTINCT ON (lccn) lccn, state FROM apb.chronam_newspaper_places ORDER BY lccn) places ON p.lccn = places.lccn
WHERE reference_id = $1 AND corpus = 'chronam'
ORDER BY date;
`
return func(w http.ResponseWriter, r *http.Request) {
refs := r.URL.Query()["ref"]
results := make([]VerseQuotation, 0)
var row VerseQuotation
rows, err := s.DB.Query(context.TODO(), query, refs[0])
if err != nil {
log.Println(err)
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&row.Reference, &row.DocID, &row.Date, &row.Probability, &row.Title, &row.State)
if err != nil {
log.Println(err)
}
results = append(results, row)
}
err = rows.Err()
if err != nil {
log.Println(err)
}
if len(results) == 0 {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404 Not found."))
}
response, _ := json.Marshal(results)
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(response))
}
}