github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/crud/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  	entfiatcurrencyhis "github.com/NpoolPlatform/chain-middleware/pkg/db/ent/fiatcurrencyhistory"
     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  	FiatID          *uuid.UUID
    18  	FeedType        *basetypes.CurrencyFeedType
    19  	MarketValueHigh *decimal.Decimal
    20  	MarketValueLow  *decimal.Decimal
    21  }
    22  
    23  func CreateSet(c *ent.FiatCurrencyHistoryCreate, req *Req) *ent.FiatCurrencyHistoryCreate {
    24  	if req.EntID != nil {
    25  		c.SetEntID(*req.EntID)
    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.FiatCurrencyHistoryUpdateOne, req *Req) *ent.FiatCurrencyHistoryUpdateOne {
    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  	FiatID  *cruder.Cond
    56  	FiatIDs *cruder.Cond
    57  	StartAt *cruder.Cond
    58  	EndAt   *cruder.Cond
    59  }
    60  
    61  // nolint:funlen,gocyclo
    62  func SetQueryConds(q *ent.FiatCurrencyHistoryQuery, conds *Conds) (*ent.FiatCurrencyHistoryQuery, error) {
    63  	if conds.EntID != nil {
    64  		id, ok := conds.EntID.Val.(uuid.UUID)
    65  		if !ok {
    66  			return nil, fmt.Errorf("invalid entid")
    67  		}
    68  		switch conds.EntID.Op {
    69  		case cruder.EQ:
    70  			q.Where(entfiatcurrencyhis.EntID(id))
    71  		default:
    72  			return nil, fmt.Errorf("invalid fiatcurrency field")
    73  		}
    74  	}
    75  	if conds.FiatID != nil {
    76  		id, ok := conds.FiatID.Val.(uuid.UUID)
    77  		if !ok {
    78  			return nil, fmt.Errorf("invalid fiatid")
    79  		}
    80  		switch conds.FiatID.Op {
    81  		case cruder.EQ:
    82  			q.Where(entfiatcurrencyhis.FiatID(id))
    83  		default:
    84  			return nil, fmt.Errorf("invalid fiatcurrency field")
    85  		}
    86  	}
    87  	if conds.FiatIDs != nil {
    88  		ids, ok := conds.FiatIDs.Val.([]uuid.UUID)
    89  		if !ok {
    90  			return nil, fmt.Errorf("invalid fiatids")
    91  		}
    92  		switch conds.FiatIDs.Op {
    93  		case cruder.IN:
    94  			q.Where(entfiatcurrencyhis.FiatIDIn(ids...))
    95  		default:
    96  			return nil, fmt.Errorf("invalid fiatcurrency field")
    97  		}
    98  	}
    99  	if conds.StartAt != nil {
   100  		at, ok := conds.StartAt.Val.(uint32)
   101  		if !ok {
   102  			return nil, fmt.Errorf("invalid startat")
   103  		}
   104  		switch conds.StartAt.Op {
   105  		case cruder.EQ:
   106  			q.Where(entfiatcurrencyhis.CreatedAtGTE(at))
   107  		case cruder.GTE:
   108  			q.Where(entfiatcurrencyhis.CreatedAtGTE(at))
   109  		case cruder.LTE:
   110  			q.Where(entfiatcurrencyhis.CreatedAtLTE(at))
   111  		default:
   112  			return nil, fmt.Errorf("invalid fiatcurrency field")
   113  		}
   114  	}
   115  	if conds.EndAt != nil {
   116  		at, ok := conds.EndAt.Val.(uint32)
   117  		if !ok {
   118  			return nil, fmt.Errorf("invalid endat")
   119  		}
   120  		switch conds.EndAt.Op {
   121  		case cruder.EQ:
   122  			q.Where(entfiatcurrencyhis.CreatedAtLTE(at))
   123  		case cruder.GTE:
   124  			q.Where(entfiatcurrencyhis.CreatedAtGTE(at))
   125  		case cruder.LTE:
   126  			q.Where(entfiatcurrencyhis.CreatedAtLTE(at))
   127  		default:
   128  			return nil, fmt.Errorf("invalid fiatcurrency field")
   129  		}
   130  	}
   131  	q.Where(entfiatcurrencyhis.DeletedAt(0))
   132  	return q, nil
   133  }