Skip to content

Commit

Permalink
feat(resource): support resource search by global urn (#141)
Browse files Browse the repository at this point in the history
* feat(resource): support resource search by global urn

* chore: use ILIKE for incasesensitive search
  • Loading branch information
rahmatrhd authored Mar 25, 2024
1 parent f33ab8a commit 2020b15
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 3 additions & 2 deletions internal/store/postgres/resource_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ func applyResourceFilter(db *gorm.DB, filter domain.ListResourcesFilter) *gorm.D
// NOTE: avoid adding conditions before this grouped where clause.
// Otherwise, it will be wrapped in parentheses and the query will be invalid.
db = db.Where(db.
Where(`LOWER("urn") LIKE ?`, fmt.Sprintf("%%%s%%", strings.ToLower(filter.Q))).
Or(`LOWER("name") LIKE ?`, fmt.Sprintf("%%%s%%", strings.ToLower(filter.Q))),
Where(`"urn" ILIKE ?`, fmt.Sprintf("%%%s%%", filter.Q)).
Or(`"name" ILIKE ?`, fmt.Sprintf("%%%s%%", filter.Q)).
Or(`"global_urn" ILIKE ?`, fmt.Sprintf("%%%s%%", filter.Q)),
)
}
if filter.IDs != nil {
Expand Down
9 changes: 8 additions & 1 deletion internal/store/postgres/resource_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (s *ResourceRepositoryTestSuite) TestFind() {
"foo": "bar",
},
CreatedAt: time.Now(),
GlobalURN: "global_urn_1",
},
{
ProviderType: s.dummyProvider.Type,
Expand All @@ -89,6 +90,7 @@ func (s *ResourceRepositoryTestSuite) TestFind() {
URN: "test_urn_2",
Name: "test_name_2",
CreatedAt: time.Now().Add(10 * time.Minute),
GlobalURN: "global_urn_2",
},
}
err := s.repository.BulkUpsert(context.Background(), dummyResources)
Expand All @@ -105,12 +107,17 @@ func (s *ResourceRepositoryTestSuite) TestFind() {
expectedResult: dummyResources,
},
{
name: "filter by ids",
name: "search by urn",
filters: domain.ListResourcesFilter{
Q: "test_urn_1",
},
expectedResult: []*domain.Resource{dummyResources[0]},
},
{
name: "search by global urn",
filters: domain.ListResourcesFilter{Q: "GLOBAL_URN_2"},
expectedResult: []*domain.Resource{dummyResources[1]},
},
{
name: "filter by ids",
filters: domain.ListResourcesFilter{
Expand Down

0 comments on commit 2020b15

Please sign in to comment.