Signed-off-by: dusk y.bera003.06@protonmail.com
+1
-1
appview/issues/issues.go
+1
-1
appview/issues/issues.go
+2
-1
appview/issues/router.go
+2
-1
appview/issues/router.go
···
5
6
"github.com/go-chi/chi/v5"
7
"tangled.sh/tangled.sh/core/appview/middleware"
8
)
9
10
func (i *Issues) Router(mw *middleware.Middleware) http.Handler {
11
r := chi.NewRouter()
12
13
r.Route("/", func(r chi.Router) {
14
-
r.With(middleware.Paginate).Get("/", i.RepoIssues)
15
r.Get("/{issue}", i.RepoSingleIssue)
16
17
r.Group(func(r chi.Router) {
···
5
6
"github.com/go-chi/chi/v5"
7
"tangled.sh/tangled.sh/core/appview/middleware"
8
+
"tangled.sh/tangled.sh/core/appview/pagination"
9
)
10
11
func (i *Issues) Router(mw *middleware.Middleware) http.Handler {
12
r := chi.NewRouter()
13
14
r.Route("/", func(r chi.Router) {
15
+
r.With(middleware.Paginate(pagination.FirstPage(10))).Get("/", i.RepoIssues)
16
r.Get("/{issue}", i.RepoSingleIssue)
17
18
r.Group(func(r chi.Router) {
+24
-22
appview/middleware/middleware.go
+24
-22
appview/middleware/middleware.go
···
81
}
82
}
83
84
-
func Paginate(next http.Handler) http.Handler {
85
-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
86
-
page := pagination.FirstPage()
87
88
-
offsetVal := r.URL.Query().Get("offset")
89
-
if offsetVal != "" {
90
-
offset, err := strconv.Atoi(offsetVal)
91
-
if err != nil {
92
-
log.Println("invalid offset")
93
-
} else {
94
-
page.Offset = offset
95
}
96
-
}
97
98
-
limitVal := r.URL.Query().Get("limit")
99
-
if limitVal != "" {
100
-
limit, err := strconv.Atoi(limitVal)
101
-
if err != nil {
102
-
log.Println("invalid limit")
103
-
} else {
104
-
page.Limit = limit
105
}
106
-
}
107
108
-
ctx := context.WithValue(r.Context(), "page", page)
109
-
next.ServeHTTP(w, r.WithContext(ctx))
110
-
})
111
}
112
113
func (mw Middleware) knotRoleMiddleware(group string) middlewareFunc {
···
81
}
82
}
83
84
+
func Paginate(firstPage pagination.Page) func(next http.Handler) http.Handler {
85
+
return func(next http.Handler) http.Handler {
86
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
87
+
page := firstPage
88
89
+
offsetVal := r.URL.Query().Get("offset")
90
+
if offsetVal != "" {
91
+
offset, err := strconv.Atoi(offsetVal)
92
+
if err != nil {
93
+
log.Println("invalid offset")
94
+
} else {
95
+
page.Offset = offset
96
+
}
97
}
98
99
+
limitVal := r.URL.Query().Get("limit")
100
+
if limitVal != "" {
101
+
limit, err := strconv.Atoi(limitVal)
102
+
if err != nil {
103
+
log.Println("invalid limit")
104
+
} else {
105
+
page.Limit = limit
106
+
}
107
}
108
109
+
ctx := context.WithValue(r.Context(), "page", page)
110
+
next.ServeHTTP(w, r.WithContext(ctx))
111
+
})
112
+
}
113
}
114
115
func (mw Middleware) knotRoleMiddleware(group string) middlewareFunc {
+3
-3
appview/pagination/page.go
+3
-3
appview/pagination/page.go
···
5
Limit int // number of items in a page
6
}
7
8
+
func FirstPage(limit int) Page {
9
return Page{
10
Offset: 0,
11
+
Limit: limit,
12
}
13
}
14
15
func (p Page) Previous() Page {
16
if p.Offset-p.Limit < 0 {
17
+
return FirstPage(p.Limit)
18
} else {
19
return Page{
20
Offset: p.Offset - p.Limit,