Skip to content

Commit

Permalink
Move validation code to service
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Marcantonio <[email protected]>
  • Loading branch information
lennysgarage committed Jun 17, 2024
1 parent ac0ba82 commit 9de8edb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
11 changes: 0 additions & 11 deletions internal/biz/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
v0 "ciam-rebac/api/relations/v0"
"context"

"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/log"
)

Expand Down Expand Up @@ -36,16 +35,6 @@ func (s *GetSubjectsUsecase) Get(ctx context.Context, req *v0.LookupSubjectsRequ
}
}

if err := req.ValidateAll(); err != nil {
s.log.WithContext(ctx).Error("Request failed to pass validation: %v", req)
return nil, nil, errors.BadRequest("Invalid request", err.Error())
}

if err := req.Resource.ValidateAll(); err != nil {
s.log.WithContext(ctx).Error("Resource failed to pass validation: %v", req)
return nil, nil, errors.BadRequest("Invalid request", err.Error())
}

if req.SubjectRelation != nil {
subjectRelation = *req.SubjectRelation
}
Expand Down
9 changes: 0 additions & 9 deletions internal/biz/relationships.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
v0 "ciam-rebac/api/relations/v0"
"context"

"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/log"
)

Expand Down Expand Up @@ -54,14 +53,6 @@ func NewCreateRelationshipsUsecase(repo ZanzibarRepository, logger log.Logger) *

func (rc *CreateRelationshipsUsecase) CreateRelationships(ctx context.Context, r []*v0.Relationship, touch bool) error {
rc.log.WithContext(ctx).Infof("CreateTuples: %v %s", r, touch)

for idx := range r {
if err := r[idx].ValidateAll(); err != nil {
rc.log.Infof("Request failed to pass validation %v", r[idx])
return errors.BadRequest("Invalid request", err.Error())
}
}

return rc.repo.CreateRelationships(ctx, r, TouchSemantics(touch))
}

Expand Down
10 changes: 10 additions & 0 deletions internal/service/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package service
import (
pb "ciam-rebac/api/relations/v0"
"ciam-rebac/internal/biz"

"github.com/go-kratos/kratos/v2/errors"
)

type LookupService struct {
Expand All @@ -20,6 +22,14 @@ func NewLookupService(subjectsUseCase *biz.GetSubjectsUsecase) *LookupService {
func (s *LookupService) LookupSubjects(req *pb.LookupSubjectsRequest, conn pb.KesselLookupService_LookupSubjectsServer) error {
ctx := conn.Context()

if err := req.ValidateAll(); err != nil {
return errors.BadRequest("Invalid request", err.Error())
}

if err := req.Resource.ValidateAll(); err != nil {
return errors.BadRequest("Invalid request", err.Error())
}

subs, errs, err := s.subjectsUsecase.Get(ctx, req)

if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions internal/service/relationships.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"ciam-rebac/internal/biz"
"context"

"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/log"

pb "ciam-rebac/api/relations/v0"
Expand All @@ -29,6 +30,13 @@ func NewRelationshipsService(logger log.Logger, createUseCase *biz.CreateRelatio
func (s *RelationshipsService) CreateTuples(ctx context.Context, req *pb.CreateTuplesRequest) (*pb.CreateTuplesResponse, error) {
s.log.Infof("Create relationships request: %v", req)

for idx := range req.Tuples {
if err := req.Tuples[idx].ValidateAll(); err != nil {
s.log.Infof("Request failed to pass validation: %v", req.Tuples[idx])
return nil, errors.BadRequest("Invalid request", err.Error())
}
}

err := s.createUsecase.CreateRelationships(ctx, req.Tuples, req.GetUpsert()) //The generated .GetUpsert() defaults to false
if err != nil {
return nil, err
Expand Down

0 comments on commit 9de8edb

Please sign in to comment.