forked from datatogether/api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource_handlers.go
56 lines (51 loc) · 1.17 KB
/
source_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
package main
import (
"github.com/datatogether/api/apiutil"
"github.com/datatogether/core"
"net/http"
)
func SourceHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "OPTIONS":
EmptyOkHandler(w, r)
case "GET":
GetSourceHandler(w, r)
default:
NotFoundHandler(w, r)
}
}
func SourcesHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
ListSourcesHandler(w, r)
default:
NotFoundHandler(w, r)
}
}
func GetSourceHandler(w http.ResponseWriter, r *http.Request) {
res := &core.Source{}
args := &SourcesGetParams{
Id: r.URL.Path[len("/sources/"):],
}
err := new(Sources).Get(args, res)
if err != nil {
apiutil.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}
apiutil.WriteResponse(w, res)
}
func ListSourcesHandler(w http.ResponseWriter, r *http.Request) {
p := apiutil.PageFromRequest(r)
res := make([]*core.Source, p.Size)
args := &SourcesListParams{
Limit: p.Limit(),
Offset: p.Offset(),
OrderBy: "created",
}
err := new(Sources).List(args, &res)
if err != nil {
apiutil.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}
apiutil.WritePageResponse(w, res, r, p)
}