Skip to content

Commit

Permalink
feat(resource): introduce search query in list resources API
Browse files Browse the repository at this point in the history
  • Loading branch information
rahmatrhd committed Feb 28, 2024
1 parent e8bdf03 commit 4ab6aef
Show file tree
Hide file tree
Showing 8 changed files with 1,238 additions and 1,203 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ COMMIT := $(shell git rev-parse --short HEAD)
TAG := "$(shell git rev-list --tags --max-count=1)"
VERSION := "$(shell git describe --tags ${TAG})-next"
BUILD_DIR=dist
PROTON_COMMIT := "3679c1e5698b5c81aa338bfbf677b81deb5f8600"
PROTON_COMMIT := "799e9b4263c29555a5d90fb8fd944f7ae6326431"

.PHONY: all build clean test tidy vet proto setup format generate

Expand Down
1 change: 1 addition & 0 deletions api/handler/v1beta1/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (s *GRPCServer) ListResources(ctx context.Context, req *guardianv1beta1.Lis
Size: req.GetSize(),
Offset: req.GetOffset(),
OrderBy: req.GetOrderBy(),
Q: req.GetQ(),
}

resources, total, err := s.listResources(ctx, filter)
Expand Down
2 changes: 2 additions & 0 deletions api/handler/v1beta1/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func (s *GrpcHandlersSuite) TestListResources() {
"key1:value1",
"key2.key3:value2",
},
Q: "test-query",
}

expectedDetails := map[string]interface{}{
Expand Down Expand Up @@ -69,6 +70,7 @@ func (s *GrpcHandlersSuite) TestListResources() {
"key1": "value1",
"key2.key3": "value2",
},
Q: "test-query",
}
dummyResources := []*domain.Resource{
{
Expand Down
2,413 changes: 1,211 additions & 1,202 deletions api/proto/gotocompany/guardian/v1beta1/guardian.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions domain/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type ListResourcesFilter struct {
Size uint32 `mapstructure:"size" validate:"omitempty"`
Offset uint32 `mapstructure:"offset" validate:"omitempty"`
OrderBy []string `mapstructure:"order_by" validate:"omitempty"`
Q string `mapstructure:"q" validate:"omitempty"`
}

type Resources []*Resource
Expand Down
9 changes: 9 additions & 0 deletions internal/store/postgres/resource_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package postgres

import (
"context"
"fmt"
"strings"

"github.com/google/uuid"
Expand Down Expand Up @@ -64,6 +65,14 @@ func (r *ResourceRepository) GetResourcesTotalCount(ctx context.Context, filter
}

func applyResourceFilter(db *gorm.DB, filter domain.ListResourcesFilter) *gorm.DB {
if filter.Q != "" {
// 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))),
)
}
if filter.IDs != nil {
db = db.Where(filter.IDs)
}
Expand Down
7 changes: 7 additions & 0 deletions internal/store/postgres/resource_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ func (s *ResourceRepositoryTestSuite) TestFind() {
filters: domain.ListResourcesFilter{},
expectedResult: dummyResources,
},
{
name: "filter by ids",
filters: domain.ListResourcesFilter{
Q: "test_urn_1",
},
expectedResult: []*domain.Resource{dummyResources[0]},
},
{
name: "filter by ids",
filters: domain.ListResourcesFilter{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1909,6 +1909,12 @@
"type": "string"
},
"collectionFormat": "multi"
},
{
"name": "q",
"in": "query",
"required": false,
"type": "string"
}
],
"tags": [
Expand Down

0 comments on commit 4ab6aef

Please sign in to comment.