-
Notifications
You must be signed in to change notification settings - Fork 103
/
list.go
47 lines (40 loc) · 1.14 KB
/
list.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
package repo
import (
"net/http"
"strconv"
"github.com/bleenco/abstruse/server/api/middlewares"
"github.com/bleenco/abstruse/server/api/render"
"github.com/bleenco/abstruse/server/core"
)
// HandleList returns an http.HandlerFunc that writes JSON encoded
// list of repositories based on user id to the http response body.
func HandleList(repos core.RepositoryStore) http.HandlerFunc {
type resp struct {
Count int64 `json:"count"`
Data []core.Repository `json:"data"`
}
return func(w http.ResponseWriter, r *http.Request) {
claims := middlewares.ClaimsFromCtx(r.Context())
limit, err := strconv.Atoi(r.URL.Query().Get("limit"))
if err != nil {
limit = 1
}
offset, err := strconv.Atoi(r.URL.Query().Get("offset"))
if err != nil {
offset = 10
}
keyword := r.URL.Query().Get("keyword")
filters := core.RepositoryFilter{
Limit: limit,
Offset: offset,
Keyword: keyword,
UserID: claims.ID,
}
repositories, count, err := repos.List(filters)
if err != nil {
render.InternalServerError(w, err.Error())
return
}
render.JSON(w, http.StatusOK, resp{Count: count, Data: repositories})
}
}