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

     1  package chainbase
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/NpoolPlatform/chain-middleware/pkg/db/ent"
     7  	entchainbase "github.com/NpoolPlatform/chain-middleware/pkg/db/ent/chainbase"
     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  	Name       *string
    17  	Logo       *string
    18  	NativeUnit *string
    19  	AtomicUnit *string
    20  	UnitExp    *uint32
    21  	ENV        *string
    22  	ChainID    *string
    23  	Nickname   *string
    24  	GasType    *basetypes.GasType
    25  	DeletedAt  *uint32
    26  }
    27  
    28  func CreateSet(c *ent.ChainBaseCreate, req *Req) *ent.ChainBaseCreate {
    29  	if req.EntID != nil {
    30  		c.SetEntID(*req.EntID)
    31  	}
    32  	if req.Name != nil {
    33  		c.SetName(*req.Name)
    34  	}
    35  	if req.Logo != nil {
    36  		c.SetLogo(*req.Logo)
    37  	}
    38  	if req.NativeUnit != nil {
    39  		c.SetNativeUnit(*req.NativeUnit)
    40  	}
    41  	if req.AtomicUnit != nil {
    42  		c.SetAtomicUnit(*req.AtomicUnit)
    43  	}
    44  	if req.UnitExp != nil {
    45  		c.SetUnitExp(*req.UnitExp)
    46  	}
    47  	if req.ENV != nil {
    48  		c.SetEnv(*req.ENV)
    49  	}
    50  	if req.ChainID != nil {
    51  		c.SetChainID(*req.ChainID)
    52  	}
    53  	if req.Nickname != nil {
    54  		c.SetNickname(*req.Nickname)
    55  	}
    56  	if req.GasType != nil {
    57  		c.SetGasType(req.GasType.String())
    58  	}
    59  	return c
    60  }
    61  
    62  func UpdateSet(u *ent.ChainBaseUpdateOne, req *Req) *ent.ChainBaseUpdateOne {
    63  	if req.Logo != nil {
    64  		u = u.SetLogo(*req.Logo)
    65  	}
    66  	if req.NativeUnit != nil {
    67  		u = u.SetNativeUnit(*req.NativeUnit)
    68  	}
    69  	if req.AtomicUnit != nil {
    70  		u = u.SetAtomicUnit(*req.AtomicUnit)
    71  	}
    72  	if req.UnitExp != nil {
    73  		u = u.SetUnitExp(*req.UnitExp)
    74  	}
    75  	if req.ENV != nil {
    76  		u = u.SetEnv(*req.ENV)
    77  	}
    78  	if req.ChainID != nil {
    79  		u = u.SetChainID(*req.ChainID)
    80  	}
    81  	if req.Nickname != nil {
    82  		u = u.SetNickname(*req.Nickname)
    83  	}
    84  	if req.GasType != nil {
    85  		u = u.SetGasType(req.GasType.String())
    86  	}
    87  	if req.DeletedAt != nil {
    88  		u = u.SetDeletedAt(*req.DeletedAt)
    89  	}
    90  	return u
    91  }
    92  
    93  type Conds struct {
    94  	EntID      *cruder.Cond
    95  	Name       *cruder.Cond
    96  	ENV        *cruder.Cond
    97  	NativeUnit *cruder.Cond
    98  	ChainID    *cruder.Cond
    99  	Nickname   *cruder.Cond
   100  	ChainType  *cruder.Cond
   101  	EntIDs     *cruder.Cond
   102  }
   103  
   104  //nolint:funlen,gocyclo
   105  func SetQueryConds(q *ent.ChainBaseQuery, conds *Conds) (*ent.ChainBaseQuery, error) {
   106  	if conds.EntID != nil {
   107  		name, ok := conds.EntID.Val.(uuid.UUID)
   108  		if !ok {
   109  			return nil, fmt.Errorf("invalid entid")
   110  		}
   111  		switch conds.EntID.Op {
   112  		case cruder.EQ:
   113  			q.Where(
   114  				entchainbase.EntID(name),
   115  			)
   116  		default:
   117  			return nil, fmt.Errorf("invalid chainbase field")
   118  		}
   119  	}
   120  	if conds.Name != nil {
   121  		name, ok := conds.Name.Val.(string)
   122  		if !ok {
   123  			return nil, fmt.Errorf("invalid name")
   124  		}
   125  		switch conds.Name.Op {
   126  		case cruder.EQ:
   127  			q.Where(
   128  				entchainbase.Name(name),
   129  			)
   130  		default:
   131  			return nil, fmt.Errorf("invalid chainbase field")
   132  		}
   133  	}
   134  	if conds.ENV != nil {
   135  		env, ok := conds.ENV.Val.(string)
   136  		if !ok {
   137  			return nil, fmt.Errorf("invalid env")
   138  		}
   139  		switch conds.ENV.Op {
   140  		case cruder.EQ:
   141  			q.Where(entchainbase.Env(env))
   142  		default:
   143  			return nil, fmt.Errorf("invalid chainbase field")
   144  		}
   145  	}
   146  	if conds.NativeUnit != nil {
   147  		unit, ok := conds.NativeUnit.Val.(string)
   148  		if !ok {
   149  			return nil, fmt.Errorf("invalid nativeunit")
   150  		}
   151  		switch conds.NativeUnit.Op {
   152  		case cruder.EQ:
   153  			q.Where(entchainbase.NativeUnit(unit))
   154  		default:
   155  			return nil, fmt.Errorf("invalid chainbase field")
   156  		}
   157  	}
   158  	if conds.ChainID != nil {
   159  		chainID, ok := conds.ChainID.Val.(string)
   160  		if !ok {
   161  			return nil, fmt.Errorf("invalid chainID")
   162  		}
   163  		switch conds.ChainID.Op {
   164  		case cruder.EQ:
   165  			q.Where(entchainbase.ChainID(chainID))
   166  		default:
   167  			return nil, fmt.Errorf("invalid chainbase field")
   168  		}
   169  	}
   170  	if conds.Nickname != nil {
   171  		nickname, ok := conds.Nickname.Val.(string)
   172  		if !ok {
   173  			return nil, fmt.Errorf("invalid nickname")
   174  		}
   175  		switch conds.Nickname.Op {
   176  		case cruder.EQ:
   177  			q.Where(entchainbase.Nickname(nickname))
   178  		default:
   179  			return nil, fmt.Errorf("invalid chainbase field")
   180  		}
   181  	}
   182  	if conds.EntIDs != nil {
   183  		ids, ok := conds.EntIDs.Val.([]uuid.UUID)
   184  		if !ok {
   185  			return nil, fmt.Errorf("invalid entids")
   186  		}
   187  		switch conds.EntIDs.Op {
   188  		case cruder.IN:
   189  			q.Where(entchainbase.EntIDIn(ids...))
   190  		default:
   191  			return nil, fmt.Errorf("invalid chainbase field")
   192  		}
   193  	}
   194  	q.Where(entchainbase.DeletedAt(0))
   195  	return q, nil
   196  }