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
5
6
6
"github.com/go-chi/chi/v5"
7
7
"tangled.sh/tangled.sh/core/appview/middleware"
8
+
"tangled.sh/tangled.sh/core/appview/pagination"
8
9
)
9
10
10
11
func (i *Issues) Router(mw *middleware.Middleware) http.Handler {
11
12
r := chi.NewRouter()
12
13
13
14
r.Route("/", func(r chi.Router) {
14
-
r.With(middleware.Paginate).Get("/", i.RepoIssues)
15
+
r.With(middleware.Paginate(pagination.FirstPage(10))).Get("/", i.RepoIssues)
15
16
r.Get("/{issue}", i.RepoSingleIssue)
16
17
17
18
r.Group(func(r chi.Router) {
+24
-22
appview/middleware/middleware.go
+24
-22
appview/middleware/middleware.go
···
81
81
}
82
82
}
83
83
84
-
func Paginate(next http.Handler) http.Handler {
85
-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
86
-
page := pagination.FirstPage()
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
87
88
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
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
+
}
95
97
}
96
-
}
97
98
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
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
+
}
105
107
}
106
-
}
107
108
108
-
ctx := context.WithValue(r.Context(), "page", page)
109
-
next.ServeHTTP(w, r.WithContext(ctx))
110
-
})
109
+
ctx := context.WithValue(r.Context(), "page", page)
110
+
next.ServeHTTP(w, r.WithContext(ctx))
111
+
})
112
+
}
111
113
}
112
114
113
115
func (mw Middleware) knotRoleMiddleware(group string) middlewareFunc {
+3
-3
appview/pagination/page.go
+3
-3
appview/pagination/page.go
···
5
5
Limit int // number of items in a page
6
6
}
7
7
8
-
func FirstPage() Page {
8
+
func FirstPage(limit int) Page {
9
9
return Page{
10
10
Offset: 0,
11
-
Limit: 10,
11
+
Limit: limit,
12
12
}
13
13
}
14
14
15
15
func (p Page) Previous() Page {
16
16
if p.Offset-p.Limit < 0 {
17
-
return FirstPage()
17
+
return FirstPage(p.Limit)
18
18
} else {
19
19
return Page{
20
20
Offset: p.Offset - p.Limit,
History
4 rounds
2 comments
expand 0 comments
closed without merging
expand 0 comments
expand 1 comment
ptr.pet
submitted
#0
1 commit
expand
collapse
8ee73a8a
appview: improve pagination.Page usage
Signed-off-by: dusk <y.bera003.06@protonmail.com>
expand 1 comment
this helper is quite strange. can we use a slightly better API? the page size does not belong in the FirstPage method (although i understand it was previously hardcoded here).
we can do something like p := NewPagination(WithSize(N), WithOffset(0), WithTotal(...)) and have p.FirstPage() perhaps.
i went with a page number api and made pagination produce pages instead, i think it fits it better (pagination still uses offset / limit / total though ofc)