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

     1  package currencyfeed
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/NpoolPlatform/chain-middleware/pkg/db/ent"
     7  	entcurrencyfeed "github.com/NpoolPlatform/chain-middleware/pkg/db/ent/fiatcurrencyfeed"
     8  	"github.com/NpoolPlatform/libent-cruder/pkg/cruder"
     9  	basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1"
    10  
    11  	"github.com/google/uuid"
    12  )
    13  
    14  type Req struct {
    15  	EntID        *uuid.UUID
    16  	FiatID       *uuid.UUID
    17  	FeedType     *basetypes.CurrencyFeedType
    18  	FeedFiatName *string
    19  	Disabled     *bool
    20  }
    21  
    22  func CreateSet(c *ent.FiatCurrencyFeedCreate, req *Req) *ent.FiatCurrencyFeedCreate {
    23  	if req.EntID != nil {
    24  		c.SetEntID(*req.EntID)
    25  	}
    26  	if req.FiatID != nil {
    27  		c.SetFiatID(*req.FiatID)
    28  	}
    29  	if req.FeedType != nil {
    30  		c.SetFeedType(req.FeedType.String())
    31  	}
    32  	if req.FeedFiatName != nil {
    33  		c.SetFeedFiatName(*req.FeedFiatName)
    34  	}
    35  	if req.Disabled != nil {
    36  		c.SetDisabled(*req.Disabled)
    37  	}
    38  	return c
    39  }
    40  
    41  func UpdateSet(u *ent.FiatCurrencyFeedUpdateOne, req *Req) *ent.FiatCurrencyFeedUpdateOne {
    42  	if req.FeedFiatName != nil {
    43  		u.SetFeedFiatName(*req.FeedFiatName)
    44  	}
    45  	if req.Disabled != nil {
    46  		u.SetDisabled(*req.Disabled)
    47  	}
    48  	return u
    49  }
    50  
    51  type Conds struct {
    52  	EntID    *cruder.Cond
    53  	FiatID   *cruder.Cond
    54  	FiatIDs  *cruder.Cond
    55  	Disabled *cruder.Cond
    56  	FeedType *cruder.Cond
    57  }
    58  
    59  //nolint:gocyclo
    60  func SetQueryConds(q *ent.FiatCurrencyFeedQuery, conds *Conds) (*ent.FiatCurrencyFeedQuery, 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(entcurrencyfeed.EntID(id))
    69  		default:
    70  			return nil, fmt.Errorf("invalid currencyfeed field")
    71  		}
    72  	}
    73  	if conds.FiatID != nil {
    74  		id, ok := conds.FiatID.Val.(uuid.UUID)
    75  		if !ok {
    76  			return nil, fmt.Errorf("invalid fiatid")
    77  		}
    78  		switch conds.FiatID.Op {
    79  		case cruder.EQ:
    80  			q.Where(entcurrencyfeed.FiatID(id))
    81  		default:
    82  			return nil, fmt.Errorf("invalid currencyfeed field")
    83  		}
    84  	}
    85  	if conds.FiatIDs != nil {
    86  		ids, ok := conds.FiatIDs.Val.([]uuid.UUID)
    87  		if !ok {
    88  			return nil, fmt.Errorf("invalid fiatids")
    89  		}
    90  		switch conds.FiatIDs.Op {
    91  		case cruder.IN:
    92  			q.Where(entcurrencyfeed.FiatIDIn(ids...))
    93  		default:
    94  			return nil, fmt.Errorf("invalid currencyfeed field")
    95  		}
    96  	}
    97  	if conds.Disabled != nil {
    98  		disabled, ok := conds.Disabled.Val.(bool)
    99  		if !ok {
   100  			return nil, fmt.Errorf("invalid disabled")
   101  		}
   102  		switch conds.Disabled.Op {
   103  		case cruder.EQ:
   104  			q.Where(entcurrencyfeed.Disabled(disabled))
   105  		default:
   106  			return nil, fmt.Errorf("invalid currencyfeed field")
   107  		}
   108  	}
   109  	if conds.FeedType != nil {
   110  		feedType, ok := conds.FeedType.Val.(basetypes.CurrencyFeedType)
   111  		if !ok {
   112  			return nil, fmt.Errorf("invalid feedtype")
   113  		}
   114  		switch conds.FeedType.Op {
   115  		case cruder.EQ:
   116  			q.Where(entcurrencyfeed.FeedType(feedType.String()))
   117  		default:
   118  			return nil, fmt.Errorf("invalid currencyfeed field")
   119  		}
   120  	}
   121  	q.Where(entcurrencyfeed.DeletedAt(0))
   122  	return q, nil
   123  }