Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5008161

Browse files
committed
chore: add fuzzy name search for templates
1 parent 2c13797 commit 5008161

File tree

6 files changed

+32
-4
lines changed

6 files changed

+32
-4
lines changed

coderd/database/dbmem/dbmem.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9647,6 +9647,11 @@ func (q *FakeQuerier) GetAuthorizedTemplates(ctx context.Context, arg database.G
96479647
if arg.Deprecated.Valid && arg.Deprecated.Bool == (template.Deprecated != "") {
96489648
continue
96499649
}
9650+
if arg.FuzzyName != "" {
9651+
if !strings.Contains(strings.ToLower(template.Name), strings.ToLower(arg.FuzzyName)) {
9652+
continue
9653+
}
9654+
}
96509655

96519656
if len(arg.IDs) > 0 {
96529657
match := false

coderd/database/modelqueries.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func (q *sqlQuerier) GetAuthorizedTemplates(ctx context.Context, arg GetTemplate
7676
arg.Deleted,
7777
arg.OrganizationID,
7878
arg.ExactName,
79+
arg.FuzzyName,
7980
pq.Array(arg.IDs),
8081
arg.Deprecated,
8182
)

coderd/database/queries.sql.go

Lines changed: 12 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/templates.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ WHERE
2828
LOWER("name") = LOWER(@exact_name)
2929
ELSE true
3030
END
31+
-- Filter by name, matching on substring
32+
AND CASE
33+
WHEN @fuzzy_name :: text != '' THEN
34+
lower(name) ILIKE '%' || lower(@fuzzy_name) || '%'
35+
ELSE true
36+
END
3137
-- Filter by ids
3238
AND CASE
3339
WHEN array_length(@ids :: uuid[], 1) > 0 THEN

coderd/searchquery/search.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ func Templates(ctx context.Context, db database.Store, query string) (database.G
198198

199199
parser := httpapi.NewQueryParamParser()
200200
filter := database.GetTemplatesWithFilterParams{
201+
FuzzyName: parser.String(values, "", "name"),
201202
Deleted: parser.Boolean(values, false, "deleted"),
202203
ExactName: parser.String(values, "", "exact_name"),
203204
IDs: parser.UUIDs(values, []uuid.UUID{}, "ids"),

coderd/searchquery/search_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,13 @@ func TestSearchTemplates(t *testing.T) {
469469
Query: "",
470470
Expected: database.GetTemplatesWithFilterParams{},
471471
},
472+
{
473+
Name: "OnlyName",
474+
Query: "foobar",
475+
Expected: database.GetTemplatesWithFilterParams{
476+
FuzzyName: "foobar",
477+
},
478+
},
472479
}
473480

474481
for _, c := range testCases {

0 commit comments

Comments
 (0)