mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
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>
This commit is contained in:
@ -2,7 +2,6 @@ package coderd
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
@ -13,53 +12,21 @@ import (
|
||||
// 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: "Query param 'after_id' must be a valid UUID.",
|
||||
Validations: []httpapi.Error{
|
||||
{Field: "after_id", Detail: err.Error()},
|
||||
},
|
||||
})
|
||||
return p, false
|
||||
}
|
||||
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 s := r.URL.Query().Get("limit"); s != "" {
|
||||
limit, err = strconv.Atoi(s)
|
||||
if err != nil {
|
||||
httpapi.Write(w, http.StatusBadRequest, httpapi.Response{
|
||||
Message: "Query param 'limit' must be a valid integer.",
|
||||
Validations: []httpapi.Error{
|
||||
{Field: "limit", Detail: 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: "Query param 'offset' must be a valid integer.",
|
||||
Validations: []httpapi.Error{
|
||||
{Field: "offset", Detail: err.Error()},
|
||||
},
|
||||
})
|
||||
return p, false
|
||||
}
|
||||
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 codersdk.Pagination{
|
||||
AfterID: afterID,
|
||||
Limit: limit,
|
||||
Offset: offset,
|
||||
}, true
|
||||
return params, true
|
||||
}
|
||||
|
Reference in New Issue
Block a user