// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"git.dcentral.systems/toolz/goplt/internal/ent/permission"
	"git.dcentral.systems/toolz/goplt/internal/ent/predicate"
	"git.dcentral.systems/toolz/goplt/internal/ent/role"
	"git.dcentral.systems/toolz/goplt/internal/ent/rolepermission"
)

// RolePermissionUpdate is the builder for updating RolePermission entities.
type RolePermissionUpdate struct {
	config
	hooks    []Hook
	mutation *RolePermissionMutation
}

// Where appends a list predicates to the RolePermissionUpdate builder.
func (_u *RolePermissionUpdate) Where(ps ...predicate.RolePermission) *RolePermissionUpdate {
	_u.mutation.Where(ps...)
	return _u
}

// SetRoleID sets the "role_id" field.
func (_u *RolePermissionUpdate) SetRoleID(v string) *RolePermissionUpdate {
	_u.mutation.SetRoleID(v)
	return _u
}

// SetNillableRoleID sets the "role_id" field if the given value is not nil.
func (_u *RolePermissionUpdate) SetNillableRoleID(v *string) *RolePermissionUpdate {
	if v != nil {
		_u.SetRoleID(*v)
	}
	return _u
}

// SetPermissionID sets the "permission_id" field.
func (_u *RolePermissionUpdate) SetPermissionID(v string) *RolePermissionUpdate {
	_u.mutation.SetPermissionID(v)
	return _u
}

// SetNillablePermissionID sets the "permission_id" field if the given value is not nil.
func (_u *RolePermissionUpdate) SetNillablePermissionID(v *string) *RolePermissionUpdate {
	if v != nil {
		_u.SetPermissionID(*v)
	}
	return _u
}

// SetRole sets the "role" edge to the Role entity.
func (_u *RolePermissionUpdate) SetRole(v *Role) *RolePermissionUpdate {
	return _u.SetRoleID(v.ID)
}

// SetPermission sets the "permission" edge to the Permission entity.
func (_u *RolePermissionUpdate) SetPermission(v *Permission) *RolePermissionUpdate {
	return _u.SetPermissionID(v.ID)
}

// Mutation returns the RolePermissionMutation object of the builder.
func (_u *RolePermissionUpdate) Mutation() *RolePermissionMutation {
	return _u.mutation
}

// ClearRole clears the "role" edge to the Role entity.
func (_u *RolePermissionUpdate) ClearRole() *RolePermissionUpdate {
	_u.mutation.ClearRole()
	return _u
}

