github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/client/coin/client.go (about)

     1  package coin
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  
     8  	grpc2 "github.com/NpoolPlatform/go-service-framework/pkg/grpc"
     9  
    10  	"github.com/NpoolPlatform/libent-cruder/pkg/cruder"
    11  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin"
    12  
    13  	servicename "github.com/NpoolPlatform/chain-middleware/pkg/servicename"
    14  )
    15  
    16  var timeout = 10 * time.Second
    17  
    18  type handler func(context.Context, npool.MiddlewareClient) (cruder.Any, error)
    19  
    20  func do(ctx context.Context, handler handler) (cruder.Any, error) {
    21  	_ctx, cancel := context.WithTimeout(ctx, timeout)
    22  	defer cancel()
    23  
    24  	conn, err := grpc2.GetGRPCConn(servicename.ServiceDomain, grpc2.GRPCTAG)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  
    29  	defer conn.Close()
    30  
    31  	cli := npool.NewMiddlewareClient(conn)
    32  
    33  	return handler(_ctx, cli)
    34  }
    35  
    36  func CreateCoin(ctx context.Context, in *npool.CoinReq) (*npool.Coin, error) {
    37  	info, err := do(ctx, func(_ctx context.Context, cli npool.MiddlewareClient) (cruder.Any, error) {
    38  		resp, err := cli.CreateCoin(ctx, &npool.CreateCoinRequest{
    39  			Info: in,
    40  		})
    41  		if err != nil {
    42  			return nil, err
    43  		}
    44  		return resp.Info, nil
    45  	})
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	return info.(*npool.Coin), nil
    50  }
    51  
    52  func GetCoin(ctx context.Context, id string) (*npool.Coin, error) {
    53  	info, err := do(ctx, func(_ctx context.Context, cli npool.MiddlewareClient) (cruder.Any, error) {
    54  		resp, err := cli.GetCoin(ctx, &npool.GetCoinRequest{
    55  			EntID: id,
    56  		})
    57  		if err != nil {
    58  			return nil, err
    59  		}
    60  		return resp.Info, nil
    61  	})
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	return info.(*npool.Coin), nil
    66  }
    67  
    68  func GetCoins(ctx context.Context, conds *npool.Conds, offset, limit int32) ([]*npool.Coin, uint32, error) {
    69  	var total uint32
    70  
    71  	infos, err := do(ctx, func(_ctx context.Context, cli npool.MiddlewareClient) (cruder.Any, error) {
    72  		resp, err := cli.GetCoins(ctx, &npool.GetCoinsRequest{
    73  			Conds:  conds,
    74  			Offset: offset,
    75  			Limit:  limit,
    76  		})
    77  		if err != nil {
    78  			return nil, err
    79  		}
    80  
    81  		total = resp.Total
    82  
    83  		return resp.Infos, nil
    84  	})
    85  	if err != nil {
    86  		return nil, 0, err
    87  	}
    88  	return infos.([]*npool.Coin), total, nil
    89  }
    90  
    91  func GetCoinOnly(ctx context.Context, conds *npool.Conds) (*npool.Coin, error) {
    92  	const limit = 2
    93  	infos, err := do(ctx, func(_ctx context.Context, cli npool.MiddlewareClient) (cruder.Any, error) {
    94  		resp, err := cli.GetCoins(ctx, &npool.GetCoinsRequest{
    95  			Conds:  conds,
    96  			Offset: 0,
    97  			Limit:  limit,
    98  		})
    99  		if err != nil {
   100  			return nil, err
   101  		}
   102  		return resp.Infos, nil
   103  	})
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  	if err != nil {
   108  		return nil, err
   109  	}
   110  	if len(infos.([]*npool.Coin)) == 0 {
   111  		return nil, nil
   112  	}
   113  	if len(infos.([]*npool.Coin)) > 1 {
   114  		return nil, fmt.Errorf("too many records")
   115  	}
   116  	return infos.([]*npool.Coin)[0], nil
   117  }
   118  
   119  func UpdateCoin(ctx context.Context, in *npool.CoinReq) (*npool.Coin, error) {
   120  	info, err := do(ctx, func(_ctx context.Context, cli npool.MiddlewareClient) (cruder.Any, error) {
   121  		resp, err := cli.UpdateCoin(ctx, &npool.UpdateCoinRequest{
   122  			Info: in,
   123  		})
   124  		if err != nil {
   125  			return nil, err
   126  		}
   127  		return resp.Info, nil
   128  	})
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  	return info.(*npool.Coin), nil
   133  }
   134  
   135  func ExistCoin(ctx context.Context, id string) (bool, error) {
   136  	info, err := do(ctx, func(_ctx context.Context, cli npool.MiddlewareClient) (cruder.Any, error) {
   137  		resp, err := cli.ExistCoin(ctx, &npool.ExistCoinRequest{
   138  			EntID: id,
   139  		})
   140  		if err != nil {
   141  			return nil, err
   142  		}
   143  		return resp.Info, nil
   144  	})
   145  	if err != nil {
   146  		return false, err
   147  	}
   148  	return info.(bool), nil
   149  }
   150  
   151  func ExistCoinConds(ctx context.Context, conds *npool.Conds) (bool, error) {
   152  	info, err := do(ctx, func(_ctx context.Context, cli npool.MiddlewareClient) (cruder.Any, error) {
   153  		resp, err := cli.ExistCoinConds(ctx, &npool.ExistCoinCondsRequest{
   154  			Conds: conds,
   155  		})
   156  		if err != nil {
   157  			return nil, err
   158  		}
   159  		return resp.Info, nil
   160  	})
   161  	if err != nil {
   162  		return false, err
   163  	}
   164  	return info.(bool), nil
   165  }