forked from joyrexus/xhub
/
study.go
248 lines (218 loc) · 6.23 KB
/
study.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
package xhub
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"github.com/joyrexus/buckets"
"github.com/julienschmidt/httprouter"
)
type Study struct {
Name string `json:"name"`
Description string `json:"desc"`
}
// NewStudyController initializes a new instance of our study controller.
func NewStudyController(host string, bux *buckets.DB) *StudyController {
// Create/open bucket for storing study-related data.
studies, err := bux.New([]byte("studies"))
if err != nil {
log.Fatalf("couldn't create/open studies bucket: %v\n", err)
}
// Create/open bucket for storing list of study IDs.
studylist, err := bux.New([]byte("studylist"))
if err != nil {
log.Fatalf("couldn't create/open studylist bucket: %v\n", err)
}
return &StudyController{host, studies, studylist}
}
// A StudyController handles requests for study resources.
type StudyController struct {
host string
studies *buckets.Bucket
studylist *buckets.Bucket
}
// Post handles POST requests for `/studies`, storing the study data sent.
func (c *StudyController) Post(w http.ResponseWriter, r *http.Request,
_ httprouter.Params) {
var study Resource
err := json.NewDecoder(r.Body).Decode(&study)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
key := []byte(study.ID)
now := []byte(time.Now().Format(time.RFC3339Nano))
if c.studylist.Put(key, now); err != nil {
http.Error(w, err.Error(), 500)
return
}
if err := c.studies.Put(key, study.Data); err != nil {
http.Error(w, err.Error(), 500)
return
}
w.WriteHeader(http.StatusCreated)
}
// List handles GET requests for `/studies`, returning a list of
// available studies.
func (c *StudyController) List(w http.ResponseWriter, r *http.Request,
_ httprouter.Params) {
// Retrieve studylist items (study-id/creation-time pairs)
items, err := c.studylist.Items()
if err != nil {
http.Error(w, err.Error(), 500)
return
}
resources := []*Resource{}
// Append each item to the list of resources.
for _, study := range items {
data, err := c.studies.Get(study.Key)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
id := string(study.Key)
url := "http://" + c.host + id
rsc := &Resource{
Version: "1",
Type: "study",
ID: id,
URL: url,
Data: data,
Created: string(study.Value),
}
resources = append(resources, rsc)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(resources)
}
// Get handles GET requests for `/studies/:study`, returning the raw json
// data payload for the requested study.
func (c *StudyController) Get(w http.ResponseWriter, r *http.Request,
p httprouter.Params) {
study := p.ByName("study")
id := fmt.Sprintf("/studies/%s", study)
data, err := c.studies.Get([]byte(id))
if err != nil {
http.Error(w, err.Error(), 500)
return
}
if data == nil {
http.Error(w, id+" not found", http.StatusNoContent)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(data)
}
// Delete handles DELETE requests for `/studies/:study`, deleting the entries
// for the given study. All items associated with the specified study are
// deleted, both its trial and file resources.
func (c *StudyController) Delete(w http.ResponseWriter, r *http.Request,
p httprouter.Params) {
study := p.ByName("study")
// Delete all items associated with study.
if err := c.DeleteChildItems(study); err != nil {
http.Error(w, err.Error(), 500)
return
}
// Delete item in studylist bucket.
key := []byte("/studies/" + study)
if err := c.studylist.Delete(key); err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
}
// DeleteChildItems deletes all items in the studies bucket with a prefix
// of `/studies/:study` or `/files/:study`.
func (c *StudyController) DeleteChildItems(study string) error {
for _, pre := range []string{"/studies/", "/files/"} {
prefix := []byte(pre + study)
items, err := c.studies.PrefixItems(prefix)
if err != nil {
return fmt.Errorf("couldn't retrieve items with prefix %q: %v",
prefix,
err,
)
}
for _, item := range items {
if err := c.studies.Delete(item.Key); err != nil {
return fmt.Errorf("couldn't delete item %q: %v", item.Key, err)
}
}
}
return nil
}
// View handles GET requests for `/view/studies/:study`, returning a web
// page with details for the requested study.
func (c *StudyController) View(w http.ResponseWriter, r *http.Request,
p httprouter.Params) {
id := "/studies/" + p.ByName("study")
data, err := c.studies.Get([]byte(id))
if err != nil {
http.Error(w, err.Error(), 500)
return
}
if data == nil {
http.Error(w, id+" not found", http.StatusNoContent)
return
}
var study Study
if err := json.Unmarshal(data, &study); err != nil {
http.Error(w, err.Error(), 500)
return
}
err = templates.ExecuteTemplate(w, "study_view.html", study)
if err != nil {
http.Error(w, err.Error(), 500)
}
}
// Edit handles GET requests for `/edit/studies/:study`, returning a web
// page with a form for editing the details of the requested study.
func (c *StudyController) Edit(w http.ResponseWriter, r *http.Request,
p httprouter.Params) {
id := "/studies/" + p.ByName("study")
data, err := c.studies.Get([]byte(id))
if err != nil {
http.Error(w, err.Error(), 500)
return
}
if data == nil {
http.Error(w, id+" not found", http.StatusNoContent)
return
}
var study Study
if err := json.Unmarshal(data, &study); err != nil {
http.Error(w, err.Error(), 500)
return
}
err = templates.ExecuteTemplate(w, "study_edit.html", study)
if err != nil {
http.Error(w, err.Error(), 500)
}
}
// Save handles POST requests for `/save/studies`, storing the study data sent.
func (c *StudyController) Save(w http.ResponseWriter, r *http.Request,
_ httprouter.Params) {
fmt.Println(r.FormValue("desc"))
/*
study Resource
err := json.NewDecoder(r.Body).Decode(&study)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
key := []byte(study.ID)
now := []byte(time.Now().Format(time.RFC3339Nano))
if c.studylist.Put(key, now); err != nil {
http.Error(w, err.Error(), 500)
return
}
if err := c.studies.Put(key, study.Data); err != nil {
http.Error(w, err.Error(), 500)
return
}
*/
w.WriteHeader(http.StatusCreated)
}