// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"example/myco-api/ent/grainjar"
	"example/myco-api/ent/predicate"
	"example/myco-api/ent/sporesyringe"
	"fmt"
	"time"

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

// GrainJarUpdate is the builder for updating GrainJar entities.
type GrainJarUpdate struct {
	config
	hooks    []Hook
	mutation *GrainJarMutation
}

// Where appends a list predicates to the GrainJarUpdate builder.
func (gju *GrainJarUpdate) Where(ps ...predicate.GrainJar) *GrainJarUpdate {
	gju.mutation.Where(ps...)
	return gju
}

// SetInnoculationDate sets the "InnoculationDate" field.
func (gju *GrainJarUpdate) SetInnoculationDate(t time.Time) *GrainJarUpdate {
	gju.mutation.SetInnoculationDate(t)
	return gju
}

// SetNillableInnoculationDate sets the "InnoculationDate" field if the given value is not nil.
func (gju *GrainJarUpdate) SetNillableInnoculationDate(t *time.Time) *GrainJarUpdate {
	if t != nil {
		gju.SetInnoculationDate(*t)
	}
	return gju
}

// SetGrain sets the "Grain" field.
func (gju *GrainJarUpdate) SetGrain(s string) *GrainJarUpdate {
	gju.mutation.SetGrain(s)
	return gju
}

// SetNillableGrain sets the "Grain" field if the given value is not nil.
func (gju *GrainJarUpdate) SetNillableGrain(s *string) *GrainJarUpdate {
	if s != nil {
		gju.SetGrain(*s)
	}
	return gju
}

// SetHarvestDate sets the "HarvestDate" field.
func (gju *GrainJarUpdate) SetHarvestDate(t time.Time) *GrainJarUpdate {
	gju.mutation.SetHarvestDate(t)
	return gju
}

// SetNillableHarvestDate sets the "HarvestDate" field if the given value is not nil.
func (gju *GrainJarUpdate) SetNillableHarvestDate(t *time.Time) *GrainJarUpdate {
	if t != nil {
		gju.SetHarvestDate(*t)
	}
	return gju
}

// ClearHarvestDate clears the value of the "HarvestDate" field.
func (gju *GrainJarUpdate) ClearHarvestDate() *GrainJarUpdate {
	gju.mutation.ClearHarvestDate()
	return gju
}

// SetSporeSyringesID sets the "sporeSyringes" edge to the SporeSyringe entity by ID.
func (gju *GrainJarUpdate) SetSporeSyringesID(id int) *GrainJarUpdate {
	gju.mutation.SetSporeSyringesID(id)
	return gju
}

// SetNillableSporeSyringesID sets the "sporeSyringes" edge to the SporeSyringe entity by ID if the given value is not nil.
func (gju *GrainJarUpdate) SetNillableSporeSyringesID(id *int) *GrainJarUpdate {
	if id != nil {
		gju = gju.SetSporeSyringesID(*id)
	}
	return gju
}

// SetSporeSyringes sets the "sporeSyringes" edge to the SporeSyringe entity.
func (gju *GrainJarUpdate) SetSporeSyringes(s *SporeSyringe) *GrainJarUpdate {
	return gju.SetSporeSyringesID(s.ID)
}

// Mutation returns the GrainJarMutation object of the builder.
func (gju *GrainJarUpdate) Mutation() *GrainJarMutation {
	return gju.mutation
}

// ClearSporeSyringes clears the "sporeSyringes" edge to the SporeSyringe entity.
func (gju *GrainJarUpdate) ClearSporeSyringes() *GrainJarUpdate {
	gju.mutation.ClearSporeSyringes()
	return gju
}

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

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

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

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

func (gju *GrainJarUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := &sqlgraph.UpdateSpec{
		Node: &sqlgraph.NodeSpec{
			Table:   grainjar.Table,
			Columns: grainjar.Columns,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeInt,
				Column: grainjar.FieldID,
			},
		},
	}
	if ps := gju.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := gju.mutation.InnoculationDate(); ok {
		_spec.SetField(grainjar.FieldInnoculationDate, field.TypeTime, value)
	}
	if value, ok := gju.mutation.Grain(); ok {
		_spec.SetField(grainjar.FieldGrain, field.TypeString, value)
	}
	if value, ok := gju.mutation.HarvestDate(); ok {
		_spec.SetField(grainjar.FieldHarvestDate, field.TypeTime, value)
	}
	if gju.mutation.HarvestDateCleared() {
		_spec.ClearField(grainjar.FieldHarvestDate, field.TypeTime)
	}
	if gju.mutation.SporeSyringesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   grainjar.SporeSyringesTable,
			Columns: []string{grainjar.SporeSyringesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: &sqlgraph.FieldSpec{
					Type:   field.TypeInt,
					Column: sporesyringe.FieldID,
				},
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := gju.mutation.SporeSyringesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   grainjar.SporeSyringesTable,
			Columns: []string{grainjar.SporeSyringesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: &sqlgraph.FieldSpec{
					Type:   field.TypeInt,
					Column: sporesyringe.FieldID,
				},
			},
		}
		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, gju.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{grainjar.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	gju.mutation.done = true
	return n, nil
}

// GrainJarUpdateOne is the builder for updating a single GrainJar entity.
type GrainJarUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *GrainJarMutation
}

