github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/crud/app/coin/crud.go (about) 1 package appcoin 2 3 import ( 4 "fmt" 5 6 "github.com/NpoolPlatform/chain-middleware/pkg/db/ent" 7 entappcoin "github.com/NpoolPlatform/chain-middleware/pkg/db/ent/appcoin" 8 "github.com/NpoolPlatform/libent-cruder/pkg/cruder" 9 10 "github.com/google/uuid" 11 "github.com/shopspring/decimal" 12 ) 13 14 type Req struct { 15 ID *uint32 16 EntID *uuid.UUID 17 AppID *uuid.UUID 18 CoinTypeID *uuid.UUID 19 Name *string 20 DisplayNames []string 21 Logo *string 22 ForPay *bool 23 WithdrawAutoReviewAmount *decimal.Decimal 24 ProductPage *string 25 Disabled *bool 26 DailyRewardAmount *decimal.Decimal 27 Display *bool 28 DisplayIndex *uint32 29 MaxAmountPerWithdraw *decimal.Decimal 30 DeletedAt *uint32 31 } 32 33 //nolint:gocyclo 34 func CreateSet(c *ent.AppCoinCreate, req *Req) *ent.AppCoinCreate { 35 if req.ID != nil { 36 c.SetID(*req.ID) 37 } 38 if req.EntID != nil { 39 c.SetEntID(*req.EntID) 40 } 41 if req.AppID != nil { 42 c.SetAppID(*req.AppID) 43 } 44 if req.CoinTypeID != nil { 45 c.SetCoinTypeID(*req.CoinTypeID) 46 } 47 if req.Name != nil { 48 c.SetName(*req.Name) 49 } 50 if len(req.DisplayNames) > 0 { 51 c.SetDisplayNames(req.DisplayNames) 52 } 53 if req.Logo != nil { 54 c.SetLogo(*req.Logo) 55 } 56 if req.ForPay != nil { 57 c.SetForPay(*req.ForPay) 58 } 59 if req.WithdrawAutoReviewAmount != nil { 60 c.SetWithdrawAutoReviewAmount(*req.WithdrawAutoReviewAmount) 61 } 62 if req.ProductPage != nil { 63 c.SetProductPage(*req.ProductPage) 64 } 65 if req.Disabled != nil { 66 c.SetDisabled(*req.Disabled) 67 } 68 if req.DailyRewardAmount != nil { 69 c.SetDailyRewardAmount(*req.DailyRewardAmount) 70 } 71 if req.Display != nil { 72 c.SetDisplay(*req.Display) 73 } 74 if req.DisplayIndex != nil { 75 c.SetDisplayIndex(*req.DisplayIndex) 76 } 77 if req.MaxAmountPerWithdraw != nil { 78 c.SetMaxAmountPerWithdraw(*req.MaxAmountPerWithdraw) 79 } 80 return c 81 } 82 83 func UpdateSet(u *ent.AppCoinUpdateOne, req *Req) *ent.AppCoinUpdateOne { 84 if req.Name != nil { 85 u = u.SetName(*req.Name) 86 } 87 if len(req.DisplayNames) > 0 { 88 u = u.SetDisplayNames(req.DisplayNames) 89 } 90 if req.Logo != nil { 91 u = u.SetLogo(*req.Logo) 92 } 93 if req.ForPay != nil { 94 u = u.SetForPay(*req.ForPay) 95 } 96 if req.WithdrawAutoReviewAmount != nil { 97 u = u.SetWithdrawAutoReviewAmount(*req.WithdrawAutoReviewAmount) 98 } 99 if req.DeletedAt != nil { 100 u = u.SetDeletedAt(*req.DeletedAt) 101 } 102 if req.ProductPage != nil { 103 u = u.SetProductPage(*req.ProductPage) 104 } 105 if req.Disabled != nil { 106 u = u.SetDisabled(*req.Disabled) 107 } 108 if req.DailyRewardAmount != nil { 109 u = u.SetDailyRewardAmount(*req.DailyRewardAmount) 110 } 111 if req.Display != nil { 112 u = u.SetDisplay(*req.Display) 113 } 114 if req.DisplayIndex != nil { 115 u = u.SetDisplayIndex(*req.DisplayIndex) 116 } 117 if req.MaxAmountPerWithdraw != nil { 118 u = u.SetMaxAmountPerWithdraw(*req.MaxAmountPerWithdraw) 119 } 120 121 return u 122 } 123 124 type Conds struct { 125 ID *cruder.Cond 126 EntID *cruder.Cond 127 AppID *cruder.Cond 128 CoinTypeID *cruder.Cond 129 ForPay *cruder.Cond 130 Disabled *cruder.Cond 131 EntIDs *cruder.Cond 132 CoinTypeIDs *cruder.Cond 133 } 134 135 //nolint 136 func SetQueryConds(q *ent.AppCoinQuery, conds *Conds) (*ent.AppCoinQuery, error) { 137 if conds.ID != nil { 138 id, ok := conds.ID.Val.(uint32) 139 if !ok { 140 return nil, fmt.Errorf("invalid id") 141 } 142 switch conds.ID.Op { 143 case cruder.EQ: 144 q.Where(entappcoin.ID(id)) 145 default: 146 return nil, fmt.Errorf("invalid appcoin field") 147 } 148 } 149 if conds.EntID != nil { 150 id, ok := conds.EntID.Val.(uuid.UUID) 151 if !ok { 152 return nil, fmt.Errorf("invalid entid") 153 } 154 switch conds.EntID.Op { 155 case cruder.EQ: 156 q.Where(entappcoin.EntID(id)) 157 default: 158 return nil, fmt.Errorf("invalid appcoin field") 159 } 160 } 161 if conds.AppID != nil { 162 id, ok := conds.AppID.Val.(uuid.UUID) 163 if !ok { 164 return nil, fmt.Errorf("invalid appid") 165 } 166 switch conds.AppID.Op { 167 case cruder.EQ: 168 q.Where(entappcoin.AppID(id)) 169 default: 170 return nil, fmt.Errorf("invalid appcoin field") 171 } 172 } 173 if conds.CoinTypeID != nil { 174 id, ok := conds.CoinTypeID.Val.(uuid.UUID) 175 if !ok { 176 return nil, fmt.Errorf("invalid cointypeid") 177 } 178 switch conds.CoinTypeID.Op { 179 case cruder.EQ: 180 q.Where(entappcoin.CoinTypeID(id)) 181 default: 182 return nil, fmt.Errorf("invalid appcoin field") 183 } 184 } 185 if conds.ForPay != nil { 186 forPay, ok := conds.ForPay.Val.(bool) 187 if !ok { 188 return nil, fmt.Errorf("invalid forpay") 189 } 190 switch conds.ForPay.Op { 191 case cruder.EQ: 192 q.Where(entappcoin.ForPay(forPay)) 193 default: 194 return nil, fmt.Errorf("invalid appcoin field") 195 } 196 } 197 if conds.Disabled != nil { 198 disabled, ok := conds.Disabled.Val.(bool) 199 if !ok { 200 return nil, fmt.Errorf("invalid disabled") 201 } 202 switch conds.Disabled.Op { 203 case cruder.EQ: 204 q.Where(entappcoin.Disabled(disabled)) 205 default: 206 return nil, fmt.Errorf("invalid appcoin field") 207 } 208 } 209 if conds.EntIDs != nil { 210 ids, ok := conds.EntIDs.Val.([]uuid.UUID) 211 if !ok { 212 return nil, fmt.Errorf("invalid entids") 213 } 214 switch conds.EntIDs.Op { 215 case cruder.IN: 216 q.Where(entappcoin.EntIDIn(ids...)) 217 default: 218 return nil, fmt.Errorf("invalid appcoin field") 219 } 220 } 221 if conds.CoinTypeIDs != nil { 222 ids, ok := conds.CoinTypeIDs.Val.([]uuid.UUID) 223 if !ok { 224 return nil, fmt.Errorf("invalid cointypeids") 225 } 226 switch conds.CoinTypeIDs.Op { 227 case cruder.IN: 228 q.Where(entappcoin.CoinTypeIDIn(ids...)) 229 default: 230 return nil, fmt.Errorf("invalid appcoin field") 231 } 232 } 233 q.Where(entappcoin.DeletedAt(0)) 234 return q, nil 235 }