github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/coin/fiat/handler.go (about) 1 package coinfiat 2 3 import ( 4 "context" 5 "fmt" 6 7 constant "github.com/NpoolPlatform/chain-middleware/pkg/const" 8 coinfiatcrud "github.com/NpoolPlatform/chain-middleware/pkg/crud/coin/fiat" 9 "github.com/NpoolPlatform/libent-cruder/pkg/cruder" 10 basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1" 11 npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin/fiat" 12 13 "github.com/google/uuid" 14 ) 15 16 type Handler struct { 17 ID *uint32 18 EntID *uuid.UUID 19 CoinTypeID *uuid.UUID 20 FiatID *uuid.UUID 21 FeedType *basetypes.CurrencyFeedType 22 Conds *coinfiatcrud.Conds 23 Offset int32 24 Limit int32 25 } 26 27 func NewHandler(ctx context.Context, options ...func(context.Context, *Handler) error) (*Handler, error) { 28 handler := &Handler{} 29 for _, opt := range options { 30 if err := opt(ctx, handler); err != nil { 31 return nil, err 32 } 33 } 34 return handler, nil 35 } 36 37 func WithID(u *uint32, must bool) func(context.Context, *Handler) error { 38 return func(ctx context.Context, h *Handler) error { 39 if u == nil { 40 if must { 41 return fmt.Errorf("invalid id") 42 } 43 return nil 44 } 45 h.ID = u 46 return nil 47 } 48 } 49 50 func WithEntID(id *string, must bool) func(context.Context, *Handler) error { 51 return func(ctx context.Context, h *Handler) error { 52 if id == nil { 53 if must { 54 return fmt.Errorf("invalid entid") 55 } 56 return nil 57 } 58 _id, err := uuid.Parse(*id) 59 if err != nil { 60 return err 61 } 62 h.EntID = &_id 63 return nil 64 } 65 } 66 67 func WithCoinTypeID(id *string, must bool) func(context.Context, *Handler) error { 68 return func(ctx context.Context, h *Handler) error { 69 if id == nil { 70 if must { 71 return fmt.Errorf("invalid cointypeid") 72 } 73 return nil 74 } 75 _id, err := uuid.Parse(*id) 76 if err != nil { 77 return err 78 } 79 h.CoinTypeID = &_id 80 return nil 81 } 82 } 83 84 func WithFiatID(id *string, must bool) func(context.Context, *Handler) error { 85 return func(ctx context.Context, h *Handler) error { 86 if id == nil { 87 if must { 88 return fmt.Errorf("invalid fiatid") 89 } 90 return nil 91 } 92 _id, err := uuid.Parse(*id) 93 if err != nil { 94 return err 95 } 96 h.FiatID = &_id 97 return nil 98 } 99 } 100 101 func WithFeedType(feedType *basetypes.CurrencyFeedType, must bool) func(context.Context, *Handler) error { 102 return func(ctx context.Context, h *Handler) error { 103 if feedType == nil { 104 if must { 105 return fmt.Errorf("invalid feedtype") 106 } 107 return nil 108 } 109 switch *feedType { 110 case basetypes.CurrencyFeedType_CoinGecko: 111 case basetypes.CurrencyFeedType_CoinBase: 112 case basetypes.CurrencyFeedType_StableUSDHardCode: 113 default: 114 return fmt.Errorf("invalid feedtype") 115 } 116 h.FeedType = feedType 117 return nil 118 } 119 } 120 121 func WithConds(conds *npool.Conds) func(context.Context, *Handler) error { 122 return func(ctx context.Context, h *Handler) error { 123 h.Conds = &coinfiatcrud.Conds{} 124 if conds == nil { 125 return nil 126 } 127 if conds.EntID != nil { 128 id, err := uuid.Parse(conds.GetEntID().GetValue()) 129 if err != nil { 130 return err 131 } 132 h.Conds.EntID = &cruder.Cond{ 133 Op: conds.GetEntID().GetOp(), 134 Val: id, 135 } 136 } 137 if conds.CoinTypeID != nil { 138 id, err := uuid.Parse(conds.GetCoinTypeID().GetValue()) 139 if err != nil { 140 return err 141 } 142 h.Conds.CoinTypeID = &cruder.Cond{ 143 Op: conds.GetCoinTypeID().GetOp(), 144 Val: id, 145 } 146 } 147 if conds.CoinTypeIDs != nil { 148 ids := []uuid.UUID{} 149 for _, id := range conds.GetCoinTypeIDs().GetValue() { 150 _id, err := uuid.Parse(id) 151 if err != nil { 152 return err 153 } 154 ids = append(ids, _id) 155 } 156 h.Conds.CoinTypeIDs = &cruder.Cond{ 157 Op: conds.GetCoinTypeIDs().GetOp(), 158 Val: ids, 159 } 160 } 161 return nil 162 } 163 } 164 165 func WithOffset(offset int32) func(context.Context, *Handler) error { 166 return func(ctx context.Context, h *Handler) error { 167 h.Offset = offset 168 return nil 169 } 170 } 171 172 func WithLimit(limit int32) func(context.Context, *Handler) error { 173 return func(ctx context.Context, h *Handler) error { 174 if limit == 0 { 175 limit = constant.DefaultRowLimit 176 } 177 h.Limit = limit 178 return nil 179 } 180 }