// Code generated by ent, DO NOT EDIT.

package ent

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

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

// ApiTagUpdate is the builder for updating ApiTag entities.
type ApiTagUpdate struct {
	config
	hooks    []Hook
	mutation *ApiTagMutation
}

// Where appends a list predicates to the ApiTagUpdate builder.
func (atu *ApiTagUpdate) Where(ps ...predicate.ApiTag) *ApiTagUpdate {
	atu.mutation.Where(ps...)
	return atu
}

// SetName sets the "name" field.
func (atu *ApiTagUpdate) SetName(s string) *ApiTagUpdate {
	atu.mutation.SetName(s)
	return atu
}

// SetSort sets the "sort" field.
func (atu *ApiTagUpdate) SetSort(i int32) *ApiTagUpdate {
	atu.mutation.ResetSort()
	atu.mutation.SetSort(i)
	return atu
}

// AddSort adds i to the "sort" field.
func (atu *ApiTagUpdate) AddSort(i int32) *ApiTagUpdate {
	atu.mutation.AddSort(i)
	return atu
}

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

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

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

// SetUpdateAt sets the "update_at" field.
func (atu *ApiTagUpdate) SetUpdateAt(t time.Time) *ApiTagUpdate {
	atu.mutation.SetUpdateAt(t)
	return atu
}

// SetUpdateBy sets the "update_by" field.
func (atu *ApiTagUpdate) SetUpdateBy(u uint32) *ApiTagUpdate {
	atu.mutation.ResetUpdateBy()
	atu.mutation.SetUpdateBy(u)
	return atu
}

// AddUpdateBy adds u to the "update_by" field.
func (atu *ApiTagUpdate) AddUpdateBy(u int32) *ApiTagUpdate {
	atu.mutation.AddUpdateBy(u)
	return atu
}

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

// Mutation returns the ApiTagMutation object of the builder.
func (atu *ApiTagUpdate) Mutation() *ApiTagMutation {
	return atu.mutation
}

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

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

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

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

func (atu *ApiTagUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(apitag.Table, apitag.Columns, sqlgraph.NewFieldSpec(apitag.FieldID, field.TypeInt32))
	if ps := atu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := atu.mutation.Name(); ok {
		_spec.SetField(apitag.FieldName, field.TypeString, value)
	}
	if value, ok := atu.mutation.Sort(); ok {
		_spec.SetField(apitag.FieldSort, field.TypeInt32, value)
	}
	if value, ok := atu.mutation.AddedSort(); ok {
		_spec.AddField(apitag.FieldSort, field.TypeInt32, value)
	}
	if value, ok := atu.mutation.CreatedAt(); ok {
		_spec.SetField(apitag.FieldCreatedAt, field.TypeTime, value)
	}
	if value, ok := atu.mutation.CreatedBy(); ok {
		_spec.SetField(apitag.FieldCreatedBy, field.TypeUint32, value)
	}
	if value, ok := atu.mutation.AddedCreatedBy(); ok {
		_spec.AddField(apitag.FieldCreatedBy, field.TypeUint32, value)
	}
	if value, ok := atu.mutation.UpdateAt(); ok {
		_spec.SetField(apitag.FieldUpdateAt, field.TypeTime, value)
	}
	if value, ok := atu.mutation.UpdateBy(); ok {
		_spec.SetField(apitag.FieldUpdateBy, field.TypeUint32, value)
	}
	if value, ok := atu.mutation.AddedUpdateBy(); ok {
		_spec.AddField(apitag.FieldUpdateBy, field.TypeUint32, value)
	}
	if value, ok := atu.mutation.Description(); ok {
		_spec.SetField(apitag.FieldDescription, field.TypeString, value)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, atu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{apitag.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	atu.mutation.done = true
	return n, nil
}

// ApiTagUpdateOne is the builder for updating a single ApiTag entity.
type ApiTagUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ApiTagMutation
}

// SetName sets the "name" field.
func (atuo *ApiTagUpdateOne) SetName(s string) *ApiTagUpdateOne {
	atuo.mutation.SetName(s)
	return atuo
}

// SetSort sets the "sort" field.
func (atuo *ApiTagUpdateOne) SetSort(i int32) *ApiTagUpdateOne {
	atuo.mutation.ResetSort()
	atuo.mutation.SetSort(i)
	return atuo
}

// AddSort adds i to the "sort" field.
func (atuo *ApiTagUpdateOne) AddSort(i int32) *ApiTagUpdateOne {
	atuo.mutation.AddSort(i)
	return atuo
}

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

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

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

// SetUpdateAt sets the "update_at" field.
func (atuo *ApiTagUpdateOne) SetUpdateAt(t time.Time) *ApiTagUpdateOne {
	atuo.mutation.SetUpdateAt(t)
	return atuo
}

// SetUpdateBy sets the "update_by" field.
func (atuo *ApiTagUpdateOne) SetUpdateBy(u uint32) *ApiTagUpdateOne {
	atuo.mutation.ResetUpdateBy()
	atuo.mutation.SetUpdateBy(u)
	return atuo
}

// AddUpdateBy adds u to the "update_by" field.
func (atuo *ApiTagUpdateOne) AddUpdateBy(u int32) *ApiTagUpdateOne {
	atuo.mutation.AddUpdateBy(u)
	return atuo
}

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

// Mutation returns the ApiTagMutation object of the builder.
func (atuo *ApiTagUpdateOne) Mutation() *ApiTagMutation {
	return atuo.mutation
}

// Where appends a list predicates to the ApiTagUpdate builder.
func (atuo *ApiTagUpdateOne) Where(ps ...predicate.ApiTag) *ApiTagUpdateOne {
	atuo.mutation.Where(ps...)
	return atuo
}

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

// Save executes the query and returns the updated ApiTag entity.
func (atuo *ApiTagUpdateOne) Save(ctx context.Context) (*ApiTag, error) {
	return withHooks[*ApiTag, ApiTagMutation](ctx, atuo.sqlSave, atuo.mutation, atuo.hooks)
}

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

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

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

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