// Code generated by ent, DO NOT EDIT.

package models

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

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

// CommentsUpdate is the builder for updating Comments entities.
type CommentsUpdate struct {
	config
	hooks    []Hook
	mutation *CommentsMutation
}

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

// SetPostID sets the "post_id" field.
func (_u *CommentsUpdate) SetPostID(v int64) *CommentsUpdate {
	_u.mutation.ResetPostID()
	_u.mutation.SetPostID(v)
	return _u
}

// SetNillablePostID sets the "post_id" field if the given value is not nil.
func (_u *CommentsUpdate) SetNillablePostID(v *int64) *CommentsUpdate {
	if v != nil {
		_u.SetPostID(*v)
	}
	return _u
}

// AddPostID adds value to the "post_id" field.
func (_u *CommentsUpdate) AddPostID(v int64) *CommentsUpdate {
	_u.mutation.AddPostID(v)
	return _u
}

// SetAuthorID sets the "author_id" field.
func (_u *CommentsUpdate) SetAuthorID(v int64) *CommentsUpdate {
	_u.mutation.ResetAuthorID()
	_u.mutation.SetAuthorID(v)
	return _u
}

// SetNillableAuthorID sets the "author_id" field if the given value is not nil.
func (_u *CommentsUpdate) SetNillableAuthorID(v *int64) *CommentsUpdate {
	if v != nil {
		_u.SetAuthorID(*v)
	}
	return _u
}

// AddAuthorID adds value to the "author_id" field.
func (_u *CommentsUpdate) AddAuthorID(v int64) *CommentsUpdate {
	_u.mutation.AddAuthorID(v)
	return _u
}

// SetContent sets the "content" field.
func (_u *CommentsUpdate) SetContent(v string) *CommentsUpdate {
	_u.mutation.SetContent(v)
	return _u
}

// SetNillableContent sets the "content" field if the given value is not nil.
func (_u *CommentsUpdate) SetNillableContent(v *string) *CommentsUpdate {
	if v != nil {
		_u.SetContent(*v)
	}
	return _u
}

// SetLikeCount sets the "like_count" field.
func (_u *CommentsUpdate) SetLikeCount(v int) *CommentsUpdate {
	_u.mutation.ResetLikeCount()
	_u.mutation.SetLikeCount(v)
	return _u
}

// SetNillableLikeCount sets the "like_count" field if the given value is not nil.
func (_u *CommentsUpdate) SetNillableLikeCount(v *int) *CommentsUpdate {
	if v != nil {
		_u.SetLikeCount(*v)
	}
	return _u
}

// AddLikeCount adds value to the "like_count" field.
func (_u *CommentsUpdate) AddLikeCount(v int) *CommentsUpdate {
	_u.mutation.AddLikeCount(v)
	return _u
}

// ClearLikeCount clears the value of the "like_count" field.
func (_u *CommentsUpdate) ClearLikeCount() *CommentsUpdate {
	_u.mutation.ClearLikeCount()
	return _u
}

