github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/crud/coin/crud.go (about) 1 package coinbase 2 3 import ( 4 "fmt" 5 6 "github.com/NpoolPlatform/chain-middleware/pkg/db/ent" 7 entcoinbase "github.com/NpoolPlatform/chain-middleware/pkg/db/ent/coinbase" 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 EntID *uuid.UUID 16 Name *string 17 Logo *string 18 Presale *bool 19 Unit *string 20 ENV *string 21 ReservedAmount *decimal.Decimal 22 ForPay *bool 23 Disabled *bool 24 DeletedAt *uint32 25 } 26 27 func CreateSet(c *ent.CoinBaseCreate, req *Req) *ent.CoinBaseCreate { 28 if req.EntID != nil { 29 c.SetEntID(*req.EntID) 30 } 31 if req.Name != nil { 32 c.SetName(*req.Name) 33 } 34 if req.Logo != nil { 35 c.SetLogo(*req.Logo) 36 } 37 if req.Presale != nil { 38 c.SetPresale(*req.Presale) 39 } 40 if req.Unit != nil { 41 c.SetUnit(*req.Unit) 42 } 43 if req.ENV != nil { 44 c.SetEnv(*req.ENV) 45 } 46 if req.ReservedAmount != nil { 47 c.SetReservedAmount(*req.ReservedAmount) 48 } 49 if req.ForPay != nil { 50 c.SetForPay(*req.ForPay) 51 } 52 if req.Disabled != nil { 53 c.SetDisabled(*req.Disabled) 54 } 55 return c 56 } 57 58 func UpdateSet(u *ent.CoinBaseUpdateOne, req *Req) *ent.CoinBaseUpdateOne { 59 if req.Logo != nil { 60 u = u.SetLogo(*req.Logo) 61 } 62 if req.Presale != nil { 63 u = u.SetPresale(*req.Presale) 64 } 65 if req.ReservedAmount != nil { 66 u = u.SetReservedAmount(*req.ReservedAmount) 67 } 68 if req.ForPay != nil { 69 u = u.SetForPay(*req.ForPay) 70 } 71 if req.Disabled != nil { 72 u = u.SetDisabled(*req.Disabled) 73 } 74 if req.DeletedAt != nil { 75 u = u.SetDeletedAt(*req.DeletedAt) 76 } 77 78 return u 79 } 80 81 type Conds struct { 82 EntID *cruder.Cond 83 EntIDs *cruder.Cond 84 Name *cruder.Cond 85 Unit *cruder.Cond 86 ENV *cruder.Cond 87 Presale *cruder.Cond 88 ForPay *cruder.Cond 89 Disabled *cruder.Cond 90 Names *cruder.Cond 91 } 92 93 // nolint:funlen,gocyclo 94 func SetQueryConds(q *ent.CoinBaseQuery, conds *Conds) (*ent.CoinBaseQuery, error) { 95 if conds.EntID != nil { 96 id, ok := conds.EntID.Val.(uuid.UUID) 97 if !ok { 98 return nil, fmt.Errorf("invalid entid") 99 } 100 switch conds.EntID.Op { 101 case cruder.EQ: 102 q.Where(entcoinbase.EntID(id)) 103 default: 104 return nil, fmt.Errorf("invalid coinbase field") 105 } 106 } 107 if conds.EntIDs != nil { 108 ids, ok := conds.EntIDs.Val.([]uuid.UUID) 109 if !ok { 110 return nil, fmt.Errorf("invalid entids") 111 } 112 switch conds.EntIDs.Op { 113 case cruder.IN: 114 q.Where(entcoinbase.EntIDIn(ids...)) 115 default: 116 return nil, fmt.Errorf("invalid coinbase field") 117 } 118 } 119 if conds.Name != nil { 120 name, ok := conds.Name.Val.(string) 121 if !ok { 122 return nil, fmt.Errorf("invalid name") 123 } 124 switch conds.Name.Op { 125 case cruder.EQ: 126 q.Where( 127 entcoinbase.Name(name), 128 ) 129 default: 130 return nil, fmt.Errorf("invalid coinbase field") 131 } 132 } 133 if conds.Unit != nil { 134 unit, ok := conds.Unit.Val.(string) 135 if !ok { 136 return nil, fmt.Errorf("invalid unit") 137 } 138 switch conds.Unit.Op { 139 case cruder.EQ: 140 q.Where( 141 entcoinbase.Unit(unit), 142 ) 143 default: 144 return nil, fmt.Errorf("invalid coinbase field") 145 } 146 } 147 if conds.ENV != nil { 148 env, ok := conds.ENV.Val.(string) 149 if !ok { 150 return nil, fmt.Errorf("invalid env") 151 } 152 switch conds.ENV.Op { 153 case cruder.EQ: 154 q.Where(entcoinbase.Env(env)) 155 default: 156 return nil, fmt.Errorf("invalid coinbase field") 157 } 158 } 159 if conds.Presale != nil { 160 presale, ok := conds.Presale.Val.(bool) 161 if !ok { 162 return nil, fmt.Errorf("invalid presale") 163 } 164 switch conds.Presale.Op { 165 case cruder.EQ: 166 q.Where(entcoinbase.Presale(presale)) 167 default: 168 return nil, fmt.Errorf("invalid coinbase field") 169 } 170 } 171 if conds.ForPay != nil { 172 forPay, ok := conds.ForPay.Val.(bool) 173 if !ok { 174 return nil, fmt.Errorf("invalid forpay") 175 } 176 switch conds.ForPay.Op { 177 case cruder.EQ: 178 q.Where(entcoinbase.ForPay(forPay)) 179 default: 180 return nil, fmt.Errorf("invalid coinbase field") 181 } 182 } 183 if conds.Disabled != nil { 184 disabled, ok := conds.Disabled.Val.(bool) 185 if !ok { 186 return nil, fmt.Errorf("invalid disabled") 187 } 188 switch conds.Disabled.Op { 189 case cruder.EQ: 190 q.Where(entcoinbase.Disabled(disabled)) 191 default: 192 return nil, fmt.Errorf("invalid coinbase field") 193 } 194 } 195 if conds.Names != nil { 196 names, ok := conds.Names.Val.([]string) 197 if !ok { 198 return nil, fmt.Errorf("invalid names") 199 } 200 switch conds.Names.Op { 201 case cruder.IN: 202 q.Where(entcoinbase.NameIn(names...)) 203 default: 204 return nil, fmt.Errorf("invalid coinbase field") 205 } 206 } 207 q.Where(entcoinbase.DeletedAt(0)) 208 return q, nil 209 }