Skip to content

Commit

Permalink
fix: add order_by in list resource request and also default order by …
Browse files Browse the repository at this point in the history
…on created_at for resources when offset is set (#123)

* fix: add default order by on created_at for resources when offset is set

* fix: add order_by in list resource request

* fix: test case

* fix: comments

---------

Co-authored-by: utsav14nov <[email protected]>
  • Loading branch information
utsav14nov and utsav14nov authored Feb 16, 2024
1 parent 607e725 commit 0f87a6f
Show file tree
Hide file tree
Showing 7 changed files with 1,260 additions and 1,198 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 := "1c64ba440874a73eddba40c77c79ff30a11347de"
PROTON_COMMIT := "3679c1e5698b5c81aa338bfbf677b81deb5f8600"

.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 @@ -38,6 +38,7 @@ func (s *GRPCServer) ListResources(ctx context.Context, req *guardianv1beta1.Lis
Details: details,
Size: req.GetSize(),
Offset: req.GetOffset(),
OrderBy: req.GetOrderBy(),
}

resources, total, err := s.listResources(ctx, filter)
Expand Down
2,397 changes: 1,203 additions & 1,194 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 @@ -41,6 +41,7 @@ type ListResourcesFilter struct {
Details map[string]string `mapstructure:"details"`
Size uint32 `mapstructure:"size" validate:"omitempty"`
Offset uint32 `mapstructure:"offset" validate:"omitempty"`
OrderBy []string `mapstructure:"order_by" validate:"omitempty"`
}

type Resources []*Resource
Expand Down
24 changes: 24 additions & 0 deletions internal/store/postgres/resource_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import (
"gorm.io/gorm"
)

var (
resourcesDefaultSort = []string{
"created_at:desc",
}
)

// ResourceRepository talks to the store/database to read/insert data
type ResourceRepository struct {
db *gorm.DB
Expand Down Expand Up @@ -89,10 +95,28 @@ func applyResourceFilter(db *gorm.DB, filter domain.ListResourcesFilter) *gorm.D
if filter.Size > 0 {
db = db.Limit(int(filter.Size))
}

if filter.Offset > 0 {
db = db.Offset(int(filter.Offset))
}

var sortOrder []string

if filter.Offset >= 0 {
sortOrder = resourcesDefaultSort
}

if filter.OrderBy != nil {
sortOrder = filter.OrderBy
}

if len(sortOrder) != 0 {
db = addOrderByClause(db, sortOrder, addOrderByClauseOptions{
statusColumnName: "",
statusesOrder: []string{},
})
}

for path, v := range filter.Details {
pathArr := "{" + strings.Join(strings.Split(path, "."), ",") + "}"
db = db.Where(`"details" #>> ? = ?`, pathArr, v)
Expand Down
21 changes: 19 additions & 2 deletions internal/store/postgres/resource_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@ func (s *ResourceRepositoryTestSuite) TestFind() {
Details: map[string]interface{}{
"foo": "bar",
},
CreatedAt: time.Now(),
},
{
ProviderType: s.dummyProvider.Type,
ProviderURN: s.dummyProvider.URN,
Type: "test_type",
URN: "test_urn_2",
Name: "test_name_2",
CreatedAt: time.Now().Add(10 * time.Minute),
},
}
err := s.repository.BulkUpsert(context.Background(), dummyResources)
Expand Down Expand Up @@ -157,16 +159,31 @@ func (s *ResourceRepositoryTestSuite) TestFind() {
name: "filter by size and offset",
filters: domain.ListResourcesFilter{
Size: 1,
Offset: 1,
Offset: 0,
},
expectedResult: []*domain.Resource{dummyResources[1]},
},
{
name: "filter by size and offset 1",
filters: domain.ListResourcesFilter{
Size: 1,
Offset: 1,
},
expectedResult: []*domain.Resource{dummyResources[0]},
},
{
name: "filter by size only",
filters: domain.ListResourcesFilter{
Size: 1,
},
expectedResult: []*domain.Resource{dummyResources[0]},
expectedResult: []*domain.Resource{dummyResources[1]},
},
{
name: "Order by created at desc",
filters: domain.ListResourcesFilter{
OrderBy: []string{"created_at:desc"},
},
expectedResult: []*domain.Resource{dummyResources[1], dummyResources[0]},
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1899,6 +1899,16 @@
"required": false,
"type": "integer",
"format": "int64"
},
{
"name": "orderBy",
"in": "query",
"required": false,
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "multi"
}
],
"tags": [
Expand Down Expand Up @@ -2297,7 +2307,7 @@
"NULL_VALUE"
],
"default": "NULL_VALUE",
"description": "`NullValue` is a singleton enumeration to represent the null value for the\n`Value` type union.\n\n The JSON representation for `NullValue` is JSON `null`.\n\n - NULL_VALUE: Null value."
"description": "`NullValue` is a singleton enumeration to represent the null value for the\n`Value` type union.\n\nThe JSON representation for `NullValue` is JSON `null`.\n\n - NULL_VALUE: Null value."
},
"rpcStatus": {
"type": "object",
Expand Down

0 comments on commit 0f87a6f

Please sign in to comment.