// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"galileo/ent/apihistory"
	"galileo/ent/predicate"
	"time"

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

// ApiHistoryUpdate is the builder for updating ApiHistory entities.
type ApiHistoryUpdate struct {
	config
	hooks    []Hook
	mutation *ApiHistoryMutation
}

// Where appends a list predicates to the ApiHistoryUpdate builder.
func (ahu *ApiHistoryUpdate) Where(ps ...predicate.ApiHistory) *ApiHistoryUpdate {
	ahu.mutation.Where(ps...)
	return ahu
}

// SetVersion sets the "version" field.
func (ahu *ApiHistoryUpdate) SetVersion(i int32) *ApiHistoryUpdate {
	ahu.mutation.ResetVersion()
	ahu.mutation.SetVersion(i)
	return ahu
}

// AddVersion adds i to the "version" field.
func (ahu *ApiHistoryUpdate) AddVersion(i int32) *ApiHistoryUpdate {
	ahu.mutation.AddVersion(i)
	return ahu
}

// SetQueryParams sets the "query_params" field.
func (ahu *ApiHistoryUpdate) SetQueryParams(s string) *ApiHistoryUpdate {
	ahu.mutation.SetQueryParams(s)
	return ahu
}

// SetCreatedAt sets the "created_at" field.
func (ahu *ApiHistoryUpdate) SetCreatedAt(t time.Time) *ApiHistoryUpdate {
	ahu.mutation.SetCreatedAt(t)
	return ahu
}

// SetCreatedBy sets the "created_by" field.
func (ahu *ApiHistoryUpdate) SetCreatedBy(u uint32) *ApiHistoryUpdate {
	ahu.mutation.ResetCreatedBy()
	ahu.mutation.SetCreatedBy(u)
	return ahu
}

// AddCreatedBy adds u to the "created_by" field.
func (ahu *ApiHistoryUpdate) AddCreatedBy(u int32) *ApiHistoryUpdate {
	ahu.mutation.AddCreatedBy(u)
	return ahu
}

// SetDescription sets the "description" field.
func (ahu *ApiHistoryUpdate) SetDescription(s string) *ApiHistoryUpdate {
	ahu.mutation.SetDescription(s)
	return ahu
}

// Mutation returns the ApiHistoryMutation object of the builder.
func (ahu *ApiHistoryUpdate) Mutation() *ApiHistoryMutation {
	return ahu.mutation
}

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

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

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

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

