// 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"
)

// SporeSyringeUpdate is the builder for updating SporeSyringe entities.
type SporeSyringeUpdate struct {
	config
	hooks    []Hook
	mutation *SporeSyringeMutation
}

// Where appends a list predicates to the SporeSyringeUpdate builder.
func (ssu *SporeSyringeUpdate) Where(ps ...predicate.SporeSyringe) *SporeSyringeUpdate {
	ssu.mutation.Where(ps...)
	return ssu
}

// SetRecievedDate sets the "RecievedDate" field.
func (ssu *SporeSyringeUpdate) SetRecievedDate(t time.Time) *SporeSyringeUpdate {
	ssu.mutation.SetRecievedDate(t)
	return ssu
}

// SetNillableRecievedDate sets the "RecievedDate" field if the given value is not nil.
func (ssu *SporeSyringeUpdate) SetNillableRecievedDate(t *time.Time) *SporeSyringeUpdate {
	if t != nil {
		ssu.SetRecievedDate(*t)
	}
	return ssu
}

// SetSpecies sets the "Species" field.
func (ssu *SporeSyringeUpdate) SetSpecies(s string) *SporeSyringeUpdate {
	ssu.mutation.SetSpecies(s)
	return ssu
}

// SetNillableSpecies sets the "Species" field if the given value is not nil.
func (ssu *SporeSyringeUpdate) SetNillableSpecies(s *string) *SporeSyringeUpdate {
	if s != nil {
		ssu.SetSpecies(*s)
	}
	return ssu
}

// SetSupplier sets the "Supplier" field.
func (ssu *SporeSyringeUpdate) SetSupplier(s string) *SporeSyringeUpdate {
	ssu.mutation.SetSupplier(s)
	return ssu
}

// SetNillableSupplier sets the "Supplier" field if the given value is not nil.
func (ssu *SporeSyringeUpdate) SetNillableSupplier(s *string) *SporeSyringeUpdate {
	if s != nil {
		ssu.SetSupplier(*s)
	}
	return ssu
}

// AddGrainJarIDs adds the "grainJars" edge to the GrainJar entity by IDs.
func (ssu *SporeSyringeUpdate) AddGrainJarIDs(ids ...int) *SporeSyringeUpdate {
	ssu.mutation.AddGrainJarIDs(ids...)
	return ssu
}

// AddGrainJars adds the "grainJars" edges to the GrainJar entity.
func (ssu *SporeSyringeUpdate) AddGrainJars(g ...*GrainJar) *SporeSyringeUpdate {
	ids := make([]int, len(g))
	for i := range g {
		ids[i] = g[i].ID
	}
	return ssu.AddGrainJarIDs(ids...)
}

// Mutation returns the SporeSyringeMutation object of the builder.
func (ssu *SporeSyringeUpdate) Mutation() *SporeSyringeMutation {
	return ssu.mutation
}

// ClearGrainJars clears all "grainJars" edges to the GrainJar entity.
func (ssu *SporeSyringeUpdate) ClearGrainJars() *SporeSyringeUpdate {
	ssu.mutation.ClearGrainJars()
	return ssu
}

// RemoveGrainJarIDs removes the "grainJars" edge to GrainJar entities by IDs.
func (ssu *SporeSyringeUpdate) RemoveGrainJarIDs(ids ...int) *SporeSyringeUpdate {
	ssu.mutation.RemoveGrainJarIDs(ids...)
	return ssu
}

// RemoveGrainJars removes "grainJars" edges to GrainJar entities.
func (ssu *SporeSyringeUpdate) RemoveGrainJars(g ...*GrainJar) *SporeSyringeUpdate {
	ids := make([]int, len(g))
	for i := range g {
		ids[i] = g[i].ID
	}
	return ssu.RemoveGrainJarIDs(ids...)
}

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

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

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

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