// ClearPermission clears the "permission" edge to the Permission entity.
func (_u *RolePermissionUpdate) ClearPermission() *RolePermissionUpdate {
	_u.mutation.ClearPermission()
	return _u
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *RolePermissionUpdate) Save(ctx context.Context) (int, error) {
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (_u *RolePermissionUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

// Exec executes the query.
func (_u *RolePermissionUpdate) Exec(ctx context.Context) error {
	_, err := _u.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (_u *RolePermissionUpdate) ExecX(ctx context.Context) {
	if err := _u.Exec(ctx); err != nil {
		panic(err)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *RolePermissionUpdate) check() error {
	if _u.mutation.RoleCleared() && len(_u.mutation.RoleIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "RolePermission.role"`)
	}
	if _u.mutation.PermissionCleared() && len(_u.mutation.PermissionIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "RolePermission.permission"`)
	}
	return nil
}

func (_u *RolePermissionUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(rolepermission.Table, rolepermission.Columns, sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt))
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if _u.mutation.RoleCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   rolepermission.RoleTable,
			Columns: []string{rolepermission.RoleColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(role.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RoleIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   rolepermission.RoleTable,
			Columns: []string{rolepermission.RoleColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(role.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.PermissionCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   rolepermission.PermissionTable,
			Columns: []string{rolepermission.PermissionColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(permission.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.PermissionIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   rolepermission.PermissionTable,
			Columns: []string{rolepermission.PermissionColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(permission.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{rolepermission.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// RolePermissionUpdateOne is the builder for updating a single RolePermission entity.
type RolePermissionUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *RolePermissionMutation
}

// SetRoleID sets the "role_id" field.
func (_u *RolePermissionUpdateOne) SetRoleID(v string) *RolePermissionUpdateOne {
	_u.mutation.SetRoleID(v)
	return _u
}

// SetNillableRoleID sets the "role_id" field if the given value is not nil.
func (_u *RolePermissionUpdateOne) SetNillableRoleID(v *string) *RolePermissionUpdateOne {
	if v != nil {
		_u.SetRoleID(*v)
	}
	return _u
}

// SetPermissionID sets the "permission_id" field.
func (_u *RolePermissionUpdateOne) SetPermissionID(v string) *RolePermissionUpdateOne {
	_u.mutation.SetPermissionID(v)
	return _u
}

// SetNillablePermissionID sets the "permission_id" field if the given value is not nil.
func (_u *RolePermissionUpdateOne) SetNillablePermissionID(v *string) *RolePermissionUpdateOne {
	if v != nil {
		_u.SetPermissionID(*v)
	}
	return _u
}

// SetRole sets the "role" edge to the Role entity.
func (_u *RolePermissionUpdateOne) SetRole(v *Role) *RolePermissionUpdateOne {
	return _u.SetRoleID(v.ID)
}

// SetPermission sets the "permission" edge to the Permission entity.
func (_u *RolePermissionUpdateOne) SetPermission(v *Permission) *RolePermissionUpdateOne {
	return _u.SetPermissionID(v.ID)
}

// Mutation returns the RolePermissionMutation object of the builder.
func (_u *RolePermissionUpdateOne) Mutation() *RolePermissionMutation {
	return _u.mutation
}

// ClearRole clears the "role" edge to the Role entity.
func (_u *RolePermissionUpdateOne) ClearRole() *RolePermissionUpdateOne {
	_u.mutation.ClearRole()
	return _u
}

// ClearPermission clears the "permission" edge to the Permission entity.
func (_u *RolePermissionUpdateOne) ClearPermission() *RolePermissionUpdateOne {
	_u.mutation.ClearPermission()
	return _u
}

// Where appends a list predicates to the RolePermissionUpdate builder.
func (_u *RolePermissionUpdateOne) Where(ps ...predicate.RolePermission) *RolePermissionUpdateOne {
	_u.mutation.Where(ps...)
	return _u
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (_u *RolePermissionUpdateOne) Select(field string, fields ...string) *RolePermissionUpdateOne {
	_u.fields = append([]string{field}, fields...)
	return _u
}

// Save executes the query and returns the updated RolePermission entity.
func (_u *RolePermissionUpdateOne) Save(ctx context.Context) (*RolePermission, error) {
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (_u *RolePermissionUpdateOne) SaveX(ctx context.Context) *RolePermission {
	node, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (_u *RolePermissionUpdateOne) Exec(ctx context.Context) error {
	_, err := _u.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (_u *RolePermissionUpdateOne) ExecX(ctx context.Context) {
	if err := _u.Exec(ctx); err != nil {
		panic(err)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *RolePermissionUpdateOne) check() error {
	if _u.mutation.RoleCleared() && len(_u.mutation.RoleIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "RolePermission.role"`)
	}
	if _u.mutation.PermissionCleared() && len(_u.mutation.PermissionIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "RolePermission.permission"`)
	}
	return nil
}

func (_u *RolePermissionUpdateOne) sqlSave(ctx context.Context) (_node *RolePermission, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(rolepermission.Table, rolepermission.Columns, sqlgraph.NewFieldSpec(rolepermission.FieldID, field.TypeInt))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "RolePermission.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := _u.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, rolepermission.FieldID)
		for _, f := range fields {
			if !rolepermission.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != rolepermission.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if _u.mutation.RoleCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   rolepermission.RoleTable,
			Columns: []string{rolepermission.RoleColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(role.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RoleIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   rolepermission.RoleTable,
			Columns: []string{rolepermission.RoleColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(role.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.PermissionCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   rolepermission.PermissionTable,
			Columns: []string{rolepermission.PermissionColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(permission.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.PermissionIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: false,
			Table:   rolepermission.PermissionTable,
			Columns: []string{rolepermission.PermissionColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(permission.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &RolePermission{config: _u.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{rolepermission.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
