github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/crud/coin/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/currency"
     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  	EntID           *uuid.UUID
    17  	CoinTypeID      *uuid.UUID
    18  	FeedType        *basetypes.CurrencyFeedType
    19  	MarketValueHigh *decimal.Decimal
    20  	MarketValueLow  *decimal.Decimal
    21  }
    22  
    23  func CreateSet(c *ent.CurrencyCreate, req *Req) *ent.CurrencyCreate {
    24  	if req.EntID != nil {
    25  		c.SetEntID(*req.EntID)
    26  	}
    27  	if req.CoinTypeID != nil {
    28  		c.SetCoinTypeID(*req.CoinTypeID)
    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.CurrencyUpdateOne, req *Req) *ent.CurrencyUpdateOne {
    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  	FeedType    *cruder.Cond
    56  	CoinTypeID  *cruder.Cond
    57  	CoinTypeIDs *cruder.Cond
    58  }
    59  
    60  func SetQueryConds(q *ent.CurrencyQuery, conds *Conds) (*ent.CurrencyQuery, 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(entcurrency.EntID(id))
    69  		default:
    70  			return nil, fmt.Errorf("invalid currency field")
    71  		}
    72  	}
    73  	if conds.FeedType != nil {
    74  		feedType, ok := conds.FeedType.Val.(basetypes.CurrencyFeedType)
    75  		if !ok {
    76  			return nil, fmt.Errorf("invalid feedtype")
    77  		}
    78  		switch conds.FeedType.Op {
    79  		case cruder.EQ:
    80  			q.Where(entcurrency.FeedType(feedType.String()))
    81  		default:
    82  			return nil, fmt.Errorf("invalid currency field")
    83  		}
    84  	}
    85  	if conds.CoinTypeID != nil {
    86  		id, ok := conds.CoinTypeID.Val.(uuid.UUID)
    87  		if !ok {
    88  			return nil, fmt.Errorf("invalid cointypeid")
    89  		}
    90  		switch conds.CoinTypeID.Op {
    91  		case cruder.EQ:
    92  			q.Where(entcurrency.CoinTypeID(id))
    93  		default:
    94  			return nil, fmt.Errorf("invalid currency field")
    95  		}
    96  	}
    97  	if conds.CoinTypeIDs != nil {
    98  		ids, ok := conds.CoinTypeIDs.Val.([]uuid.UUID)
    99  		if !ok {
   100  			return nil, fmt.Errorf("invalid cointypeids")
   101  		}
   102  		switch conds.CoinTypeIDs.Op {
   103  		case cruder.IN:
   104  			q.Where(entcurrency.CoinTypeIDIn(ids...))
   105  		default:
   106  			return nil, fmt.Errorf("invalid currency field")
   107  		}
   108  	}
   109  	q.Where(entcurrency.DeletedAt(0))
   110  	return q, nil
   111  }