// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"demo/ent/department"
	"demo/ent/instructor"
	"demo/ent/predicate"
	"errors"
	"fmt"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// InstructorUpdate is the builder for updating Instructor entities.
type InstructorUpdate struct {
	config
	hooks    []Hook
	mutation *InstructorMutation
}

// Where appends a list predicates to the InstructorUpdate builder.
func (iu *InstructorUpdate) Where(ps ...predicate.Instructor) *InstructorUpdate {
	iu.mutation.Where(ps...)
	return iu
}

// SetName sets the "name" field.
func (iu *InstructorUpdate) SetName(s string) *InstructorUpdate {
	iu.mutation.SetName(s)
	return iu
}

// SetNillableName sets the "name" field if the given value is not nil.
func (iu *InstructorUpdate) SetNillableName(s *string) *InstructorUpdate {
	if s != nil {
		iu.SetName(*s)
	}
	return iu
}

// SetDepartmentID sets the "department" edge to the Department entity by ID.
func (iu *InstructorUpdate) SetDepartmentID(id int) *InstructorUpdate {
	iu.mutation.SetDepartmentID(id)
	return iu
}

// SetDepartment sets the "department" edge to the Department entity.
func (iu *InstructorUpdate) SetDepartment(d *Department) *InstructorUpdate {
	return iu.SetDepartmentID(d.ID)
}

// Mutation returns the InstructorMutation object of the builder.
func (iu *InstructorUpdate) Mutation() *InstructorMutation {
	return iu.mutation
}

// ClearDepartment clears the "department" edge to the Department entity.
func (iu *InstructorUpdate) ClearDepartment() *InstructorUpdate {
	iu.mutation.ClearDepartment()
	return iu
}

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (iu *InstructorUpdate) check() error {
	if v, ok := iu.mutation.Name(); ok {
		if err := instructor.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Instructor.name": %w`, err)}
		}
	}
	if _, ok := iu.mutation.DepartmentID(); iu.mutation.DepartmentCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "Instructor.department"`)
	}
	return nil
}

func (iu *InstructorUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := iu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(instructor.Table, instructor.Columns, sqlgraph.NewFieldSpec(instructor.FieldID, field.TypeInt))
	if ps := iu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := iu.mutation.Name(); ok {
		_spec.SetField(instructor.FieldName, field.TypeString, value)
	}
	if iu.mutation.DepartmentCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   instructor.DepartmentTable,
			Columns: []string{instructor.DepartmentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(department.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := iu.mutation.DepartmentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   instructor.DepartmentTable,
			Columns: []string{instructor.DepartmentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(department.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, iu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{instructor.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	iu.mutation.done = true
	return n, nil
}

// InstructorUpdateOne is the builder for updating a single Instructor entity.
type InstructorUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *InstructorMutation
}

// SetName sets the "name" field.
func (iuo *InstructorUpdateOne) SetName(s string) *InstructorUpdateOne {
	iuo.mutation.SetName(s)
	return iuo
}

// SetNillableName sets the "name" field if the given value is not nil.
func (iuo *InstructorUpdateOne) SetNillableName(s *string) *InstructorUpdateOne {
	if s != nil {
		iuo.SetName(*s)
	}
	return iuo
}

// SetDepartmentID sets the "department" edge to the Department entity by ID.
func (iuo *InstructorUpdateOne) SetDepartmentID(id int) *InstructorUpdateOne {
	iuo.mutation.SetDepartmentID(id)
	return iuo
}

// SetDepartment sets the "department" edge to the Department entity.
func (iuo *InstructorUpdateOne) SetDepartment(d *Department) *InstructorUpdateOne {
	return iuo.SetDepartmentID(d.ID)
}

// Mutation returns the InstructorMutation object of the builder.
func (iuo *InstructorUpdateOne) Mutation() *InstructorMutation {
	return iuo.mutation
}

// ClearDepartment clears the "department" edge to the Department entity.
func (iuo *InstructorUpdateOne) ClearDepartment() *InstructorUpdateOne {
	iuo.mutation.ClearDepartment()
	return iuo
}

// Where appends a list predicates to the InstructorUpdate builder.
func (iuo *InstructorUpdateOne) Where(ps ...predicate.Instructor) *InstructorUpdateOne {
	iuo.mutation.Where(ps...)
	return iuo
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (iuo *InstructorUpdateOne) check() error {
	if v, ok := iuo.mutation.Name(); ok {
		if err := instructor.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Instructor.name": %w`, err)}
		}
	}
	if _, ok := iuo.mutation.DepartmentID(); iuo.mutation.DepartmentCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "Instructor.department"`)
	}
	return nil
}

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