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

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