Skip to content

Commit

Permalink
chore: resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Lifosmin Simon authored and Lifosmin Simon committed Aug 30, 2023
1 parent cf917ec commit 93cc1ba
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 29 deletions.
Binary file added __debug_bin1379664372
Binary file not shown.
26 changes: 18 additions & 8 deletions api/handler/v1beta1/appeal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
guardianv1beta1 "github.com/goto/guardian/api/proto/gotocompany/guardian/v1beta1"
"github.com/goto/guardian/core/appeal"
"github.com/goto/guardian/domain"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand Down Expand Up @@ -177,14 +178,23 @@ func (s *GRPCServer) CancelAppeal(ctx context.Context, req *guardianv1beta1.Canc
}

func (s *GRPCServer) listAppeals(ctx context.Context, filters *domain.ListAppealsFilter) ([]*guardianv1beta1.Appeal, int64, error) {
appeals, err := s.appealService.Find(ctx, filters)
if err != nil {
return nil, 0, status.Errorf(codes.Internal, "failed to get appeal list: %s", err)
}

total, err := s.appealService.GetAppealsTotalCount(ctx, filters)
if err != nil {
return nil, 0, status.Errorf(codes.Internal, "failed to get appeal count list: %s", err)
eg, ctx := errgroup.WithContext(ctx)
var appeals []*domain.Appeal
var total int64

eg.Go(func() error {
appealRecords, err := s.appealService.Find(ctx, filters)
appeals = appealRecords
return err
})
eg.Go(func() error {
totalRecord, err := s.appealService.GetAppealsTotalCount(ctx, filters)
total = totalRecord
return err
})

if err := eg.Wait(); err != nil {
return nil, 0, err
}

appealProtos := []*guardianv1beta1.Appeal{}
Expand Down
24 changes: 17 additions & 7 deletions api/handler/v1beta1/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/goto/guardian/core/grant"
"github.com/goto/guardian/core/provider"
"github.com/goto/guardian/domain"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand Down Expand Up @@ -175,14 +176,23 @@ func (s *GRPCServer) RevokeGrants(ctx context.Context, req *guardianv1beta1.Revo
}

func (s *GRPCServer) listGrants(ctx context.Context, filter domain.ListGrantsFilter) ([]*guardianv1beta1.Grant, int64, error) {
grants, err := s.grantService.List(ctx, filter)
if err != nil {
return nil, 0, status.Errorf(codes.Internal, "failed to list grants: %v", err)
}
eg, ctx := errgroup.WithContext(ctx)
var grants []domain.Grant
var total int64

eg.Go(func() error {
grantRecords, err := s.grantService.List(ctx, filter)
grants = grantRecords
return err
})
eg.Go(func() error {
totalRecord, err := s.grantService.GetGrantsTotalCount(ctx, filter)
total = totalRecord
return err
})

total, err := s.grantService.GetGrantsTotalCount(ctx, filter)
if err != nil {
return nil, 0, status.Errorf(codes.Internal, "failed to get Grant count list: %s", err)
if err := eg.Wait(); err != nil {
return nil, 0, err
}

var grantProtos []*guardianv1beta1.Grant
Expand Down
2 changes: 1 addition & 1 deletion buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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
# 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:
Expand Down
8 changes: 2 additions & 6 deletions internal/store/postgres/appeal_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,10 @@ func (r *AppealRepository) Find(ctx context.Context, filters *domain.ListAppeals
func (r *AppealRepository) GetAppealsTotalCount(ctx context.Context, filter *domain.ListAppealsFilter) (int64, error) {
db := r.db.WithContext(ctx)
db = applyAppealFilter(db, filter)

var count int64
if err := db.Model(&model.Appeal{}).Count(&count).Error; err != nil {
return 0, err
}
err := db.Model(&model.Appeal{}).Count(&count).Error

return count, nil
return count, err
}

// BulkUpsert new record to database
Expand Down Expand Up @@ -153,7 +150,6 @@ func (r *AppealRepository) Update(ctx context.Context, a *domain.Appeal) error {
}

func applyAppealFilter(db *gorm.DB, filters *domain.ListAppealsFilter) *gorm.DB {

db = db.Joins("JOIN resources ON appeals.resource_id = resources.id")
if filters.Q != "" {
// NOTE: avoid adding conditions before this grouped where clause.
Expand Down
1 change: 0 additions & 1 deletion internal/store/postgres/appeal_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ func (s *AppealRepositoryTestSuite) TestGetByID() {
}

func (s *AppealRepositoryTestSuite) TestGetAppealsTotalCount() {

s.Run("should return 0", func() {
_, actualError := s.repository.GetAppealsTotalCount(context.Background(), &domain.ListAppealsFilter{})
s.Nil(actualError)
Expand Down
7 changes: 2 additions & 5 deletions internal/store/postgres/grant_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,10 @@ func (r *GrantRepository) List(ctx context.Context, filter domain.ListGrantsFilt
func (r *GrantRepository) GetGrantsTotalCount(ctx context.Context, filter domain.ListGrantsFilter) (int64, error) {
db := r.db.WithContext(ctx)
db = applyGrantFilter(db, filter)

var count int64
if err := db.Model(&model.Grant{}).Count(&count).Error; err != nil {
return 0, err
}
err := db.Model(&model.Grant{}).Count(&count).Error

return count, nil
return count, err
}

func (r *GrantRepository) GetByID(ctx context.Context, id string) (*domain.Grant, error) {
Expand Down
1 change: 0 additions & 1 deletion internal/store/postgres/grant_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ func (s *GrantRepositoryTestSuite) TearDownSuite() {
}

func (s *GrantRepositoryTestSuite) TestGetGrantsTotalCount() {

s.Run("should return 0", func() {
_, actualError := s.repository.GetGrantsTotalCount(context.Background(), domain.ListGrantsFilter{})

Expand Down

0 comments on commit 93cc1ba

Please sign in to comment.