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

     1  package coinextra
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/NpoolPlatform/chain-middleware/pkg/db/ent"
     7  	entcoinextra "github.com/NpoolPlatform/chain-middleware/pkg/db/ent/coinextra"
     8  	"github.com/NpoolPlatform/libent-cruder/pkg/cruder"
     9  
    10  	"github.com/google/uuid"
    11  )
    12  
    13  type Req struct {
    14  	EntID      *uuid.UUID
    15  	CoinTypeID *uuid.UUID
    16  	HomePage   *string
    17  	Specs      *string
    18  	StableUSD  *bool
    19  	DeletedAt  *uint32
    20  }
    21  
    22  func CreateSet(c *ent.CoinExtraCreate, req *Req) *ent.CoinExtraCreate {
    23  	if req.EntID != nil {
    24  		c.SetEntID(*req.EntID)
    25  	}
    26  	if req.CoinTypeID != nil {
    27  		c.SetCoinTypeID(*req.CoinTypeID)
    28  	}
    29  	if req.HomePage != nil {
    30  		c.SetHomePage(*req.HomePage)
    31  	}
    32  	if req.Specs != nil {
    33  		c.SetSpecs(*req.Specs)
    34  	}
    35  	if req.StableUSD != nil {
    36  		c.SetStableUsd(*req.StableUSD)
    37  	}
    38  	return c
    39  }
    40  
    41  func UpdateSet(u *ent.CoinExtraUpdateOne, req *Req) *ent.CoinExtraUpdateOne {
    42  	if req.HomePage != nil {
    43  		u = u.SetHomePage(*req.HomePage)
    44  	}
    45  	if req.Specs != nil {
    46  		u = u.SetSpecs(*req.Specs)
    47  	}
    48  	if req.StableUSD != nil {
    49  		u = u.SetStableUsd(*req.StableUSD)
    50  	}
    51  	if req.DeletedAt != nil {
    52  		u = u.SetDeletedAt(*req.DeletedAt)
    53  	}
    54  	return u
    55  }
    56  
    57  type Conds struct {
    58  	EntID      *cruder.Cond
    59  	CoinTypeID *cruder.Cond
    60  	StableUSD  *cruder.Cond
    61  }
    62  
    63  func SetQueryConds(q *ent.CoinExtraQuery, conds *Conds) (*ent.CoinExtraQuery, 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(
    72  				entcoinextra.EntID(id),
    73  			)
    74  		default:
    75  			return nil, fmt.Errorf("invalid coinextra field")
    76  		}
    77  	}
    78  	if conds.CoinTypeID != nil {
    79  		id, ok := conds.CoinTypeID.Val.(uuid.UUID)
    80  		if !ok {
    81  			return nil, fmt.Errorf("invalid cointypeid")
    82  		}
    83  		switch conds.CoinTypeID.Op {
    84  		case cruder.EQ:
    85  			q.Where(
    86  				entcoinextra.CoinTypeID(id),
    87  			)
    88  		default:
    89  			return nil, fmt.Errorf("invalid coinextra field")
    90  		}
    91  	}
    92  	if conds.StableUSD != nil {
    93  		stable, ok := conds.StableUSD.Val.(bool)
    94  		if !ok {
    95  			return nil, fmt.Errorf("invalid stableusd")
    96  		}
    97  		switch conds.StableUSD.Op {
    98  		case cruder.EQ:
    99  			q.Where(
   100  				entcoinextra.StableUsd(stable),
   101  			)
   102  		default:
   103  			return nil, fmt.Errorf("invalid coinextra field")
   104  		}
   105  	}
   106  	return q, nil
   107  }