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

     1  package appcoin
     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/app/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 ExistCoinConds(ctx context.Context, conds *npool.Conds) (bool, error) {
    92  	info, err := do(ctx, func(_ctx context.Context, cli npool.MiddlewareClient) (cruder.Any, error) {
    93  		resp, err := cli.ExistCoinConds(ctx, &npool.ExistCoinCondsRequest{
    94  			Conds: conds,
    95  		})
    96  		if err != nil {
    97  			return nil, err
    98  		}
    99  		return resp.Info, nil
   100  	})
   101  	if err != nil {
   102  		return false, err
   103  	}
   104  	return info.(bool), nil
   105  }
   106  
   107  func GetCoinOnly(ctx context.Context, conds *npool.Conds) (*npool.Coin, error) {
   108  	infos, err := do(ctx, func(_ctx context.Context, cli npool.MiddlewareClient) (cruder.Any, error) {
   109  		const singleRowLimit = 2
   110  		resp, err := cli.GetCoins(ctx, &npool.GetCoinsRequest{
   111  			Conds:  conds,
   112  			Offset: 0,
   113  			Limit:  singleRowLimit,
   114  		})
   115  		if err != nil {
   116  			return nil, err
   117  		}
   118  		return resp.Infos, nil
   119  	})
   120  	if err != nil {
   121  		return nil, err
   122  	}
   123  	if len(infos.([]*npool.Coin)) == 0 {
   124  		return nil, nil
   125  	}
   126  	if len(infos.([]*npool.Coin)) > 1 {
   127  		return nil, fmt.Errorf("too many record")
   128  	}
   129  	return infos.([]*npool.Coin)[0], nil
   130  }
   131  
   132  func UpdateCoin(ctx context.Context, in *npool.CoinReq) (*npool.Coin, error) {
   133  	info, err := do(ctx, func(_ctx context.Context, cli npool.MiddlewareClient) (cruder.Any, error) {
   134  		resp, err := cli.UpdateCoin(ctx, &npool.UpdateCoinRequest{
   135  			Info: in,
   136  		})
   137  		if err != nil {
   138  			return nil, err
   139  		}
   140  		return resp.Info, nil
   141  	})
   142  	if err != nil {
   143  		return nil, err
   144  	}
   145  	return info.(*npool.Coin), nil
   146  }
   147  
   148  func DeleteCoin(ctx context.Context, id uint32) (*npool.Coin, error) {
   149  	info, err := do(ctx, func(_ctx context.Context, cli npool.MiddlewareClient) (cruder.Any, error) {
   150  		resp, err := cli.DeleteCoin(ctx, &npool.DeleteCoinRequest{
   151  			Info: &npool.CoinReq{
   152  				ID: &id,
   153  			},
   154  		})
   155  		if err != nil {
   156  			return nil, err
   157  		}
   158  		return resp.Info, nil
   159  	})
   160  	if err != nil {
   161  		return nil, err
   162  	}
   163  	return info.(*npool.Coin), nil
   164  }