github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/crud/coin/fiat/currency/crud.go (about)

     1  package currency
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/NpoolPlatform/chain-middleware/pkg/db/ent"
     7  	entcurrency "github.com/NpoolPlatform/chain-middleware/pkg/db/ent/coinfiatcurrency"
     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.CoinFiatCurrencyCreate, req *Req) *ent.CoinFiatCurrencyCreate {
    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.CoinFiatCurrencyUpdateOne, req *Req) *ent.CoinFiatCurrencyUpdateOne {
    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  }
    58  
    59  func SetQueryConds(q *ent.CoinFiatCurrencyQuery, conds *Conds) (*ent.CoinFiatCurrencyQuery, error) {
    60  	if conds.CoinTypeID != nil {
    61  		id, ok := conds.CoinTypeID.Val.(uuid.UUID)
    62  		if !ok {
    63  			return nil, fmt.Errorf("invalid cointypeid")
    64  		}
    65  		switch conds.CoinTypeID.Op {
    66  		case cruder.EQ:
    67  			q.Where(entcurrency.CoinTypeID(id))
    68  		default:
    69  			return nil, fmt.Errorf("invalid currency field")
    70  		}
    71  	}
    72  	if conds.CoinTypeIDs != nil {
    73  		ids, ok := conds.CoinTypeIDs.Val.([]uuid.UUID)
    74  		if !ok {
    75  			return nil, fmt.Errorf("invalid cointypeids")
    76  		}
    77  		switch conds.CoinTypeIDs.Op {
    78  		case cruder.EQ:
    79  			q.Where(entcurrency.CoinTypeIDIn(ids...))
    80  		default:
    81  			return nil, fmt.Errorf("invalid currency field")
    82  		}
    83  	}
    84  	q.Where(entcurrency.DeletedAt(0))
    85  	return q, nil
    86  }