github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/coin/fiat/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/fiat/currency/history" 8 npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin/fiat/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.CoinTypeID != nil { 37 id, err := uuid.Parse(conds.GetCoinTypeID().GetValue()) 38 if err != nil { 39 return err 40 } 41 h.Conds.CoinTypeID = &cruder.Cond{ 42 Op: conds.GetCoinTypeID().GetOp(), 43 Val: id, 44 } 45 } 46 if conds.CoinTypeIDs != nil { 47 ids := []uuid.UUID{} 48 for _, id := range conds.GetCoinTypeIDs().GetValue() { 49 _id, err := uuid.Parse(id) 50 if err != nil { 51 return err 52 } 53 ids = append(ids, _id) 54 } 55 h.Conds.CoinTypeIDs = &cruder.Cond{ 56 Op: conds.GetCoinTypeIDs().GetOp(), 57 Val: ids, 58 } 59 } 60 if conds.StartAt != nil { 61 h.Conds.StartAt = &cruder.Cond{ 62 Op: conds.GetStartAt().GetOp(), 63 Val: conds.GetStartAt().GetValue(), 64 } 65 } 66 if conds.EndAt != nil { 67 h.Conds.EndAt = &cruder.Cond{ 68 Op: conds.GetEndAt().GetOp(), 69 Val: conds.GetEndAt().GetValue(), 70 } 71 } 72 return nil 73 } 74 } 75 76 func WithOffset(offset int32) func(context.Context, *Handler) error { 77 return func(ctx context.Context, h *Handler) error { 78 h.Offset = offset 79 return nil 80 } 81 } 82 83 func WithLimit(limit int32) func(context.Context, *Handler) error { 84 return func(ctx context.Context, h *Handler) error { 85 if limit == 0 { 86 limit = constant.DefaultRowLimit 87 } 88 h.Limit = limit 89 return nil 90 } 91 }