// SetDeletedAt sets the "deleted_at" field.
func (_u *CommentsUpdate) SetDeletedAt(v time.Time) *CommentsUpdate {
	_u.mutation.SetDeletedAt(v)
	return _u
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (_u *CommentsUpdate) SetNillableDeletedAt(v *time.Time) *CommentsUpdate {
	if v != nil {
		_u.SetDeletedAt(*v)
	}
	return _u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (_u *CommentsUpdate) ClearDeletedAt() *CommentsUpdate {
	_u.mutation.ClearDeletedAt()
	return _u
}

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

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

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

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

func (_u *CommentsUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	_spec := sqlgraph.NewUpdateSpec(comments.Table, comments.Columns, sqlgraph.NewFieldSpec(comments.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.PostID(); ok {
		_spec.SetField(comments.FieldPostID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedPostID(); ok {
		_spec.AddField(comments.FieldPostID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AuthorID(); ok {
		_spec.SetField(comments.FieldAuthorID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedAuthorID(); ok {
		_spec.AddField(comments.FieldAuthorID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.Content(); ok {
		_spec.SetField(comments.FieldContent, field.TypeString, value)
	}
	if value, ok := _u.mutation.LikeCount(); ok {
		_spec.SetField(comments.FieldLikeCount, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedLikeCount(); ok {
		_spec.AddField(comments.FieldLikeCount, field.TypeInt, value)
	}
	if _u.mutation.LikeCountCleared() {
		_spec.ClearField(comments.FieldLikeCount, field.TypeInt)
	}
	if value, ok := _u.mutation.DeletedAt(); ok {
		_spec.SetField(comments.FieldDeletedAt, field.TypeTime, value)
	}
	if _u.mutation.DeletedAtCleared() {
		_spec.ClearField(comments.FieldDeletedAt, field.TypeTime)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{comments.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// CommentsUpdateOne is the builder for updating a single Comments entity.
type CommentsUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *CommentsMutation
}

// SetPostID sets the "post_id" field.
func (_u *CommentsUpdateOne) SetPostID(v int64) *CommentsUpdateOne {
	_u.mutation.ResetPostID()
	_u.mutation.SetPostID(v)
	return _u
}

// SetNillablePostID sets the "post_id" field if the given value is not nil.
func (_u *CommentsUpdateOne) SetNillablePostID(v *int64) *CommentsUpdateOne {
	if v != nil {
		_u.SetPostID(*v)
	}
	return _u
}

// AddPostID adds value to the "post_id" field.
func (_u *CommentsUpdateOne) AddPostID(v int64) *CommentsUpdateOne {
	_u.mutation.AddPostID(v)
	return _u
}

// SetAuthorID sets the "author_id" field.
func (_u *CommentsUpdateOne) SetAuthorID(v int64) *CommentsUpdateOne {
	_u.mutation.ResetAuthorID()
	_u.mutation.SetAuthorID(v)
	return _u
}

// SetNillableAuthorID sets the "author_id" field if the given value is not nil.
func (_u *CommentsUpdateOne) SetNillableAuthorID(v *int64) *CommentsUpdateOne {
	if v != nil {
		_u.SetAuthorID(*v)
	}
	return _u
}

// AddAuthorID adds value to the "author_id" field.
func (_u *CommentsUpdateOne) AddAuthorID(v int64) *CommentsUpdateOne {
	_u.mutation.AddAuthorID(v)
	return _u
}

// SetContent sets the "content" field.
func (_u *CommentsUpdateOne) SetContent(v string) *CommentsUpdateOne {
	_u.mutation.SetContent(v)
	return _u
}

// SetNillableContent sets the "content" field if the given value is not nil.
func (_u *CommentsUpdateOne) SetNillableContent(v *string) *CommentsUpdateOne {
	if v != nil {
		_u.SetContent(*v)
	}
	return _u
}

// SetLikeCount sets the "like_count" field.
func (_u *CommentsUpdateOne) SetLikeCount(v int) *CommentsUpdateOne {
	_u.mutation.ResetLikeCount()
	_u.mutation.SetLikeCount(v)
	return _u
}

// SetNillableLikeCount sets the "like_count" field if the given value is not nil.
func (_u *CommentsUpdateOne) SetNillableLikeCount(v *int) *CommentsUpdateOne {
	if v != nil {
		_u.SetLikeCount(*v)
	}
	return _u
}

// AddLikeCount adds value to the "like_count" field.
func (_u *CommentsUpdateOne) AddLikeCount(v int) *CommentsUpdateOne {
	_u.mutation.AddLikeCount(v)
	return _u
}

// ClearLikeCount clears the value of the "like_count" field.
func (_u *CommentsUpdateOne) ClearLikeCount() *CommentsUpdateOne {
	_u.mutation.ClearLikeCount()
	return _u
}

// SetDeletedAt sets the "deleted_at" field.
func (_u *CommentsUpdateOne) SetDeletedAt(v time.Time) *CommentsUpdateOne {
	_u.mutation.SetDeletedAt(v)
	return _u
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (_u *CommentsUpdateOne) SetNillableDeletedAt(v *time.Time) *CommentsUpdateOne {
	if v != nil {
		_u.SetDeletedAt(*v)
	}
	return _u
}

// ClearDeletedAt clears the value of the "deleted_at" field.
func (_u *CommentsUpdateOne) ClearDeletedAt() *CommentsUpdateOne {
	_u.mutation.ClearDeletedAt()
	return _u
}

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

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

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

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

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

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

func (_u *CommentsUpdateOne) sqlSave(ctx context.Context) (_node *Comments, err error) {
	_spec := sqlgraph.NewUpdateSpec(comments.Table, comments.Columns, sqlgraph.NewFieldSpec(comments.FieldID, field.TypeInt64))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`models: missing "Comments.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, comments.FieldID)
		for _, f := range fields {
			if !comments.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("models: invalid field %q for query", f)}
			}
			if f != comments.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.PostID(); ok {
		_spec.SetField(comments.FieldPostID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedPostID(); ok {
		_spec.AddField(comments.FieldPostID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AuthorID(); ok {
		_spec.SetField(comments.FieldAuthorID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedAuthorID(); ok {
		_spec.AddField(comments.FieldAuthorID, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.Content(); ok {
		_spec.SetField(comments.FieldContent, field.TypeString, value)
	}
	if value, ok := _u.mutation.LikeCount(); ok {
		_spec.SetField(comments.FieldLikeCount, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedLikeCount(); ok {
		_spec.AddField(comments.FieldLikeCount, field.TypeInt, value)
	}
	if _u.mutation.LikeCountCleared() {
		_spec.ClearField(comments.FieldLikeCount, field.TypeInt)
	}
	if value, ok := _u.mutation.DeletedAt(); ok {
		_spec.SetField(comments.FieldDeletedAt, field.TypeTime, value)
	}
	if _u.mutation.DeletedAtCleared() {
		_spec.ClearField(comments.FieldDeletedAt, field.TypeTime)
	}
	_node = &Comments{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{comments.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
