Skip to content

Commit

Permalink
Apply feedbacks on reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
0Tech committed Apr 10, 2023
1 parent a1b5f40 commit 13e72ee
Showing 1 changed file with 31 additions and 34 deletions.
65 changes: 31 additions & 34 deletions x/collection/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,55 +510,23 @@ func (s queryServer) Root(c context.Context, req *collection.QueryRootRequest) (
}

func (s queryServer) HasParent(c context.Context, req *collection.QueryHasParentRequest) (*collection.QueryHasParentResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

if err := collection.ValidateContractID(req.ContractId); err != nil {
return nil, err
}

if err := collection.ValidateNFTID(req.TokenId); err != nil {
return nil, err
}

ctx := sdk.UnwrapSDKContext(c)

if err := s.validateExistenceOfCollectionGRPC(ctx, req.ContractId); err != nil {
if err := s.validateGetParentVariants(ctx, req); err != nil {
return nil, err
}

if err := s.keeper.hasNFT(ctx, req.ContractId, req.TokenId); err != nil {
return nil, status.Error(codes.NotFound, err.Error())
}

_, err := s.keeper.GetParent(ctx, req.ContractId, req.TokenId)
return &collection.QueryHasParentResponse{HasParent: (err == nil)}, nil
}

func (s queryServer) Parent(c context.Context, req *collection.QueryParentRequest) (*collection.QueryParentResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

if err := collection.ValidateContractID(req.ContractId); err != nil {
return nil, err
}

if err := collection.ValidateNFTID(req.TokenId); err != nil {
return nil, err
}

ctx := sdk.UnwrapSDKContext(c)

if err := s.validateExistenceOfCollectionGRPC(ctx, req.ContractId); err != nil {
if err := s.validateGetParentVariants(ctx, req); err != nil {
return nil, err
}

if err := s.keeper.hasNFT(ctx, req.ContractId, req.TokenId); err != nil {
return nil, status.Error(codes.NotFound, err.Error())
}

parent, err := s.keeper.GetParent(ctx, req.ContractId, req.TokenId)
if err != nil {
return nil, status.Error(codes.NotFound, err.Error())
Expand All @@ -572,6 +540,35 @@ func (s queryServer) Parent(c context.Context, req *collection.QueryParentReques
return &collection.QueryParentResponse{Parent: *token}, nil
}

type parentRequest interface {
GetContractId() string
GetTokenId() string
}

func (s queryServer) validateGetParentVariants(ctx sdk.Context, req parentRequest) error {
if req == nil {
return status.Error(codes.InvalidArgument, "empty request")
}

if err := collection.ValidateContractID(req.GetContractId()); err != nil {
return err
}

if err := collection.ValidateNFTID(req.GetTokenId()); err != nil {
return err
}

if err := s.validateExistenceOfCollectionGRPC(ctx, req.GetContractId()); err != nil {
return err
}

if err := s.keeper.hasNFT(ctx, req.GetContractId(), req.GetTokenId()); err != nil {
return status.Error(codes.NotFound, err.Error())
}

return nil
}

func (s queryServer) Children(c context.Context, req *collection.QueryChildrenRequest) (*collection.QueryChildrenResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
Expand Down

0 comments on commit 13e72ee

Please sign in to comment.