Skip to content

Commit

Permalink
feat: add pagination support in ListUserApprovals api (#6)
Browse files Browse the repository at this point in the history
- add pagination support in ListUserApprovals api
- add pagination support in ListApprovals api
- update buf version
  • Loading branch information
bsushmith authored Mar 17, 2023
1 parent 536bd55 commit 31b0908
Show file tree
Hide file tree
Showing 10 changed files with 1,421 additions and 1,314 deletions.
4 changes: 2 additions & 2 deletions 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 := "561323badd70e4321bf868b667d8da3d90fb208b"
PROTON_COMMIT := "246242731323907ffc7fb7fe24723b5b932a186c"

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

Expand Down Expand Up @@ -74,4 +74,4 @@ setup:
go get google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1.0
go get github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@v2.5.0
go get github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@v2.5.0
go get github.com/bufbuild/buf/cmd/buf@v0.54.1
go get github.com/bufbuild/buf/cmd/buf@v1.15.1
4 changes: 4 additions & 0 deletions api/handler/v1beta1/approval.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func (s *GRPCServer) ListUserApprovals(ctx context.Context, req *guardianv1beta1
CreatedBy: user,
Statuses: req.GetStatuses(),
OrderBy: req.GetOrderBy(),
Size: int(req.GetSize()),
Offset: int(req.GetOffset()),
})
if err != nil {
return nil, err
Expand All @@ -38,6 +40,8 @@ func (s *GRPCServer) ListApprovals(ctx context.Context, req *guardianv1beta1.Lis
CreatedBy: req.GetCreatedBy(),
Statuses: req.GetStatuses(),
OrderBy: req.GetOrderBy(),
Size: int(req.GetSize()),
Offset: int(req.GetOffset()),
})
if err != nil {
return nil, err
Expand Down
2,654 changes: 1,348 additions & 1,306 deletions api/proto/gotocompany/guardian/v1beta1/guardian.pb.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ plugins:
opt: paths=source_relative
- plugin: buf.build/grpc-ecosystem/openapiv2:v2.15.1
out: third_party/OpenAPI
# See https://github.com/bufbuild/protoc-gen-validate/issues/523
- remote: buf.build/jirkad/plugins/protoc-gen-validate:v0.6.7
out: api/proto
opt:
- paths=source_relative
- lang=go
2 changes: 2 additions & 0 deletions domain/approval.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ type ListApprovalsFilter struct {
CreatedBy string `mapstructure:"created_by" validate:"omitempty,required"`
Statuses []string `mapstructure:"statuses" validate:"omitempty,min=1"`
OrderBy []string `mapstructure:"order_by" validate:"omitempty,min=1"`
Size int `mapstructure:"size" validate:"omitempty"`
Offset int `mapstructure:"offset" validate:"omitempty"`
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/MakeNowJust/heredoc v1.0.0
github.com/antonmedv/expr v1.9.0
github.com/envoyproxy/protoc-gen-validate v0.6.7
github.com/go-playground/validator/v10 v10.4.1
github.com/golang-migrate/migrate/v4 v4.15.2
github.com/google/go-cmp v0.5.9
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ github.com/envoyproxy/go-control-plane v0.10.1/go.mod h1:AY7fTTXNdv/aJ2O5jwpxAPO
github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/envoyproxy/protoc-gen-validate v0.6.2/go.mod h1:2t7qjJNvHPx8IjnBOzl9E9/baC+qXE/TeeyBRzgJDws=
github.com/envoyproxy/protoc-gen-validate v0.6.7 h1:qcZcULcd/abmQg6dwigimCNEyi4gg31M/xaciQlDml8=
github.com/envoyproxy/protoc-gen-validate v0.6.7/go.mod h1:dyJXwwfPK2VSqiB9Klm1J6romD608Ba7Hij42vrOBCo=
github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch v4.11.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
Expand Down
8 changes: 8 additions & 0 deletions internal/store/postgres/approval_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ func (r *ApprovalRepository) ListApprovals(ctx context.Context, conditions *doma
})
}

if conditions.Size > 0 {
db = db.Limit(conditions.Size)
}

if conditions.Offset > 0 {
db = db.Offset(conditions.Offset)
}

var models []*model.Approval
if err := db.Find(&models).Error; err != nil {
return nil, err
Expand Down
27 changes: 21 additions & 6 deletions internal/store/postgres/approval_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ func (s *ApprovalRepositoryTestSuite) TestListApprovals() {
Status: "test-status-1",
PolicyID: "test-policy-id",
PolicyVersion: 1,

Appeal: s.dummyAppeal,
Appeal: s.dummyAppeal,
},
{
Name: "test-approval-name-2",
Expand All @@ -136,6 +135,15 @@ func (s *ApprovalRepositoryTestSuite) TestListApprovals() {
PolicyVersion: 1,
Appeal: s.dummyAppeal,
},
{
Name: "test-approval-name-3",
Index: 1,
AppealID: s.dummyAppeal.ID,
Status: "test-status-1",
PolicyID: "test-policy-id",
PolicyVersion: 1,
Appeal: s.dummyAppeal,
},
}

err := s.repository.BulkInsert(context.Background(), dummyApprovals)
Expand All @@ -152,20 +160,27 @@ func (s *ApprovalRepositoryTestSuite) TestListApprovals() {
AppealID: s.dummyAppeal.ID,
Email: "approver2@email.com",
},
{
ApprovalID: dummyApprovals[2].ID,
AppealID: s.dummyAppeal.ID,
Email: "approver1@email.com",
},
}

ctx := context.Background()
err = s.repository.AddApprover(ctx, dummyApprover[0])
s.Require().NoError(err)
err = s.repository.AddApprover(ctx, dummyApprover[1])
s.Require().NoError(err)

for _, ap := range dummyApprover {
err = s.repository.AddApprover(ctx, ap)
s.Require().NoError(err)
}

s.Run("should return list of approvals on success", func() {
approvals, err := s.repository.ListApprovals(context.Background(), &domain.ListApprovalsFilter{
AccountID: s.dummyAppeal.AccountID,
CreatedBy: dummyApprover[0].Email,
Statuses: []string{"test-status-1"},
OrderBy: []string{"status", "updated_at:desc", "created_at"},
Size: 1,
})

s.NoError(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,20 @@
"in": "query",
"required": false,
"type": "string"
},
{
"name": "size",
"in": "query",
"required": false,
"type": "integer",
"format": "int64"
},
{
"name": "offset",
"in": "query",
"required": false,
"type": "integer",
"format": "int64"
}
],
"tags": [
Expand Down Expand Up @@ -989,6 +1003,20 @@
"in": "query",
"required": false,
"type": "string"
},
{
"name": "size",
"in": "query",
"required": false,
"type": "integer",
"format": "int64"
},
{
"name": "offset",
"in": "query",
"required": false,
"type": "integer",
"format": "int64"
}
],
"tags": [
Expand Down

0 comments on commit 31b0908

Please sign in to comment.