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