diff --git a/pkg/util/request/request.go b/pkg/util/request/request.go index 1d17ba2..f239f79 100644 --- a/pkg/util/request/request.go +++ b/pkg/util/request/request.go @@ -1,6 +1,7 @@ package request import ( + "github.com/Masterminds/squirrel" "github.com/onepanelio/core/pkg/util/request/pagination" "github.com/onepanelio/core/pkg/util/request/sort" ) @@ -14,5 +15,22 @@ type Request struct { // HasSorting returns true if there are any sorting criteria in the request func (r *Request) HasSorting() bool { - return r.Sort != nil && len(r.Sort.Properties) > 0 + return r != nil && + r.Sort != nil && + len(r.Sort.Properties) > 0 +} + +// HasFilter returns true if there is any filtering criteria in the request +func (r *Request) HasFilter() bool { + return r != nil && + r.Filter != nil +} + +// ApplyPaginationToSelect applies the pagination to the selectBuilder, if there is a pagination. +func (r *Request) ApplyPaginationToSelect(sb *squirrel.SelectBuilder) *squirrel.SelectBuilder { + if r == nil || r.Pagination == nil { + return sb + } + + return r.Pagination.ApplyToSelect(sb) } diff --git a/pkg/workflow_execution.go b/pkg/workflow_execution.go index 9b85e32..48bf9a1 100644 --- a/pkg/workflow_execution.go +++ b/pkg/workflow_execution.go @@ -75,7 +75,7 @@ func (wf *WorkflowExecutionFilter) GetLabels() []*Label { } func applyWorkflowExecutionFilter(sb sq.SelectBuilder, request *request.Request) (sq.SelectBuilder, error) { - if request.Filter == nil { + if !request.HasFilter() { return sb, nil } @@ -865,8 +865,7 @@ func (c *Client) ListWorkflowExecutions(namespace, workflowTemplateUID, workflow return nil, err } - sb = *request.Pagination.ApplyToSelect(&sb) - + sb = *request.ApplyPaginationToSelect(&sb) if err := c.DB.Selectx(&workflows, sb); err != nil { return nil, err } diff --git a/pkg/workflow_template.go b/pkg/workflow_template.go index d2eb90c..ff7ed4b 100644 --- a/pkg/workflow_template.go +++ b/pkg/workflow_template.go @@ -171,7 +171,7 @@ func (c *Client) createWorkflowTemplate(namespace string, workflowTemplate *Work workflowTemplateVersion := &WorkflowTemplateVersion{ WorkflowTemplate: workflowTemplate, Manifest: workflowTemplate.Manifest, - Labels: workflowTemplate.Labels, + Labels: workflowTemplate.Labels, } err = createWorkflowTemplateVersionDB(tx, workflowTemplateVersion, params) if err != nil { @@ -422,7 +422,7 @@ func (c *Client) selectWorkflowTemplatesQuery(namespace string, request *request OrderBy("wt.created_at DESC") sb = applyLabelSelectQuery(sb, request) - sb = *request.Pagination.ApplyToSelect(&sb) + sb = *request.ApplyPaginationToSelect(&sb) return } diff --git a/pkg/workspace.go b/pkg/workspace.go index 729d1b8..4b840cb 100644 --- a/pkg/workspace.go +++ b/pkg/workspace.go @@ -32,7 +32,7 @@ func (wf *WorkspaceFilter) GetLabels() []*Label { } func applyWorkspaceFilter(sb sq.SelectBuilder, request *request.Request) (sq.SelectBuilder, error) { - if request.Filter == nil { + if !request.HasFilter() { return sb, nil } @@ -711,7 +711,7 @@ func (c *Client) ListWorkspaces(namespace string, request *request.Request) (wor return nil, err } - sb = *request.Pagination.ApplyToSelect(&sb) + sb = *request.ApplyPaginationToSelect(&sb) if err := c.DB.Selectx(&workspaces, sb); err != nil { return nil, err diff --git a/pkg/workspace_template.go b/pkg/workspace_template.go index 4546353..c33c465 100644 --- a/pkg/workspace_template.go +++ b/pkg/workspace_template.go @@ -89,7 +89,7 @@ func (wt *WorkspaceTemplateFilter) GetLabels() []*Label { } func applyWorkspaceTemplateFilter(sb sq.SelectBuilder, request *request.Request) (sq.SelectBuilder, error) { - if request.Filter == nil { + if !request.HasFilter() { return sb, nil } @@ -1185,7 +1185,7 @@ func (c *Client) ListWorkspaceTemplates(namespace string, request *request.Reque return nil, err } - sb = *request.Pagination.ApplyToSelect(&sb) + sb = *request.ApplyPaginationToSelect(&sb) err = c.DB.Selectx(&workspaceTemplates, sb)