// 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/dalbodeule/hop-gate/ent/domain"
	"github.com/dalbodeule/hop-gate/ent/predicate"
)

// DomainUpdate is the builder for updating Domain entities.
type DomainUpdate struct {
	config
	hooks    []Hook
	mutation *DomainMutation
}

// Where appends a list predicates to the DomainUpdate builder.
func (_u *DomainUpdate) Where(ps ...predicate.Domain) *DomainUpdate {
	_u.mutation.Where(ps...)
	return _u
}

// SetClientAPIKey sets the "client_api_key" field.
func (_u *DomainUpdate) SetClientAPIKey(v string) *DomainUpdate {
	_u.mutation.SetClientAPIKey(v)
	return _u
}

// SetNillableClientAPIKey sets the "client_api_key" field if the given value is not nil.
func (_u *DomainUpdate) SetNillableClientAPIKey(v *string) *DomainUpdate {
	if v != nil {
		_u.SetClientAPIKey(*v)
	}
	return _u
}

// SetMemo sets the "memo" field.
func (_u *DomainUpdate) SetMemo(v string) *DomainUpdate {
	_u.mutation.SetMemo(v)
	return _u
}

// SetNillableMemo sets the "memo" field if the given value is not nil.
func (_u *DomainUpdate) SetNillableMemo(v *string) *DomainUpdate {
	if v != nil {
		_u.SetMemo(*v)
	}
	return _u
}

// SetCreatedAt sets the "created_at" field.
func (_u *DomainUpdate) SetCreatedAt(v time.Time) *DomainUpdate {
	_u.mutation.SetCreatedAt(v)
	return _u
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_u *DomainUpdate) SetNillableCreatedAt(v *time.Time) *DomainUpdate {
	if v != nil {
		_u.SetCreatedAt(*v)
	}
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *DomainUpdate) SetUpdatedAt(v time.Time) *DomainUpdate {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// Mutation returns the DomainMutation object of the builder.
func (_u *DomainUpdate) Mutation() *DomainMutation {
	return _u.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (_u *DomainUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := domain.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *DomainUpdate) check() error {
	if v, ok := _u.mutation.ClientAPIKey(); ok {
		if err := domain.ClientAPIKeyValidator(v); err != nil {
			return &ValidationError{Name: "client_api_key", err: fmt.Errorf(`ent: validator failed for field "Domain.client_api_key": %w`, err)}
		}
	}
	return nil
}

func (_u *DomainUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(domain.Table, domain.Columns, sqlgraph.NewFieldSpec(domain.FieldID, field.TypeUUID))
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.ClientAPIKey(); ok {
		_spec.SetField(domain.FieldClientAPIKey, field.TypeString, value)
	}
	if value, ok := _u.mutation.Memo(); ok {
		_spec.SetField(domain.FieldMemo, field.TypeString, value)
	}
	if value, ok := _u.mutation.CreatedAt(); ok {
		_spec.SetField(domain.FieldCreatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(domain.FieldUpdatedAt, field.TypeTime, value)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{domain.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// DomainUpdateOne is the builder for updating a single Domain entity.
type DomainUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *DomainMutation
}

// SetClientAPIKey sets the "client_api_key" field.
func (_u *DomainUpdateOne) SetClientAPIKey(v string) *DomainUpdateOne {
	_u.mutation.SetClientAPIKey(v)
	return _u
}

// SetNillableClientAPIKey sets the "client_api_key" field if the given value is not nil.
func (_u *DomainUpdateOne) SetNillableClientAPIKey(v *string) *DomainUpdateOne {
	if v != nil {
		_u.SetClientAPIKey(*v)
	}
	return _u
}

// SetMemo sets the "memo" field.
func (_u *DomainUpdateOne) SetMemo(v string) *DomainUpdateOne {
	_u.mutation.SetMemo(v)
	return _u
}

// SetNillableMemo sets the "memo" field if the given value is not nil.
func (_u *DomainUpdateOne) SetNillableMemo(v *string) *DomainUpdateOne {
	if v != nil {
		_u.SetMemo(*v)
	}
	return _u
}

// SetCreatedAt sets the "created_at" field.
func (_u *DomainUpdateOne) SetCreatedAt(v time.Time) *DomainUpdateOne {
	_u.mutation.SetCreatedAt(v)
	return _u
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_u *DomainUpdateOne) SetNillableCreatedAt(v *time.Time) *DomainUpdateOne {
	if v != nil {
		_u.SetCreatedAt(*v)
	}
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *DomainUpdateOne) SetUpdatedAt(v time.Time) *DomainUpdateOne {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// Mutation returns the DomainMutation object of the builder.
func (_u *DomainUpdateOne) Mutation() *DomainMutation {
	return _u.mutation
}

// Where appends a list predicates to the DomainUpdate builder.
func (_u *DomainUpdateOne) Where(ps ...predicate.Domain) *DomainUpdateOne {
	_u.mutation.Where(ps...)
	return _u
}

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

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

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

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

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

// defaults sets the default values of the builder before save.
func (_u *DomainUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := domain.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *DomainUpdateOne) check() error {
	if v, ok := _u.mutation.ClientAPIKey(); ok {
		if err := domain.ClientAPIKeyValidator(v); err != nil {
			return &ValidationError{Name: "client_api_key", err: fmt.Errorf(`ent: validator failed for field "Domain.client_api_key": %w`, err)}
		}
	}
	return nil
}

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