github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/crud/coin/currency/feed/crud.go (about) 1 package currencyfeed 2 3 import ( 4 "fmt" 5 6 "github.com/NpoolPlatform/chain-middleware/pkg/db/ent" 7 entcurrencyfeed "github.com/NpoolPlatform/chain-middleware/pkg/db/ent/currencyfeed" 8 "github.com/NpoolPlatform/libent-cruder/pkg/cruder" 9 basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1" 10 11 "github.com/google/uuid" 12 ) 13 14 type Req struct { 15 EntID *uuid.UUID 16 CoinTypeID *uuid.UUID 17 FeedType *basetypes.CurrencyFeedType 18 FeedCoinName *string 19 Disabled *bool 20 } 21 22 func CreateSet(c *ent.CurrencyFeedCreate, req *Req) *ent.CurrencyFeedCreate { 23 if req.EntID != nil { 24 c.SetEntID(*req.EntID) 25 } 26 if req.CoinTypeID != nil { 27 c.SetCoinTypeID(*req.CoinTypeID) 28 } 29 if req.FeedType != nil { 30 c.SetFeedType(req.FeedType.String()) 31 } 32 if req.FeedCoinName != nil { 33 c.SetFeedCoinName(*req.FeedCoinName) 34 } 35 if req.Disabled != nil { 36 c.SetDisabled(*req.Disabled) 37 } 38 return c 39 } 40 41 func UpdateSet(u *ent.CurrencyFeedUpdateOne, req *Req) *ent.CurrencyFeedUpdateOne { 42 if req.FeedCoinName != nil { 43 u.SetFeedCoinName(*req.FeedCoinName) 44 } 45 if req.Disabled != nil { 46 u.SetDisabled(*req.Disabled) 47 } 48 return u 49 } 50 51 type Conds struct { 52 EntID *cruder.Cond 53 CoinTypeID *cruder.Cond 54 CoinTypeIDs *cruder.Cond 55 Disabled *cruder.Cond 56 FeedType *cruder.Cond 57 } 58 59 //nolint:gocyclo 60 func SetQueryConds(q *ent.CurrencyFeedQuery, conds *Conds) (*ent.CurrencyFeedQuery, error) { 61 if conds.EntID != nil { 62 id, ok := conds.EntID.Val.(uuid.UUID) 63 if !ok { 64 return nil, fmt.Errorf("invalid entid") 65 } 66 switch conds.EntID.Op { 67 case cruder.EQ: 68 q.Where(entcurrencyfeed.EntID(id)) 69 default: 70 return nil, fmt.Errorf("invalid currencyfeed field") 71 } 72 } 73 if conds.CoinTypeID != nil { 74 id, ok := conds.CoinTypeID.Val.(uuid.UUID) 75 if !ok { 76 return nil, fmt.Errorf("invalid cointypeid") 77 } 78 switch conds.CoinTypeID.Op { 79 case cruder.EQ: 80 q.Where(entcurrencyfeed.CoinTypeID(id)) 81 default: 82 return nil, fmt.Errorf("invalid currencyfeed field") 83 } 84 } 85 if conds.CoinTypeIDs != nil { 86 ids, ok := conds.CoinTypeIDs.Val.([]uuid.UUID) 87 if !ok { 88 return nil, fmt.Errorf("invalid cointypeids") 89 } 90 switch conds.CoinTypeIDs.Op { 91 case cruder.IN: 92 q.Where(entcurrencyfeed.CoinTypeIDIn(ids...)) 93 default: 94 return nil, fmt.Errorf("invalid currencyfeed field") 95 } 96 } 97 if conds.Disabled != nil { 98 disabled, ok := conds.Disabled.Val.(bool) 99 if !ok { 100 return nil, fmt.Errorf("invalid disabled") 101 } 102 switch conds.Disabled.Op { 103 case cruder.EQ: 104 q.Where(entcurrencyfeed.Disabled(disabled)) 105 default: 106 return nil, fmt.Errorf("invalid currencyfeed field") 107 } 108 } 109 if conds.FeedType != nil { 110 feedType, ok := conds.FeedType.Val.(basetypes.CurrencyFeedType) 111 if !ok { 112 return nil, fmt.Errorf("invalid feedtype") 113 } 114 switch conds.FeedType.Op { 115 case cruder.EQ: 116 q.Where(entcurrencyfeed.FeedType(feedType.String())) 117 default: 118 return nil, fmt.Errorf("invalid currencyfeed field") 119 } 120 } 121 q.Where(entcurrencyfeed.DeletedAt(0)) 122 return q, nil 123 }