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

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

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

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

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

// SetURL sets the "url" field.
func (_u *LinkUpdate) SetURL(v string) *LinkUpdate {
	_u.mutation.SetURL(v)
	return _u
}

// SetNillableURL sets the "url" field if the given value is not nil.
func (_u *LinkUpdate) SetNillableURL(v *string) *LinkUpdate {
	if v != nil {
		_u.SetURL(*v)
	}
	return _u
}

// SetLogo sets the "logo" field.
func (_u *LinkUpdate) SetLogo(v string) *LinkUpdate {
	_u.mutation.SetLogo(v)
	return _u
}

// SetNillableLogo sets the "logo" field if the given value is not nil.
func (_u *LinkUpdate) SetNillableLogo(v *string) *LinkUpdate {
	if v != nil {
		_u.SetLogo(*v)
	}
	return _u
}

// ClearLogo clears the value of the "logo" field.
func (_u *LinkUpdate) ClearLogo() *LinkUpdate {
	_u.mutation.ClearLogo()
	return _u
}

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

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

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

// SetStatus sets the "status" field.
func (_u *LinkUpdate) SetStatus(v link.Status) *LinkUpdate {
	_u.mutation.SetStatus(v)
	return _u
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_u *LinkUpdate) SetNillableStatus(v *link.Status) *LinkUpdate {
	if v != nil {
		_u.SetStatus(*v)
	}
	return _u
}

// SetSiteshot sets the "siteshot" field.
func (_u *LinkUpdate) SetSiteshot(v string) *LinkUpdate {
	_u.mutation.SetSiteshot(v)
	return _u
}

// SetNillableSiteshot sets the "siteshot" field if the given value is not nil.
func (_u *LinkUpdate) SetNillableSiteshot(v *string) *LinkUpdate {
	if v != nil {
		_u.SetSiteshot(*v)
	}
	return _u
}

// ClearSiteshot clears the value of the "siteshot" field.
func (_u *LinkUpdate) ClearSiteshot() *LinkUpdate {
	_u.mutation.ClearSiteshot()
	return _u
}

// SetSortOrder sets the "sort_order" field.
func (_u *LinkUpdate) SetSortOrder(v int) *LinkUpdate {
	_u.mutation.ResetSortOrder()
	_u.mutation.SetSortOrder(v)
	return _u
}

// SetNillableSortOrder sets the "sort_order" field if the given value is not nil.
func (_u *LinkUpdate) SetNillableSortOrder(v *int) *LinkUpdate {
	if v != nil {
		_u.SetSortOrder(*v)
	}
	return _u
}

// AddSortOrder adds value to the "sort_order" field.
func (_u *LinkUpdate) AddSortOrder(v int) *LinkUpdate {
	_u.mutation.AddSortOrder(v)
	return _u
}

// SetSkipHealthCheck sets the "skip_health_check" field.
func (_u *LinkUpdate) SetSkipHealthCheck(v bool) *LinkUpdate {
	_u.mutation.SetSkipHealthCheck(v)
	return _u
}

// SetNillableSkipHealthCheck sets the "skip_health_check" field if the given value is not nil.
func (_u *LinkUpdate) SetNillableSkipHealthCheck(v *bool) *LinkUpdate {
	if v != nil {
		_u.SetSkipHealthCheck(*v)
	}
	return _u
}

// SetCategoryID sets the "category" edge to the LinkCategory entity by ID.
func (_u *LinkUpdate) SetCategoryID(id int) *LinkUpdate {
	_u.mutation.SetCategoryID(id)
	return _u
}

// SetCategory sets the "category" edge to the LinkCategory entity.
func (_u *LinkUpdate) SetCategory(v *LinkCategory) *LinkUpdate {
	return _u.SetCategoryID(v.ID)
}

// AddTagIDs adds the "tags" edge to the LinkTag entity by IDs.
func (_u *LinkUpdate) AddTagIDs(ids ...int) *LinkUpdate {
	_u.mutation.AddTagIDs(ids...)
	return _u
}

