mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
feat: Implement unified pagination and add template versions support (#1308)
* feat: Implement pagination for template versions * feat: Use unified pagination between users and template versions * Sync codepaths between users and template versions * Create requestOption type in codersdk and add test * Fix created_at edge case for pagination cursor in queries * feat: Add support for json omitempty and embedded structs in apitypings (#1318) * Add scripts/apitypings/main.go to Makefile
This commit is contained in:
committed by
GitHub
parent
dc115b8ca0
commit
2d3dc436a8
57
coderd/pagination.go
Normal file
57
coderd/pagination.go
Normal file
@ -0,0 +1,57 @@
|
||||
package coderd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/coder/coder/coderd/httpapi"
|
||||
"github.com/coder/coder/codersdk"
|
||||
)
|
||||
|
||||
// parsePagination extracts pagination query params from the http request.
|
||||
// If an error is encountered, the error is written to w and ok is set to false.
|
||||
func parsePagination(w http.ResponseWriter, r *http.Request) (p codersdk.Pagination, ok bool) {
|
||||
var (
|
||||
afterID = uuid.Nil
|
||||
limit = -1 // Default to no limit and return all results.
|
||||
offset = 0
|
||||
)
|
||||
|
||||
var err error
|
||||
if s := r.URL.Query().Get("after_id"); s != "" {
|
||||
afterID, err = uuid.Parse(r.URL.Query().Get("after_id"))
|
||||
if err != nil {
|
||||
httpapi.Write(w, http.StatusBadRequest, httpapi.Response{
|
||||
Message: fmt.Sprintf("after_id must be a valid uuid: %s", err.Error()),
|
||||
})
|
||||
return p, false
|
||||
}
|
||||
}
|
||||
if s := r.URL.Query().Get("limit"); s != "" {
|
||||
limit, err = strconv.Atoi(s)
|
||||
if err != nil {
|
||||
httpapi.Write(w, http.StatusBadRequest, httpapi.Response{
|
||||
Message: fmt.Sprintf("limit must be an integer: %s", err.Error()),
|
||||
})
|
||||
return p, false
|
||||
}
|
||||
}
|
||||
if s := r.URL.Query().Get("offset"); s != "" {
|
||||
offset, err = strconv.Atoi(s)
|
||||
if err != nil {
|
||||
httpapi.Write(w, http.StatusBadRequest, httpapi.Response{
|
||||
Message: fmt.Sprintf("offset must be an integer: %s", err.Error()),
|
||||
})
|
||||
return p, false
|
||||
}
|
||||
}
|
||||
|
||||
return codersdk.Pagination{
|
||||
AfterID: afterID,
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
}, true
|
||||
}
|
Reference in New Issue
Block a user