forked from knaw-huc/evidence-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.go
161 lines (143 loc) · 4.03 KB
/
seed.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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/julienschmidt/httprouter"
)
func (s *server) addSeed(tx *sql.Tx, w http.ResponseWriter, r *http.Request, ps httprouter.Params) (err error) {
var ids []string
err = json.NewDecoder(r.Body).Decode(&ids)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// elasticEndpoint == "" turns off validation, for testing purposes.
if s.elasticEndpoint != "" && !s.validateId(w, r, ids) {
return
}
for _, id := range ids {
_, err = tx.Exec(`INSERT OR IGNORE INTO seed (id, userid) VALUES (?, ?)`,
id, userId(r))
if err != nil {
return
}
}
return
}
// ListPositives allows listing the union of seed set and positive assessments.
func listPositives(tx *sql.Tx, w http.ResponseWriter, r *http.Request, ps httprouter.Params) (err error) {
uparams := r.URL.Query()
offset := intValue(w, uparams, "from", 0)
if offset == -1 {
return
}
size := intValue(w, uparams, "size", 10)
if size == -1 {
return
}
userid := userId(r)
rows, err := tx.Query(
`SELECT * FROM (
SELECT id FROM seed WHERE userid = ?
UNION
SELECT id FROM assessments
WHERE userid = ? AND relevant = 1
) LIMIT ? OFFSET ?`,
userid, userid, size, offset)
ids, err := gatherIds(rows, w)
if err != nil {
return
}
return json.NewEncoder(w).Encode(ids)
}
func numPositives(tx *sql.Tx, w http.ResponseWriter, r *http.Request, ps httprouter.Params) (err error) {
userid := userId(r)
row := tx.QueryRow(`SELECT COUNT(*) FROM (
SELECT id FROM seed WHERE userid = ?
UNION
SELECT id FROM assessments
WHERE userid = ? AND relevant = 1
)`, userid, userid)
var n uint
err = row.Scan(&n)
if err != nil {
log.Print("numPositives: ", err)
http.Error(w, "database error", http.StatusInternalServerError)
return
}
_, err = fmt.Fprint(w, n)
return
}
func listSeed(tx *sql.Tx, w http.ResponseWriter, r *http.Request, ps httprouter.Params) (err error) {
ids, err := gatherSeed(w, tx, userId(r))
if err != nil {
return
}
return json.NewEncoder(w).Encode(ids)
}
// GatherSeed returns the seed set for the specified user. It reports any
// errors it encounters to w and log.
func gatherSeed(w http.ResponseWriter, tx *sql.Tx, userid int) (ids []string, err error) {
rows, err := tx.Query(`SELECT id FROM seed WHERE userid = ?`, userid)
if err != nil {
log.Print("listSeed: ", err)
http.Error(w, "database error", http.StatusInternalServerError)
return
}
defer rows.Close()
return gatherIds(rows, w)
}
func gatherIds(rows *sql.Rows, w http.ResponseWriter) (ids []string, err error) {
for rows.Next() {
var id string
if err = rows.Scan(&id); err != nil {
log.Print("gatherIds: ", err)
http.Error(w, "database error", http.StatusInternalServerError)
return
}
ids = append(ids, id)
}
return
}
func (s *server) removeSeed(tx *sql.Tx, w http.ResponseWriter, r *http.Request, ps httprouter.Params) (err error) {
id := ps.ByName("id")
// elasticEndpoint == "" turns off validation, for testing purposes.
if s.elasticEndpoint != "" && !s.validateId(w, r, []string{id}) {
return
}
res, err := tx.Exec(`DELETE FROM seed WHERE id = ? AND userid = ?`,
id, userId(r))
if err == nil {
changed, err := res.RowsAffected()
if err == nil && changed == 0 {
notInSeedSet(w, id)
}
}
if err != nil {
log.Print("removeSeed: ", err)
http.Error(w, "database error", http.StatusInternalServerError)
}
return
}
func (s *server) seedContains(tx *sql.Tx, w http.ResponseWriter, r *http.Request, ps httprouter.Params) (err error) {
id := ps.ByName("id")
row := tx.QueryRow(`SELECT 1 FROM seed WHERE id = ? AND userid = ?`,
id, userId(r))
var i int
switch err = row.Scan(&i); err {
case nil:
// Report 200 to client. Currently no output.
case sql.ErrNoRows:
notInSeedSet(w, id)
default:
log.Print("seedContains: ", err)
http.Error(w, "database error", http.StatusInternalServerError)
}
return
}
func notInSeedSet(w http.ResponseWriter, id string) {
http.Error(w, fmt.Sprintf("%q not in seed set", id), http.StatusNotFound)
}