func (ahu *ApiHistoryUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(apihistory.Table, apihistory.Columns, sqlgraph.NewFieldSpec(apihistory.FieldID, field.TypeInt32))
	if ps := ahu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := ahu.mutation.Version(); ok {
		_spec.SetField(apihistory.FieldVersion, field.TypeInt32, value)
	}
	if value, ok := ahu.mutation.AddedVersion(); ok {
		_spec.AddField(apihistory.FieldVersion, field.TypeInt32, value)
	}
	if value, ok := ahu.mutation.QueryParams(); ok {
		_spec.SetField(apihistory.FieldQueryParams, field.TypeString, value)
	}
	if value, ok := ahu.mutation.CreatedAt(); ok {
		_spec.SetField(apihistory.FieldCreatedAt, field.TypeTime, value)
	}
	if value, ok := ahu.mutation.CreatedBy(); ok {
		_spec.SetField(apihistory.FieldCreatedBy, field.TypeUint32, value)
	}
	if value, ok := ahu.mutation.AddedCreatedBy(); ok {
		_spec.AddField(apihistory.FieldCreatedBy, field.TypeUint32, value)
	}
	if value, ok := ahu.mutation.Description(); ok {
		_spec.SetField(apihistory.FieldDescription, field.TypeString, value)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, ahu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{apihistory.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	ahu.mutation.done = true
	return n, nil
}

// ApiHistoryUpdateOne is the builder for updating a single ApiHistory entity.
type ApiHistoryUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ApiHistoryMutation
}

// SetVersion sets the "version" field.
func (ahuo *ApiHistoryUpdateOne) SetVersion(i int32) *ApiHistoryUpdateOne {
	ahuo.mutation.ResetVersion()
	ahuo.mutation.SetVersion(i)
	return ahuo
}

// AddVersion adds i to the "version" field.
func (ahuo *ApiHistoryUpdateOne) AddVersion(i int32) *ApiHistoryUpdateOne {
	ahuo.mutation.AddVersion(i)
	return ahuo
}

// SetQueryParams sets the "query_params" field.
func (ahuo *ApiHistoryUpdateOne) SetQueryParams(s string) *ApiHistoryUpdateOne {
	ahuo.mutation.SetQueryParams(s)
	return ahuo
}

// SetCreatedAt sets the "created_at" field.
func (ahuo *ApiHistoryUpdateOne) SetCreatedAt(t time.Time) *ApiHistoryUpdateOne {
	ahuo.mutation.SetCreatedAt(t)
	return ahuo
}

// SetCreatedBy sets the "created_by" field.
func (ahuo *ApiHistoryUpdateOne) SetCreatedBy(u uint32) *ApiHistoryUpdateOne {
	ahuo.mutation.ResetCreatedBy()
	ahuo.mutation.SetCreatedBy(u)
	return ahuo
}

// AddCreatedBy adds u to the "created_by" field.
func (ahuo *ApiHistoryUpdateOne) AddCreatedBy(u int32) *ApiHistoryUpdateOne {
	ahuo.mutation.AddCreatedBy(u)
	return ahuo
}

// SetDescription sets the "description" field.
func (ahuo *ApiHistoryUpdateOne) SetDescription(s string) *ApiHistoryUpdateOne {
	ahuo.mutation.SetDescription(s)
	return ahuo
}

// Mutation returns the ApiHistoryMutation object of the builder.
func (ahuo *ApiHistoryUpdateOne) Mutation() *ApiHistoryMutation {
	return ahuo.mutation
}

// Where appends a list predicates to the ApiHistoryUpdate builder.
func (ahuo *ApiHistoryUpdateOne) Where(ps ...predicate.ApiHistory) *ApiHistoryUpdateOne {
	ahuo.mutation.Where(ps...)
	return ahuo
}

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

// Save executes the query and returns the updated ApiHistory entity.
func (ahuo *ApiHistoryUpdateOne) Save(ctx context.Context) (*ApiHistory, error) {
	return withHooks[*ApiHistory, ApiHistoryMutation](ctx, ahuo.sqlSave, ahuo.mutation, ahuo.hooks)
}

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

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

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

func (ahuo *ApiHistoryUpdateOne) sqlSave(ctx context.Context) (_node *ApiHistory, err error) {
	_spec := sqlgraph.NewUpdateSpec(apihistory.Table, apihistory.Columns, sqlgraph.NewFieldSpec(apihistory.FieldID, field.TypeInt32))
	id, ok := ahuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "ApiHistory.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := ahuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, apihistory.FieldID)
		for _, f := range fields {
			if !apihistory.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != apihistory.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := ahuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := ahuo.mutation.Version(); ok {
		_spec.SetField(apihistory.FieldVersion, field.TypeInt32, value)
	}
	if value, ok := ahuo.mutation.AddedVersion(); ok {
		_spec.AddField(apihistory.FieldVersion, field.TypeInt32, value)
	}
	if value, ok := ahuo.mutation.QueryParams(); ok {
		_spec.SetField(apihistory.FieldQueryParams, field.TypeString, value)
	}
	if value, ok := ahuo.mutation.CreatedAt(); ok {
		_spec.SetField(apihistory.FieldCreatedAt, field.TypeTime, value)
	}
	if value, ok := ahuo.mutation.CreatedBy(); ok {
		_spec.SetField(apihistory.FieldCreatedBy, field.TypeUint32, value)
	}
	if value, ok := ahuo.mutation.AddedCreatedBy(); ok {
		_spec.AddField(apihistory.FieldCreatedBy, field.TypeUint32, value)
	}
	if value, ok := ahuo.mutation.Description(); ok {
		_spec.SetField(apihistory.FieldDescription, field.TypeString, value)
	}
	_node = &ApiHistory{config: ahuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, ahuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{apihistory.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	ahuo.mutation.done = true
	return _node, nil
}
