// Code generated by ent, DO NOT EDIT.

package models

import (
	"context"
	"errors"
	"fmt"
	"juwan-backend/app/search/rpc/internal/models/favorites"
	"juwan-backend/app/search/rpc/internal/models/predicate"

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

// FavoritesUpdate is the builder for updating Favorites entities.
type FavoritesUpdate struct {
	config
	hooks    []Hook
	mutation *FavoritesMutation
}

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

// SetUserID sets the "user_id" field.
func (_u *FavoritesUpdate) SetUserID(v int64) *FavoritesUpdate {
	_u.mutation.ResetUserID()
	_u.mutation.SetUserID(v)
	return _u
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (_u *FavoritesUpdate) SetNillableUserID(v *int64) *FavoritesUpdate {
	if v != nil {
		_u.SetUserID(*v)
	}
	return _u
}

// AddUserID adds value to the "user_id" field.
func (_u *FavoritesUpdate) AddUserID(v int64) *FavoritesUpdate {
	_u.mutation.AddUserID(v)
	return _u
}

// SetTargetType sets the "target_type" field.
func (_u *FavoritesUpdate) SetTargetType(v string) *FavoritesUpdate {
	_u.mutation.SetTargetType(v)
	return _u
}

// SetNillableTargetType sets the "target_type" field if the given value is not nil.
func (_u *FavoritesUpdate) SetNillableTargetType(v *string) *FavoritesUpdate {
	if v != nil {
		_u.SetTargetType(*v)
	}
	return _u
}

// SetTargetID sets the "target_id" field.
func (_u *FavoritesUpdate) SetTargetID(v int64) *FavoritesUpdate {
	_u.mutation.ResetTargetID()
	_u.mutation.SetTargetID(v)
	return _u
}

// SetNillableTargetID sets the "target_id" field if the given value is not nil.
func (_u *FavoritesUpdate) SetNillableTargetID(v *int64) *FavoritesUpdate {
	if v != nil {
		_u.SetTargetID(*v)
	}
	return _u
}

// AddTargetID adds value to the "target_id" field.
func (_u *FavoritesUpdate) AddTargetID(v int64) *FavoritesUpdate {
	_u.mutation.AddTargetID(v)
	return _u
}

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *FavoritesUpdate) 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 *FavoritesUpdate) check() error {
	if v, ok := _u.mutation.TargetType(); ok {
		if err := favorites.TargetTypeValidator(v); err != nil {
			return &ValidationError{Name: "target_type", err: fmt.Errorf(`models: validator failed for field "Favorites.target_type": %w`, err)}
		}
	}
	return nil
}

func (_u *FavoritesUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(favorites.Table, favorites.Columns, sqlgraph.NewFieldSpec(favorites.FieldID, field.TypeInt64))
	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.UserID(); ok {
		_spec.SetField(favorites.FieldUserID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedUserID(); ok {
		_spec.AddField(favorites.FieldUserID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.TargetType(); ok {
		_spec.SetField(favorites.FieldTargetType, field.TypeString, value)
	}
	if value, ok := _u.mutation.TargetID(); ok {
		_spec.SetField(favorites.FieldTargetID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedTargetID(); ok {
		_spec.AddField(favorites.FieldTargetID, field.TypeInt64, value)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{favorites.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// FavoritesUpdateOne is the builder for updating a single Favorites entity.
type FavoritesUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *FavoritesMutation
}

// SetUserID sets the "user_id" field.
func (_u *FavoritesUpdateOne) SetUserID(v int64) *FavoritesUpdateOne {
	_u.mutation.ResetUserID()
	_u.mutation.SetUserID(v)
	return _u
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (_u *FavoritesUpdateOne) SetNillableUserID(v *int64) *FavoritesUpdateOne {
	if v != nil {
		_u.SetUserID(*v)
	}
	return _u
}

// AddUserID adds value to the "user_id" field.
func (_u *FavoritesUpdateOne) AddUserID(v int64) *FavoritesUpdateOne {
	_u.mutation.AddUserID(v)
	return _u
}

// SetTargetType sets the "target_type" field.
func (_u *FavoritesUpdateOne) SetTargetType(v string) *FavoritesUpdateOne {
	_u.mutation.SetTargetType(v)
	return _u
}

// SetNillableTargetType sets the "target_type" field if the given value is not nil.
func (_u *FavoritesUpdateOne) SetNillableTargetType(v *string) *FavoritesUpdateOne {
	if v != nil {
		_u.SetTargetType(*v)
	}
	return _u
}

// SetTargetID sets the "target_id" field.
func (_u *FavoritesUpdateOne) SetTargetID(v int64) *FavoritesUpdateOne {
	_u.mutation.ResetTargetID()
	_u.mutation.SetTargetID(v)
	return _u
}

// SetNillableTargetID sets the "target_id" field if the given value is not nil.
func (_u *FavoritesUpdateOne) SetNillableTargetID(v *int64) *FavoritesUpdateOne {
	if v != nil {
		_u.SetTargetID(*v)
	}
	return _u
}

// AddTargetID adds value to the "target_id" field.
func (_u *FavoritesUpdateOne) AddTargetID(v int64) *FavoritesUpdateOne {
	_u.mutation.AddTargetID(v)
	return _u
}

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

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *FavoritesUpdateOne) 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 *FavoritesUpdateOne) check() error {
	if v, ok := _u.mutation.TargetType(); ok {
		if err := favorites.TargetTypeValidator(v); err != nil {
			return &ValidationError{Name: "target_type", err: fmt.Errorf(`models: validator failed for field "Favorites.target_type": %w`, err)}
		}
	}
	return nil
}

func (_u *FavoritesUpdateOne) sqlSave(ctx context.Context) (_node *Favorites, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(favorites.Table, favorites.Columns, sqlgraph.NewFieldSpec(favorites.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`models: missing "Favorites.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, favorites.FieldID)
		for _, f := range fields {
			if !favorites.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("models: invalid field %q for query", f)}
			}
			if f != favorites.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.UserID(); ok {
		_spec.SetField(favorites.FieldUserID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedUserID(); ok {
		_spec.AddField(favorites.FieldUserID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.TargetType(); ok {
		_spec.SetField(favorites.FieldTargetType, field.TypeString, value)
	}
	if value, ok := _u.mutation.TargetID(); ok {
		_spec.SetField(favorites.FieldTargetID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedTargetID(); ok {
		_spec.AddField(favorites.FieldTargetID, field.TypeInt64, value)
	}
	_node = &Favorites{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{favorites.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
