github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/fiat/handler.go (about) 1 package fiat 2 3 import ( 4 "context" 5 "fmt" 6 7 constant "github.com/NpoolPlatform/chain-middleware/pkg/const" 8 fiatcrud "github.com/NpoolPlatform/chain-middleware/pkg/crud/fiat" 9 npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/fiat" 10 11 "github.com/NpoolPlatform/libent-cruder/pkg/cruder" 12 "github.com/google/uuid" 13 ) 14 15 type Handler struct { 16 ID *uint32 17 EntID *uuid.UUID 18 Name *string 19 Logo *string 20 Unit *string 21 Conds *fiatcrud.Conds 22 Offset int32 23 Limit int32 24 } 25 26 func NewHandler(ctx context.Context, options ...func(context.Context, *Handler) error) (*Handler, error) { 27 handler := &Handler{} 28 for _, opt := range options { 29 if err := opt(ctx, handler); err != nil { 30 return nil, err 31 } 32 } 33 return handler, nil 34 } 35 36 func WithID(u *uint32, must bool) func(context.Context, *Handler) error { 37 return func(ctx context.Context, h *Handler) error { 38 if u == nil { 39 if must { 40 return fmt.Errorf("invalid id") 41 } 42 return nil 43 } 44 h.ID = u 45 return nil 46 } 47 } 48 49 func WithEntID(id *string, must bool) func(context.Context, *Handler) error { 50 return func(ctx context.Context, h *Handler) error { 51 if id == nil { 52 if must { 53 return fmt.Errorf("invalid entid") 54 } 55 return nil 56 } 57 _id, err := uuid.Parse(*id) 58 if err != nil { 59 return err 60 } 61 h.EntID = &_id 62 return nil 63 } 64 } 65 66 func WithName(name *string, must bool) func(context.Context, *Handler) error { 67 return func(ctx context.Context, h *Handler) error { 68 if name == nil { 69 if must { 70 return fmt.Errorf("invalid name") 71 } 72 return nil 73 } 74 if *name == "" { 75 return fmt.Errorf("invalid fiatname") 76 } 77 h.Name = name 78 return nil 79 } 80 } 81 82 func WithLogo(logo *string, must bool) func(context.Context, *Handler) error { 83 return func(ctx context.Context, h *Handler) error { 84 h.Logo = logo 85 return nil 86 } 87 } 88 89 func WithUnit(unit *string, must bool) func(context.Context, *Handler) error { 90 return func(ctx context.Context, h *Handler) error { 91 if unit == nil { 92 if must { 93 return fmt.Errorf("invalid unit") 94 } 95 return nil 96 } 97 if *unit == "" { 98 return fmt.Errorf("invalid fiatunit") 99 } 100 h.Unit = unit 101 return nil 102 } 103 } 104 105 func WithConds(conds *npool.Conds) func(context.Context, *Handler) error { 106 return func(ctx context.Context, h *Handler) error { 107 h.Conds = &fiatcrud.Conds{} 108 if conds == nil { 109 return nil 110 } 111 if conds.EntID != nil { 112 id, err := uuid.Parse(conds.GetEntID().GetValue()) 113 if err != nil { 114 return err 115 } 116 h.Conds.EntID = &cruder.Cond{ 117 Op: conds.GetEntID().GetOp(), 118 Val: id, 119 } 120 } 121 if conds.Name != nil { 122 h.Conds.Name = &cruder.Cond{ 123 Op: conds.GetName().GetOp(), 124 Val: conds.GetName().GetValue(), 125 } 126 } 127 return nil 128 } 129 } 130 131 func WithOffset(offset int32) func(context.Context, *Handler) error { 132 return func(ctx context.Context, h *Handler) error { 133 h.Offset = offset 134 return nil 135 } 136 } 137 138 func WithLimit(limit int32) func(context.Context, *Handler) error { 139 return func(ctx context.Context, h *Handler) error { 140 if limit == 0 { 141 limit = constant.DefaultRowLimit 142 } 143 h.Limit = limit 144 return nil 145 } 146 }