// Code generated by ent, DO NOT EDIT.

package models

import (
	"context"
	"errors"
	"fmt"
	"juwan-backend/app/community/rpc/internal/models/commentlikes"
	"juwan-backend/app/community/rpc/internal/models/predicate"

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

// CommentLikesUpdate is the builder for updating CommentLikes entities.
type CommentLikesUpdate struct {
	config
	hooks    []Hook
	mutation *CommentLikesMutation
}

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

// SetCommentID sets the "comment_id" field.
func (_u *CommentLikesUpdate) SetCommentID(v int64) *CommentLikesUpdate {
	_u.mutation.ResetCommentID()
	_u.mutation.SetCommentID(v)
	return _u
}

// SetNillableCommentID sets the "comment_id" field if the given value is not nil.
func (_u *CommentLikesUpdate) SetNillableCommentID(v *int64) *CommentLikesUpdate {
	if v != nil {
		_u.SetCommentID(*v)
	}
	return _u
}

// AddCommentID adds value to the "comment_id" field.
func (_u *CommentLikesUpdate) AddCommentID(v int64) *CommentLikesUpdate {
	_u.mutation.AddCommentID(v)
	return _u
}

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

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

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

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

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

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

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

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

// CommentLikesUpdateOne is the builder for updating a single CommentLikes entity.
type CommentLikesUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *CommentLikesMutation
}

// SetCommentID sets the "comment_id" field.
func (_u *CommentLikesUpdateOne) SetCommentID(v int64) *CommentLikesUpdateOne {
	_u.mutation.ResetCommentID()
	_u.mutation.SetCommentID(v)
	return _u
}

// SetNillableCommentID sets the "comment_id" field if the given value is not nil.
func (_u *CommentLikesUpdateOne) SetNillableCommentID(v *int64) *CommentLikesUpdateOne {
	if v != nil {
		_u.SetCommentID(*v)
	}
	return _u
}

// AddCommentID adds value to the "comment_id" field.
func (_u *CommentLikesUpdateOne) AddCommentID(v int64) *CommentLikesUpdateOne {
	_u.mutation.AddCommentID(v)
	return _u
}

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

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

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

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

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

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

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

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

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

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