// Code generated by ent, DO NOT EDIT.

package models

import (
	"context"
	"errors"
	"fmt"
	"juwan-backend/app/users/rpc/internal/models/predicate"
	"juwan-backend/app/users/rpc/internal/models/userfollows"

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

// UserFollowsUpdate is the builder for updating UserFollows entities.
type UserFollowsUpdate struct {
	config
	hooks    []Hook
	mutation *UserFollowsMutation
}

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

// SetFollowerID sets the "follower_id" field.
func (_u *UserFollowsUpdate) SetFollowerID(v int64) *UserFollowsUpdate {
	_u.mutation.ResetFollowerID()
	_u.mutation.SetFollowerID(v)
	return _u
}

// SetNillableFollowerID sets the "follower_id" field if the given value is not nil.
func (_u *UserFollowsUpdate) SetNillableFollowerID(v *int64) *UserFollowsUpdate {
	if v != nil {
		_u.SetFollowerID(*v)
	}
	return _u
}

// AddFollowerID adds value to the "follower_id" field.
func (_u *UserFollowsUpdate) AddFollowerID(v int64) *UserFollowsUpdate {
	_u.mutation.AddFollowerID(v)
	return _u
}

// SetFolloweeID sets the "followee_id" field.
func (_u *UserFollowsUpdate) SetFolloweeID(v int64) *UserFollowsUpdate {
	_u.mutation.ResetFolloweeID()
	_u.mutation.SetFolloweeID(v)
	return _u
}

// SetNillableFolloweeID sets the "followee_id" field if the given value is not nil.
func (_u *UserFollowsUpdate) SetNillableFolloweeID(v *int64) *UserFollowsUpdate {
	if v != nil {
		_u.SetFolloweeID(*v)
	}
	return _u
}

// AddFolloweeID adds value to the "followee_id" field.
func (_u *UserFollowsUpdate) AddFolloweeID(v int64) *UserFollowsUpdate {
	_u.mutation.AddFolloweeID(v)
	return _u
}

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

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

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

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

func (_u *UserFollowsUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	_spec := sqlgraph.NewUpdateSpec(userfollows.Table, userfollows.Columns, sqlgraph.NewFieldSpec(userfollows.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.FollowerID(); ok {
		_spec.SetField(userfollows.FieldFollowerID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedFollowerID(); ok {
		_spec.AddField(userfollows.FieldFollowerID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.FolloweeID(); ok {
		_spec.SetField(userfollows.FieldFolloweeID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedFolloweeID(); ok {
		_spec.AddField(userfollows.FieldFolloweeID, field.TypeInt64, value)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{userfollows.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// UserFollowsUpdateOne is the builder for updating a single UserFollows entity.
type UserFollowsUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *UserFollowsMutation
}

// SetFollowerID sets the "follower_id" field.
func (_u *UserFollowsUpdateOne) SetFollowerID(v int64) *UserFollowsUpdateOne {
	_u.mutation.ResetFollowerID()
	_u.mutation.SetFollowerID(v)
	return _u
}

// SetNillableFollowerID sets the "follower_id" field if the given value is not nil.
func (_u *UserFollowsUpdateOne) SetNillableFollowerID(v *int64) *UserFollowsUpdateOne {
	if v != nil {
		_u.SetFollowerID(*v)
	}
	return _u
}

// AddFollowerID adds value to the "follower_id" field.
func (_u *UserFollowsUpdateOne) AddFollowerID(v int64) *UserFollowsUpdateOne {
	_u.mutation.AddFollowerID(v)
	return _u
}

// SetFolloweeID sets the "followee_id" field.
func (_u *UserFollowsUpdateOne) SetFolloweeID(v int64) *UserFollowsUpdateOne {
	_u.mutation.ResetFolloweeID()
	_u.mutation.SetFolloweeID(v)
	return _u
}

// SetNillableFolloweeID sets the "followee_id" field if the given value is not nil.
func (_u *UserFollowsUpdateOne) SetNillableFolloweeID(v *int64) *UserFollowsUpdateOne {
	if v != nil {
		_u.SetFolloweeID(*v)
	}
	return _u
}

// AddFolloweeID adds value to the "followee_id" field.
func (_u *UserFollowsUpdateOne) AddFolloweeID(v int64) *UserFollowsUpdateOne {
	_u.mutation.AddFolloweeID(v)
	return _u
}

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

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

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

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

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

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

func (_u *UserFollowsUpdateOne) sqlSave(ctx context.Context) (_node *UserFollows, err error) {
	_spec := sqlgraph.NewUpdateSpec(userfollows.Table, userfollows.Columns, sqlgraph.NewFieldSpec(userfollows.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`models: missing "UserFollows.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, userfollows.FieldID)
		for _, f := range fields {
			if !userfollows.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("models: invalid field %q for query", f)}
			}
			if f != userfollows.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.FollowerID(); ok {
		_spec.SetField(userfollows.FieldFollowerID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedFollowerID(); ok {
		_spec.AddField(userfollows.FieldFollowerID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.FolloweeID(); ok {
		_spec.SetField(userfollows.FieldFolloweeID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedFolloweeID(); ok {
		_spec.AddField(userfollows.FieldFolloweeID, field.TypeInt64, value)
	}
	_node = &UserFollows{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{userfollows.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
