// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/anzhiyu-c/anheyu-app/ent/page"
	"github.com/anzhiyu-c/anheyu-app/ent/predicate"
)

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

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

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

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

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

// SetTitle sets the "title" field.
func (_u *PageUpdate) SetTitle(v string) *PageUpdate {
	_u.mutation.SetTitle(v)
	return _u
}

// SetNillableTitle sets the "title" field if the given value is not nil.
func (_u *PageUpdate) SetNillableTitle(v *string) *PageUpdate {
	if v != nil {
		_u.SetTitle(*v)
	}
	return _u
}

// SetPath sets the "path" field.
func (_u *PageUpdate) SetPath(v string) *PageUpdate {
	_u.mutation.SetPath(v)
	return _u
}

// SetNillablePath sets the "path" field if the given value is not nil.
func (_u *PageUpdate) SetNillablePath(v *string) *PageUpdate {
	if v != nil {
		_u.SetPath(*v)
	}
	return _u
}

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

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

// SetMarkdownContent sets the "markdown_content" field.
func (_u *PageUpdate) SetMarkdownContent(v string) *PageUpdate {
	_u.mutation.SetMarkdownContent(v)
	return _u
}

// SetNillableMarkdownContent sets the "markdown_content" field if the given value is not nil.
func (_u *PageUpdate) SetNillableMarkdownContent(v *string) *PageUpdate {
	if v != nil {
		_u.SetMarkdownContent(*v)
	}
	return _u
}

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

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

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

// SetIsPublished sets the "is_published" field.
func (_u *PageUpdate) SetIsPublished(v bool) *PageUpdate {
	_u.mutation.SetIsPublished(v)
	return _u
}

// SetNillableIsPublished sets the "is_published" field if the given value is not nil.
func (_u *PageUpdate) SetNillableIsPublished(v *bool) *PageUpdate {
	if v != nil {
		_u.SetIsPublished(*v)
	}
	return _u
}

// SetShowComment sets the "show_comment" field.
func (_u *PageUpdate) SetShowComment(v bool) *PageUpdate {
	_u.mutation.SetShowComment(v)
	return _u
}

// SetNillableShowComment sets the "show_comment" field if the given value is not nil.
func (_u *PageUpdate) SetNillableShowComment(v *bool) *PageUpdate {
	if v != nil {
		_u.SetShowComment(*v)
	}
	return _u
}

// SetSort sets the "sort" field.
func (_u *PageUpdate) SetSort(v int) *PageUpdate {
	_u.mutation.ResetSort()
	_u.mutation.SetSort(v)
	return _u
}

// SetNillableSort sets the "sort" field if the given value is not nil.
func (_u *PageUpdate) SetNillableSort(v *int) *PageUpdate {
	if v != nil {
		_u.SetSort(*v)
	}
	return _u
}

