// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"galileo/ent/apicategory"
	"galileo/ent/predicate"

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

// ApiCategoryUpdate is the builder for updating ApiCategory entities.
type ApiCategoryUpdate struct {
	config
	hooks    []Hook
	mutation *ApiCategoryMutation
}

// Where appends a list predicates to the ApiCategoryUpdate builder.
func (acu *ApiCategoryUpdate) Where(ps ...predicate.ApiCategory) *ApiCategoryUpdate {
	acu.mutation.Where(ps...)
	return acu
}

// Mutation returns the ApiCategoryMutation object of the builder.
func (acu *ApiCategoryUpdate) Mutation() *ApiCategoryMutation {
	return acu.mutation
}

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

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

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

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

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

// ApiCategoryUpdateOne is the builder for updating a single ApiCategory entity.
type ApiCategoryUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ApiCategoryMutation
}

// Mutation returns the ApiCategoryMutation object of the builder.
func (acuo *ApiCategoryUpdateOne) Mutation() *ApiCategoryMutation {
	return acuo.mutation
}

// Where appends a list predicates to the ApiCategoryUpdate builder.
func (acuo *ApiCategoryUpdateOne) Where(ps ...predicate.ApiCategory) *ApiCategoryUpdateOne {
	acuo.mutation.Where(ps...)
	return acuo
}

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

// Save executes the query and returns the updated ApiCategory entity.
func (acuo *ApiCategoryUpdateOne) Save(ctx context.Context) (*ApiCategory, error) {
	return withHooks[*ApiCategory, ApiCategoryMutation](ctx, acuo.sqlSave, acuo.mutation, acuo.hooks)
}

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

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

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

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