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.
(w http.ResponseWriter, r *http.Request)
| 12 | // ParsePagination extracts pagination query params from the http request. |
| 13 | // If an error is encountered, the error is written to w and ok is set to false. |
| 14 | func ParsePagination(w http.ResponseWriter, r *http.Request) (p codersdk.Pagination, ok bool) { |
| 15 | ctx := r.Context() |
| 16 | queryParams := r.URL.Query() |
| 17 | parser := httpapi.NewQueryParamParser() |
| 18 | params := codersdk.Pagination{ |
| 19 | AfterID: parser.UUID(queryParams, uuid.Nil, "after_id"), |
| 20 | // A limit of 0 should be interpreted by the SQL query as "null" or |
| 21 | // "no limit". Do not make this value anything besides 0. |
| 22 | Limit: int(parser.PositiveInt32(queryParams, 0, "limit")), |
| 23 | Offset: int(parser.PositiveInt32(queryParams, 0, "offset")), |
| 24 | } |
| 25 | if len(parser.Errors) > 0 { |
| 26 | httpapi.Write(ctx, w, http.StatusBadRequest, codersdk.Response{ |
| 27 | Message: "Query parameters have invalid values.", |
| 28 | Validations: parser.Errors, |
| 29 | }) |
| 30 | return params, false |
| 31 | } |
| 32 | |
| 33 | return params, true |
| 34 | } |