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