github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/api/app/coin/query.go (about)

     1  package appcoin
     2  
     3  import (
     4  	"context"
     5  
     6  	appcoin1 "github.com/NpoolPlatform/chain-middleware/pkg/mw/app/coin"
     7  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/app/coin"
     8  
     9  	"github.com/NpoolPlatform/go-service-framework/pkg/logger"
    10  
    11  	"google.golang.org/grpc/codes"
    12  	"google.golang.org/grpc/status"
    13  )
    14  
    15  func (s *Server) GetCoin(ctx context.Context, in *npool.GetCoinRequest) (*npool.GetCoinResponse, error) {
    16  	handler, err := appcoin1.NewHandler(
    17  		ctx,
    18  		appcoin1.WithEntID(&in.EntID, true),
    19  	)
    20  	if err != nil {
    21  		logger.Sugar().Errorw(
    22  			"GetCoin",
    23  			"In", in,
    24  			"Error", err,
    25  		)
    26  		return &npool.GetCoinResponse{}, status.Error(codes.InvalidArgument, err.Error())
    27  	}
    28  
    29  	info, err := handler.GetCoin(ctx)
    30  	if err != nil {
    31  		logger.Sugar().Errorw(
    32  			"GetCoin",
    33  			"In", in,
    34  			"Error", err,
    35  		)
    36  		return &npool.GetCoinResponse{}, status.Error(codes.Internal, err.Error())
    37  	}
    38  
    39  	return &npool.GetCoinResponse{
    40  		Info: info,
    41  	}, nil
    42  }
    43  
    44  func (s *Server) GetCoins(ctx context.Context, in *npool.GetCoinsRequest) (*npool.GetCoinsResponse, error) {
    45  	handler, err := appcoin1.NewHandler(
    46  		ctx,
    47  		appcoin1.WithConds(in.Conds),
    48  		appcoin1.WithOffset(in.GetOffset()),
    49  		appcoin1.WithLimit(in.GetLimit()),
    50  	)
    51  	if err != nil {
    52  		logger.Sugar().Errorw(
    53  			"GetCoins",
    54  			"In", in,
    55  			"Error", err,
    56  		)
    57  		return &npool.GetCoinsResponse{}, status.Error(codes.InvalidArgument, err.Error())
    58  	}
    59  
    60  	infos, total, err := handler.GetCoins(ctx)
    61  	if err != nil {
    62  		logger.Sugar().Errorw(
    63  			"GetCoins",
    64  			"In", in,
    65  			"Error", err,
    66  		)
    67  		return &npool.GetCoinsResponse{}, status.Error(codes.Internal, err.Error())
    68  	}
    69  
    70  	return &npool.GetCoinsResponse{
    71  		Infos: infos,
    72  		Total: total,
    73  	}, nil
    74  }