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

     1  package description
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/NpoolPlatform/chain-middleware/pkg/db/ent"
     7  	entcoindescription "github.com/NpoolPlatform/chain-middleware/pkg/db/ent/coindescription"
     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  	ID         *uint32
    16  	EntID      *uuid.UUID
    17  	AppID      *uuid.UUID
    18  	CoinTypeID *uuid.UUID
    19  	UsedFor    *basetypes.UsedFor
    20  	Title      *string
    21  	Message    *string
    22  	DeletedAt  *uint32
    23  }
    24  
    25  func CreateSet(c *ent.CoinDescriptionCreate, req *Req) *ent.CoinDescriptionCreate {
    26  	if req.ID != nil {
    27  		c.SetID(*req.ID)
    28  	}
    29  	if req.EntID != nil {
    30  		c.SetEntID(*req.EntID)
    31  	}
    32  	if req.AppID != nil {
    33  		c.SetAppID(*req.AppID)
    34  	}
    35  	if req.CoinTypeID != nil {
    36  		c.SetCoinTypeID(*req.CoinTypeID)
    37  	}
    38  	if req.UsedFor != nil {
    39  		c.SetUsedFor(req.UsedFor.String())
    40  	}
    41  	if req.Title != nil {
    42  		c.SetTitle(*req.Title)
    43  	}
    44  	if req.Message != nil {
    45  		c.SetMessage(*req.Message)
    46  	}
    47  	return c
    48  }
    49  
    50  func UpdateSet(u *ent.CoinDescriptionUpdateOne, req *Req) *ent.CoinDescriptionUpdateOne {
    51  	if req.Title != nil {
    52  		u = u.SetTitle(*req.Title)
    53  	}
    54  	if req.Message != nil {
    55  		u = u.SetMessage(*req.Message)
    56  	}
    57  	if req.DeletedAt != nil {
    58  		u = u.SetDeletedAt(*req.DeletedAt)
    59  	}
    60  
    61  	return u
    62  }
    63  
    64  type Conds struct {
    65  	EntID      *cruder.Cond
    66  	AppID      *cruder.Cond
    67  	CoinTypeID *cruder.Cond
    68  	UsedFor    *cruder.Cond
    69  }
    70  
    71  func SetQueryConds(q *ent.CoinDescriptionQuery, conds *Conds) (*ent.CoinDescriptionQuery, error) {
    72  	if conds.EntID != nil {
    73  		id, ok := conds.EntID.Val.(uuid.UUID)
    74  		if !ok {
    75  			return nil, fmt.Errorf("invalid entid")
    76  		}
    77  		switch conds.EntID.Op {
    78  		case cruder.EQ:
    79  			q.Where(entcoindescription.EntID(id))
    80  		default:
    81  			return nil, fmt.Errorf("invalid entcoindescription field")
    82  		}
    83  	}
    84  	if conds.AppID != nil {
    85  		id, ok := conds.AppID.Val.(uuid.UUID)
    86  		if !ok {
    87  			return nil, fmt.Errorf("invalid appid")
    88  		}
    89  		switch conds.AppID.Op {
    90  		case cruder.EQ:
    91  			q.Where(entcoindescription.AppID(id))
    92  		default:
    93  			return nil, fmt.Errorf("invalid entcoindescription field")
    94  		}
    95  	}
    96  	if conds.CoinTypeID != nil {
    97  		id, ok := conds.CoinTypeID.Val.(uuid.UUID)
    98  		if !ok {
    99  			return nil, fmt.Errorf("invalid cointypeid")
   100  		}
   101  		switch conds.CoinTypeID.Op {
   102  		case cruder.EQ:
   103  			q.Where(entcoindescription.CoinTypeID(id))
   104  		default:
   105  			return nil, fmt.Errorf("invalid entcoindescription field")
   106  		}
   107  	}
   108  	if conds.UsedFor != nil {
   109  		usedFor, ok := conds.UsedFor.Val.(basetypes.UsedFor)
   110  		if !ok {
   111  			return nil, fmt.Errorf("invalid usedfor")
   112  		}
   113  		switch conds.UsedFor.Op {
   114  		case cruder.EQ:
   115  			q.Where(entcoindescription.UsedFor(usedFor.String()))
   116  		default:
   117  			return nil, fmt.Errorf("invalid entcoindescription field")
   118  		}
   119  	}
   120  	q.Where(entcoindescription.DeletedAt(0))
   121  	return q, nil
   122  }