github.com/ngocphuongnb/tetua@v0.0.7-alpha/packages/entrepository/ent/permission_update.go (about)

     1  // Code generated by entc, DO NOT EDIT.
     2  
     3  package ent
     4  
     5  import (
     6  	"context"
     7  	"errors"
     8  	"fmt"
     9  	"time"
    10  
    11  	"entgo.io/ent/dialect/sql"
    12  	"entgo.io/ent/dialect/sql/sqlgraph"
    13  	"entgo.io/ent/schema/field"
    14  	"github.com/ngocphuongnb/tetua/packages/entrepository/ent/permission"
    15  	"github.com/ngocphuongnb/tetua/packages/entrepository/ent/predicate"
    16  	"github.com/ngocphuongnb/tetua/packages/entrepository/ent/role"
    17  )
    18  
    19  // PermissionUpdate is the builder for updating Permission entities.
    20  type PermissionUpdate struct {
    21  	config
    22  	hooks    []Hook
    23  	mutation *PermissionMutation
    24  }
    25  
    26  // Where appends a list predicates to the PermissionUpdate builder.
    27  func (pu *PermissionUpdate) Where(ps ...predicate.Permission) *PermissionUpdate {
    28  	pu.mutation.Where(ps...)
    29  	return pu
    30  }
    31  
    32  // SetUpdatedAt sets the "updated_at" field.
    33  func (pu *PermissionUpdate) SetUpdatedAt(t time.Time) *PermissionUpdate {
    34  	pu.mutation.SetUpdatedAt(t)
    35  	return pu
    36  }
    37  
    38  // SetDeletedAt sets the "deleted_at" field.
    39  func (pu *PermissionUpdate) SetDeletedAt(t time.Time) *PermissionUpdate {
    40  	pu.mutation.SetDeletedAt(t)
    41  	return pu
    42  }
    43  
    44  // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
    45  func (pu *PermissionUpdate) SetNillableDeletedAt(t *time.Time) *PermissionUpdate {
    46  	if t != nil {
    47  		pu.SetDeletedAt(*t)
    48  	}
    49  	return pu
    50  }
    51  
    52  // ClearDeletedAt clears the value of the "deleted_at" field.
    53  func (pu *PermissionUpdate) ClearDeletedAt() *PermissionUpdate {
    54  	pu.mutation.ClearDeletedAt()
    55  	return pu
    56  }
    57  
    58  // SetRoleID sets the "role_id" field.
    59  func (pu *PermissionUpdate) SetRoleID(i int) *PermissionUpdate {
    60  	pu.mutation.SetRoleID(i)
    61  	return pu
    62  }
    63  
    64  // SetAction sets the "action" field.
    65  func (pu *PermissionUpdate) SetAction(s string) *PermissionUpdate {
    66  	pu.mutation.SetAction(s)
    67  	return pu
    68  }
    69  
    70  // SetValue sets the "value" field.
    71  func (pu *PermissionUpdate) SetValue(s string) *PermissionUpdate {
    72  	pu.mutation.SetValue(s)
    73  	return pu
    74  }
    75  
    76  // SetRole sets the "role" edge to the Role entity.
    77  func (pu *PermissionUpdate) SetRole(r *Role) *PermissionUpdate {
    78  	return pu.SetRoleID(r.ID)
    79  }
    80  
    81  // Mutation returns the PermissionMutation object of the builder.
    82  func (pu *PermissionUpdate) Mutation() *PermissionMutation {
    83  	return pu.mutation
    84  }
    85  
    86  // ClearRole clears the "role" edge to the Role entity.
    87  func (pu *PermissionUpdate) ClearRole() *PermissionUpdate {
    88  	pu.mutation.ClearRole()
    89  	return pu
    90  }
    91  
    92  // Save executes the query and returns the number of nodes affected by the update operation.
    93  func (pu *PermissionUpdate) Save(ctx context.Context) (int, error) {
    94  	var (
    95  		err      error
    96  		affected int
    97  	)
    98  	pu.defaults()
    99  	if len(pu.hooks) == 0 {
   100  		if err = pu.check(); err != nil {
   101  			return 0, err
   102  		}
   103  		affected, err = pu.sqlSave(ctx)
   104  	} else {
   105  		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
   106  			mutation, ok := m.(*PermissionMutation)
   107  			if !ok {
   108  				return nil, fmt.Errorf("unexpected mutation type %T", m)
   109  			}
   110  			if err = pu.check(); err != nil {
   111  				return 0, err
   112  			}
   113  			pu.mutation = mutation
   114  			affected, err = pu.sqlSave(ctx)
   115  			mutation.done = true
   116  			return affected, err
   117  		})
   118  		for i := len(pu.hooks) - 1; i >= 0; i-- {
   119  			if pu.hooks[i] == nil {
   120  				return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
   121  			}
   122  			mut = pu.hooks[i](mut)
   123  		}
   124  		if _, err := mut.Mutate(ctx, pu.mutation); err != nil {
   125  			return 0, err
   126  		}
   127  	}
   128  	return affected, err
   129  }
   130  
   131  // SaveX is like Save, but panics if an error occurs.
   132  func (pu *PermissionUpdate) SaveX(ctx context.Context) int {
   133  	affected, err := pu.Save(ctx)
   134  	if err != nil {
   135  		panic(err)
   136  	}
   137  	return affected
   138  }
   139  
   140  // Exec executes the query.
   141  func (pu *PermissionUpdate) Exec(ctx context.Context) error {
   142  	_, err := pu.Save(ctx)
   143  	return err
   144  }
   145  
   146  // ExecX is like Exec, but panics if an error occurs.
   147  func (pu *PermissionUpdate) ExecX(ctx context.Context) {
   148  	if err := pu.Exec(ctx); err != nil {
   149  		panic(err)
   150  	}
   151  }
   152  
   153  // defaults sets the default values of the builder before save.
   154  func (pu *PermissionUpdate) defaults() {
   155  	if _, ok := pu.mutation.UpdatedAt(); !ok {
   156  		v := permission.UpdateDefaultUpdatedAt()
   157  		pu.mutation.SetUpdatedAt(v)
   158  	}
   159  }
   160  
   161  // check runs all checks and user-defined validators on the builder.
   162  func (pu *PermissionUpdate) check() error {
   163  	if _, ok := pu.mutation.RoleID(); pu.mutation.RoleCleared() && !ok {
   164  		return errors.New(`ent: clearing a required unique edge "Permission.role"`)
   165  	}
   166  	return nil
   167  }
   168  
   169  func (pu *PermissionUpdate) sqlSave(ctx context.Context) (n int, err error) {
   170  	_spec := &sqlgraph.UpdateSpec{
   171  		Node: &sqlgraph.NodeSpec{
   172  			Table:   permission.Table,
   173  			Columns: permission.Columns,
   174  			ID: &sqlgraph.FieldSpec{
   175  				Type:   field.TypeInt,
   176  				Column: permission.FieldID,
   177  			},
   178  		},
   179  	}
   180  	if ps := pu.mutation.predicates; len(ps) > 0 {
   181  		_spec.Predicate = func(selector *sql.Selector) {
   182  			for i := range ps {
   183  				ps[i](selector)
   184  			}
   185  		}
   186  	}
   187  	if value, ok := pu.mutation.UpdatedAt(); ok {
   188  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   189  			Type:   field.TypeTime,
   190  			Value:  value,
   191  			Column: permission.FieldUpdatedAt,
   192  		})
   193  	}
   194  	if value, ok := pu.mutation.DeletedAt(); ok {
   195  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   196  			Type:   field.TypeTime,
   197  			Value:  value,
   198  			Column: permission.FieldDeletedAt,
   199  		})
   200  	}
   201  	if pu.mutation.DeletedAtCleared() {
   202  		_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
   203  			Type:   field.TypeTime,
   204  			Column: permission.FieldDeletedAt,
   205  		})
   206  	}
   207  	if value, ok := pu.mutation.Action(); ok {
   208  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   209  			Type:   field.TypeString,
   210  			Value:  value,
   211  			Column: permission.FieldAction,
   212  		})
   213  	}
   214  	if value, ok := pu.mutation.Value(); ok {
   215  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   216  			Type:   field.TypeString,
   217  			Value:  value,
   218  			Column: permission.FieldValue,
   219  		})
   220  	}
   221  	if pu.mutation.RoleCleared() {
   222  		edge := &sqlgraph.EdgeSpec{
   223  			Rel:     sqlgraph.M2O,
   224  			Inverse: true,
   225  			Table:   permission.RoleTable,
   226  			Columns: []string{permission.RoleColumn},
   227  			Bidi:    false,
   228  			Target: &sqlgraph.EdgeTarget{
   229  				IDSpec: &sqlgraph.FieldSpec{
   230  					Type:   field.TypeInt,
   231  					Column: role.FieldID,
   232  				},
   233  			},
   234  		}
   235  		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
   236  	}
   237  	if nodes := pu.mutation.RoleIDs(); len(nodes) > 0 {
   238  		edge := &sqlgraph.EdgeSpec{
   239  			Rel:     sqlgraph.M2O,
   240  			Inverse: true,
   241  			Table:   permission.RoleTable,
   242  			Columns: []string{permission.RoleColumn},
   243  			Bidi:    false,
   244  			Target: &sqlgraph.EdgeTarget{
   245  				IDSpec: &sqlgraph.FieldSpec{
   246  					Type:   field.TypeInt,
   247  					Column: role.FieldID,
   248  				},
   249  			},
   250  		}
   251  		for _, k := range nodes {
   252  			edge.Target.Nodes = append(edge.Target.Nodes, k)
   253  		}
   254  		_spec.Edges.Add = append(_spec.Edges.Add, edge)
   255  	}
   256  	if n, err = sqlgraph.UpdateNodes(ctx, pu.driver, _spec); err != nil {
   257  		if _, ok := err.(*sqlgraph.NotFoundError); ok {
   258  			err = &NotFoundError{permission.Label}
   259  		} else if sqlgraph.IsConstraintError(err) {
   260  			err = &ConstraintError{err.Error(), err}
   261  		}
   262  		return 0, err
   263  	}
   264  	return n, nil
   265  }
   266  
   267  // PermissionUpdateOne is the builder for updating a single Permission entity.
   268  type PermissionUpdateOne struct {
   269  	config
   270  	fields   []string
   271  	hooks    []Hook
   272  	mutation *PermissionMutation
   273  }
   274  
   275  // SetUpdatedAt sets the "updated_at" field.
   276  func (puo *PermissionUpdateOne) SetUpdatedAt(t time.Time) *PermissionUpdateOne {
   277  	puo.mutation.SetUpdatedAt(t)
   278  	return puo
   279  }
   280  
   281  // SetDeletedAt sets the "deleted_at" field.
   282  func (puo *PermissionUpdateOne) SetDeletedAt(t time.Time) *PermissionUpdateOne {
   283  	puo.mutation.SetDeletedAt(t)
   284  	return puo
   285  }
   286  
   287  // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
   288  func (puo *PermissionUpdateOne) SetNillableDeletedAt(t *time.Time) *PermissionUpdateOne {
   289  	if t != nil {
   290  		puo.SetDeletedAt(*t)
   291  	}
   292  	return puo
   293  }
   294  
   295  // ClearDeletedAt clears the value of the "deleted_at" field.
   296  func (puo *PermissionUpdateOne) ClearDeletedAt() *PermissionUpdateOne {
   297  	puo.mutation.ClearDeletedAt()
   298  	return puo
   299  }
   300  
   301  // SetRoleID sets the "role_id" field.
   302  func (puo *PermissionUpdateOne) SetRoleID(i int) *PermissionUpdateOne {
   303  	puo.mutation.SetRoleID(i)
   304  	return puo
   305  }
   306  
   307  // SetAction sets the "action" field.
   308  func (puo *PermissionUpdateOne) SetAction(s string) *PermissionUpdateOne {
   309  	puo.mutation.SetAction(s)
   310  	return puo
   311  }
   312  
   313  // SetValue sets the "value" field.
   314  func (puo *PermissionUpdateOne) SetValue(s string) *PermissionUpdateOne {
   315  	puo.mutation.SetValue(s)
   316  	return puo
   317  }
   318  
   319  // SetRole sets the "role" edge to the Role entity.
   320  func (puo *PermissionUpdateOne) SetRole(r *Role) *PermissionUpdateOne {
   321  	return puo.SetRoleID(r.ID)
   322  }
   323  
   324  // Mutation returns the PermissionMutation object of the builder.
   325  func (puo *PermissionUpdateOne) Mutation() *PermissionMutation {
   326  	return puo.mutation
   327  }
   328  
   329  // ClearRole clears the "role" edge to the Role entity.
   330  func (puo *PermissionUpdateOne) ClearRole() *PermissionUpdateOne {
   331  	puo.mutation.ClearRole()
   332  	return puo
   333  }
   334  
   335  // Select allows selecting one or more fields (columns) of the returned entity.
   336  // The default is selecting all fields defined in the entity schema.
   337  func (puo *PermissionUpdateOne) Select(field string, fields ...string) *PermissionUpdateOne {
   338  	puo.fields = append([]string{field}, fields...)
   339  	return puo
   340  }
   341  
   342  // Save executes the query and returns the updated Permission entity.
   343  func (puo *PermissionUpdateOne) Save(ctx context.Context) (*Permission, error) {
   344  	var (
   345  		err  error
   346  		node *Permission
   347  	)
   348  	puo.defaults()
   349  	if len(puo.hooks) == 0 {
   350  		if err = puo.check(); err != nil {
   351  			return nil, err
   352  		}
   353  		node, err = puo.sqlSave(ctx)
   354  	} else {
   355  		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
   356  			mutation, ok := m.(*PermissionMutation)
   357  			if !ok {
   358  				return nil, fmt.Errorf("unexpected mutation type %T", m)
   359  			}
   360  			if err = puo.check(); err != nil {
   361  				return nil, err
   362  			}
   363  			puo.mutation = mutation
   364  			node, err = puo.sqlSave(ctx)
   365  			mutation.done = true
   366  			return node, err
   367  		})
   368  		for i := len(puo.hooks) - 1; i >= 0; i-- {
   369  			if puo.hooks[i] == nil {
   370  				return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
   371  			}
   372  			mut = puo.hooks[i](mut)
   373  		}
   374  		if _, err := mut.Mutate(ctx, puo.mutation); err != nil {
   375  			return nil, err
   376  		}
   377  	}
   378  	return node, err
   379  }
   380  
   381  // SaveX is like Save, but panics if an error occurs.
   382  func (puo *PermissionUpdateOne) SaveX(ctx context.Context) *Permission {
   383  	node, err := puo.Save(ctx)
   384  	if err != nil {
   385  		panic(err)
   386  	}
   387  	return node
   388  }
   389  
   390  // Exec executes the query on the entity.
   391  func (puo *PermissionUpdateOne) Exec(ctx context.Context) error {
   392  	_, err := puo.Save(ctx)
   393  	return err
   394  }
   395  
   396  // ExecX is like Exec, but panics if an error occurs.
   397  func (puo *PermissionUpdateOne) ExecX(ctx context.Context) {
   398  	if err := puo.Exec(ctx); err != nil {
   399  		panic(err)
   400  	}
   401  }
   402  
   403  // defaults sets the default values of the builder before save.
   404  func (puo *PermissionUpdateOne) defaults() {
   405  	if _, ok := puo.mutation.UpdatedAt(); !ok {
   406  		v := permission.UpdateDefaultUpdatedAt()
   407  		puo.mutation.SetUpdatedAt(v)
   408  	}
   409  }
   410  
   411  // check runs all checks and user-defined validators on the builder.
   412  func (puo *PermissionUpdateOne) check() error {
   413  	if _, ok := puo.mutation.RoleID(); puo.mutation.RoleCleared() && !ok {
   414  		return errors.New(`ent: clearing a required unique edge "Permission.role"`)
   415  	}
   416  	return nil
   417  }
   418  
   419  func (puo *PermissionUpdateOne) sqlSave(ctx context.Context) (_node *Permission, err error) {
   420  	_spec := &sqlgraph.UpdateSpec{
   421  		Node: &sqlgraph.NodeSpec{
   422  			Table:   permission.Table,
   423  			Columns: permission.Columns,
   424  			ID: &sqlgraph.FieldSpec{
   425  				Type:   field.TypeInt,
   426  				Column: permission.FieldID,
   427  			},
   428  		},
   429  	}
   430  	id, ok := puo.mutation.ID()
   431  	if !ok {
   432  		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Permission.id" for update`)}
   433  	}
   434  	_spec.Node.ID.Value = id
   435  	if fields := puo.fields; len(fields) > 0 {
   436  		_spec.Node.Columns = make([]string, 0, len(fields))
   437  		_spec.Node.Columns = append(_spec.Node.Columns, permission.FieldID)
   438  		for _, f := range fields {
   439  			if !permission.ValidColumn(f) {
   440  				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
   441  			}
   442  			if f != permission.FieldID {
   443  				_spec.Node.Columns = append(_spec.Node.Columns, f)
   444  			}
   445  		}
   446  	}
   447  	if ps := puo.mutation.predicates; len(ps) > 0 {
   448  		_spec.Predicate = func(selector *sql.Selector) {
   449  			for i := range ps {
   450  				ps[i](selector)
   451  			}
   452  		}
   453  	}
   454  	if value, ok := puo.mutation.UpdatedAt(); ok {
   455  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   456  			Type:   field.TypeTime,
   457  			Value:  value,
   458  			Column: permission.FieldUpdatedAt,
   459  		})
   460  	}
   461  	if value, ok := puo.mutation.DeletedAt(); ok {
   462  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   463  			Type:   field.TypeTime,
   464  			Value:  value,
   465  			Column: permission.FieldDeletedAt,
   466  		})
   467  	}
   468  	if puo.mutation.DeletedAtCleared() {
   469  		_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
   470  			Type:   field.TypeTime,
   471  			Column: permission.FieldDeletedAt,
   472  		})
   473  	}
   474  	if value, ok := puo.mutation.Action(); ok {
   475  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   476  			Type:   field.TypeString,
   477  			Value:  value,
   478  			Column: permission.FieldAction,
   479  		})
   480  	}
   481  	if value, ok := puo.mutation.Value(); ok {
   482  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   483  			Type:   field.TypeString,
   484  			Value:  value,
   485  			Column: permission.FieldValue,
   486  		})
   487  	}
   488  	if puo.mutation.RoleCleared() {
   489  		edge := &sqlgraph.EdgeSpec{
   490  			Rel:     sqlgraph.M2O,
   491  			Inverse: true,
   492  			Table:   permission.RoleTable,
   493  			Columns: []string{permission.RoleColumn},
   494  			Bidi:    false,
   495  			Target: &sqlgraph.EdgeTarget{
   496  				IDSpec: &sqlgraph.FieldSpec{
   497  					Type:   field.TypeInt,
   498  					Column: role.FieldID,
   499  				},
   500  			},
   501  		}
   502  		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
   503  	}
   504  	if nodes := puo.mutation.RoleIDs(); len(nodes) > 0 {
   505  		edge := &sqlgraph.EdgeSpec{
   506  			Rel:     sqlgraph.M2O,
   507  			Inverse: true,
   508  			Table:   permission.RoleTable,
   509  			Columns: []string{permission.RoleColumn},
   510  			Bidi:    false,
   511  			Target: &sqlgraph.EdgeTarget{
   512  				IDSpec: &sqlgraph.FieldSpec{
   513  					Type:   field.TypeInt,
   514  					Column: role.FieldID,
   515  				},
   516  			},
   517  		}
   518  		for _, k := range nodes {
   519  			edge.Target.Nodes = append(edge.Target.Nodes, k)
   520  		}
   521  		_spec.Edges.Add = append(_spec.Edges.Add, edge)
   522  	}
   523  	_node = &Permission{config: puo.config}
   524  	_spec.Assign = _node.assignValues
   525  	_spec.ScanValues = _node.scanValues
   526  	if err = sqlgraph.UpdateNode(ctx, puo.driver, _spec); err != nil {
   527  		if _, ok := err.(*sqlgraph.NotFoundError); ok {
   528  			err = &NotFoundError{permission.Label}
   529  		} else if sqlgraph.IsConstraintError(err) {
   530  			err = &ConstraintError{err.Error(), err}
   531  		}
   532  		return nil, err
   533  	}
   534  	return _node, nil
   535  }