github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/client/coin/usedfor/client.go (about) 1 package coinusedfor 2 3 import ( 4 "context" 5 "time" 6 7 grpc2 "github.com/NpoolPlatform/go-service-framework/pkg/grpc" 8 9 "github.com/NpoolPlatform/libent-cruder/pkg/cruder" 10 npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin/usedfor" 11 12 servicename "github.com/NpoolPlatform/chain-middleware/pkg/servicename" 13 ) 14 15 var timeout = 10 * time.Second 16 17 type handler func(context.Context, npool.MiddlewareClient) (cruder.Any, error) 18 19 func withCRUD(ctx context.Context, handler handler) (cruder.Any, error) { 20 _ctx, cancel := context.WithTimeout(ctx, timeout) 21 defer cancel() 22 23 conn, err := grpc2.GetGRPCConn(servicename.ServiceDomain, grpc2.GRPCTAG) 24 if err != nil { 25 return nil, err 26 } 27 28 defer conn.Close() 29 30 cli := npool.NewMiddlewareClient(conn) 31 32 return handler(_ctx, cli) 33 } 34 35 func CreateCoinUsedFor(ctx context.Context, in *npool.CoinUsedForReq) (*npool.CoinUsedFor, error) { 36 info, err := withCRUD(ctx, func(_ctx context.Context, cli npool.MiddlewareClient) (cruder.Any, error) { 37 resp, err := cli.CreateCoinUsedFor(ctx, &npool.CreateCoinUsedForRequest{ 38 Info: in, 39 }) 40 if err != nil { 41 return nil, err 42 } 43 return resp.Info, nil 44 }) 45 if err != nil { 46 return nil, err 47 } 48 return info.(*npool.CoinUsedFor), nil 49 } 50 51 func GetCoinUsedFor(ctx context.Context, id string) (*npool.CoinUsedFor, error) { 52 info, err := withCRUD(ctx, func(_ctx context.Context, cli npool.MiddlewareClient) (cruder.Any, error) { 53 resp, err := cli.GetCoinUsedFor(ctx, &npool.GetCoinUsedForRequest{ 54 EntID: id, 55 }) 56 if err != nil { 57 return nil, err 58 } 59 return resp.Info, nil 60 }) 61 if err != nil { 62 return nil, err 63 } 64 return info.(*npool.CoinUsedFor), nil 65 } 66 67 func GetCoinUsedFors(ctx context.Context, conds *npool.Conds, offset, limit int32) ([]*npool.CoinUsedFor, uint32, error) { 68 var total uint32 69 70 infos, err := withCRUD(ctx, func(_ctx context.Context, cli npool.MiddlewareClient) (cruder.Any, error) { 71 resp, err := cli.GetCoinUsedFors(ctx, &npool.GetCoinUsedForsRequest{ 72 Conds: conds, 73 Offset: offset, 74 Limit: limit, 75 }) 76 if err != nil { 77 return nil, err 78 } 79 80 total = resp.Total 81 82 return resp.Infos, nil 83 }) 84 if err != nil { 85 return nil, 0, err 86 } 87 return infos.([]*npool.CoinUsedFor), total, nil 88 } 89 90 func DeleteCoinUsedFor(ctx context.Context, id uint32) (*npool.CoinUsedFor, error) { 91 info, err := withCRUD(ctx, func(_ctx context.Context, cli npool.MiddlewareClient) (cruder.Any, error) { 92 resp, err := cli.DeleteCoinUsedFor(ctx, &npool.DeleteCoinUsedForRequest{ 93 Info: &npool.CoinUsedForReq{ 94 ID: &id, 95 }, 96 }) 97 if err != nil { 98 return nil, err 99 } 100 return resp.Info, nil 101 }) 102 if err != nil { 103 return nil, err 104 } 105 return info.(*npool.CoinUsedFor), nil 106 }