github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/coin/currency/feed/handler.go (about)

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