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

     1  package coinusedfor
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	constant "github.com/NpoolPlatform/chain-middleware/pkg/const"
     8  	coinusedforcrud "github.com/NpoolPlatform/chain-middleware/pkg/crud/coin/usedfor"
     9  	"github.com/NpoolPlatform/libent-cruder/pkg/cruder"
    10  	types "github.com/NpoolPlatform/message/npool/basetypes/chain/v1"
    11  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin/usedfor"
    12  
    13  	"github.com/google/uuid"
    14  )
    15  
    16  type Handler struct {
    17  	ID         *uint32
    18  	EntID      *uuid.UUID
    19  	CoinTypeID *uuid.UUID
    20  	UsedFor    *types.CoinUsedFor
    21  	Priority   *uint32
    22  	Conds      *coinusedforcrud.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 WithUsedFor(usedFor *types.CoinUsedFor, must bool) func(context.Context, *Handler) error {
    85  	return func(ctx context.Context, h *Handler) error {
    86  		if usedFor == nil {
    87  			if must {
    88  				return fmt.Errorf("invalid usedfor")
    89  			}
    90  			return nil
    91  		}
    92  		switch *usedFor {
    93  		case types.CoinUsedFor_CoinUsedForCouponCash:
    94  		case types.CoinUsedFor_CoinUsedForGoodFee:
    95  		default:
    96  			return fmt.Errorf("invalid usedfor")
    97  		}
    98  		h.UsedFor = usedFor
    99  		return nil
   100  	}
   101  }
   102  
   103  func WithPriority(n *uint32, must bool) func(context.Context, *Handler) error {
   104  	return func(ctx context.Context, h *Handler) error {
   105  		h.Priority = n
   106  		return nil
   107  	}
   108  }
   109  
   110  func WithConds(conds *npool.Conds) func(context.Context, *Handler) error {
   111  	return func(ctx context.Context, h *Handler) error {
   112  		h.Conds = &coinusedforcrud.Conds{}
   113  		if conds == nil {
   114  			return nil
   115  		}
   116  		if conds.EntID != nil {
   117  			id, err := uuid.Parse(conds.GetEntID().GetValue())
   118  			if err != nil {
   119  				return err
   120  			}
   121  			h.Conds.EntID = &cruder.Cond{
   122  				Op:  conds.GetEntID().GetOp(),
   123  				Val: id,
   124  			}
   125  		}
   126  		if conds.CoinTypeID != nil {
   127  			id, err := uuid.Parse(conds.GetCoinTypeID().GetValue())
   128  			if err != nil {
   129  				return err
   130  			}
   131  			h.Conds.CoinTypeID = &cruder.Cond{
   132  				Op:  conds.GetCoinTypeID().GetOp(),
   133  				Val: id,
   134  			}
   135  		}
   136  		if conds.CoinTypeIDs != nil {
   137  			ids := []uuid.UUID{}
   138  			for _, id := range conds.GetCoinTypeIDs().GetValue() {
   139  				_id, err := uuid.Parse(id)
   140  				if err != nil {
   141  					return err
   142  				}
   143  				ids = append(ids, _id)
   144  			}
   145  			h.Conds.CoinTypeIDs = &cruder.Cond{
   146  				Op:  conds.GetCoinTypeIDs().GetOp(),
   147  				Val: ids,
   148  			}
   149  		}
   150  		if conds.UsedFor != nil {
   151  			h.Conds.UsedFor = &cruder.Cond{
   152  				Op:  cruder.EQ,
   153  				Val: types.CoinUsedFor(conds.GetUsedFor().GetValue()),
   154  			}
   155  		}
   156  		if conds.UsedFors != nil {
   157  			usedFors := []types.CoinUsedFor{}
   158  			for _, usedFor := range conds.GetUsedFors().GetValue() {
   159  				usedFors = append(usedFors, types.CoinUsedFor(usedFor))
   160  			}
   161  			h.Conds.UsedFors = &cruder.Cond{
   162  				Op:  cruder.IN,
   163  				Val: usedFors,
   164  			}
   165  		}
   166  		return nil
   167  	}
   168  }
   169  
   170  func WithOffset(offset int32) func(context.Context, *Handler) error {
   171  	return func(ctx context.Context, h *Handler) error {
   172  		h.Offset = offset
   173  		return nil
   174  	}
   175  }
   176  
   177  func WithLimit(limit int32) func(context.Context, *Handler) error {
   178  	return func(ctx context.Context, h *Handler) error {
   179  		if limit == 0 {
   180  			limit = constant.DefaultRowLimit
   181  		}
   182  		h.Limit = limit
   183  		return nil
   184  	}
   185  }