// Code generated by ent, DO NOT EDIT.

package serversitemicros

import (
	"context"
	"convert-server/domain/entity/serversitemicros/docserverproject"
	"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"
)

// DocServerProjectUpdate is the builder for updating DocServerProject entities.
type DocServerProjectUpdate struct {
	config
	hooks    []Hook
	mutation *DocServerProjectMutation
}

// Where appends a list predicates to the DocServerProjectUpdate builder.
func (dspu *DocServerProjectUpdate) Where(ps ...predicate.DocServerProject) *DocServerProjectUpdate {
	dspu.mutation.Where(ps...)
	return dspu
}

// SetProjectName sets the "project_name" field.
func (dspu *DocServerProjectUpdate) SetProjectName(s string) *DocServerProjectUpdate {
	dspu.mutation.SetProjectName(s)
	return dspu
}

// SetNillableProjectName sets the "project_name" field if the given value is not nil.
func (dspu *DocServerProjectUpdate) SetNillableProjectName(s *string) *DocServerProjectUpdate {
	if s != nil {
		dspu.SetProjectName(*s)
	}
	return dspu
}

// SetSourceType sets the "source_type" field.
func (dspu *DocServerProjectUpdate) SetSourceType(s string) *DocServerProjectUpdate {
	dspu.mutation.SetSourceType(s)
	return dspu
}

// SetNillableSourceType sets the "source_type" field if the given value is not nil.
func (dspu *DocServerProjectUpdate) SetNillableSourceType(s *string) *DocServerProjectUpdate {
	if s != nil {
		dspu.SetSourceType(*s)
	}
	return dspu
}

// SetTargetType sets the "target_type" field.
func (dspu *DocServerProjectUpdate) SetTargetType(s string) *DocServerProjectUpdate {
	dspu.mutation.SetTargetType(s)
	return dspu
}

// SetNillableTargetType sets the "target_type" field if the given value is not nil.
func (dspu *DocServerProjectUpdate) SetNillableTargetType(s *string) *DocServerProjectUpdate {
	if s != nil {
		dspu.SetTargetType(*s)
	}
	return dspu
}

// SetSourceURL sets the "source_url" field.
func (dspu *DocServerProjectUpdate) SetSourceURL(s string) *DocServerProjectUpdate {
	dspu.mutation.SetSourceURL(s)
	return dspu
}

// SetNillableSourceURL sets the "source_url" field if the given value is not nil.
func (dspu *DocServerProjectUpdate) SetNillableSourceURL(s *string) *DocServerProjectUpdate {
	if s != nil {
		dspu.SetSourceURL(*s)
	}
	return dspu
}

// ClearSourceURL clears the value of the "source_url" field.
func (dspu *DocServerProjectUpdate) ClearSourceURL() *DocServerProjectUpdate {
	dspu.mutation.ClearSourceURL()
	return dspu
}

// SetTargetURL sets the "target_url" field.
func (dspu *DocServerProjectUpdate) SetTargetURL(s string) *DocServerProjectUpdate {
	dspu.mutation.SetTargetURL(s)
	return dspu
}

// SetNillableTargetURL sets the "target_url" field if the given value is not nil.
func (dspu *DocServerProjectUpdate) SetNillableTargetURL(s *string) *DocServerProjectUpdate {
	if s != nil {
		dspu.SetTargetURL(*s)
	}
	return dspu
}

// ClearTargetURL clears the value of the "target_url" field.
func (dspu *DocServerProjectUpdate) ClearTargetURL() *DocServerProjectUpdate {
	dspu.mutation.ClearTargetURL()
	return dspu
}

// SetTaskID sets the "task_id" field.
func (dspu *DocServerProjectUpdate) SetTaskID(s string) *DocServerProjectUpdate {
	dspu.mutation.SetTaskID(s)
	return dspu
}

// SetNillableTaskID sets the "task_id" field if the given value is not nil.
func (dspu *DocServerProjectUpdate) SetNillableTaskID(s *string) *DocServerProjectUpdate {
	if s != nil {
		dspu.SetTaskID(*s)
	}
	return dspu
}

// SetAppID sets the "app_id" field.
func (dspu *DocServerProjectUpdate) SetAppID(i int32) *DocServerProjectUpdate {
	dspu.mutation.ResetAppID()
	dspu.mutation.SetAppID(i)
	return dspu
}

// SetNillableAppID sets the "app_id" field if the given value is not nil.
func (dspu *DocServerProjectUpdate) SetNillableAppID(i *int32) *DocServerProjectUpdate {
	if i != nil {
		dspu.SetAppID(*i)
	}
	return dspu
}

// AddAppID adds i to the "app_id" field.
func (dspu *DocServerProjectUpdate) AddAppID(i int32) *DocServerProjectUpdate {
	dspu.mutation.AddAppID(i)
	return dspu
}

