forked from datatogether/api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection_handlers.go
58 lines (53 loc) · 1.3 KB
/
collection_handlers.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
package main
import (
"github.com/datatogether/api/apiutil"
"github.com/datatogether/core"
"net/http"
)
func CollectionHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "OPTIONS":
EmptyOkHandler(w, r)
case "GET":
GetCollectionHandler(w, r)
default:
NotFoundHandler(w, r)
}
}
func CollectionsHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
ListCollectionsHandler(w, r)
default:
NotFoundHandler(w, r)
}
}
func GetCollectionHandler(w http.ResponseWriter, r *http.Request) {
res := &core.Collection{}
args := &CollectionsGetParams{
Id: r.URL.Path[len("/collections/"):],
// Collection: r.FormValue("collection"),
// Hash: r.FormValue("hash"),
}
err := new(Collections).Get(args, res)
if err != nil {
apiutil.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}
apiutil.WriteResponse(w, res)
}
func ListCollectionsHandler(w http.ResponseWriter, r *http.Request) {
p := apiutil.PageFromRequest(r)
res := make([]*core.Collection, p.Size)
args := &CollectionsListParams{
Limit: p.Limit(),
Offset: p.Offset(),
OrderBy: "created",
}
err := new(Collections).List(args, &res)
if err != nil {
apiutil.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}
apiutil.WritePageResponse(w, res, r, p)
}