// Code generated by ent, DO NOT EDIT.

package serversitemicros

import (
	"context"
	"convert-server/domain/entity/serversitemicros/docserverapp"
	"convert-server/domain/entity/serversitemicros/predicate"
	"errors"
	"fmt"
	"time"

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

// DocServerAppUpdate is the builder for updating DocServerApp entities.
type DocServerAppUpdate struct {
	config
	hooks    []Hook
	mutation *DocServerAppMutation
}

// Where appends a list predicates to the DocServerAppUpdate builder.
func (dsau *DocServerAppUpdate) Where(ps ...predicate.DocServerApp) *DocServerAppUpdate {
	dsau.mutation.Where(ps...)
	return dsau
}

// SetApplication sets the "application" field.
func (dsau *DocServerAppUpdate) SetApplication(s string) *DocServerAppUpdate {
	dsau.mutation.SetApplication(s)
	return dsau
}

// SetNillableApplication sets the "application" field if the given value is not nil.
func (dsau *DocServerAppUpdate) SetNillableApplication(s *string) *DocServerAppUpdate {
	if s != nil {
		dsau.SetApplication(*s)
	}
	return dsau
}

// SetName sets the "name" field.
func (dsau *DocServerAppUpdate) SetName(s string) *DocServerAppUpdate {
	dsau.mutation.SetName(s)
	return dsau
}

// SetNillableName sets the "name" field if the given value is not nil.
func (dsau *DocServerAppUpdate) SetNillableName(s *string) *DocServerAppUpdate {
	if s != nil {
		dsau.SetName(*s)
	}
	return dsau
}

// SetCreatedAt sets the "created_at" field.
func (dsau *DocServerAppUpdate) SetCreatedAt(t time.Time) *DocServerAppUpdate {
	dsau.mutation.SetCreatedAt(t)
	return dsau
}

// ClearCreatedAt clears the value of the "created_at" field.
func (dsau *DocServerAppUpdate) ClearCreatedAt() *DocServerAppUpdate {
	dsau.mutation.ClearCreatedAt()
	return dsau
}

// Mutation returns the DocServerAppMutation object of the builder.
func (dsau *DocServerAppUpdate) Mutation() *DocServerAppMutation {
	return dsau.mutation
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (dsau *DocServerAppUpdate) defaults() error {
	if _, ok := dsau.mutation.CreatedAt(); !ok && !dsau.mutation.CreatedAtCleared() {
		if docserverapp.UpdateDefaultCreatedAt == nil {
			return fmt.Errorf("serversitemicros: uninitialized docserverapp.UpdateDefaultCreatedAt (forgotten import serversitemicros/runtime?)")
		}
		v := docserverapp.UpdateDefaultCreatedAt()
		dsau.mutation.SetCreatedAt(v)
	}
	return nil
}

func (dsau *DocServerAppUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(docserverapp.Table, docserverapp.Columns, sqlgraph.NewFieldSpec(docserverapp.FieldID, field.TypeInt32))
	if ps := dsau.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := dsau.mutation.Application(); ok {
		_spec.SetField(docserverapp.FieldApplication, field.TypeString, value)
	}
	if value, ok := dsau.mutation.Name(); ok {
		_spec.SetField(docserverapp.FieldName, field.TypeString, value)
	}
	if value, ok := dsau.mutation.CreatedAt(); ok {
		_spec.SetField(docserverapp.FieldCreatedAt, field.TypeTime, value)
	}
	if dsau.mutation.CreatedAtCleared() {
		_spec.ClearField(docserverapp.FieldCreatedAt, field.TypeTime)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, dsau.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{docserverapp.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	dsau.mutation.done = true
	return n, nil
}

// DocServerAppUpdateOne is the builder for updating a single DocServerApp entity.
type DocServerAppUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *DocServerAppMutation
}

// SetApplication sets the "application" field.
func (dsauo *DocServerAppUpdateOne) SetApplication(s string) *DocServerAppUpdateOne {
	dsauo.mutation.SetApplication(s)
	return dsauo
}

// SetNillableApplication sets the "application" field if the given value is not nil.
func (dsauo *DocServerAppUpdateOne) SetNillableApplication(s *string) *DocServerAppUpdateOne {
	if s != nil {
		dsauo.SetApplication(*s)
	}
	return dsauo
}

// SetName sets the "name" field.
func (dsauo *DocServerAppUpdateOne) SetName(s string) *DocServerAppUpdateOne {
	dsauo.mutation.SetName(s)
	return dsauo
}

// SetNillableName sets the "name" field if the given value is not nil.
func (dsauo *DocServerAppUpdateOne) SetNillableName(s *string) *DocServerAppUpdateOne {
	if s != nil {
		dsauo.SetName(*s)
	}
	return dsauo
}

// SetCreatedAt sets the "created_at" field.
func (dsauo *DocServerAppUpdateOne) SetCreatedAt(t time.Time) *DocServerAppUpdateOne {
	dsauo.mutation.SetCreatedAt(t)
	return dsauo
}

// ClearCreatedAt clears the value of the "created_at" field.
func (dsauo *DocServerAppUpdateOne) ClearCreatedAt() *DocServerAppUpdateOne {
	dsauo.mutation.ClearCreatedAt()
	return dsauo
}

// Mutation returns the DocServerAppMutation object of the builder.
func (dsauo *DocServerAppUpdateOne) Mutation() *DocServerAppMutation {
	return dsauo.mutation
}

// Where appends a list predicates to the DocServerAppUpdate builder.
func (dsauo *DocServerAppUpdateOne) Where(ps ...predicate.DocServerApp) *DocServerAppUpdateOne {
	dsauo.mutation.Where(ps...)
	return dsauo
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (dsauo *DocServerAppUpdateOne) Select(field string, fields ...string) *DocServerAppUpdateOne {
	dsauo.fields = append([]string{field}, fields...)
	return dsauo
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (dsauo *DocServerAppUpdateOne) defaults() error {
	if _, ok := dsauo.mutation.CreatedAt(); !ok && !dsauo.mutation.CreatedAtCleared() {
		if docserverapp.UpdateDefaultCreatedAt == nil {
			return fmt.Errorf("serversitemicros: uninitialized docserverapp.UpdateDefaultCreatedAt (forgotten import serversitemicros/runtime?)")
		}
		v := docserverapp.UpdateDefaultCreatedAt()
		dsauo.mutation.SetCreatedAt(v)
	}
	return nil
}

func (dsauo *DocServerAppUpdateOne) sqlSave(ctx context.Context) (_node *DocServerApp, err error) {
	_spec := sqlgraph.NewUpdateSpec(docserverapp.Table, docserverapp.Columns, sqlgraph.NewFieldSpec(docserverapp.FieldID, field.TypeInt32))
	id, ok := dsauo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`serversitemicros: missing "DocServerApp.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := dsauo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, docserverapp.FieldID)
		for _, f := range fields {
			if !docserverapp.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("serversitemicros: invalid field %q for query", f)}
			}
			if f != docserverapp.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := dsauo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := dsauo.mutation.Application(); ok {
		_spec.SetField(docserverapp.FieldApplication, field.TypeString, value)
	}
	if value, ok := dsauo.mutation.Name(); ok {
		_spec.SetField(docserverapp.FieldName, field.TypeString, value)
	}
	if value, ok := dsauo.mutation.CreatedAt(); ok {
		_spec.SetField(docserverapp.FieldCreatedAt, field.TypeTime, value)
	}
	if dsauo.mutation.CreatedAtCleared() {
		_spec.ClearField(docserverapp.FieldCreatedAt, field.TypeTime)
	}
	_node = &DocServerApp{config: dsauo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, dsauo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{docserverapp.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	dsauo.mutation.done = true
	return _node, nil
}
