github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/crud/fiat/crud.go (about) 1 package currencytype 2 3 import ( 4 "fmt" 5 6 "github.com/NpoolPlatform/chain-middleware/pkg/db/ent" 7 entfiat "github.com/NpoolPlatform/chain-middleware/pkg/db/ent/fiat" 8 "github.com/NpoolPlatform/libent-cruder/pkg/cruder" 9 10 "github.com/google/uuid" 11 ) 12 13 type Req struct { 14 EntID *uuid.UUID 15 Name *string 16 Logo *string 17 Unit *string 18 } 19 20 func CreateSet(c *ent.FiatCreate, req *Req) *ent.FiatCreate { 21 if req.EntID != nil { 22 c.SetEntID(*req.EntID) 23 } 24 if req.Name != nil { 25 c.SetName(*req.Name) 26 } 27 if req.Logo != nil { 28 c.SetLogo(*req.Logo) 29 } 30 if req.Unit != nil { 31 c.SetUnit(*req.Unit) 32 } 33 return c 34 } 35 36 func UpdateSet(u *ent.FiatUpdateOne, req *Req) *ent.FiatUpdateOne { 37 if req.Name != nil { 38 u.SetName(*req.Name) 39 } 40 if req.Logo != nil { 41 u.SetLogo(*req.Logo) 42 } 43 if req.Unit != nil { 44 u.SetUnit(*req.Unit) 45 } 46 return u 47 } 48 49 type Conds struct { 50 EntID *cruder.Cond 51 EntIDs *cruder.Cond 52 Name *cruder.Cond 53 Unit *cruder.Cond 54 } 55 56 func SetQueryConds(q *ent.FiatQuery, conds *Conds) (*ent.FiatQuery, error) { 57 if conds.EntID != nil { 58 id, ok := conds.EntID.Val.(uuid.UUID) 59 if !ok { 60 return nil, fmt.Errorf("invalid entid") 61 } 62 switch conds.EntID.Op { 63 case cruder.EQ: 64 q.Where(entfiat.EntID(id)) 65 default: 66 return nil, fmt.Errorf("invalid fiat field") 67 } 68 } 69 if conds.EntIDs != nil { 70 ids, ok := conds.EntIDs.Val.([]uuid.UUID) 71 if !ok { 72 return nil, fmt.Errorf("invalid entids") 73 } 74 switch conds.EntIDs.Op { 75 case cruder.IN: 76 q.Where(entfiat.EntIDIn(ids...)) 77 default: 78 return nil, fmt.Errorf("invalid fiat field") 79 } 80 } 81 if conds.Name != nil { 82 name, ok := conds.Name.Val.(string) 83 if !ok { 84 return nil, fmt.Errorf("invalid name") 85 } 86 switch conds.Name.Op { 87 case cruder.EQ: 88 q.Where(entfiat.Name(name)) 89 default: 90 return nil, fmt.Errorf("invalid fiat field") 91 } 92 } 93 if conds.Unit != nil { 94 unit, ok := conds.Unit.Val.(string) 95 if !ok { 96 return nil, fmt.Errorf("invalid name") 97 } 98 switch conds.Unit.Op { 99 case cruder.EQ: 100 q.Where(entfiat.Unit(unit)) 101 default: 102 return nil, fmt.Errorf("invalid fiat field") 103 } 104 } 105 q.Where(entfiat.DeletedAt(0)) 106 return q, nil 107 }