func (ssu *SporeSyringeUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := &sqlgraph.UpdateSpec{
		Node: &sqlgraph.NodeSpec{
			Table:   sporesyringe.Table,
			Columns: sporesyringe.Columns,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeInt,
				Column: sporesyringe.FieldID,
			},
		},
	}
	if ps := ssu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := ssu.mutation.RecievedDate(); ok {
		_spec.SetField(sporesyringe.FieldRecievedDate, field.TypeTime, value)
	}
	if value, ok := ssu.mutation.Species(); ok {
		_spec.SetField(sporesyringe.FieldSpecies, field.TypeString, value)
	}
	if value, ok := ssu.mutation.Supplier(); ok {
		_spec.SetField(sporesyringe.FieldSupplier, field.TypeString, value)
	}
	if ssu.mutation.GrainJarsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   sporesyringe.GrainJarsTable,
			Columns: []string{sporesyringe.GrainJarsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: &sqlgraph.FieldSpec{
					Type:   field.TypeInt,
					Column: grainjar.FieldID,
				},
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := ssu.mutation.RemovedGrainJarsIDs(); len(nodes) > 0 && !ssu.mutation.GrainJarsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   sporesyringe.GrainJarsTable,
			Columns: []string{sporesyringe.GrainJarsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: &sqlgraph.FieldSpec{
					Type:   field.TypeInt,
					Column: grainjar.FieldID,
				},
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := ssu.mutation.GrainJarsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   sporesyringe.GrainJarsTable,
			Columns: []string{sporesyringe.GrainJarsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: &sqlgraph.FieldSpec{
					Type:   field.TypeInt,
					Column: grainjar.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, ssu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{sporesyringe.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	ssu.mutation.done = true
	return n, nil
}

// SporeSyringeUpdateOne is the builder for updating a single SporeSyringe entity.
type SporeSyringeUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *SporeSyringeMutation
}

// SetRecievedDate sets the "RecievedDate" field.
func (ssuo *SporeSyringeUpdateOne) SetRecievedDate(t time.Time) *SporeSyringeUpdateOne {
	ssuo.mutation.SetRecievedDate(t)
	return ssuo
}

// SetNillableRecievedDate sets the "RecievedDate" field if the given value is not nil.
func (ssuo *SporeSyringeUpdateOne) SetNillableRecievedDate(t *time.Time) *SporeSyringeUpdateOne {
	if t != nil {
		ssuo.SetRecievedDate(*t)
	}
	return ssuo
}

// SetSpecies sets the "Species" field.
func (ssuo *SporeSyringeUpdateOne) SetSpecies(s string) *SporeSyringeUpdateOne {
	ssuo.mutation.SetSpecies(s)
	return ssuo
}

// SetNillableSpecies sets the "Species" field if the given value is not nil.
func (ssuo *SporeSyringeUpdateOne) SetNillableSpecies(s *string) *SporeSyringeUpdateOne {
	if s != nil {
		ssuo.SetSpecies(*s)
	}
	return ssuo
}

// SetSupplier sets the "Supplier" field.
func (ssuo *SporeSyringeUpdateOne) SetSupplier(s string) *SporeSyringeUpdateOne {
	ssuo.mutation.SetSupplier(s)
	return ssuo
}

// SetNillableSupplier sets the "Supplier" field if the given value is not nil.
func (ssuo *SporeSyringeUpdateOne) SetNillableSupplier(s *string) *SporeSyringeUpdateOne {
	if s != nil {
		ssuo.SetSupplier(*s)
	}
	return ssuo
}

// AddGrainJarIDs adds the "grainJars" edge to the GrainJar entity by IDs.
func (ssuo *SporeSyringeUpdateOne) AddGrainJarIDs(ids ...int) *SporeSyringeUpdateOne {
	ssuo.mutation.AddGrainJarIDs(ids...)
	return ssuo
}

// AddGrainJars adds the "grainJars" edges to the GrainJar entity.
func (ssuo *SporeSyringeUpdateOne) AddGrainJars(g ...*GrainJar) *SporeSyringeUpdateOne {
	ids := make([]int, len(g))
	for i := range g {
		ids[i] = g[i].ID
	}
	return ssuo.AddGrainJarIDs(ids...)
}

// Mutation returns the SporeSyringeMutation object of the builder.
func (ssuo *SporeSyringeUpdateOne) Mutation() *SporeSyringeMutation {
	return ssuo.mutation
}

// ClearGrainJars clears all "grainJars" edges to the GrainJar entity.
func (ssuo *SporeSyringeUpdateOne) ClearGrainJars() *SporeSyringeUpdateOne {
	ssuo.mutation.ClearGrainJars()
	return ssuo
}

// RemoveGrainJarIDs removes the "grainJars" edge to GrainJar entities by IDs.
func (ssuo *SporeSyringeUpdateOne) RemoveGrainJarIDs(ids ...int) *SporeSyringeUpdateOne {
	ssuo.mutation.RemoveGrainJarIDs(ids...)
	return ssuo
}

// RemoveGrainJars removes "grainJars" edges to GrainJar entities.
func (ssuo *SporeSyringeUpdateOne) RemoveGrainJars(g ...*GrainJar) *SporeSyringeUpdateOne {
	ids := make([]int, len(g))
	for i := range g {
		ids[i] = g[i].ID
	}
	return ssuo.RemoveGrainJarIDs(ids...)
}

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

// Save executes the query and returns the updated SporeSyringe entity.
func (ssuo *SporeSyringeUpdateOne) Save(ctx context.Context) (*SporeSyringe, error) {
	return withHooks[*SporeSyringe, SporeSyringeMutation](ctx, ssuo.sqlSave, ssuo.mutation, ssuo.hooks)
}

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

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

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

func (ssuo *SporeSyringeUpdateOne) sqlSave(ctx context.Context) (_node *SporeSyringe, err error) {
	_spec := &sqlgraph.UpdateSpec{
		Node: &sqlgraph.NodeSpec{
			Table:   sporesyringe.Table,
			Columns: sporesyringe.Columns,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeInt,
				Column: sporesyringe.FieldID,
			},
		},
	}
	id, ok := ssuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "SporeSyringe.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := ssuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, sporesyringe.FieldID)
		for _, f := range fields {
			if !sporesyringe.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != sporesyringe.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := ssuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := ssuo.mutation.RecievedDate(); ok {
		_spec.SetField(sporesyringe.FieldRecievedDate, field.TypeTime, value)
	}
	if value, ok := ssuo.mutation.Species(); ok {
		_spec.SetField(sporesyringe.FieldSpecies, field.TypeString, value)
	}
	if value, ok := ssuo.mutation.Supplier(); ok {
		_spec.SetField(sporesyringe.FieldSupplier, field.TypeString, value)
	}
	if ssuo.mutation.GrainJarsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   sporesyringe.GrainJarsTable,
			Columns: []string{sporesyringe.GrainJarsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: &sqlgraph.FieldSpec{
					Type:   field.TypeInt,
					Column: grainjar.FieldID,
				},
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := ssuo.mutation.RemovedGrainJarsIDs(); len(nodes) > 0 && !ssuo.mutation.GrainJarsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   sporesyringe.GrainJarsTable,
			Columns: []string{sporesyringe.GrainJarsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: &sqlgraph.FieldSpec{
					Type:   field.TypeInt,
					Column: grainjar.FieldID,
				},
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := ssuo.mutation.GrainJarsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   sporesyringe.GrainJarsTable,
			Columns: []string{sporesyringe.GrainJarsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: &sqlgraph.FieldSpec{
					Type:   field.TypeInt,
					Column: grainjar.FieldID,
				},
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &SporeSyringe{config: ssuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, ssuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{sporesyringe.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	ssuo.mutation.done = true
	return _node, nil
}
