// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"demo/ent/course"
	"demo/ent/enrollment"
	"demo/ent/predicate"
	"demo/ent/student"
	"errors"
	"fmt"

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

// EnrollmentUpdate is the builder for updating Enrollment entities.
type EnrollmentUpdate struct {
	config
	hooks    []Hook
	mutation *EnrollmentMutation
}

// Where appends a list predicates to the EnrollmentUpdate builder.
func (eu *EnrollmentUpdate) Where(ps ...predicate.Enrollment) *EnrollmentUpdate {
	eu.mutation.Where(ps...)
	return eu
}

// SetSemester sets the "semester" field.
func (eu *EnrollmentUpdate) SetSemester(s string) *EnrollmentUpdate {
	eu.mutation.SetSemester(s)
	return eu
}

// SetNillableSemester sets the "semester" field if the given value is not nil.
func (eu *EnrollmentUpdate) SetNillableSemester(s *string) *EnrollmentUpdate {
	if s != nil {
		eu.SetSemester(*s)
	}
	return eu
}

// SetYear sets the "year" field.
func (eu *EnrollmentUpdate) SetYear(i int) *EnrollmentUpdate {
	eu.mutation.ResetYear()
	eu.mutation.SetYear(i)
	return eu
}

// SetNillableYear sets the "year" field if the given value is not nil.
func (eu *EnrollmentUpdate) SetNillableYear(i *int) *EnrollmentUpdate {
	if i != nil {
		eu.SetYear(*i)
	}
	return eu
}

// AddYear adds i to the "year" field.
func (eu *EnrollmentUpdate) AddYear(i int) *EnrollmentUpdate {
	eu.mutation.AddYear(i)
	return eu
}

// SetStudentID sets the "student" edge to the Student entity by ID.
func (eu *EnrollmentUpdate) SetStudentID(id int) *EnrollmentUpdate {
	eu.mutation.SetStudentID(id)
	return eu
}

// SetStudent sets the "student" edge to the Student entity.
func (eu *EnrollmentUpdate) SetStudent(s *Student) *EnrollmentUpdate {
	return eu.SetStudentID(s.ID)
}

// SetCourseID sets the "course" edge to the Course entity by ID.
func (eu *EnrollmentUpdate) SetCourseID(id int) *EnrollmentUpdate {
	eu.mutation.SetCourseID(id)
	return eu
}

// SetCourse sets the "course" edge to the Course entity.
func (eu *EnrollmentUpdate) SetCourse(c *Course) *EnrollmentUpdate {
	return eu.SetCourseID(c.ID)
}

// Mutation returns the EnrollmentMutation object of the builder.
func (eu *EnrollmentUpdate) Mutation() *EnrollmentMutation {
	return eu.mutation
}

// ClearStudent clears the "student" edge to the Student entity.
func (eu *EnrollmentUpdate) ClearStudent() *EnrollmentUpdate {
	eu.mutation.ClearStudent()
	return eu
}

// ClearCourse clears the "course" edge to the Course entity.
func (eu *EnrollmentUpdate) ClearCourse() *EnrollmentUpdate {
	eu.mutation.ClearCourse()
	return eu
}

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (eu *EnrollmentUpdate) check() error {
	if v, ok := eu.mutation.Semester(); ok {
		if err := enrollment.SemesterValidator(v); err != nil {
			return &ValidationError{Name: "semester", err: fmt.Errorf(`ent: validator failed for field "Enrollment.semester": %w`, err)}
		}
	}
	if v, ok := eu.mutation.Year(); ok {
		if err := enrollment.YearValidator(v); err != nil {
			return &ValidationError{Name: "year", err: fmt.Errorf(`ent: validator failed for field "Enrollment.year": %w`, err)}
		}
	}
	if _, ok := eu.mutation.StudentID(); eu.mutation.StudentCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "Enrollment.student"`)
	}
	if _, ok := eu.mutation.CourseID(); eu.mutation.CourseCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "Enrollment.course"`)
	}
	return nil
}

