From b59e15f928214861b85ca9ab6bd2089531a7cbd3 Mon Sep 17 00:00:00 2001 From: thxCode Date: Tue, 15 Aug 2023 14:36:59 +0800 Subject: [PATCH] fix: failed query templates with catalog id Signed-off-by: thxCode --- pkg/apis/template/basic.go | 8 ++++++++ pkg/apis/template/basic_view.go | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/pkg/apis/template/basic.go b/pkg/apis/template/basic.go index cb519b8ef..0c5eacaa9 100644 --- a/pkg/apis/template/basic.go +++ b/pkg/apis/template/basic.go @@ -83,6 +83,10 @@ var ( func (h Handler) CollectionGet(req CollectionGetRequest) (CollectionGetResponse, int, error) { query := h.modelClient.Templates().Query() + if len(req.CatalogIDs) != 0 { + query.Where(template.CatalogIDIn(req.CatalogIDs...)) + } + if queries, ok := req.Querying(queryFields); ok { query.Where(queries) } @@ -122,6 +126,8 @@ func (h Handler) CollectionGet(req CollectionGetRequest) (CollectionGetResponse, switch dm.Type { case datamessage.EventCreate, datamessage.EventUpdate: entities, err := query.Clone(). + // Must extract catalog ID. + Select(template.FieldCatalogID). Where(template.IDIn(dm.Data...)). Unique(false). All(stream) @@ -172,6 +178,8 @@ func (h Handler) CollectionGet(req CollectionGetRequest) (CollectionGetResponse, } entities, err := query. + // Must extract catalog ID. + Select(template.FieldCatalogID). Unique(false). All(req.Context) if err != nil { diff --git a/pkg/apis/template/basic_view.go b/pkg/apis/template/basic_view.go index 6a59a0f9f..b42fc82a3 100644 --- a/pkg/apis/template/basic_view.go +++ b/pkg/apis/template/basic_view.go @@ -1,6 +1,7 @@ package template import ( + "errors" "fmt" "github.com/hashicorp/go-getter" @@ -9,6 +10,7 @@ import ( "github.com/seal-io/seal/pkg/dao/model" "github.com/seal-io/seal/pkg/dao/model/predicate" "github.com/seal-io/seal/pkg/dao/model/template" + "github.com/seal-io/seal/pkg/dao/types/object" "github.com/seal-io/seal/utils/validation" ) @@ -68,12 +70,30 @@ type ( predicate.Template, template.OrderOption, ] `query:",inline"` + CatalogIDs []object.ID `query:"catalogID,omitempty"` + Stream *runtime.RequestUnidiStream } CollectionGetResponse = []*model.TemplateOutput ) +func (r *CollectionGetRequest) Validate() error { + if err := r.TemplateQueryInputs.Validate(); err != nil { + return err + } + + if r.CatalogIDs != nil { + for i := range r.CatalogIDs { + if !r.CatalogIDs[i].Valid() { + return errors.New("invalid catalog id") + } + } + } + + return nil +} + func (r *CollectionGetRequest) SetStream(stream runtime.RequestUnidiStream) { r.Stream = &stream }