// SetInnoculationDate sets the "InnoculationDate" field.
func (gjuo *GrainJarUpdateOne) SetInnoculationDate(t time.Time) *GrainJarUpdateOne {
	gjuo.mutation.SetInnoculationDate(t)
	return gjuo
}

// SetNillableInnoculationDate sets the "InnoculationDate" field if the given value is not nil.
func (gjuo *GrainJarUpdateOne) SetNillableInnoculationDate(t *time.Time) *GrainJarUpdateOne {
	if t != nil {
		gjuo.SetInnoculationDate(*t)
	}
	return gjuo
}

// SetGrain sets the "Grain" field.
func (gjuo *GrainJarUpdateOne) SetGrain(s string) *GrainJarUpdateOne {
	gjuo.mutation.SetGrain(s)
	return gjuo
}

// SetNillableGrain sets the "Grain" field if the given value is not nil.
func (gjuo *GrainJarUpdateOne) SetNillableGrain(s *string) *GrainJarUpdateOne {
	if s != nil {
		gjuo.SetGrain(*s)
	}
	return gjuo
}

// SetHarvestDate sets the "HarvestDate" field.
func (gjuo *GrainJarUpdateOne) SetHarvestDate(t time.Time) *GrainJarUpdateOne {
	gjuo.mutation.SetHarvestDate(t)
	return gjuo
}

// SetNillableHarvestDate sets the "HarvestDate" field if the given value is not nil.
func (gjuo *GrainJarUpdateOne) SetNillableHarvestDate(t *time.Time) *GrainJarUpdateOne {
	if t != nil {
		gjuo.SetHarvestDate(*t)
	}
	return gjuo
}

// ClearHarvestDate clears the value of the "HarvestDate" field.
func (gjuo *GrainJarUpdateOne) ClearHarvestDate() *GrainJarUpdateOne {
	gjuo.mutation.ClearHarvestDate()
	return gjuo
}

// SetSporeSyringesID sets the "sporeSyringes" edge to the SporeSyringe entity by ID.
func (gjuo *GrainJarUpdateOne) SetSporeSyringesID(id int) *GrainJarUpdateOne {
	gjuo.mutation.SetSporeSyringesID(id)
	return gjuo
}

// SetNillableSporeSyringesID sets the "sporeSyringes" edge to the SporeSyringe entity by ID if the given value is not nil.
func (gjuo *GrainJarUpdateOne) SetNillableSporeSyringesID(id *int) *GrainJarUpdateOne {
	if id != nil {
		gjuo = gjuo.SetSporeSyringesID(*id)
	}
	return gjuo
}

// SetSporeSyringes sets the "sporeSyringes" edge to the SporeSyringe entity.
func (gjuo *GrainJarUpdateOne) SetSporeSyringes(s *SporeSyringe) *GrainJarUpdateOne {
	return gjuo.SetSporeSyringesID(s.ID)
}

// Mutation returns the GrainJarMutation object of the builder.
func (gjuo *GrainJarUpdateOne) Mutation() *GrainJarMutation {
	return gjuo.mutation
}

// ClearSporeSyringes clears the "sporeSyringes" edge to the SporeSyringe entity.
func (gjuo *GrainJarUpdateOne) ClearSporeSyringes() *GrainJarUpdateOne {
	gjuo.mutation.ClearSporeSyringes()
	return gjuo
}

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

// Save executes the query and returns the updated GrainJar entity.
func (gjuo *GrainJarUpdateOne) Save(ctx context.Context) (*GrainJar, error) {
	return withHooks[*GrainJar, GrainJarMutation](ctx, gjuo.sqlSave, gjuo.mutation, gjuo.hooks)
}

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

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

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

func (gjuo *GrainJarUpdateOne) sqlSave(ctx context.Context) (_node *GrainJar, err error) {
	_spec := &sqlgraph.UpdateSpec{
		Node: &sqlgraph.NodeSpec{
			Table:   grainjar.Table,
			Columns: grainjar.Columns,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeInt,
				Column: grainjar.FieldID,
			},
		},
	}
	id, ok := gjuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "GrainJar.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := gjuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, grainjar.FieldID)
		for _, f := range fields {
			if !grainjar.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != grainjar.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := gjuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := gjuo.mutation.InnoculationDate(); ok {
		_spec.SetField(grainjar.FieldInnoculationDate, field.TypeTime, value)
	}
	if value, ok := gjuo.mutation.Grain(); ok {
		_spec.SetField(grainjar.FieldGrain, field.TypeString, value)
	}
	if value, ok := gjuo.mutation.HarvestDate(); ok {
		_spec.SetField(grainjar.FieldHarvestDate, field.TypeTime, value)
	}
	if gjuo.mutation.HarvestDateCleared() {
		_spec.ClearField(grainjar.FieldHarvestDate, field.TypeTime)
	}
	if gjuo.mutation.SporeSyringesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   grainjar.SporeSyringesTable,
			Columns: []string{grainjar.SporeSyringesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: &sqlgraph.FieldSpec{
					Type:   field.TypeInt,
					Column: sporesyringe.FieldID,
				},
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := gjuo.mutation.SporeSyringesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   grainjar.SporeSyringesTable,
			Columns: []string{grainjar.SporeSyringesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: &sqlgraph.FieldSpec{
					Type:   field.TypeInt,
					Column: sporesyringe.FieldID,
				},
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &GrainJar{config: gjuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, gjuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{grainjar.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	gjuo.mutation.done = true
	return _node, nil
}
