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

     1  package appcoin
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	constant "github.com/NpoolPlatform/chain-middleware/pkg/const"
     8  	coincrud "github.com/NpoolPlatform/chain-middleware/pkg/crud/app/coin"
     9  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/app/coin"
    10  
    11  	"github.com/NpoolPlatform/libent-cruder/pkg/cruder"
    12  
    13  	"github.com/google/uuid"
    14  	"github.com/shopspring/decimal"
    15  )
    16  
    17  type Handler struct {
    18  	ID                       *uint32
    19  	EntID                    *uuid.UUID
    20  	AppID                    *uuid.UUID
    21  	CoinTypeID               *uuid.UUID
    22  	Name                     *string
    23  	DisplayNames             []string
    24  	Logo                     *string
    25  	ForPay                   *bool
    26  	WithdrawAutoReviewAmount *decimal.Decimal
    27  	MarketValue              *decimal.Decimal
    28  	SettlePercent            *uint32
    29  	SettleTips               []string
    30  	Setter                   *uuid.UUID
    31  	ProductPage              *string
    32  	DailyRewardAmount        *decimal.Decimal
    33  	Disabled                 *bool
    34  	Display                  *bool
    35  	DisplayIndex             *uint32
    36  	MaxAmountPerWithdraw     *decimal.Decimal
    37  	Conds                    *coincrud.Conds
    38  	Offset                   int32
    39  	Limit                    int32
    40  }
    41  
    42  func NewHandler(ctx context.Context, options ...func(context.Context, *Handler) error) (*Handler, error) {
    43  	handler := &Handler{}
    44  	for _, opt := range options {
    45  		if err := opt(ctx, handler); err != nil {
    46  			return nil, err
    47  		}
    48  	}
    49  	return handler, nil
    50  }
    51  
    52  func WithID(u *uint32, must bool) func(context.Context, *Handler) error {
    53  	return func(ctx context.Context, h *Handler) error {
    54  		if u == nil {
    55  			if must {
    56  				return fmt.Errorf("invalid id")
    57  			}
    58  			return nil
    59  		}
    60  		h.ID = u
    61  		return nil
    62  	}
    63  }
    64  
    65  func WithEntID(id *string, must bool) func(context.Context, *Handler) error {
    66  	return func(ctx context.Context, h *Handler) error {
    67  		if id == nil {
    68  			if must {
    69  				return fmt.Errorf("invalid entid")
    70  			}
    71  			return nil
    72  		}
    73  		_id, err := uuid.Parse(*id)
    74  		if err != nil {
    75  			return err
    76  		}
    77  		h.EntID = &_id
    78  		return nil
    79  	}
    80  }
    81  
    82  func WithAppID(id *string, must bool) func(context.Context, *Handler) error {
    83  	return func(ctx context.Context, h *Handler) error {
    84  		if id == nil {
    85  			if must {
    86  				return fmt.Errorf("invalid appid")
    87  			}
    88  			return nil
    89  		}
    90  		_id, err := uuid.Parse(*id)
    91  		if err != nil {
    92  			return err
    93  		}
    94  		h.AppID = &_id
    95  		return nil
    96  	}
    97  }
    98  
    99  func WithCoinTypeID(id *string, must bool) func(context.Context, *Handler) error {
   100  	return func(ctx context.Context, h *Handler) error {
   101  		if id == nil {
   102  			if must {
   103  				return fmt.Errorf("invalid cointypeid")
   104  			}
   105  			return nil
   106  		}
   107  		_id, err := uuid.Parse(*id)
   108  		if err != nil {
   109  			return err
   110  		}
   111  		h.CoinTypeID = &_id
   112  		return nil
   113  	}
   114  }
   115  
   116  func WithName(name *string, must bool) func(context.Context, *Handler) error {
   117  	return func(ctx context.Context, h *Handler) error {
   118  		if name == nil {
   119  			if must {
   120  				return fmt.Errorf("invalid name")
   121  			}
   122  			return nil
   123  		}
   124  		if *name == "" {
   125  			return fmt.Errorf("invalid coinname")
   126  		}
   127  		h.Name = name
   128  		return nil
   129  	}
   130  }
   131  
   132  func WithDisplayNames(names []string, must bool) func(context.Context, *Handler) error {
   133  	return func(ctx context.Context, h *Handler) error {
   134  		h.DisplayNames = names
   135  		return nil
   136  	}
   137  }
   138  
   139  func WithLogo(logo *string, must bool) func(context.Context, *Handler) error {
   140  	return func(ctx context.Context, h *Handler) error {
   141  		h.Logo = logo
   142  		return nil
   143  	}
   144  }
   145  
   146  func WithForPay(forPay *bool, must bool) func(context.Context, *Handler) error {
   147  	return func(ctx context.Context, h *Handler) error {
   148  		h.ForPay = forPay
   149  		return nil
   150  	}
   151  }
   152  
   153  func WithWithdrawAutoReviewAmount(amount *string, must bool) func(context.Context, *Handler) error {
   154  	return func(ctx context.Context, h *Handler) error {
   155  		if amount == nil {
   156  			if must {
   157  				return fmt.Errorf("invalid amount")
   158  			}
   159  			return nil
   160  		}
   161  		_amount, err := decimal.NewFromString(*amount)
   162  		if err != nil {
   163  			return err
   164  		}
   165  		h.WithdrawAutoReviewAmount = &_amount
   166  		return nil
   167  	}
   168  }
   169  
   170  func WithMarketValue(amount *string, must bool) func(context.Context, *Handler) error {
   171  	return func(ctx context.Context, h *Handler) error {
   172  		if amount == nil {
   173  			if must {
   174  				return fmt.Errorf("invalid marketvalue")
   175  			}
   176  			return nil
   177  		}
   178  		_amount, err := decimal.NewFromString(*amount)
   179  		if err != nil {
   180  			return err
   181  		}
   182  		h.MarketValue = &_amount
   183  		return nil
   184  	}
   185  }
   186  
   187  func WithSettlePercent(percent *uint32, must bool) func(context.Context, *Handler) error {
   188  	return func(ctx context.Context, h *Handler) error {
   189  		if percent == nil {
   190  			if must {
   191  				return fmt.Errorf("invalid settlepercent")
   192  			}
   193  			return nil
   194  		}
   195  		if *percent == 0 {
   196  			return fmt.Errorf("invalid percent")
   197  		}
   198  		h.SettlePercent = percent
   199  		return nil
   200  	}
   201  }
   202  
   203  func WithSettleTips(tips []string, must bool) func(context.Context, *Handler) error {
   204  	return func(ctx context.Context, h *Handler) error {
   205  		h.SettleTips = tips
   206  		return nil
   207  	}
   208  }
   209  
   210  func WithSetter(id *string, must bool) func(context.Context, *Handler) error {
   211  	return func(ctx context.Context, h *Handler) error {
   212  		if id == nil {
   213  			if must {
   214  				return fmt.Errorf("invalid setter")
   215  			}
   216  			return nil
   217  		}
   218  		_id, err := uuid.Parse(*id)
   219  		if err != nil {
   220  			return err
   221  		}
   222  		h.Setter = &_id
   223  		return nil
   224  	}
   225  }
   226  
   227  func WithProductPage(page *string, must bool) func(context.Context, *Handler) error {
   228  	return func(ctx context.Context, h *Handler) error {
   229  		h.ProductPage = page
   230  		return nil
   231  	}
   232  }
   233  
   234  func WithDisabled(disabled *bool, must bool) func(context.Context, *Handler) error {
   235  	return func(ctx context.Context, h *Handler) error {
   236  		h.Disabled = disabled
   237  		return nil
   238  	}
   239  }
   240  
   241  func WithDailyRewardAmount(amount *string, must bool) func(context.Context, *Handler) error {
   242  	return func(ctx context.Context, h *Handler) error {
   243  		if amount == nil {
   244  			if must {
   245  				return fmt.Errorf("invalid dailyrewardamount")
   246  			}
   247  			return nil
   248  		}
   249  		_amount, err := decimal.NewFromString(*amount)
   250  		if err != nil {
   251  			return err
   252  		}
   253  		h.DailyRewardAmount = &_amount
   254  		return nil
   255  	}
   256  }
   257  
   258  func WithDisplay(display *bool, must bool) func(context.Context, *Handler) error {
   259  	return func(ctx context.Context, h *Handler) error {
   260  		h.Display = display
   261  		return nil
   262  	}
   263  }
   264  
   265  func WithDisplayIndex(index *uint32, must bool) func(context.Context, *Handler) error {
   266  	return func(ctx context.Context, h *Handler) error {
   267  		h.DisplayIndex = index
   268  		return nil
   269  	}
   270  }
   271  
   272  func WithMaxAmountPerWithdraw(amount *string, must bool) func(context.Context, *Handler) error {
   273  	return func(ctx context.Context, h *Handler) error {
   274  		if amount == nil {
   275  			if must {
   276  				return fmt.Errorf("invalid maxamountperwithdraw")
   277  			}
   278  			return nil
   279  		}
   280  		_amount, err := decimal.NewFromString(*amount)
   281  		if err != nil {
   282  			return err
   283  		}
   284  		h.MaxAmountPerWithdraw = &_amount
   285  		return nil
   286  	}
   287  }
   288  
   289  //nolint:gocyclo
   290  func WithConds(conds *npool.Conds) func(context.Context, *Handler) error {
   291  	return func(ctx context.Context, h *Handler) error {
   292  		h.Conds = &coincrud.Conds{}
   293  		if conds == nil {
   294  			return nil
   295  		}
   296  		if conds.EntID != nil {
   297  			id, err := uuid.Parse(conds.GetEntID().GetValue())
   298  			if err != nil {
   299  				return err
   300  			}
   301  			h.Conds.EntID = &cruder.Cond{
   302  				Op:  conds.GetEntID().GetOp(),
   303  				Val: id,
   304  			}
   305  		}
   306  		if conds.AppID != nil {
   307  			id, err := uuid.Parse(conds.GetAppID().GetValue())
   308  			if err != nil {
   309  				return err
   310  			}
   311  			h.Conds.AppID = &cruder.Cond{
   312  				Op:  conds.GetAppID().GetOp(),
   313  				Val: id,
   314  			}
   315  		}
   316  		if conds.CoinTypeID != nil {
   317  			id, err := uuid.Parse(conds.GetCoinTypeID().GetValue())
   318  			if err != nil {
   319  				return err
   320  			}
   321  			h.Conds.CoinTypeID = &cruder.Cond{
   322  				Op:  conds.GetCoinTypeID().GetOp(),
   323  				Val: id,
   324  			}
   325  		}
   326  		if conds.ForPay != nil {
   327  			h.Conds.ForPay = &cruder.Cond{
   328  				Op:  conds.GetForPay().GetOp(),
   329  				Val: conds.GetForPay().GetValue(),
   330  			}
   331  		}
   332  		if conds.Disabled != nil {
   333  			h.Conds.Disabled = &cruder.Cond{
   334  				Op:  conds.GetDisabled().GetOp(),
   335  				Val: conds.GetDisabled().GetValue(),
   336  			}
   337  		}
   338  		if conds.EntIDs != nil {
   339  			ids := []uuid.UUID{}
   340  			for _, id := range conds.GetEntIDs().GetValue() {
   341  				_id, err := uuid.Parse(id)
   342  				if err != nil {
   343  					return err
   344  				}
   345  				ids = append(ids, _id)
   346  			}
   347  			h.Conds.EntIDs = &cruder.Cond{
   348  				Op:  conds.GetEntIDs().GetOp(),
   349  				Val: ids,
   350  			}
   351  		}
   352  		if conds.CoinTypeIDs != nil {
   353  			ids := []uuid.UUID{}
   354  			for _, id := range conds.GetCoinTypeIDs().GetValue() {
   355  				_id, err := uuid.Parse(id)
   356  				if err != nil {
   357  					return err
   358  				}
   359  				ids = append(ids, _id)
   360  			}
   361  			h.Conds.CoinTypeIDs = &cruder.Cond{
   362  				Op:  conds.GetCoinTypeIDs().GetOp(),
   363  				Val: ids,
   364  			}
   365  		}
   366  		return nil
   367  	}
   368  }
   369  
   370  func WithOffset(offset int32) func(context.Context, *Handler) error {
   371  	return func(ctx context.Context, h *Handler) error {
   372  		h.Offset = offset
   373  		return nil
   374  	}
   375  }
   376  
   377  func WithLimit(limit int32) func(context.Context, *Handler) error {
   378  	return func(ctx context.Context, h *Handler) error {
   379  		if limit == 0 {
   380  			limit = constant.DefaultRowLimit
   381  		}
   382  		h.Limit = limit
   383  		return nil
   384  	}
   385  }