github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/app/coin/description/handler.go (about) 1 package description 2 3 import ( 4 "context" 5 "fmt" 6 7 constant "github.com/NpoolPlatform/chain-middleware/pkg/const" 8 descriptioncrud "github.com/NpoolPlatform/chain-middleware/pkg/crud/app/coin/description" 9 basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1" 10 npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/app/coin/description" 11 12 "github.com/NpoolPlatform/libent-cruder/pkg/cruder" 13 14 "github.com/google/uuid" 15 ) 16 17 type Handler struct { 18 ID *uint32 19 EntID *uuid.UUID 20 AppID *uuid.UUID 21 CoinTypeID *uuid.UUID 22 UsedFor *basetypes.UsedFor 23 Title *string 24 Message *string 25 Conds *descriptioncrud.Conds 26 Offset int32 27 Limit int32 28 } 29 30 func NewHandler(ctx context.Context, options ...func(context.Context, *Handler) error) (*Handler, error) { 31 handler := &Handler{} 32 for _, opt := range options { 33 if err := opt(ctx, handler); err != nil { 34 return nil, err 35 } 36 } 37 return handler, nil 38 } 39 40 func WithID(u *uint32, must bool) func(context.Context, *Handler) error { 41 return func(ctx context.Context, h *Handler) error { 42 if u == nil { 43 if must { 44 return fmt.Errorf("invalid id") 45 } 46 return nil 47 } 48 h.ID = u 49 return nil 50 } 51 } 52 53 func WithEntID(id *string, must bool) func(context.Context, *Handler) error { 54 return func(ctx context.Context, h *Handler) error { 55 if id == nil { 56 if must { 57 return fmt.Errorf("invalid entid") 58 } 59 return nil 60 } 61 _id, err := uuid.Parse(*id) 62 if err != nil { 63 return err 64 } 65 h.EntID = &_id 66 return nil 67 } 68 } 69 70 func WithAppID(id *string, must bool) func(context.Context, *Handler) error { 71 return func(ctx context.Context, h *Handler) error { 72 if id == nil { 73 if must { 74 return fmt.Errorf("invalid appid") 75 } 76 return nil 77 } 78 _id, err := uuid.Parse(*id) 79 if err != nil { 80 return err 81 } 82 h.AppID = &_id 83 return nil 84 } 85 } 86 87 func WithCoinTypeID(id *string, must bool) func(context.Context, *Handler) error { 88 return func(ctx context.Context, h *Handler) error { 89 if id == nil { 90 if must { 91 return fmt.Errorf("invalid cointypeid") 92 } 93 return nil 94 } 95 _id, err := uuid.Parse(*id) 96 if err != nil { 97 return err 98 } 99 h.CoinTypeID = &_id 100 return nil 101 } 102 } 103 104 func WithUsedFor(usedFor *basetypes.UsedFor, must bool) func(context.Context, *Handler) error { 105 return func(ctx context.Context, h *Handler) error { 106 if usedFor == nil { 107 if must { 108 return fmt.Errorf("invalid usedfor") 109 } 110 return nil 111 } 112 switch *usedFor { 113 case basetypes.UsedFor_ProductPage: 114 default: 115 return fmt.Errorf("invalid usedfor") 116 } 117 h.UsedFor = usedFor 118 return nil 119 } 120 } 121 122 func WithTitle(title *string, must bool) func(context.Context, *Handler) error { 123 return func(ctx context.Context, h *Handler) error { 124 if title == nil { 125 if must { 126 return fmt.Errorf("invalid title") 127 } 128 return nil 129 } 130 if *title == "" { 131 return fmt.Errorf("invalid title") 132 } 133 h.Title = title 134 return nil 135 } 136 } 137 138 func WithMessage(message *string, must bool) func(context.Context, *Handler) error { 139 return func(ctx context.Context, h *Handler) error { 140 if message == nil { 141 if must { 142 return fmt.Errorf("invalid message") 143 } 144 return nil 145 } 146 if *message == "" { 147 return fmt.Errorf("invalid message") 148 } 149 h.Message = message 150 return nil 151 } 152 } 153 154 func WithConds(conds *npool.Conds) func(context.Context, *Handler) error { 155 return func(ctx context.Context, h *Handler) error { 156 h.Conds = &descriptioncrud.Conds{} 157 if conds == nil { 158 return nil 159 } 160 if conds.EntID != nil { 161 id, err := uuid.Parse(conds.GetEntID().GetValue()) 162 if err != nil { 163 return err 164 } 165 h.Conds.EntID = &cruder.Cond{ 166 Op: conds.GetEntID().GetOp(), 167 Val: id, 168 } 169 } 170 if conds.AppID != nil { 171 id, err := uuid.Parse(conds.GetAppID().GetValue()) 172 if err != nil { 173 return err 174 } 175 h.Conds.AppID = &cruder.Cond{ 176 Op: conds.GetAppID().GetOp(), 177 Val: id, 178 } 179 } 180 if conds.CoinTypeID != nil { 181 id, err := uuid.Parse(conds.GetCoinTypeID().GetValue()) 182 if err != nil { 183 return err 184 } 185 h.Conds.CoinTypeID = &cruder.Cond{ 186 Op: conds.GetCoinTypeID().GetOp(), 187 Val: id, 188 } 189 } 190 if conds.UsedFor != nil { 191 h.Conds.UsedFor = &cruder.Cond{ 192 Op: conds.GetUsedFor().GetOp(), 193 Val: basetypes.UsedFor(conds.GetUsedFor().GetValue()), 194 } 195 } 196 return nil 197 } 198 } 199 200 func WithOffset(offset int32) func(context.Context, *Handler) error { 201 return func(ctx context.Context, h *Handler) error { 202 h.Offset = offset 203 return nil 204 } 205 } 206 207 func WithLimit(limit int32) func(context.Context, *Handler) error { 208 return func(ctx context.Context, h *Handler) error { 209 if limit == 0 { 210 limit = constant.DefaultRowLimit 211 } 212 h.Limit = limit 213 return nil 214 } 215 }