github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/coin/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/currency"
     9  	basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1"
    10  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin/currency"
    11  
    12  	"github.com/NpoolPlatform/libent-cruder/pkg/cruder"
    13  
    14  	"github.com/google/uuid"
    15  	"github.com/shopspring/decimal"
    16  )
    17  
    18  type Handler struct {
    19  	ID              *uint32
    20  	EntID           *uuid.UUID
    21  	CoinTypeID      *uuid.UUID
    22  	FeedType        *basetypes.CurrencyFeedType
    23  	MarketValueHigh *decimal.Decimal
    24  	MarketValueLow  *decimal.Decimal
    25  	Reqs            []*currencycrud.Req
    26  	Conds           *currencycrud.Conds
    27  	Offset          int32
    28  	Limit           int32
    29  }
    30  
    31  func NewHandler(ctx context.Context, options ...func(context.Context, *Handler) error) (*Handler, error) {
    32  	handler := &Handler{}
    33  	for _, opt := range options {
    34  		if err := opt(ctx, handler); err != nil {
    35  			return nil, err
    36  		}
    37  	}
    38  	return handler, nil
    39  }
    40  
    41  func WithID(u *uint32, must bool) func(context.Context, *Handler) error {
    42  	return func(ctx context.Context, h *Handler) error {
    43  		if u == nil {
    44  			if must {
    45  				return fmt.Errorf("invalid id")
    46  			}
    47  			return nil
    48  		}
    49  		h.ID = u
    50  		return nil
    51  	}
    52  }
    53  
    54  func WithEntID(id *string, must bool) func(context.Context, *Handler) error {
    55  	return func(ctx context.Context, h *Handler) error {
    56  		if id == nil {
    57  			if must {
    58  				return fmt.Errorf("invalid entid")
    59  			}
    60  			return nil
    61  		}
    62  		_id, err := uuid.Parse(*id)
    63  		if err != nil {
    64  			return err
    65  		}
    66  		h.EntID = &_id
    67  		return nil
    68  	}
    69  }
    70  
    71  func WithCoinTypeID(id *string, must bool) func(context.Context, *Handler) error {
    72  	return func(ctx context.Context, h *Handler) error {
    73  		if id == nil {
    74  			if must {
    75  				return fmt.Errorf("invalid cointypeid")
    76  			}
    77  			return nil
    78  		}
    79  		_id, err := uuid.Parse(*id)
    80  		if err != nil {
    81  			return err
    82  		}
    83  		h.CoinTypeID = &_id
    84  		return nil
    85  	}
    86  }
    87  
    88  func WithFeedType(feedType *basetypes.CurrencyFeedType, must bool) func(context.Context, *Handler) error {
    89  	return func(ctx context.Context, h *Handler) error {
    90  		if feedType == nil {
    91  			if must {
    92  				return fmt.Errorf("invalid feedtype")
    93  			}
    94  			return nil
    95  		}
    96  		switch *feedType {
    97  		case basetypes.CurrencyFeedType_CoinGecko:
    98  		case basetypes.CurrencyFeedType_CoinBase:
    99  		case basetypes.CurrencyFeedType_StableUSDHardCode:
   100  		default:
   101  			return fmt.Errorf("invalid feedtype")
   102  		}
   103  		h.FeedType = feedType
   104  		return nil
   105  	}
   106  }
   107  
   108  func WithMarketValueHigh(value *string, must bool) func(context.Context, *Handler) error {
   109  	return func(ctx context.Context, h *Handler) error {
   110  		if value == nil {
   111  			if must {
   112  				return fmt.Errorf("invalid marketvaluehigh")
   113  			}
   114  			return nil
   115  		}
   116  		_value, err := decimal.NewFromString(*value)
   117  		if err != nil {
   118  			return err
   119  		}
   120  		h.MarketValueHigh = &_value
   121  		return nil
   122  	}
   123  }
   124  
   125  func WithMarketValueLow(value *string, must bool) func(context.Context, *Handler) error {
   126  	return func(ctx context.Context, h *Handler) error {
   127  		if value == nil {
   128  			if must {
   129  				return fmt.Errorf("invalid marketvaluelow")
   130  			}
   131  			return nil
   132  		}
   133  		_value, err := decimal.NewFromString(*value)
   134  		if err != nil {
   135  			return err
   136  		}
   137  		h.MarketValueLow = &_value
   138  		return nil
   139  	}
   140  }
   141  
   142  func WithReqs(reqs []*npool.CurrencyReq, must bool) func(context.Context, *Handler) error {
   143  	return func(ctx context.Context, h *Handler) error {
   144  		_reqs := []*currencycrud.Req{}
   145  		for _, req := range reqs {
   146  			_req := &currencycrud.Req{}
   147  			if req.EntID != nil {
   148  				id, err := uuid.Parse(*req.EntID)
   149  				if err != nil {
   150  					return err
   151  				}
   152  				_req.EntID = &id
   153  			}
   154  			if req.CoinTypeID != nil {
   155  				id, err := uuid.Parse(*req.CoinTypeID)
   156  				if err != nil {
   157  					return err
   158  				}
   159  				_req.CoinTypeID = &id
   160  			}
   161  			if req.FeedType != nil {
   162  				switch *req.FeedType {
   163  				case basetypes.CurrencyFeedType_CoinGecko:
   164  				case basetypes.CurrencyFeedType_CoinBase:
   165  				case basetypes.CurrencyFeedType_StableUSDHardCode:
   166  				default:
   167  					return fmt.Errorf("invalid feedtype")
   168  				}
   169  				_req.FeedType = req.FeedType
   170  			}
   171  			if req.MarketValueHigh != nil {
   172  				amount, err := decimal.NewFromString(*req.MarketValueHigh)
   173  				if err != nil {
   174  					return err
   175  				}
   176  				_req.MarketValueHigh = &amount
   177  			}
   178  			if req.MarketValueLow != nil {
   179  				amount, err := decimal.NewFromString(*req.MarketValueLow)
   180  				if err != nil {
   181  					return err
   182  				}
   183  				_req.MarketValueLow = &amount
   184  			}
   185  			_reqs = append(_reqs, _req)
   186  		}
   187  		h.Reqs = _reqs
   188  		return nil
   189  	}
   190  }
   191  
   192  func WithConds(conds *npool.Conds) func(context.Context, *Handler) error {
   193  	return func(ctx context.Context, h *Handler) error {
   194  		h.Conds = &currencycrud.Conds{}
   195  		if conds == nil {
   196  			return nil
   197  		}
   198  		if conds.EntID != nil {
   199  			id, err := uuid.Parse(conds.GetEntID().GetValue())
   200  			if err != nil {
   201  				return err
   202  			}
   203  			h.Conds.EntID = &cruder.Cond{
   204  				Op:  conds.GetEntID().GetOp(),
   205  				Val: id,
   206  			}
   207  		}
   208  		if conds.FeedType != nil {
   209  			switch conds.GetFeedType().GetValue() {
   210  			case uint32(basetypes.CurrencyFeedType_CoinGecko):
   211  			case uint32(basetypes.CurrencyFeedType_CoinBase):
   212  			case uint32(basetypes.CurrencyFeedType_StableUSDHardCode):
   213  			default:
   214  				return fmt.Errorf("invalid feedtype")
   215  			}
   216  			_type := conds.GetFeedType().GetValue()
   217  			h.Conds.FeedType = &cruder.Cond{Op: conds.GetFeedType().GetOp(), Val: basetypes.CurrencyFeedType(_type)}
   218  		}
   219  		if conds.CoinTypeID != nil {
   220  			id, err := uuid.Parse(conds.GetCoinTypeID().GetValue())
   221  			if err != nil {
   222  				return err
   223  			}
   224  			h.Conds.CoinTypeID = &cruder.Cond{
   225  				Op:  conds.GetCoinTypeID().GetOp(),
   226  				Val: id,
   227  			}
   228  		}
   229  		if conds.CoinTypeIDs != nil {
   230  			ids := []uuid.UUID{}
   231  			for _, id := range conds.GetCoinTypeIDs().GetValue() {
   232  				_id, err := uuid.Parse(id)
   233  				if err != nil {
   234  					return err
   235  				}
   236  				ids = append(ids, _id)
   237  			}
   238  			h.Conds.CoinTypeIDs = &cruder.Cond{
   239  				Op:  conds.GetCoinTypeIDs().GetOp(),
   240  				Val: ids,
   241  			}
   242  		}
   243  		return nil
   244  	}
   245  }
   246  
   247  func WithOffset(offset int32) func(context.Context, *Handler) error {
   248  	return func(ctx context.Context, h *Handler) error {
   249  		h.Offset = offset
   250  		return nil
   251  	}
   252  }
   253  
   254  func WithLimit(limit int32) func(context.Context, *Handler) error {
   255  	return func(ctx context.Context, h *Handler) error {
   256  		if limit == 0 {
   257  			limit = constant.DefaultRowLimit
   258  		}
   259  		h.Limit = limit
   260  		return nil
   261  	}
   262  }