// AddSort adds value to the "sort" field.
func (_u *PageUpdate) AddSort(v int) *PageUpdate {
	_u.mutation.AddSort(v)
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *PageUpdate) SetUpdatedAt(v time.Time) *PageUpdate {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

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

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *PageUpdate) Save(ctx context.Context) (int, error) {
	if err := _u.defaults(); err != nil {
		return 0, err
	}
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (_u *PageUpdate) defaults() error {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		if page.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized page.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := page.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (_u *PageUpdate) check() error {
	if v, ok := _u.mutation.Title(); ok {
		if err := page.TitleValidator(v); err != nil {
			return &ValidationError{Name: "title", err: fmt.Errorf(`ent: validator failed for field "Page.title": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Path(); ok {
		if err := page.PathValidator(v); err != nil {
			return &ValidationError{Name: "path", err: fmt.Errorf(`ent: validator failed for field "Page.path": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Description(); ok {
		if err := page.DescriptionValidator(v); err != nil {
			return &ValidationError{Name: "description", err: fmt.Errorf(`ent: validator failed for field "Page.description": %w`, err)}
		}
	}
	return nil
}

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

func (_u *PageUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(page.Table, page.Columns, sqlgraph.NewFieldSpec(page.FieldID, field.TypeUint))
	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.DeletedAt(); ok {
		_spec.SetField(page.FieldDeletedAt, field.TypeTime, value)
	}
	if _u.mutation.DeletedAtCleared() {
		_spec.ClearField(page.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.Title(); ok {
		_spec.SetField(page.FieldTitle, field.TypeString, value)
	}
	if value, ok := _u.mutation.Path(); ok {
		_spec.SetField(page.FieldPath, field.TypeString, value)
	}
	if value, ok := _u.mutation.Content(); ok {
		_spec.SetField(page.FieldContent, field.TypeString, value)
	}
	if value, ok := _u.mutation.MarkdownContent(); ok {
		_spec.SetField(page.FieldMarkdownContent, field.TypeString, value)
	}
	if value, ok := _u.mutation.Description(); ok {
		_spec.SetField(page.FieldDescription, field.TypeString, value)
	}
	if _u.mutation.DescriptionCleared() {
		_spec.ClearField(page.FieldDescription, field.TypeString)
	}
	if value, ok := _u.mutation.IsPublished(); ok {
		_spec.SetField(page.FieldIsPublished, field.TypeBool, value)
	}
	if value, ok := _u.mutation.ShowComment(); ok {
		_spec.SetField(page.FieldShowComment, field.TypeBool, value)
	}
	if value, ok := _u.mutation.Sort(); ok {
		_spec.SetField(page.FieldSort, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedSort(); ok {
		_spec.AddField(page.FieldSort, field.TypeInt, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(page.FieldUpdatedAt, field.TypeTime, value)
	}
	_spec.AddModifiers(_u.modifiers...)
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{page.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

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

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

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

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

// SetTitle sets the "title" field.
func (_u *PageUpdateOne) SetTitle(v string) *PageUpdateOne {
	_u.mutation.SetTitle(v)
	return _u
}

// SetNillableTitle sets the "title" field if the given value is not nil.
func (_u *PageUpdateOne) SetNillableTitle(v *string) *PageUpdateOne {
	if v != nil {
		_u.SetTitle(*v)
	}
	return _u
}

// SetPath sets the "path" field.
func (_u *PageUpdateOne) SetPath(v string) *PageUpdateOne {
	_u.mutation.SetPath(v)
	return _u
}

// SetNillablePath sets the "path" field if the given value is not nil.
func (_u *PageUpdateOne) SetNillablePath(v *string) *PageUpdateOne {
	if v != nil {
		_u.SetPath(*v)
	}
	return _u
}

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

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

// SetMarkdownContent sets the "markdown_content" field.
func (_u *PageUpdateOne) SetMarkdownContent(v string) *PageUpdateOne {
	_u.mutation.SetMarkdownContent(v)
	return _u
}

// SetNillableMarkdownContent sets the "markdown_content" field if the given value is not nil.
func (_u *PageUpdateOne) SetNillableMarkdownContent(v *string) *PageUpdateOne {
	if v != nil {
		_u.SetMarkdownContent(*v)
	}
	return _u
}

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

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

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

// SetIsPublished sets the "is_published" field.
func (_u *PageUpdateOne) SetIsPublished(v bool) *PageUpdateOne {
	_u.mutation.SetIsPublished(v)
	return _u
}

// SetNillableIsPublished sets the "is_published" field if the given value is not nil.
func (_u *PageUpdateOne) SetNillableIsPublished(v *bool) *PageUpdateOne {
	if v != nil {
		_u.SetIsPublished(*v)
	}
	return _u
}

// SetShowComment sets the "show_comment" field.
func (_u *PageUpdateOne) SetShowComment(v bool) *PageUpdateOne {
	_u.mutation.SetShowComment(v)
	return _u
}

// SetNillableShowComment sets the "show_comment" field if the given value is not nil.
func (_u *PageUpdateOne) SetNillableShowComment(v *bool) *PageUpdateOne {
	if v != nil {
		_u.SetShowComment(*v)
	}
	return _u
}

// SetSort sets the "sort" field.
func (_u *PageUpdateOne) SetSort(v int) *PageUpdateOne {
	_u.mutation.ResetSort()
	_u.mutation.SetSort(v)
	return _u
}

// SetNillableSort sets the "sort" field if the given value is not nil.
func (_u *PageUpdateOne) SetNillableSort(v *int) *PageUpdateOne {
	if v != nil {
		_u.SetSort(*v)
	}
	return _u
}

// AddSort adds value to the "sort" field.
func (_u *PageUpdateOne) AddSort(v int) *PageUpdateOne {
	_u.mutation.AddSort(v)
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *PageUpdateOne) SetUpdatedAt(v time.Time) *PageUpdateOne {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

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

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

// Save executes the query and returns the updated Page entity.
func (_u *PageUpdateOne) Save(ctx context.Context) (*Page, error) {
	if err := _u.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (_u *PageUpdateOne) defaults() error {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		if page.UpdateDefaultUpdatedAt == nil {
			return fmt.Errorf("ent: uninitialized page.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
		}
		v := page.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (_u *PageUpdateOne) check() error {
	if v, ok := _u.mutation.Title(); ok {
		if err := page.TitleValidator(v); err != nil {
			return &ValidationError{Name: "title", err: fmt.Errorf(`ent: validator failed for field "Page.title": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Path(); ok {
		if err := page.PathValidator(v); err != nil {
			return &ValidationError{Name: "path", err: fmt.Errorf(`ent: validator failed for field "Page.path": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Description(); ok {
		if err := page.DescriptionValidator(v); err != nil {
			return &ValidationError{Name: "description", err: fmt.Errorf(`ent: validator failed for field "Page.description": %w`, err)}
		}
	}
	return nil
}

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

func (_u *PageUpdateOne) sqlSave(ctx context.Context) (_node *Page, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(page.Table, page.Columns, sqlgraph.NewFieldSpec(page.FieldID, field.TypeUint))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Page.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, page.FieldID)
		for _, f := range fields {
			if !page.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != page.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.DeletedAt(); ok {
		_spec.SetField(page.FieldDeletedAt, field.TypeTime, value)
	}
	if _u.mutation.DeletedAtCleared() {
		_spec.ClearField(page.FieldDeletedAt, field.TypeTime)
	}
	if value, ok := _u.mutation.Title(); ok {
		_spec.SetField(page.FieldTitle, field.TypeString, value)
	}
	if value, ok := _u.mutation.Path(); ok {
		_spec.SetField(page.FieldPath, field.TypeString, value)
	}
	if value, ok := _u.mutation.Content(); ok {
		_spec.SetField(page.FieldContent, field.TypeString, value)
	}
	if value, ok := _u.mutation.MarkdownContent(); ok {
		_spec.SetField(page.FieldMarkdownContent, field.TypeString, value)
	}
	if value, ok := _u.mutation.Description(); ok {
		_spec.SetField(page.FieldDescription, field.TypeString, value)
	}
	if _u.mutation.DescriptionCleared() {
		_spec.ClearField(page.FieldDescription, field.TypeString)
	}
	if value, ok := _u.mutation.IsPublished(); ok {
		_spec.SetField(page.FieldIsPublished, field.TypeBool, value)
	}
	if value, ok := _u.mutation.ShowComment(); ok {
		_spec.SetField(page.FieldShowComment, field.TypeBool, value)
	}
	if value, ok := _u.mutation.Sort(); ok {
		_spec.SetField(page.FieldSort, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedSort(); ok {
		_spec.AddField(page.FieldSort, field.TypeInt, value)
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(page.FieldUpdatedAt, field.TypeTime, value)
	}
	_spec.AddModifiers(_u.modifiers...)
	_node = &Page{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{page.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