func (eu *EnrollmentUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := eu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(enrollment.Table, enrollment.Columns, sqlgraph.NewFieldSpec(enrollment.FieldID, field.TypeInt))
	if ps := eu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := eu.mutation.Semester(); ok {
		_spec.SetField(enrollment.FieldSemester, field.TypeString, value)
	}
	if value, ok := eu.mutation.Year(); ok {
		_spec.SetField(enrollment.FieldYear, field.TypeInt, value)
	}
	if value, ok := eu.mutation.AddedYear(); ok {
		_spec.AddField(enrollment.FieldYear, field.TypeInt, value)
	}
	if eu.mutation.StudentCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   enrollment.StudentTable,
			Columns: []string{enrollment.StudentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(student.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := eu.mutation.StudentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   enrollment.StudentTable,
			Columns: []string{enrollment.StudentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(student.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if eu.mutation.CourseCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   enrollment.CourseTable,
			Columns: []string{enrollment.CourseColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(course.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := eu.mutation.CourseIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   enrollment.CourseTable,
			Columns: []string{enrollment.CourseColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(course.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, eu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{enrollment.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	eu.mutation.done = true
	return n, nil
}

// EnrollmentUpdateOne is the builder for updating a single Enrollment entity.
type EnrollmentUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *EnrollmentMutation
}

// SetSemester sets the "semester" field.
func (euo *EnrollmentUpdateOne) SetSemester(s string) *EnrollmentUpdateOne {
	euo.mutation.SetSemester(s)
	return euo
}

// SetNillableSemester sets the "semester" field if the given value is not nil.
func (euo *EnrollmentUpdateOne) SetNillableSemester(s *string) *EnrollmentUpdateOne {
	if s != nil {
		euo.SetSemester(*s)
	}
	return euo
}

// SetYear sets the "year" field.
func (euo *EnrollmentUpdateOne) SetYear(i int) *EnrollmentUpdateOne {
	euo.mutation.ResetYear()
	euo.mutation.SetYear(i)
	return euo
}

// SetNillableYear sets the "year" field if the given value is not nil.
func (euo *EnrollmentUpdateOne) SetNillableYear(i *int) *EnrollmentUpdateOne {
	if i != nil {
		euo.SetYear(*i)
	}
	return euo
}

// AddYear adds i to the "year" field.
func (euo *EnrollmentUpdateOne) AddYear(i int) *EnrollmentUpdateOne {
	euo.mutation.AddYear(i)
	return euo
}

// SetStudentID sets the "student" edge to the Student entity by ID.
func (euo *EnrollmentUpdateOne) SetStudentID(id int) *EnrollmentUpdateOne {
	euo.mutation.SetStudentID(id)
	return euo
}

// SetStudent sets the "student" edge to the Student entity.
func (euo *EnrollmentUpdateOne) SetStudent(s *Student) *EnrollmentUpdateOne {
	return euo.SetStudentID(s.ID)
}

// SetCourseID sets the "course" edge to the Course entity by ID.
func (euo *EnrollmentUpdateOne) SetCourseID(id int) *EnrollmentUpdateOne {
	euo.mutation.SetCourseID(id)
	return euo
}

// SetCourse sets the "course" edge to the Course entity.
func (euo *EnrollmentUpdateOne) SetCourse(c *Course) *EnrollmentUpdateOne {
	return euo.SetCourseID(c.ID)
}

// Mutation returns the EnrollmentMutation object of the builder.
func (euo *EnrollmentUpdateOne) Mutation() *EnrollmentMutation {
	return euo.mutation
}

// ClearStudent clears the "student" edge to the Student entity.
func (euo *EnrollmentUpdateOne) ClearStudent() *EnrollmentUpdateOne {
	euo.mutation.ClearStudent()
	return euo
}

// ClearCourse clears the "course" edge to the Course entity.
func (euo *EnrollmentUpdateOne) ClearCourse() *EnrollmentUpdateOne {
	euo.mutation.ClearCourse()
	return euo
}

// Where appends a list predicates to the EnrollmentUpdate builder.
func (euo *EnrollmentUpdateOne) Where(ps ...predicate.Enrollment) *EnrollmentUpdateOne {
	euo.mutation.Where(ps...)
	return euo
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (euo *EnrollmentUpdateOne) check() error {
	if v, ok := euo.mutation.Semester(); ok {
		if err := enrollment.SemesterValidator(v); err != nil {
			return &ValidationError{Name: "semester", err: fmt.Errorf(`ent: validator failed for field "Enrollment.semester": %w`, err)}
		}
	}
	if v, ok := euo.mutation.Year(); ok {
		if err := enrollment.YearValidator(v); err != nil {
			return &ValidationError{Name: "year", err: fmt.Errorf(`ent: validator failed for field "Enrollment.year": %w`, err)}
		}
	}
	if _, ok := euo.mutation.StudentID(); euo.mutation.StudentCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "Enrollment.student"`)
	}
	if _, ok := euo.mutation.CourseID(); euo.mutation.CourseCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "Enrollment.course"`)
	}
	return nil
}

func (euo *EnrollmentUpdateOne) sqlSave(ctx context.Context) (_node *Enrollment, err error) {
	if err := euo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(enrollment.Table, enrollment.Columns, sqlgraph.NewFieldSpec(enrollment.FieldID, field.TypeInt))
	id, ok := euo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Enrollment.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := euo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, enrollment.FieldID)
		for _, f := range fields {
			if !enrollment.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != enrollment.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := euo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := euo.mutation.Semester(); ok {
		_spec.SetField(enrollment.FieldSemester, field.TypeString, value)
	}
	if value, ok := euo.mutation.Year(); ok {
		_spec.SetField(enrollment.FieldYear, field.TypeInt, value)
	}
	if value, ok := euo.mutation.AddedYear(); ok {
		_spec.AddField(enrollment.FieldYear, field.TypeInt, value)
	}
	if euo.mutation.StudentCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   enrollment.StudentTable,
			Columns: []string{enrollment.StudentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(student.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := euo.mutation.StudentIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   enrollment.StudentTable,
			Columns: []string{enrollment.StudentColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(student.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if euo.mutation.CourseCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   enrollment.CourseTable,
			Columns: []string{enrollment.CourseColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(course.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := euo.mutation.CourseIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   enrollment.CourseTable,
			Columns: []string{enrollment.CourseColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(course.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Enrollment{config: euo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, euo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{enrollment.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	euo.mutation.done = true
	return _node, nil
}