// AddTags adds the "tags" edges to the LinkTag entity.
func (_u *LinkUpdate) AddTags(v ...*LinkTag) *LinkUpdate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddTagIDs(ids...)
}

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

// ClearCategory clears the "category" edge to the LinkCategory entity.
func (_u *LinkUpdate) ClearCategory() *LinkUpdate {
	_u.mutation.ClearCategory()
	return _u
}

// ClearTags clears all "tags" edges to the LinkTag entity.
func (_u *LinkUpdate) ClearTags() *LinkUpdate {
	_u.mutation.ClearTags()
	return _u
}

// RemoveTagIDs removes the "tags" edge to LinkTag entities by IDs.
func (_u *LinkUpdate) RemoveTagIDs(ids ...int) *LinkUpdate {
	_u.mutation.RemoveTagIDs(ids...)
	return _u
}

// RemoveTags removes "tags" edges to LinkTag entities.
func (_u *LinkUpdate) RemoveTags(v ...*LinkTag) *LinkUpdate {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveTagIDs(ids...)
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *LinkUpdate) 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 *LinkUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *LinkUpdate) 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 *LinkUpdate) check() error {
	if v, ok := _u.mutation.Name(); ok {
		if err := link.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Link.name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.URL(); ok {
		if err := link.URLValidator(v); err != nil {
			return &ValidationError{Name: "url", err: fmt.Errorf(`ent: validator failed for field "Link.url": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Status(); ok {
		if err := link.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Link.status": %w`, err)}
		}
	}
	if _u.mutation.CategoryCleared() && len(_u.mutation.CategoryIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "Link.category"`)
	}
	return nil
}

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

func (_u *LinkUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(link.Table, link.Columns, sqlgraph.NewFieldSpec(link.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(link.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.URL(); ok {
		_spec.SetField(link.FieldURL, field.TypeString, value)
	}
	if value, ok := _u.mutation.Logo(); ok {
		_spec.SetField(link.FieldLogo, field.TypeString, value)
	}
	if _u.mutation.LogoCleared() {
		_spec.ClearField(link.FieldLogo, field.TypeString)
	}
	if value, ok := _u.mutation.Description(); ok {
		_spec.SetField(link.FieldDescription, field.TypeString, value)
	}
	if _u.mutation.DescriptionCleared() {
		_spec.ClearField(link.FieldDescription, field.TypeString)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(link.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.Siteshot(); ok {
		_spec.SetField(link.FieldSiteshot, field.TypeString, value)
	}
	if _u.mutation.SiteshotCleared() {
		_spec.ClearField(link.FieldSiteshot, field.TypeString)
	}
	if value, ok := _u.mutation.SortOrder(); ok {
		_spec.SetField(link.FieldSortOrder, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedSortOrder(); ok {
		_spec.AddField(link.FieldSortOrder, field.TypeInt, value)
	}
	if value, ok := _u.mutation.SkipHealthCheck(); ok {
		_spec.SetField(link.FieldSkipHealthCheck, field.TypeBool, value)
	}
	if _u.mutation.CategoryCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   link.CategoryTable,
			Columns: []string{link.CategoryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(linkcategory.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.CategoryIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   link.CategoryTable,
			Columns: []string{link.CategoryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(linkcategory.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.TagsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: false,
			Table:   link.TagsTable,
			Columns: link.TagsPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(linktag.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedTagsIDs(); len(nodes) > 0 && !_u.mutation.TagsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: false,
			Table:   link.TagsTable,
			Columns: link.TagsPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(linktag.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.TagsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: false,
			Table:   link.TagsTable,
			Columns: link.TagsPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(linktag.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{link.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

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

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

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

// SetURL sets the "url" field.
func (_u *LinkUpdateOne) SetURL(v string) *LinkUpdateOne {
	_u.mutation.SetURL(v)
	return _u
}

// SetNillableURL sets the "url" field if the given value is not nil.
func (_u *LinkUpdateOne) SetNillableURL(v *string) *LinkUpdateOne {
	if v != nil {
		_u.SetURL(*v)
	}
	return _u
}

// SetLogo sets the "logo" field.
func (_u *LinkUpdateOne) SetLogo(v string) *LinkUpdateOne {
	_u.mutation.SetLogo(v)
	return _u
}

// SetNillableLogo sets the "logo" field if the given value is not nil.
func (_u *LinkUpdateOne) SetNillableLogo(v *string) *LinkUpdateOne {
	if v != nil {
		_u.SetLogo(*v)
	}
	return _u
}

// ClearLogo clears the value of the "logo" field.
func (_u *LinkUpdateOne) ClearLogo() *LinkUpdateOne {
	_u.mutation.ClearLogo()
	return _u
}

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

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

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

// SetStatus sets the "status" field.
func (_u *LinkUpdateOne) SetStatus(v link.Status) *LinkUpdateOne {
	_u.mutation.SetStatus(v)
	return _u
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_u *LinkUpdateOne) SetNillableStatus(v *link.Status) *LinkUpdateOne {
	if v != nil {
		_u.SetStatus(*v)
	}
	return _u
}

// SetSiteshot sets the "siteshot" field.
func (_u *LinkUpdateOne) SetSiteshot(v string) *LinkUpdateOne {
	_u.mutation.SetSiteshot(v)
	return _u
}

// SetNillableSiteshot sets the "siteshot" field if the given value is not nil.
func (_u *LinkUpdateOne) SetNillableSiteshot(v *string) *LinkUpdateOne {
	if v != nil {
		_u.SetSiteshot(*v)
	}
	return _u
}

// ClearSiteshot clears the value of the "siteshot" field.
func (_u *LinkUpdateOne) ClearSiteshot() *LinkUpdateOne {
	_u.mutation.ClearSiteshot()
	return _u
}

// SetSortOrder sets the "sort_order" field.
func (_u *LinkUpdateOne) SetSortOrder(v int) *LinkUpdateOne {
	_u.mutation.ResetSortOrder()
	_u.mutation.SetSortOrder(v)
	return _u
}

// SetNillableSortOrder sets the "sort_order" field if the given value is not nil.
func (_u *LinkUpdateOne) SetNillableSortOrder(v *int) *LinkUpdateOne {
	if v != nil {
		_u.SetSortOrder(*v)
	}
	return _u
}

// AddSortOrder adds value to the "sort_order" field.
func (_u *LinkUpdateOne) AddSortOrder(v int) *LinkUpdateOne {
	_u.mutation.AddSortOrder(v)
	return _u
}

// SetSkipHealthCheck sets the "skip_health_check" field.
func (_u *LinkUpdateOne) SetSkipHealthCheck(v bool) *LinkUpdateOne {
	_u.mutation.SetSkipHealthCheck(v)
	return _u
}

// SetNillableSkipHealthCheck sets the "skip_health_check" field if the given value is not nil.
func (_u *LinkUpdateOne) SetNillableSkipHealthCheck(v *bool) *LinkUpdateOne {
	if v != nil {
		_u.SetSkipHealthCheck(*v)
	}
	return _u
}

// SetCategoryID sets the "category" edge to the LinkCategory entity by ID.
func (_u *LinkUpdateOne) SetCategoryID(id int) *LinkUpdateOne {
	_u.mutation.SetCategoryID(id)
	return _u
}

// SetCategory sets the "category" edge to the LinkCategory entity.
func (_u *LinkUpdateOne) SetCategory(v *LinkCategory) *LinkUpdateOne {
	return _u.SetCategoryID(v.ID)
}

// AddTagIDs adds the "tags" edge to the LinkTag entity by IDs.
func (_u *LinkUpdateOne) AddTagIDs(ids ...int) *LinkUpdateOne {
	_u.mutation.AddTagIDs(ids...)
	return _u
}

// AddTags adds the "tags" edges to the LinkTag entity.
func (_u *LinkUpdateOne) AddTags(v ...*LinkTag) *LinkUpdateOne {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.AddTagIDs(ids...)
}

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

// ClearCategory clears the "category" edge to the LinkCategory entity.
func (_u *LinkUpdateOne) ClearCategory() *LinkUpdateOne {
	_u.mutation.ClearCategory()
	return _u
}

// ClearTags clears all "tags" edges to the LinkTag entity.
func (_u *LinkUpdateOne) ClearTags() *LinkUpdateOne {
	_u.mutation.ClearTags()
	return _u
}

// RemoveTagIDs removes the "tags" edge to LinkTag entities by IDs.
func (_u *LinkUpdateOne) RemoveTagIDs(ids ...int) *LinkUpdateOne {
	_u.mutation.RemoveTagIDs(ids...)
	return _u
}

// RemoveTags removes "tags" edges to LinkTag entities.
func (_u *LinkUpdateOne) RemoveTags(v ...*LinkTag) *LinkUpdateOne {
	ids := make([]int, len(v))
	for i := range v {
		ids[i] = v[i].ID
	}
	return _u.RemoveTagIDs(ids...)
}

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *LinkUpdateOne) 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 *LinkUpdateOne) check() error {
	if v, ok := _u.mutation.Name(); ok {
		if err := link.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Link.name": %w`, err)}
		}
	}
	if v, ok := _u.mutation.URL(); ok {
		if err := link.URLValidator(v); err != nil {
			return &ValidationError{Name: "url", err: fmt.Errorf(`ent: validator failed for field "Link.url": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Status(); ok {
		if err := link.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Link.status": %w`, err)}
		}
	}
	if _u.mutation.CategoryCleared() && len(_u.mutation.CategoryIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "Link.category"`)
	}
	return nil
}

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

func (_u *LinkUpdateOne) sqlSave(ctx context.Context) (_node *Link, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(link.Table, link.Columns, sqlgraph.NewFieldSpec(link.FieldID, field.TypeInt))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Link.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, link.FieldID)
		for _, f := range fields {
			if !link.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != link.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(link.FieldName, field.TypeString, value)
	}
	if value, ok := _u.mutation.URL(); ok {
		_spec.SetField(link.FieldURL, field.TypeString, value)
	}
	if value, ok := _u.mutation.Logo(); ok {
		_spec.SetField(link.FieldLogo, field.TypeString, value)
	}
	if _u.mutation.LogoCleared() {
		_spec.ClearField(link.FieldLogo, field.TypeString)
	}
	if value, ok := _u.mutation.Description(); ok {
		_spec.SetField(link.FieldDescription, field.TypeString, value)
	}
	if _u.mutation.DescriptionCleared() {
		_spec.ClearField(link.FieldDescription, field.TypeString)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(link.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.Siteshot(); ok {
		_spec.SetField(link.FieldSiteshot, field.TypeString, value)
	}
	if _u.mutation.SiteshotCleared() {
		_spec.ClearField(link.FieldSiteshot, field.TypeString)
	}
	if value, ok := _u.mutation.SortOrder(); ok {
		_spec.SetField(link.FieldSortOrder, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedSortOrder(); ok {
		_spec.AddField(link.FieldSortOrder, field.TypeInt, value)
	}
	if value, ok := _u.mutation.SkipHealthCheck(); ok {
		_spec.SetField(link.FieldSkipHealthCheck, field.TypeBool, value)
	}
	if _u.mutation.CategoryCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   link.CategoryTable,
			Columns: []string{link.CategoryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(linkcategory.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.CategoryIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   link.CategoryTable,
			Columns: []string{link.CategoryColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(linkcategory.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.TagsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: false,
			Table:   link.TagsTable,
			Columns: link.TagsPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(linktag.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.RemovedTagsIDs(); len(nodes) > 0 && !_u.mutation.TagsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: false,
			Table:   link.TagsTable,
			Columns: link.TagsPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(linktag.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.TagsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2M,
			Inverse: false,
			Table:   link.TagsTable,
			Columns: link.TagsPrimaryKey,
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(linktag.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 = &Link{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{link.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
