Files
coder/coderd/pagination.go
Steven Masley dc1de58857 feat: workspace filter query supported in backend (#2232)
* feat: add support for template in workspace filter
* feat: Implement workspace search filter to support names
* Use new query param parser for pagination fields
* Remove excessive calls, use filters on a single query

Co-authored-by: Garrett <garrett@coder.com>
2022-06-14 08:46:33 -05:00

33 lines
942 B
Go

package coderd
import (
"net/http"
"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) {
queryParams := r.URL.Query()
parser := httpapi.NewQueryParamParser()
params := codersdk.Pagination{
AfterID: parser.UUID(queryParams, uuid.Nil, "after_id"),
// Limit default to "-1" which returns all results
Limit: parser.Int(queryParams, -1, "limit"),
Offset: parser.Int(queryParams, 0, "offset"),
}
if len(parser.Errors) > 0 {
httpapi.Write(w, http.StatusBadRequest, httpapi.Response{
Message: "Query parameters have invalid values.",
Validations: parser.Errors,
})
return params, false
}
return params, true
}