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:
Steven Masley
2022-06-14 08:46:33 -05:00
committed by GitHub
parent 5be52de593
commit dc1de58857
20 changed files with 1068 additions and 464 deletions

View File

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/google/uuid"
@ -217,26 +218,39 @@ func (c *Client) PutExtendWorkspace(ctx context.Context, id uuid.UUID, req PutEx
}
type WorkspaceFilter struct {
OrganizationID uuid.UUID `json:"organization_id,omitempty"`
// Owner can be a user_id (uuid), "me", or a username
Owner string `json:"owner,omitempty"`
Name string `json:"name,omitempty"`
// Owner can be "me" or a username
Owner string `json:"owner,omitempty" typescript:"-"`
// Template is a template name
Template string `json:"template,omitempty" typescript:"-"`
// Name will return partial matches
Name string `json:"name,omitempty" typescript:"-"`
// FilterQuery supports a raw filter query string
FilterQuery string `json:"q,omitempty"`
}
// asRequestOption returns a function that can be used in (*Client).Request.
// It modifies the request query parameters.
func (f WorkspaceFilter) asRequestOption() requestOption {
return func(r *http.Request) {
q := r.URL.Query()
if f.OrganizationID != uuid.Nil {
q.Set("organization_id", f.OrganizationID.String())
}
var params []string
// Make sure all user input is quoted to ensure it's parsed as a single
// string.
if f.Owner != "" {
q.Set("owner", f.Owner)
params = append(params, fmt.Sprintf("owner:%q", f.Owner))
}
if f.Name != "" {
q.Set("name", f.Name)
params = append(params, fmt.Sprintf("name:%q", f.Name))
}
if f.Template != "" {
params = append(params, fmt.Sprintf("template:%q", f.Template))
}
if f.FilterQuery != "" {
// If custom stuff is added, just add it on here.
params = append(params, f.FilterQuery)
}
q := r.URL.Query()
q.Set("q", strings.Join(params, " "))
r.URL.RawQuery = q.Encode()
}
}