// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/anzhiyu-c/anheyu-app/ent/link"
	"github.com/anzhiyu-c/anheyu-app/ent/linktag"
	"github.com/anzhiyu-c/anheyu-app/ent/predicate"
)

// LinkTagUpdate is the builder for updating LinkTag entities.
type LinkTagUpdate struct {
	config
	hooks     []Hook
	mutation  *LinkTagMutation
	modifiers []func(*sql.UpdateBuilder)
}

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

// SetName sets the "name" field.
func (_u *LinkTagUpdate) SetName(v string) *LinkTagUpdate {
	_u.mutation.SetName(v)
	return _u
}

// SetNillableName sets the "name" field if the given value is not nil.
func (_u *LinkTagUpdate) SetNillableName(v *string) *LinkTagUpdate {
	if v != nil {
		_u.SetName(*v)
	}
	return _u
}

// SetColor sets the "color" field.
func (_u *LinkTagUpdate) SetColor(v string) *LinkTagUpdate {
	_u.mutation.SetColor(v)
	return _u
}

// SetNillableColor sets the "color" field if the given value is not nil.
func (_u *LinkTagUpdate) SetNillableColor(v *string) *LinkTagUpdate {
	if v != nil {
		_u.SetColor(*v)
	}
	return _u
}

// AddLinkIDs adds the "links" edge to the Link entity by IDs.
func (_u *LinkTagUpdate) AddLinkIDs(ids ...int) *LinkTagUpdate {
	_u.mutation.AddLinkIDs(ids...)
	return _u
}

// AddLinks adds the "links" edges to the Link entity.
func (_u *LinkTagUpdate) AddLinks(v ...*Link) *LinkTagUpdate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddLinkIDs(ids...)
}

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

// ClearLinks clears all "links" edges to the Link entity.
func (_u *LinkTagUpdate) ClearLinks() *LinkTagUpdate {
	_u.mutation.ClearLinks()
	return _u
}

// RemoveLinkIDs removes the "links" edge to Link entities by IDs.
func (_u *LinkTagUpdate) RemoveLinkIDs(ids ...int) *LinkTagUpdate {
	_u.mutation.RemoveLinkIDs(ids...)
	return _u
}

// RemoveLinks removes "links" edges to Link entities.
func (_u *LinkTagUpdate) RemoveLinks(v ...*Link) *LinkTagUpdate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveLinkIDs(ids...)
}

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

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

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

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

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

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *LinkTagUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *LinkTagUpdate {
	_u.modifiers = append(_u.modifiers, modifiers...)
	return _u
}

func (_u *LinkTagUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(linktag.Table, linktag.Columns, sqlgraph.NewFieldSpec(linktag.FieldID, field.TypeInt))
	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.Name(); ok {
		_spec.SetField(linktag.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Color(); ok {
		_spec.SetField(linktag.FieldColor, field.TypeString, value)
	}
	if _u.mutation.LinksCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   linktag.LinksTable,
			Columns: linktag.LinksPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(link.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedLinksIDs(); len(nodes) > 0 && !_u.mutation.LinksCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   linktag.LinksTable,
			Columns: linktag.LinksPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(link.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.LinksIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   linktag.LinksTable,
			Columns: linktag.LinksPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(link.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_spec.AddModifiers(_u.modifiers...)
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{linktag.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// LinkTagUpdateOne is the builder for updating a single LinkTag entity.
type LinkTagUpdateOne struct {
	config
	fields    []string
	hooks     []Hook
	mutation  *LinkTagMutation
	modifiers []func(*sql.UpdateBuilder)
}

// SetName sets the "name" field.
func (_u *LinkTagUpdateOne) SetName(v string) *LinkTagUpdateOne {
	_u.mutation.SetName(v)
	return _u
}

// SetNillableName sets the "name" field if the given value is not nil.
func (_u *LinkTagUpdateOne) SetNillableName(v *string) *LinkTagUpdateOne {
	if v != nil {
		_u.SetName(*v)
	}
	return _u
}

// SetColor sets the "color" field.
func (_u *LinkTagUpdateOne) SetColor(v string) *LinkTagUpdateOne {
	_u.mutation.SetColor(v)
	return _u
}

// SetNillableColor sets the "color" field if the given value is not nil.
func (_u *LinkTagUpdateOne) SetNillableColor(v *string) *LinkTagUpdateOne {
	if v != nil {
		_u.SetColor(*v)
	}
	return _u
}

// AddLinkIDs adds the "links" edge to the Link entity by IDs.
func (_u *LinkTagUpdateOne) AddLinkIDs(ids ...int) *LinkTagUpdateOne {
	_u.mutation.AddLinkIDs(ids...)
	return _u
}

// AddLinks adds the "links" edges to the Link entity.
func (_u *LinkTagUpdateOne) AddLinks(v ...*Link) *LinkTagUpdateOne {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddLinkIDs(ids...)
}

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

// ClearLinks clears all "links" edges to the Link entity.
func (_u *LinkTagUpdateOne) ClearLinks() *LinkTagUpdateOne {
	_u.mutation.ClearLinks()
	return _u
}

// RemoveLinkIDs removes the "links" edge to Link entities by IDs.
func (_u *LinkTagUpdateOne) RemoveLinkIDs(ids ...int) *LinkTagUpdateOne {
	_u.mutation.RemoveLinkIDs(ids...)
	return _u
}

// RemoveLinks removes "links" edges to Link entities.
func (_u *LinkTagUpdateOne) RemoveLinks(v ...*Link) *LinkTagUpdateOne {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveLinkIDs(ids...)
}

// Where appends a list predicates to the LinkTagUpdate builder.
func (_u *LinkTagUpdateOne) Where(ps ...predicate.LinkTag) *LinkTagUpdateOne {
	_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 *LinkTagUpdateOne) Select(field string, fields ...string) *LinkTagUpdateOne {
	_u.fields = append([]string{field}, fields...)
	return _u
}

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

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

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

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

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

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *LinkTagUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *LinkTagUpdateOne {
	_u.modifiers = append(_u.modifiers, modifiers...)
	return _u
}

func (_u *LinkTagUpdateOne) sqlSave(ctx context.Context) (_node *LinkTag, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(linktag.Table, linktag.Columns, sqlgraph.NewFieldSpec(linktag.FieldID, field.TypeInt))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "LinkTag.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, linktag.FieldID)
		for _, f := range fields {
			if !linktag.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != linktag.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.Name(); ok {
		_spec.SetField(linktag.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Color(); ok {
		_spec.SetField(linktag.FieldColor, field.TypeString, value)
	}
	if _u.mutation.LinksCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   linktag.LinksTable,
			Columns: linktag.LinksPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(link.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedLinksIDs(); len(nodes) > 0 && !_u.mutation.LinksCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   linktag.LinksTable,
			Columns: linktag.LinksPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(link.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.LinksIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: true,
			Table:   linktag.LinksTable,
			Columns: linktag.LinksPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(link.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_spec.AddModifiers(_u.modifiers...)
	_node = &LinkTag{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{linktag.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
