Skip to content

Commit

Permalink
feat: add pagination for list appeal and list grant (#60)
Browse files Browse the repository at this point in the history
* feat:add pagination for list appeal and list grant

* feat: add listusergrants

* chore: Add testing for listappeal and listgrant

* feat:add pagination test for list appeal and list grant

* chore: Add testing for offset

* chore: made test tables for grant_repo and appeal_repo

* chore: Update proto commit makefile

* chore: proto buf setup

---------

Co-authored-by: Lifosmin Simon <lifosmin.simon@Lifosmin-Simon.local>
  • Loading branch information
lifosmin and Lifosmin Simon authored Aug 29, 2023
1 parent ad1350e commit 9fb85ea
Show file tree
Hide file tree
Showing 12 changed files with 1,486 additions and 1,117 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 := "07884ade7983c5de0fde7d426f23574d82ce31a6"
PROTON_COMMIT := "8300157e4ac12a15f0dcc7f02b4b80c3c76be9fe"

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

Expand Down
5 changes: 5 additions & 0 deletions api/handler/v1beta1/appeal.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func (s *GRPCServer) ListUserAppeals(ctx context.Context, req *guardianv1beta1.L
if req.GetOrderBy() != nil {
filters.OrderBy = req.GetOrderBy()
}
filters.Offset = int(req.GetOffset())
filters.Size = int(req.GetSize())

appeals, err := s.listAppeals(ctx, filters)
if err != nil {
return nil, err
Expand All @@ -60,6 +63,8 @@ func (s *GRPCServer) ListAppeals(ctx context.Context, req *guardianv1beta1.ListA
ProviderURNs: req.GetProviderUrns(),
ResourceTypes: req.GetResourceTypes(),
ResourceURNs: req.GetResourceUrns(),
Size: int(req.GetSize()),
Offset: int(req.GetOffset()),
OrderBy: req.GetOrderBy(),
}
appeals, err := s.listAppeals(ctx, filters)
Expand Down
4 changes: 4 additions & 0 deletions api/handler/v1beta1/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func (s *GRPCServer) ListGrants(ctx context.Context, req *guardianv1beta1.ListGr
CreatedBy: req.GetCreatedBy(),
Owner: req.GetOwner(),
OrderBy: req.GetOrderBy(),
Size: int(req.GetSize()),
Offset: int(req.GetOffset()),
}
grants, err := s.listGrants(ctx, filter)
if err != nil {
Expand Down Expand Up @@ -54,6 +56,8 @@ func (s *GRPCServer) ListUserGrants(ctx context.Context, req *guardianv1beta1.Li
ResourceTypes: req.GetResourceTypes(),
ResourceURNs: req.GetResourceUrns(),
OrderBy: req.GetOrderBy(),
Size: int(req.GetSize()),
Offset: int(req.GetOffset()),
Owner: user,
}
grants, err := s.listGrants(ctx, filter)
Expand Down
2,404 changes: 1,289 additions & 1,115 deletions api/proto/gotocompany/guardian/v1beta1/guardian.pb.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins:
opt: paths=source_relative
- plugin: buf.build/grpc/go:v1.2.0
out: api/proto
opt: paths=source_relative,require_unimplemented_servers=true
opt: paths=source_relative,require_unimplemented_servers=true
- plugin: buf.build/grpc-ecosystem/gateway:v2.15.1
out: api/proto
opt: paths=source_relative
Expand Down
2 changes: 2 additions & 0 deletions domain/appeal.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,6 @@ type ListAppealsFilter struct {
ResourceTypes []string `mapstructure:"resource_types" validate:"omitempty,min=1"`
ResourceURNs []string `mapstructure:"resource_urns" 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"`
}
2 changes: 2 additions & 0 deletions domain/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ type ListGrantsFilter struct {
ExpirationDateGreaterThan time.Time
IsPermanent *bool
CreatedAtLte time.Time
Size int `mapstructure:"size" validate:"omitempty"`
Offset int `mapstructure:"offset" validate:"omitempty"`
}

type RevokeGrantsFilter struct {
Expand Down
6 changes: 6 additions & 0 deletions internal/store/postgres/appeal_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ func (r *AppealRepository) Find(ctx context.Context, filters *domain.ListAppeals
}

db := r.db.WithContext(ctx)
if filters.Size > 0 {
db = db.Limit(filters.Size)
}
if filters.Offset > 0 {
db = db.Offset(filters.Offset)
}
if filters.CreatedBy != "" {
db = db.Where(`"appeals"."created_by" = ?`, filters.CreatedBy)
}
Expand Down
27 changes: 27 additions & 0 deletions internal/store/postgres/appeal_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,33 @@ func (s *AppealRepositoryTestSuite) TestFind() {
s.Nil(actualError)
}
})

s.Run("Should return an array size and offset of n on success", func() {
testCases := []struct {
filters *domain.ListAppealsFilter
expectedArgs []driver.Value
expectedResult []*domain.Appeal
}{
{
filters: &domain.ListAppealsFilter{
Size: 1,
Offset: 0,
},
expectedResult: []*domain.Appeal{dummyAppeals[0]},
},
{
filters: &domain.ListAppealsFilter{
Offset: 1,
},
expectedResult: []*domain.Appeal{dummyAppeals[1]},
},
}
for _, tc := range testCases {
_, actualError := s.repository.Find(context.Background(), tc.filters)
s.Nil(actualError)
}

})
}

func (s *AppealRepositoryTestSuite) TestBulkUpsert() {
Expand Down
6 changes: 6 additions & 0 deletions internal/store/postgres/grant_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func NewGrantRepository(db *gorm.DB) *GrantRepository {

func (r *GrantRepository) List(ctx context.Context, filter domain.ListGrantsFilter) ([]domain.Grant, error) {
db := r.db.WithContext(ctx)
if filter.Size > 0 {
db = db.Limit(filter.Size)
}
if filter.Offset > 0 {
db = db.Offset(filter.Offset)
}
if filter.AccountIDs != nil {
db = db.Where(`"grants"."account_id" IN ?`, filter.AccountIDs)
}
Expand Down
27 changes: 27 additions & 0 deletions internal/store/postgres/grant_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package postgres_test

import (
"context"
"database/sql/driver"
"testing"
"time"

Expand Down Expand Up @@ -179,6 +180,32 @@ func (s *GrantRepositoryTestSuite) TestList() {
s.Error(err)
s.Nil(grants)
})
s.Run("Should return an array size and offset of n on success", func() {
testCases := []struct {
filters domain.ListGrantsFilter
expectedArgs []driver.Value
expectedResult []*domain.Grant
}{
{
filters: domain.ListGrantsFilter{
Size: 1,
Offset: 0,
},
expectedResult: []*domain.Grant{dummyGrants[0]},
},
{
filters: domain.ListGrantsFilter{
Offset: 1,
},
expectedResult: []*domain.Grant{dummyGrants[0]},
},
}
for _, tc := range testCases {
_, actualError := s.repository.List(context.Background(), tc.filters)
s.Nil(actualError)
}

})
}

func (s *GrantRepositoryTestSuite) TestGetByID() {
Expand Down
116 changes: 116 additions & 0 deletions third_party/OpenAPI/gotocompany/guardian/v1beta1/guardian.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,36 @@
"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"
},
{
"name": "q",
"in": "query",
"required": false,
"type": "string"
},
{
"name": "accountTypes",
"in": "query",
"required": false,
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "multi"
}
],
"tags": [
Expand Down Expand Up @@ -721,6 +751,26 @@
"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"
},
{
"name": "q",
"in": "query",
"required": false,
"type": "string"
}
],
"tags": [
Expand Down Expand Up @@ -989,6 +1039,36 @@
"type": "string"
},
"collectionFormat": "multi"
},
{
"name": "size",
"in": "query",
"required": false,
"type": "integer",
"format": "int64"
},
{
"name": "offset",
"in": "query",
"required": false,
"type": "integer",
"format": "int64"
},
{
"name": "q",
"in": "query",
"required": false,
"type": "string"
},
{
"name": "accountTypes",
"in": "query",
"required": false,
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "multi"
}
],
"tags": [
Expand Down Expand Up @@ -1213,6 +1293,26 @@
"type": "string"
},
"collectionFormat": "multi"
},
{
"name": "size",
"in": "query",
"required": false,
"type": "integer",
"format": "int64"
},
{
"name": "offset",
"in": "query",
"required": false,
"type": "integer",
"format": "int64"
},
{
"name": "q",
"in": "query",
"required": false,
"type": "string"
}
],
"tags": [
Expand Down Expand Up @@ -2668,6 +2768,10 @@
"type": "object",
"$ref": "#/definitions/v1beta1Appeal"
}
},
"total": {
"type": "integer",
"format": "int32"
}
}
},
Expand Down Expand Up @@ -2696,6 +2800,10 @@
"type": "object",
"$ref": "#/definitions/v1beta1Grant"
}
},
"total": {
"type": "integer",
"format": "int32"
}
}
},
Expand Down Expand Up @@ -2756,6 +2864,10 @@
"type": "object",
"$ref": "#/definitions/v1beta1Appeal"
}
},
"total": {
"type": "integer",
"format": "int32"
}
}
},
Expand Down Expand Up @@ -2784,6 +2896,10 @@
"type": "object",
"$ref": "#/definitions/v1beta1Grant"
}
},
"total": {
"type": "integer",
"format": "int32"
}
}
},
Expand Down

0 comments on commit 9fb85ea

Please sign in to comment.