// SetStatus sets the "status" field.
func (dspu *DocServerProjectUpdate) SetStatus(i int32) *DocServerProjectUpdate {
	dspu.mutation.ResetStatus()
	dspu.mutation.SetStatus(i)
	return dspu
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (dspu *DocServerProjectUpdate) SetNillableStatus(i *int32) *DocServerProjectUpdate {
	if i != nil {
		dspu.SetStatus(*i)
	}
	return dspu
}

// AddStatus adds i to the "status" field.
func (dspu *DocServerProjectUpdate) AddStatus(i int32) *DocServerProjectUpdate {
	dspu.mutation.AddStatus(i)
	return dspu
}

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

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (dspu *DocServerProjectUpdate) SetNillableCreatedAt(t *time.Time) *DocServerProjectUpdate {
	if t != nil {
		dspu.SetCreatedAt(*t)
	}
	return dspu
}

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

// Mutation returns the DocServerProjectMutation object of the builder.
func (dspu *DocServerProjectUpdate) Mutation() *DocServerProjectMutation {
	return dspu.mutation
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (dspu *DocServerProjectUpdate) Save(ctx context.Context) (int, error) {
	return withHooks(ctx, dspu.sqlSave, dspu.mutation, dspu.hooks)
}

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

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

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

// check runs all checks and user-defined validators on the builder.
func (dspu *DocServerProjectUpdate) check() error {
	if v, ok := dspu.mutation.SourceURL(); ok {
		if err := docserverproject.SourceURLValidator(v); err != nil {
			return &ValidationError{Name: "source_url", err: fmt.Errorf(`serversitemicros: validator failed for field "DocServerProject.source_url": %w`, err)}
		}
	}
	if v, ok := dspu.mutation.TargetURL(); ok {
		if err := docserverproject.TargetURLValidator(v); err != nil {
			return &ValidationError{Name: "target_url", err: fmt.Errorf(`serversitemicros: validator failed for field "DocServerProject.target_url": %w`, err)}
		}
	}
	return nil
}

func (dspu *DocServerProjectUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := dspu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(docserverproject.Table, docserverproject.Columns, sqlgraph.NewFieldSpec(docserverproject.FieldID, field.TypeInt32))
	if ps := dspu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := dspu.mutation.ProjectName(); ok {
		_spec.SetField(docserverproject.FieldProjectName, field.TypeString, value)
	}
	if value, ok := dspu.mutation.SourceType(); ok {
		_spec.SetField(docserverproject.FieldSourceType, field.TypeString, value)
	}
	if value, ok := dspu.mutation.TargetType(); ok {
		_spec.SetField(docserverproject.FieldTargetType, field.TypeString, value)
	}
	if value, ok := dspu.mutation.SourceURL(); ok {
		_spec.SetField(docserverproject.FieldSourceURL, field.TypeString, value)
	}
	if dspu.mutation.SourceURLCleared() {
		_spec.ClearField(docserverproject.FieldSourceURL, field.TypeString)
	}
	if value, ok := dspu.mutation.TargetURL(); ok {
		_spec.SetField(docserverproject.FieldTargetURL, field.TypeString, value)
	}
	if dspu.mutation.TargetURLCleared() {
		_spec.ClearField(docserverproject.FieldTargetURL, field.TypeString)
	}
	if value, ok := dspu.mutation.TaskID(); ok {
		_spec.SetField(docserverproject.FieldTaskID, field.TypeString, value)
	}
	if value, ok := dspu.mutation.AppID(); ok {
		_spec.SetField(docserverproject.FieldAppID, field.TypeInt32, value)
	}
	if value, ok := dspu.mutation.AddedAppID(); ok {
		_spec.AddField(docserverproject.FieldAppID, field.TypeInt32, value)
	}
	if value, ok := dspu.mutation.Status(); ok {
		_spec.SetField(docserverproject.FieldStatus, field.TypeInt32, value)
	}
	if value, ok := dspu.mutation.AddedStatus(); ok {
		_spec.AddField(docserverproject.FieldStatus, field.TypeInt32, value)
	}
	if value, ok := dspu.mutation.CreatedAt(); ok {
		_spec.SetField(docserverproject.FieldCreatedAt, field.TypeTime, value)
	}
	if dspu.mutation.CreatedAtCleared() {
		_spec.ClearField(docserverproject.FieldCreatedAt, field.TypeTime)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, dspu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{docserverproject.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	dspu.mutation.done = true
	return n, nil
}

// DocServerProjectUpdateOne is the builder for updating a single DocServerProject entity.
type DocServerProjectUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *DocServerProjectMutation
}

// SetProjectName sets the "project_name" field.
func (dspuo *DocServerProjectUpdateOne) SetProjectName(s string) *DocServerProjectUpdateOne {
	dspuo.mutation.SetProjectName(s)
	return dspuo
}

// SetNillableProjectName sets the "project_name" field if the given value is not nil.
func (dspuo *DocServerProjectUpdateOne) SetNillableProjectName(s *string) *DocServerProjectUpdateOne {
	if s != nil {
		dspuo.SetProjectName(*s)
	}
	return dspuo
}

// SetSourceType sets the "source_type" field.
func (dspuo *DocServerProjectUpdateOne) SetSourceType(s string) *DocServerProjectUpdateOne {
	dspuo.mutation.SetSourceType(s)
	return dspuo
}

// SetNillableSourceType sets the "source_type" field if the given value is not nil.
func (dspuo *DocServerProjectUpdateOne) SetNillableSourceType(s *string) *DocServerProjectUpdateOne {
	if s != nil {
		dspuo.SetSourceType(*s)
	}
	return dspuo
}

// SetTargetType sets the "target_type" field.
func (dspuo *DocServerProjectUpdateOne) SetTargetType(s string) *DocServerProjectUpdateOne {
	dspuo.mutation.SetTargetType(s)
	return dspuo
}

// SetNillableTargetType sets the "target_type" field if the given value is not nil.
func (dspuo *DocServerProjectUpdateOne) SetNillableTargetType(s *string) *DocServerProjectUpdateOne {
	if s != nil {
		dspuo.SetTargetType(*s)
	}
	return dspuo
}

// SetSourceURL sets the "source_url" field.
func (dspuo *DocServerProjectUpdateOne) SetSourceURL(s string) *DocServerProjectUpdateOne {
	dspuo.mutation.SetSourceURL(s)
	return dspuo
}

// SetNillableSourceURL sets the "source_url" field if the given value is not nil.
func (dspuo *DocServerProjectUpdateOne) SetNillableSourceURL(s *string) *DocServerProjectUpdateOne {
	if s != nil {
		dspuo.SetSourceURL(*s)
	}
	return dspuo
}

// ClearSourceURL clears the value of the "source_url" field.
func (dspuo *DocServerProjectUpdateOne) ClearSourceURL() *DocServerProjectUpdateOne {
	dspuo.mutation.ClearSourceURL()
	return dspuo
}

// SetTargetURL sets the "target_url" field.
func (dspuo *DocServerProjectUpdateOne) SetTargetURL(s string) *DocServerProjectUpdateOne {
	dspuo.mutation.SetTargetURL(s)
	return dspuo
}

// SetNillableTargetURL sets the "target_url" field if the given value is not nil.
func (dspuo *DocServerProjectUpdateOne) SetNillableTargetURL(s *string) *DocServerProjectUpdateOne {
	if s != nil {
		dspuo.SetTargetURL(*s)
	}
	return dspuo
}

// ClearTargetURL clears the value of the "target_url" field.
func (dspuo *DocServerProjectUpdateOne) ClearTargetURL() *DocServerProjectUpdateOne {
	dspuo.mutation.ClearTargetURL()
	return dspuo
}

// SetTaskID sets the "task_id" field.
func (dspuo *DocServerProjectUpdateOne) SetTaskID(s string) *DocServerProjectUpdateOne {
	dspuo.mutation.SetTaskID(s)
	return dspuo
}

// SetNillableTaskID sets the "task_id" field if the given value is not nil.
func (dspuo *DocServerProjectUpdateOne) SetNillableTaskID(s *string) *DocServerProjectUpdateOne {
	if s != nil {
		dspuo.SetTaskID(*s)
	}
	return dspuo
}

// SetAppID sets the "app_id" field.
func (dspuo *DocServerProjectUpdateOne) SetAppID(i int32) *DocServerProjectUpdateOne {
	dspuo.mutation.ResetAppID()
	dspuo.mutation.SetAppID(i)
	return dspuo
}

// SetNillableAppID sets the "app_id" field if the given value is not nil.
func (dspuo *DocServerProjectUpdateOne) SetNillableAppID(i *int32) *DocServerProjectUpdateOne {
	if i != nil {
		dspuo.SetAppID(*i)
	}
	return dspuo
}

// AddAppID adds i to the "app_id" field.
func (dspuo *DocServerProjectUpdateOne) AddAppID(i int32) *DocServerProjectUpdateOne {
	dspuo.mutation.AddAppID(i)
	return dspuo
}

// SetStatus sets the "status" field.
func (dspuo *DocServerProjectUpdateOne) SetStatus(i int32) *DocServerProjectUpdateOne {
	dspuo.mutation.ResetStatus()
	dspuo.mutation.SetStatus(i)
	return dspuo
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (dspuo *DocServerProjectUpdateOne) SetNillableStatus(i *int32) *DocServerProjectUpdateOne {
	if i != nil {
		dspuo.SetStatus(*i)
	}
	return dspuo
}

// AddStatus adds i to the "status" field.
func (dspuo *DocServerProjectUpdateOne) AddStatus(i int32) *DocServerProjectUpdateOne {
	dspuo.mutation.AddStatus(i)
	return dspuo
}

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

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (dspuo *DocServerProjectUpdateOne) SetNillableCreatedAt(t *time.Time) *DocServerProjectUpdateOne {
	if t != nil {
		dspuo.SetCreatedAt(*t)
	}
	return dspuo
}

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

// Mutation returns the DocServerProjectMutation object of the builder.
func (dspuo *DocServerProjectUpdateOne) Mutation() *DocServerProjectMutation {
	return dspuo.mutation
}

// Where appends a list predicates to the DocServerProjectUpdate builder.
func (dspuo *DocServerProjectUpdateOne) Where(ps ...predicate.DocServerProject) *DocServerProjectUpdateOne {
	dspuo.mutation.Where(ps...)
	return dspuo
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (dspuo *DocServerProjectUpdateOne) check() error {
	if v, ok := dspuo.mutation.SourceURL(); ok {
		if err := docserverproject.SourceURLValidator(v); err != nil {
			return &ValidationError{Name: "source_url", err: fmt.Errorf(`serversitemicros: validator failed for field "DocServerProject.source_url": %w`, err)}
		}
	}
	if v, ok := dspuo.mutation.TargetURL(); ok {
		if err := docserverproject.TargetURLValidator(v); err != nil {
			return &ValidationError{Name: "target_url", err: fmt.Errorf(`serversitemicros: validator failed for field "DocServerProject.target_url": %w`, err)}
		}
	}
	return nil
}

func (dspuo *DocServerProjectUpdateOne) sqlSave(ctx context.Context) (_node *DocServerProject, err error) {
	if err := dspuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(docserverproject.Table, docserverproject.Columns, sqlgraph.NewFieldSpec(docserverproject.FieldID, field.TypeInt32))
	id, ok := dspuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`serversitemicros: missing "DocServerProject.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := dspuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, docserverproject.FieldID)
		for _, f := range fields {
			if !docserverproject.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("serversitemicros: invalid field %q for query", f)}
			}
			if f != docserverproject.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := dspuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := dspuo.mutation.ProjectName(); ok {
		_spec.SetField(docserverproject.FieldProjectName, field.TypeString, value)
	}
	if value, ok := dspuo.mutation.SourceType(); ok {
		_spec.SetField(docserverproject.FieldSourceType, field.TypeString, value)
	}
	if value, ok := dspuo.mutation.TargetType(); ok {
		_spec.SetField(docserverproject.FieldTargetType, field.TypeString, value)
	}
	if value, ok := dspuo.mutation.SourceURL(); ok {
		_spec.SetField(docserverproject.FieldSourceURL, field.TypeString, value)
	}
	if dspuo.mutation.SourceURLCleared() {
		_spec.ClearField(docserverproject.FieldSourceURL, field.TypeString)
	}
	if value, ok := dspuo.mutation.TargetURL(); ok {
		_spec.SetField(docserverproject.FieldTargetURL, field.TypeString, value)
	}
	if dspuo.mutation.TargetURLCleared() {
		_spec.ClearField(docserverproject.FieldTargetURL, field.TypeString)
	}
	if value, ok := dspuo.mutation.TaskID(); ok {
		_spec.SetField(docserverproject.FieldTaskID, field.TypeString, value)
	}
	if value, ok := dspuo.mutation.AppID(); ok {
		_spec.SetField(docserverproject.FieldAppID, field.TypeInt32, value)
	}
	if value, ok := dspuo.mutation.AddedAppID(); ok {
		_spec.AddField(docserverproject.FieldAppID, field.TypeInt32, value)
	}
	if value, ok := dspuo.mutation.Status(); ok {
		_spec.SetField(docserverproject.FieldStatus, field.TypeInt32, value)
	}
	if value, ok := dspuo.mutation.AddedStatus(); ok {
		_spec.AddField(docserverproject.FieldStatus, field.TypeInt32, value)
	}
	if value, ok := dspuo.mutation.CreatedAt(); ok {
		_spec.SetField(docserverproject.FieldCreatedAt, field.TypeTime, value)
	}
	if dspuo.mutation.CreatedAtCleared() {
		_spec.ClearField(docserverproject.FieldCreatedAt, field.TypeTime)
	}
	_node = &DocServerProject{config: dspuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, dspuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{docserverproject.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	dspuo.mutation.done = true
	return _node, nil
}
