2022-05-10 10:44:09 +03:00
|
|
|
package coderd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
|
2023-08-18 13:55:43 -05:00
|
|
|
"github.com/coder/coder/v2/coderd/httpapi"
|
|
|
|
|
"github.com/coder/coder/v2/codersdk"
|
2022-05-10 10:44:09 +03:00
|
|
|
)
|
|
|
|
|
|
2025-07-15 14:55:34 +10:00
|
|
|
// ParsePagination extracts pagination query params from the http request.
|
2022-05-10 10:44:09 +03:00
|
|
|
// If an error is encountered, the error is written to w and ok is set to false.
|
2025-07-15 14:55:34 +10:00
|
|
|
func ParsePagination(w http.ResponseWriter, r *http.Request) (p codersdk.Pagination, ok bool) {
|
2022-09-21 17:07:00 -05:00
|
|
|
ctx := r.Context()
|
2022-06-14 08:46:33 -05:00
|
|
|
queryParams := r.URL.Query()
|
|
|
|
|
parser := httpapi.NewQueryParamParser()
|
|
|
|
|
params := codersdk.Pagination{
|
|
|
|
|
AfterID: parser.UUID(queryParams, uuid.Nil, "after_id"),
|
2024-06-28 02:38:04 -10:00
|
|
|
// A limit of 0 should be interpreted by the SQL query as "null" or
|
|
|
|
|
// "no limit". Do not make this value anything besides 0.
|
|
|
|
|
Limit: int(parser.PositiveInt32(queryParams, 0, "limit")),
|
|
|
|
|
Offset: int(parser.PositiveInt32(queryParams, 0, "offset")),
|
2022-05-10 10:44:09 +03:00
|
|
|
}
|
2022-06-14 08:46:33 -05:00
|
|
|
if len(parser.Errors) > 0 {
|
2022-09-21 17:07:00 -05:00
|
|
|
httpapi.Write(ctx, w, http.StatusBadRequest, codersdk.Response{
|
2022-06-14 08:46:33 -05:00
|
|
|
Message: "Query parameters have invalid values.",
|
|
|
|
|
Validations: parser.Errors,
|
|
|
|
|
})
|
|
|
|
|
return params, false
|
2022-05-10 10:44:09 +03:00
|
|
|
}
|
|
|
|
|
|
2022-06-14 08:46:33 -05:00
|
|
|
return params, true
|
2022-05-10 10:44:09 +03:00
|
|
|
}
|