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