// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/lightsparkdev/spark/so/ent/l1tokencreate"
	"github.com/lightsparkdev/spark/so/ent/predicate"
)

// L1TokenCreateUpdate is the builder for updating L1TokenCreate entities.
type L1TokenCreateUpdate struct {
	config
	hooks    []Hook
	mutation *L1TokenCreateMutation
}

// Where appends a list predicates to the L1TokenCreateUpdate builder.
func (lcu *L1TokenCreateUpdate) Where(ps ...predicate.L1TokenCreate) *L1TokenCreateUpdate {
	lcu.mutation.Where(ps...)
	return lcu
}

// SetUpdateTime sets the "update_time" field.
func (lcu *L1TokenCreateUpdate) SetUpdateTime(t time.Time) *L1TokenCreateUpdate {
	lcu.mutation.SetUpdateTime(t)
	return lcu
}

// Mutation returns the L1TokenCreateMutation object of the builder.
func (lcu *L1TokenCreateUpdate) Mutation() *L1TokenCreateMutation {
	return lcu.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (lcu *L1TokenCreateUpdate) defaults() {
	if _, ok := lcu.mutation.UpdateTime(); !ok {
		v := l1tokencreate.UpdateDefaultUpdateTime()
		lcu.mutation.SetUpdateTime(v)
	}
}

func (lcu *L1TokenCreateUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(l1tokencreate.Table, l1tokencreate.Columns, sqlgraph.NewFieldSpec(l1tokencreate.FieldID, field.TypeUUID))
	if ps := lcu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := lcu.mutation.UpdateTime(); ok {
		_spec.SetField(l1tokencreate.FieldUpdateTime, field.TypeTime, value)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, lcu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{l1tokencreate.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	lcu.mutation.done = true
	return n, nil
}

// L1TokenCreateUpdateOne is the builder for updating a single L1TokenCreate entity.
type L1TokenCreateUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *L1TokenCreateMutation
}

// SetUpdateTime sets the "update_time" field.
func (lcuo *L1TokenCreateUpdateOne) SetUpdateTime(t time.Time) *L1TokenCreateUpdateOne {
	lcuo.mutation.SetUpdateTime(t)
	return lcuo
}

// Mutation returns the L1TokenCreateMutation object of the builder.
func (lcuo *L1TokenCreateUpdateOne) Mutation() *L1TokenCreateMutation {
	return lcuo.mutation
}

// Where appends a list predicates to the L1TokenCreateUpdate builder.
func (lcuo *L1TokenCreateUpdateOne) Where(ps ...predicate.L1TokenCreate) *L1TokenCreateUpdateOne {
	lcuo.mutation.Where(ps...)
	return lcuo
}

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

// Save executes the query and returns the updated L1TokenCreate entity.
func (lcuo *L1TokenCreateUpdateOne) Save(ctx context.Context) (*L1TokenCreate, error) {
	lcuo.defaults()
	return withHooks(ctx, lcuo.sqlSave, lcuo.mutation, lcuo.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (lcuo *L1TokenCreateUpdateOne) defaults() {
	if _, ok := lcuo.mutation.UpdateTime(); !ok {
		v := l1tokencreate.UpdateDefaultUpdateTime()
		lcuo.mutation.SetUpdateTime(v)
	}
}

func (lcuo *L1TokenCreateUpdateOne) sqlSave(ctx context.Context) (_node *L1TokenCreate, err error) {
	_spec := sqlgraph.NewUpdateSpec(l1tokencreate.Table, l1tokencreate.Columns, sqlgraph.NewFieldSpec(l1tokencreate.FieldID, field.TypeUUID))
	id, ok := lcuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "L1TokenCreate.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := lcuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, l1tokencreate.FieldID)
		for _, f := range fields {
			if !l1tokencreate.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != l1tokencreate.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := lcuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := lcuo.mutation.UpdateTime(); ok {
		_spec.SetField(l1tokencreate.FieldUpdateTime, field.TypeTime, value)
	}
	_node = &L1TokenCreate{config: lcuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, lcuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{l1tokencreate.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	lcuo.mutation.done = true
	return _node, nil
}
