From 6233664c5d65aadcaba77277c62c012fbdc30d6c Mon Sep 17 00:00:00 2001 From: Virgil Date: Wed, 1 Apr 2026 06:40:27 +0000 Subject: [PATCH] fix(pkg/api): combine marketplace query and category filters Co-Authored-By: Virgil --- pkg/api/provider.go | 28 ++++++++++++++++++++-------- pkg/api/provider_handlers_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/pkg/api/provider.go b/pkg/api/provider.go index 5487485..0a478f0 100644 --- a/pkg/api/provider.go +++ b/pkg/api/provider.go @@ -11,6 +11,7 @@ import ( "encoding/hex" "net/http" "net/url" + "strings" "dappco.re/go/core/api" "dappco.re/go/core/api/pkg/provider" @@ -117,7 +118,7 @@ func (p *ScmProvider) Describe() []api.RouteDescription { Method: "GET", Path: "/marketplace", Summary: "List available providers", - Description: "Returns all providers from the marketplace index, optionally filtered by query or category.", + Description: "Returns all providers from the marketplace index, optionally filtered by query and category.", Tags: []string{"scm", "marketplace"}, }, { @@ -229,14 +230,18 @@ func (p *ScmProvider) listMarketplace(c *gin.Context) { query := c.Query("q") category := c.Query("category") - var modules []marketplace.Module - switch { - case query != "": - modules = p.index.Search(query) - case category != "": + modules := p.index.Modules + if category != "" { modules = p.index.ByCategory(category) - default: - modules = p.index.Modules + } + if query != "" { + filtered := make([]marketplace.Module, 0, len(modules)) + for _, mod := range modules { + if moduleMatchesQuery(mod, query) { + filtered = append(filtered, mod) + } + } + modules = filtered } if modules == nil { @@ -541,3 +546,10 @@ func normaliseMarketplaceCode(raw string) (string, error) { return agentci.ValidatePathElement(decoded) } + +func moduleMatchesQuery(mod marketplace.Module, query string) bool { + q := strings.ToLower(query) + return strings.Contains(strings.ToLower(mod.Code), q) || + strings.Contains(strings.ToLower(mod.Name), q) || + strings.Contains(strings.ToLower(mod.Category), q) +} diff --git a/pkg/api/provider_handlers_test.go b/pkg/api/provider_handlers_test.go index bd14b84..997e6bd 100644 --- a/pkg/api/provider_handlers_test.go +++ b/pkg/api/provider_handlers_test.go @@ -42,6 +42,33 @@ func TestScmProvider_ListMarketplace_Category_Good(t *testing.T) { assert.Equal(t, "lint", resp.Data[0].Code) } +func TestScmProvider_ListMarketplace_QueryAndCategory_Good(t *testing.T) { + idx := &marketplace.Index{ + Version: 1, + Modules: []marketplace.Module{ + {Code: "analytics", Name: "Analytics", Category: "product"}, + {Code: "toolkit", Name: "Tool Kit", Category: "tool"}, + {Code: "toolbox", Name: "Tool Box", Category: "tool"}, + }, + Categories: []string{"product", "tool"}, + } + p := scmapi.NewProvider(idx, nil, nil, nil) + + r := setupRouter(p) + w := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/api/v1/scm/marketplace?q=tool&category=tool", nil) + r.ServeHTTP(w, req) + + assert.Equal(t, http.StatusOK, w.Code) + + var resp goapi.Response[[]marketplace.Module] + err := json.Unmarshal(w.Body.Bytes(), &resp) + require.NoError(t, err) + assert.Len(t, resp.Data, 2) + assert.Equal(t, "toolkit", resp.Data[0].Code) + assert.Equal(t, "toolbox", resp.Data[1].Code) +} + // -- Marketplace: nil search results ------------------------------------------ func TestScmProvider_ListMarketplace_SearchNoResults_Good(t *testing.T) {