appview: improve pagination.Page usage #519

closed
opened by ptr.pet targeting master from ptr.pet/core: pipeline-paginated
Changed files
+30 -27
appview
issues
middleware
pagination
+1 -1
appview/issues/issues.go
···
page, ok := r.Context().Value("page").(pagination.Page)
if !ok {
log.Println("failed to get page")
-
page = pagination.FirstPage()
+
page = pagination.FirstPage(10)
}
user := rp.oauth.GetUser(r)
+2 -1
appview/issues/router.go
···
"github.com/go-chi/chi/v5"
"tangled.sh/tangled.sh/core/appview/middleware"
+
"tangled.sh/tangled.sh/core/appview/pagination"
)
func (i *Issues) Router(mw *middleware.Middleware) http.Handler {
r := chi.NewRouter()
r.Route("/", func(r chi.Router) {
-
r.With(middleware.Paginate).Get("/", i.RepoIssues)
+
r.With(middleware.Paginate(pagination.FirstPage(10))).Get("/", i.RepoIssues)
r.Get("/{issue}", i.RepoSingleIssue)
r.Group(func(r chi.Router) {
+24 -22
appview/middleware/middleware.go
···
}
}
-
func Paginate(next http.Handler) http.Handler {
-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-
page := pagination.FirstPage()
+
func Paginate(firstPage pagination.Page) func(next http.Handler) http.Handler {
+
return func(next http.Handler) http.Handler {
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+
page := firstPage
-
offsetVal := r.URL.Query().Get("offset")
-
if offsetVal != "" {
-
offset, err := strconv.Atoi(offsetVal)
-
if err != nil {
-
log.Println("invalid offset")
-
} else {
-
page.Offset = offset
+
offsetVal := r.URL.Query().Get("offset")
+
if offsetVal != "" {
+
offset, err := strconv.Atoi(offsetVal)
+
if err != nil {
+
log.Println("invalid offset")
+
} else {
+
page.Offset = offset
+
}
}
-
}
-
limitVal := r.URL.Query().Get("limit")
-
if limitVal != "" {
-
limit, err := strconv.Atoi(limitVal)
-
if err != nil {
-
log.Println("invalid limit")
-
} else {
-
page.Limit = limit
+
limitVal := r.URL.Query().Get("limit")
+
if limitVal != "" {
+
limit, err := strconv.Atoi(limitVal)
+
if err != nil {
+
log.Println("invalid limit")
+
} else {
+
page.Limit = limit
+
}
}
-
}
-
ctx := context.WithValue(r.Context(), "page", page)
-
next.ServeHTTP(w, r.WithContext(ctx))
-
})
+
ctx := context.WithValue(r.Context(), "page", page)
+
next.ServeHTTP(w, r.WithContext(ctx))
+
})
+
}
}
func (mw Middleware) knotRoleMiddleware(group string) middlewareFunc {
+3 -3
appview/pagination/page.go
···
Limit int // number of items in a page
}
-
func FirstPage() Page {
+
func FirstPage(limit int) Page {
return Page{
Offset: 0,
-
Limit: 10,
+
Limit: limit,
}
}
func (p Page) Previous() Page {
if p.Offset-p.Limit < 0 {
-
return FirstPage()
+
return FirstPage(p.Limit)
} else {
return Page{
Offset: p.Offset - p.Limit,