fix(pkg/api): combine marketplace query and category filters
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
369103f8dc
commit
6233664c5d
2 changed files with 47 additions and 8 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue