// 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/linkcategory"
	"github.com/anzhiyu-c/anheyu-app/ent/predicate"
)

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

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

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

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

// SetDescription sets the "description" field.
func (_u *LinkCategoryUpdate) SetDescription(v string) *LinkCategoryUpdate {
	_u.mutation.SetDescription(v)
	return _u
}

// SetNillableDescription sets the "description" field if the given value is not nil.
func (_u *LinkCategoryUpdate) SetNillableDescription(v *string) *LinkCategoryUpdate {
	if v != nil {
		_u.SetDescription(*v)
	}
	return _u
}

// ClearDescription clears the value of the "description" field.
func (_u *LinkCategoryUpdate) ClearDescription() *LinkCategoryUpdate {
	_u.mutation.ClearDescription()
	return _u
}

// SetStyle sets the "style" field.
func (_u *LinkCategoryUpdate) SetStyle(v linkcategory.Style) *LinkCategoryUpdate {
	_u.mutation.SetStyle(v)
	return _u
}

// SetNillableStyle sets the "style" field if the given value is not nil.
func (_u *LinkCategoryUpdate) SetNillableStyle(v *linkcategory.Style) *LinkCategoryUpdate {
	if v != nil {
		_u.SetStyle(*v)
	}
	return _u
}

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

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

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

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

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

// RemoveLinks removes "links" edges to Link entities.
func (_u *LinkCategoryUpdate) RemoveLinks(v ...*Link) *LinkCategoryUpdate {
	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 *LinkCategoryUpdate) 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 *LinkCategoryUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *LinkCategoryUpdate) 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 *LinkCategoryUpdate) check() error {
	if v, ok := _u.mutation.Name(); ok {
		if err := linkcategory.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "LinkCategory.name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Style(); ok {
		if err := linkcategory.StyleValidator(v); err != nil {
			return &ValidationError{Name: "style", err: fmt.Errorf(`ent: validator failed for field "LinkCategory.style": %w`, err)}
		}
	}
	return nil
}

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

func (_u *LinkCategoryUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(linkcategory.Table, linkcategory.Columns, sqlgraph.NewFieldSpec(linkcategory.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(linkcategory.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Description(); ok {
		_spec.SetField(linkcategory.FieldDescription, field.TypeString, value)
	}
	if _u.mutation.DescriptionCleared() {
		_spec.ClearField(linkcategory.FieldDescription, field.TypeString)
	}
	if value, ok := _u.mutation.Style(); ok {
		_spec.SetField(linkcategory.FieldStyle, field.TypeEnum, value)
	}
	if _u.mutation.LinksCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   linkcategory.LinksTable,
			Columns: []string{linkcategory.LinksColumn},
			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.O2M,
			Inverse: false,
			Table:   linkcategory.LinksTable,
			Columns: []string{linkcategory.LinksColumn},
			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.O2M,
			Inverse: false,
			Table:   linkcategory.LinksTable,
			Columns: []string{linkcategory.LinksColumn},
			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{linkcategory.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

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

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

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

// SetDescription sets the "description" field.
func (_u *LinkCategoryUpdateOne) SetDescription(v string) *LinkCategoryUpdateOne {
	_u.mutation.SetDescription(v)
	return _u
}

// SetNillableDescription sets the "description" field if the given value is not nil.
func (_u *LinkCategoryUpdateOne) SetNillableDescription(v *string) *LinkCategoryUpdateOne {
	if v != nil {
		_u.SetDescription(*v)
	}
	return _u
}

// ClearDescription clears the value of the "description" field.
func (_u *LinkCategoryUpdateOne) ClearDescription() *LinkCategoryUpdateOne {
	_u.mutation.ClearDescription()
	return _u
}

// SetStyle sets the "style" field.
func (_u *LinkCategoryUpdateOne) SetStyle(v linkcategory.Style) *LinkCategoryUpdateOne {
	_u.mutation.SetStyle(v)
	return _u
}

// SetNillableStyle sets the "style" field if the given value is not nil.
func (_u *LinkCategoryUpdateOne) SetNillableStyle(v *linkcategory.Style) *LinkCategoryUpdateOne {
	if v != nil {
		_u.SetStyle(*v)
	}
	return _u
}

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

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

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

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

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

// RemoveLinks removes "links" edges to Link entities.
func (_u *LinkCategoryUpdateOne) RemoveLinks(v ...*Link) *LinkCategoryUpdateOne {
	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 LinkCategoryUpdate builder.
func (_u *LinkCategoryUpdateOne) Where(ps ...predicate.LinkCategory) *LinkCategoryUpdateOne {
	_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 *LinkCategoryUpdateOne) Select(field string, fields ...string) *LinkCategoryUpdateOne {
	_u.fields = append([]string{field}, fields...)
	return _u
}

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *LinkCategoryUpdateOne) 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 *LinkCategoryUpdateOne) check() error {
	if v, ok := _u.mutation.Name(); ok {
		if err := linkcategory.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "LinkCategory.name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Style(); ok {
		if err := linkcategory.StyleValidator(v); err != nil {
			return &ValidationError{Name: "style", err: fmt.Errorf(`ent: validator failed for field "LinkCategory.style": %w`, err)}
		}
	}
	return nil
}

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

func (_u *LinkCategoryUpdateOne) sqlSave(ctx context.Context) (_node *LinkCategory, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(linkcategory.Table, linkcategory.Columns, sqlgraph.NewFieldSpec(linkcategory.FieldID, field.TypeInt))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "LinkCategory.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, linkcategory.FieldID)
		for _, f := range fields {
			if !linkcategory.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != linkcategory.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(linkcategory.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.Description(); ok {
		_spec.SetField(linkcategory.FieldDescription, field.TypeString, value)
	}
	if _u.mutation.DescriptionCleared() {
		_spec.ClearField(linkcategory.FieldDescription, field.TypeString)
	}
	if value, ok := _u.mutation.Style(); ok {
		_spec.SetField(linkcategory.FieldStyle, field.TypeEnum, value)
	}
	if _u.mutation.LinksCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   linkcategory.LinksTable,
			Columns: []string{linkcategory.LinksColumn},
			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.O2M,
			Inverse: false,
			Table:   linkcategory.LinksTable,
			Columns: []string{linkcategory.LinksColumn},
			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.O2M,
			Inverse: false,
			Table:   linkcategory.LinksTable,
			Columns: []string{linkcategory.LinksColumn},
			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 = &LinkCategory{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{linkcategory.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
