github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/fiat/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/fiat/currency/feed"
     9  	basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1"
    10  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/fiat/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  	FiatID       *uuid.UUID
    21  	FeedType     *basetypes.CurrencyFeedType
    22  	FeedFiatName *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 WithFiatID(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 fiatid")
    74  			}
    75  			return nil
    76  		}
    77  		_id, err := uuid.Parse(*id)
    78  		if err != nil {
    79  			return err
    80  		}
    81  		h.FiatID = &_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 WithFeedFiatName(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 name")
   111  			}
   112  			return nil
   113  		}
   114  		if *name == "" {
   115  			return fmt.Errorf("invalid feedfiatname")
   116  		}
   117  		h.FeedFiatName = 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.FiatID != nil {
   146  			id, err := uuid.Parse(conds.GetFiatID().GetValue())
   147  			if err != nil {
   148  				return err
   149  			}
   150  			h.Conds.FiatID = &cruder.Cond{
   151  				Op:  conds.GetFiatID().GetOp(),
   152  				Val: id,
   153  			}
   154  		}
   155  		if conds.FiatIDs != nil {
   156  			ids := []uuid.UUID{}
   157  			for _, id := range conds.GetFiatIDs().GetValue() {
   158  				_id, err := uuid.Parse(id)
   159  				if err != nil {
   160  					return err
   161  				}
   162  				ids = append(ids, _id)
   163  			}
   164  			h.Conds.FiatIDs = &cruder.Cond{
   165  				Op:  conds.GetFiatIDs().GetOp(),
   166  				Val: ids,
   167  			}
   168  		}
   169  		if conds.Disabled != nil {
   170  			h.Conds.Disabled = &cruder.Cond{
   171  				Op:  conds.GetDisabled().GetOp(),
   172  				Val: conds.GetDisabled().GetValue(),
   173  			}
   174  		}
   175  		return nil
   176  	}
   177  }
   178  
   179  func WithOffset(offset int32) func(context.Context, *Handler) error {
   180  	return func(ctx context.Context, h *Handler) error {
   181  		h.Offset = offset
   182  		return nil
   183  	}
   184  }
   185  
   186  func WithLimit(limit int32) func(context.Context, *Handler) error {
   187  	return func(ctx context.Context, h *Handler) error {
   188  		if limit == 0 {
   189  			limit = constant.DefaultRowLimit
   190  		}
   191  		h.Limit = limit
   192  		return nil
   193  	}
   194  }