github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/crud/coin/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/currencyhistory"
     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.CurrencyHistoryCreate, req *Req) *ent.CurrencyHistoryCreate {
    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.CurrencyHistoryUpdateOne, req *Req) *ent.CurrencyHistoryUpdateOne {
    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  	CoinNames   *cruder.Cond
    60  }
    61  
    62  // nolint:funlen,gocyclo
    63  func SetQueryConds(q *ent.CurrencyHistoryQuery, conds *Conds) (*ent.CurrencyHistoryQuery, error) {
    64  	if conds.EntID != nil {
    65  		id, ok := conds.EntID.Val.(uuid.UUID)
    66  		if !ok {
    67  			return nil, fmt.Errorf("invalid entid")
    68  		}
    69  		switch conds.EntID.Op {
    70  		case cruder.EQ:
    71  			q.Where(entcurrencyhis.EntID(id))
    72  		default:
    73  			return nil, fmt.Errorf("invalid currency field")
    74  		}
    75  	}
    76  	if conds.CoinTypeID != nil {
    77  		id, ok := conds.CoinTypeID.Val.(uuid.UUID)
    78  		if !ok {
    79  			return nil, fmt.Errorf("invalid cointypeid")
    80  		}
    81  		switch conds.CoinTypeID.Op {
    82  		case cruder.EQ:
    83  			q.Where(entcurrencyhis.CoinTypeID(id))
    84  		default:
    85  			return nil, fmt.Errorf("invalid currency field")
    86  		}
    87  	}
    88  	if conds.CoinTypeIDs != nil {
    89  		ids, ok := conds.CoinTypeIDs.Val.([]uuid.UUID)
    90  		if !ok {
    91  			return nil, fmt.Errorf("invalid cointypeids")
    92  		}
    93  		switch conds.CoinTypeIDs.Op {
    94  		case cruder.IN:
    95  			q.Where(entcurrencyhis.CoinTypeIDIn(ids...))
    96  		default:
    97  			return nil, fmt.Errorf("invalid currency field")
    98  		}
    99  	}
   100  	if conds.StartAt != nil {
   101  		at, ok := conds.StartAt.Val.(uint32)
   102  		if !ok {
   103  			return nil, fmt.Errorf("invalid startat")
   104  		}
   105  		switch conds.StartAt.Op {
   106  		case cruder.EQ:
   107  			q.Where(entcurrencyhis.CreatedAtGTE(at))
   108  		case cruder.LTE:
   109  			q.Where(entcurrencyhis.CreatedAtLTE(at))
   110  		case cruder.GTE:
   111  			q.Where(entcurrencyhis.CreatedAtGTE(at))
   112  		default:
   113  			return nil, fmt.Errorf("invalid currency field")
   114  		}
   115  	}
   116  	if conds.EndAt != nil {
   117  		at, ok := conds.EndAt.Val.(uint32)
   118  		if !ok {
   119  			return nil, fmt.Errorf("invalid endat")
   120  		}
   121  		switch conds.EndAt.Op {
   122  		case cruder.EQ:
   123  			q.Where(entcurrencyhis.CreatedAtLTE(at))
   124  		case cruder.GTE:
   125  			q.Where(entcurrencyhis.CreatedAtGTE(at))
   126  		case cruder.LTE:
   127  			q.Where(entcurrencyhis.CreatedAtLTE(at))
   128  		default:
   129  			return nil, fmt.Errorf("invalid currency field")
   130  		}
   131  	}
   132  	q.Where(entcurrencyhis.DeletedAt(0))
   133  	return q, nil
   134  }