github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/coin/currency/history/handler.go (about) 1 package currencyhistory 2 3 import ( 4 "context" 5 6 constant "github.com/NpoolPlatform/chain-middleware/pkg/const" 7 historycrud "github.com/NpoolPlatform/chain-middleware/pkg/crud/coin/currency/history" 8 npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin/currency/history" 9 10 "github.com/NpoolPlatform/libent-cruder/pkg/cruder" 11 "github.com/google/uuid" 12 ) 13 14 type Handler struct { 15 Conds *historycrud.Conds 16 Offset int32 17 Limit int32 18 } 19 20 func NewHandler(ctx context.Context, options ...func(context.Context, *Handler) error) (*Handler, error) { 21 handler := &Handler{} 22 for _, opt := range options { 23 if err := opt(ctx, handler); err != nil { 24 return nil, err 25 } 26 } 27 return handler, nil 28 } 29 30 func WithConds(conds *npool.Conds) func(context.Context, *Handler) error { 31 return func(ctx context.Context, h *Handler) error { 32 h.Conds = &historycrud.Conds{} 33 if conds == nil { 34 return nil 35 } 36 if conds.EntID != nil { 37 id, err := uuid.Parse(conds.GetEntID().GetValue()) 38 if err != nil { 39 return err 40 } 41 h.Conds.EntID = &cruder.Cond{ 42 Op: conds.GetEntID().GetOp(), 43 Val: id, 44 } 45 } 46 if conds.CoinTypeID != nil { 47 id, err := uuid.Parse(conds.GetCoinTypeID().GetValue()) 48 if err != nil { 49 return err 50 } 51 h.Conds.CoinTypeID = &cruder.Cond{ 52 Op: conds.GetCoinTypeID().GetOp(), 53 Val: id, 54 } 55 } 56 if conds.CoinTypeIDs != nil { 57 ids := []uuid.UUID{} 58 for _, id := range conds.GetCoinTypeIDs().GetValue() { 59 _id, err := uuid.Parse(id) 60 if err != nil { 61 return err 62 } 63 ids = append(ids, _id) 64 } 65 if len(ids) > 0 { 66 h.Conds.CoinTypeIDs = &cruder.Cond{ 67 Op: conds.GetCoinTypeIDs().GetOp(), 68 Val: ids, 69 } 70 } 71 } 72 if conds.StartAt != nil { 73 h.Conds.StartAt = &cruder.Cond{ 74 Op: conds.GetStartAt().GetOp(), 75 Val: conds.GetStartAt().GetValue(), 76 } 77 } 78 if conds.EndAt != nil { 79 h.Conds.EndAt = &cruder.Cond{ 80 Op: conds.GetEndAt().GetOp(), 81 Val: conds.GetEndAt().GetValue(), 82 } 83 } 84 if conds.CoinNames != nil && len(conds.CoinNames.Value) > 0 { 85 h.Conds.CoinNames = &cruder.Cond{ 86 Op: conds.GetCoinNames().GetOp(), 87 Val: conds.GetCoinNames().GetValue(), 88 } 89 } 90 return nil 91 } 92 } 93 94 func WithOffset(offset int32) func(context.Context, *Handler) error { 95 return func(ctx context.Context, h *Handler) error { 96 h.Offset = offset 97 return nil 98 } 99 } 100 101 func WithLimit(limit int32) func(context.Context, *Handler) error { 102 return func(ctx context.Context, h *Handler) error { 103 if limit == 0 { 104 limit = constant.DefaultRowLimit 105 } 106 h.Limit = limit 107 return nil 108 } 109 }