// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"

	"code.icod.de/dalu/gomanager/ent/logentry"
	"code.icod.de/dalu/gomanager/ent/predicate"
	"code.icod.de/dalu/gomanager/ent/project"
	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// ProjectUpdate is the builder for updating Project entities.
type ProjectUpdate struct {
	config
	hooks    []Hook
	mutation *ProjectMutation
}

// Where appends a list predicates to the ProjectUpdate builder.
func (pu *ProjectUpdate) Where(ps ...predicate.Project) *ProjectUpdate {
	pu.mutation.Where(ps...)
	return pu
}

// SetUser sets the "user" field.
func (pu *ProjectUpdate) SetUser(s string) *ProjectUpdate {
	pu.mutation.SetUser(s)
	return pu
}

// SetNillableUser sets the "user" field if the given value is not nil.
func (pu *ProjectUpdate) SetNillableUser(s *string) *ProjectUpdate {
	if s != nil {
		pu.SetUser(*s)
	}
	return pu
}

// SetGroup sets the "group" field.
func (pu *ProjectUpdate) SetGroup(s string) *ProjectUpdate {
	pu.mutation.SetGroup(s)
	return pu
}

// SetNillableGroup sets the "group" field if the given value is not nil.
func (pu *ProjectUpdate) SetNillableGroup(s *string) *ProjectUpdate {
	if s != nil {
		pu.SetGroup(*s)
	}
	return pu
}

// SetRootPath sets the "root_path" field.
func (pu *ProjectUpdate) SetRootPath(s string) *ProjectUpdate {
	pu.mutation.SetRootPath(s)
	return pu
}

// SetNillableRootPath sets the "root_path" field if the given value is not nil.
func (pu *ProjectUpdate) SetNillableRootPath(s *string) *ProjectUpdate {
	if s != nil {
		pu.SetRootPath(*s)
	}
	return pu
}

// SetServiceName sets the "service_name" field.
func (pu *ProjectUpdate) SetServiceName(s string) *ProjectUpdate {
	pu.mutation.SetServiceName(s)
	return pu
}

// SetNillableServiceName sets the "service_name" field if the given value is not nil.
func (pu *ProjectUpdate) SetNillableServiceName(s *string) *ProjectUpdate {
	if s != nil {
		pu.SetServiceName(*s)
	}
	return pu
}

// SetBinaryPath sets the "binary_path" field.
func (pu *ProjectUpdate) SetBinaryPath(s string) *ProjectUpdate {
	pu.mutation.SetBinaryPath(s)
	return pu
}

// SetNillableBinaryPath sets the "binary_path" field if the given value is not nil.
func (pu *ProjectUpdate) SetNillableBinaryPath(s *string) *ProjectUpdate {
	if s != nil {
		pu.SetBinaryPath(*s)
	}
	return pu
}

// SetMoveToTarget sets the "move_to_target" field.
func (pu *ProjectUpdate) SetMoveToTarget(b bool) *ProjectUpdate {
	pu.mutation.SetMoveToTarget(b)
	return pu
}

// SetNillableMoveToTarget sets the "move_to_target" field if the given value is not nil.
func (pu *ProjectUpdate) SetNillableMoveToTarget(b *bool) *ProjectUpdate {
	if b != nil {
		pu.SetMoveToTarget(*b)
	}
	return pu
}

// SetBinaryTargetPath sets the "binary_target_path" field.
func (pu *ProjectUpdate) SetBinaryTargetPath(s string) *ProjectUpdate {
	pu.mutation.SetBinaryTargetPath(s)
	return pu
}

// SetNillableBinaryTargetPath sets the "binary_target_path" field if the given value is not nil.
func (pu *ProjectUpdate) SetNillableBinaryTargetPath(s *string) *ProjectUpdate {
	if s != nil {
		pu.SetBinaryTargetPath(*s)
	}
	return pu
}

// ClearBinaryTargetPath clears the value of the "binary_target_path" field.
func (pu *ProjectUpdate) ClearBinaryTargetPath() *ProjectUpdate {
	pu.mutation.ClearBinaryTargetPath()
	return pu
}

// AddLogentryIDs adds the "logentries" edge to the Logentry entity by IDs.
func (pu *ProjectUpdate) AddLogentryIDs(ids ...int) *ProjectUpdate {
	pu.mutation.AddLogentryIDs(ids...)
	return pu
}

// AddLogentries adds the "logentries" edges to the Logentry entity.
func (pu *ProjectUpdate) AddLogentries(l ...*Logentry) *ProjectUpdate {
	ids := make([]int, len(l))
	for i := range l {
		ids[i] = l[i].ID
	}
	return pu.AddLogentryIDs(ids...)
}

// Mutation returns the ProjectMutation object of the builder.
func (pu *ProjectUpdate) Mutation() *ProjectMutation {
	return pu.mutation
}

// ClearLogentries clears all "logentries" edges to the Logentry entity.
func (pu *ProjectUpdate) ClearLogentries() *ProjectUpdate {
	pu.mutation.ClearLogentries()
	return pu
}

// RemoveLogentryIDs removes the "logentries" edge to Logentry entities by IDs.
func (pu *ProjectUpdate) RemoveLogentryIDs(ids ...int) *ProjectUpdate {
	pu.mutation.RemoveLogentryIDs(ids...)
	return pu
}

// RemoveLogentries removes "logentries" edges to Logentry entities.
func (pu *ProjectUpdate) RemoveLogentries(l ...*Logentry) *ProjectUpdate {
	ids := make([]int, len(l))
	for i := range l {
		ids[i] = l[i].ID
	}
	return pu.RemoveLogentryIDs(ids...)
}

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

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

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

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

func (pu *ProjectUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(project.Table, project.Columns, sqlgraph.NewFieldSpec(project.FieldID, field.TypeInt))
	if ps := pu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := pu.mutation.User(); ok {
		_spec.SetField(project.FieldUser, field.TypeString, value)
	}
	if value, ok := pu.mutation.Group(); ok {
		_spec.SetField(project.FieldGroup, field.TypeString, value)
	}
	if value, ok := pu.mutation.RootPath(); ok {
		_spec.SetField(project.FieldRootPath, field.TypeString, value)
	}
	if value, ok := pu.mutation.ServiceName(); ok {
		_spec.SetField(project.FieldServiceName, field.TypeString, value)
	}
	if value, ok := pu.mutation.BinaryPath(); ok {
		_spec.SetField(project.FieldBinaryPath, field.TypeString, value)
	}
	if value, ok := pu.mutation.MoveToTarget(); ok {
		_spec.SetField(project.FieldMoveToTarget, field.TypeBool, value)
	}
	if value, ok := pu.mutation.BinaryTargetPath(); ok {
		_spec.SetField(project.FieldBinaryTargetPath, field.TypeString, value)
	}
	if pu.mutation.BinaryTargetPathCleared() {
		_spec.ClearField(project.FieldBinaryTargetPath, field.TypeString)
	}
	if pu.mutation.LogentriesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   project.LogentriesTable,
			Columns: []string{project.LogentriesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(logentry.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := pu.mutation.RemovedLogentriesIDs(); len(nodes) > 0 && !pu.mutation.LogentriesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   project.LogentriesTable,
			Columns: []string{project.LogentriesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(logentry.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := pu.mutation.LogentriesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   project.LogentriesTable,
			Columns: []string{project.LogentriesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(logentry.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, pu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{project.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	pu.mutation.done = true
	return n, nil
}

// ProjectUpdateOne is the builder for updating a single Project entity.
type ProjectUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ProjectMutation
}

// SetUser sets the "user" field.
func (puo *ProjectUpdateOne) SetUser(s string) *ProjectUpdateOne {
	puo.mutation.SetUser(s)
	return puo
}

// SetNillableUser sets the "user" field if the given value is not nil.
func (puo *ProjectUpdateOne) SetNillableUser(s *string) *ProjectUpdateOne {
	if s != nil {
		puo.SetUser(*s)
	}
	return puo
}

// SetGroup sets the "group" field.
func (puo *ProjectUpdateOne) SetGroup(s string) *ProjectUpdateOne {
	puo.mutation.SetGroup(s)
	return puo
}

// SetNillableGroup sets the "group" field if the given value is not nil.
func (puo *ProjectUpdateOne) SetNillableGroup(s *string) *ProjectUpdateOne {
	if s != nil {
		puo.SetGroup(*s)
	}
	return puo
}

// SetRootPath sets the "root_path" field.
func (puo *ProjectUpdateOne) SetRootPath(s string) *ProjectUpdateOne {
	puo.mutation.SetRootPath(s)
	return puo
}

// SetNillableRootPath sets the "root_path" field if the given value is not nil.
func (puo *ProjectUpdateOne) SetNillableRootPath(s *string) *ProjectUpdateOne {
	if s != nil {
		puo.SetRootPath(*s)
	}
	return puo
}

// SetServiceName sets the "service_name" field.
func (puo *ProjectUpdateOne) SetServiceName(s string) *ProjectUpdateOne {
	puo.mutation.SetServiceName(s)
	return puo
}

// SetNillableServiceName sets the "service_name" field if the given value is not nil.
func (puo *ProjectUpdateOne) SetNillableServiceName(s *string) *ProjectUpdateOne {
	if s != nil {
		puo.SetServiceName(*s)
	}
	return puo
}

// SetBinaryPath sets the "binary_path" field.
func (puo *ProjectUpdateOne) SetBinaryPath(s string) *ProjectUpdateOne {
	puo.mutation.SetBinaryPath(s)
	return puo
}

// SetNillableBinaryPath sets the "binary_path" field if the given value is not nil.
func (puo *ProjectUpdateOne) SetNillableBinaryPath(s *string) *ProjectUpdateOne {
	if s != nil {
		puo.SetBinaryPath(*s)
	}
	return puo
}

// SetMoveToTarget sets the "move_to_target" field.
func (puo *ProjectUpdateOne) SetMoveToTarget(b bool) *ProjectUpdateOne {
	puo.mutation.SetMoveToTarget(b)
	return puo
}

// SetNillableMoveToTarget sets the "move_to_target" field if the given value is not nil.
func (puo *ProjectUpdateOne) SetNillableMoveToTarget(b *bool) *ProjectUpdateOne {
	if b != nil {
		puo.SetMoveToTarget(*b)
	}
	return puo
}

// SetBinaryTargetPath sets the "binary_target_path" field.
func (puo *ProjectUpdateOne) SetBinaryTargetPath(s string) *ProjectUpdateOne {
	puo.mutation.SetBinaryTargetPath(s)
	return puo
}

// SetNillableBinaryTargetPath sets the "binary_target_path" field if the given value is not nil.
func (puo *ProjectUpdateOne) SetNillableBinaryTargetPath(s *string) *ProjectUpdateOne {
	if s != nil {
		puo.SetBinaryTargetPath(*s)
	}
	return puo
}

// ClearBinaryTargetPath clears the value of the "binary_target_path" field.
func (puo *ProjectUpdateOne) ClearBinaryTargetPath() *ProjectUpdateOne {
	puo.mutation.ClearBinaryTargetPath()
	return puo
}

// AddLogentryIDs adds the "logentries" edge to the Logentry entity by IDs.
func (puo *ProjectUpdateOne) AddLogentryIDs(ids ...int) *ProjectUpdateOne {
	puo.mutation.AddLogentryIDs(ids...)
	return puo
}

// AddLogentries adds the "logentries" edges to the Logentry entity.
func (puo *ProjectUpdateOne) AddLogentries(l ...*Logentry) *ProjectUpdateOne {
	ids := make([]int, len(l))
	for i := range l {
		ids[i] = l[i].ID
	}
	return puo.AddLogentryIDs(ids...)
}

// Mutation returns the ProjectMutation object of the builder.
func (puo *ProjectUpdateOne) Mutation() *ProjectMutation {
	return puo.mutation
}

// ClearLogentries clears all "logentries" edges to the Logentry entity.
func (puo *ProjectUpdateOne) ClearLogentries() *ProjectUpdateOne {
	puo.mutation.ClearLogentries()
	return puo
}

// RemoveLogentryIDs removes the "logentries" edge to Logentry entities by IDs.
func (puo *ProjectUpdateOne) RemoveLogentryIDs(ids ...int) *ProjectUpdateOne {
	puo.mutation.RemoveLogentryIDs(ids...)
	return puo
}

// RemoveLogentries removes "logentries" edges to Logentry entities.
func (puo *ProjectUpdateOne) RemoveLogentries(l ...*Logentry) *ProjectUpdateOne {
	ids := make([]int, len(l))
	for i := range l {
		ids[i] = l[i].ID
	}
	return puo.RemoveLogentryIDs(ids...)
}

// Where appends a list predicates to the ProjectUpdate builder.
func (puo *ProjectUpdateOne) Where(ps ...predicate.Project) *ProjectUpdateOne {
	puo.mutation.Where(ps...)
	return puo
}

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

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

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

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

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

func (puo *ProjectUpdateOne) sqlSave(ctx context.Context) (_node *Project, err error) {
	_spec := sqlgraph.NewUpdateSpec(project.Table, project.Columns, sqlgraph.NewFieldSpec(project.FieldID, field.TypeInt))
	id, ok := puo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Project.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := puo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, project.FieldID)
		for _, f := range fields {
			if !project.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != project.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := puo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := puo.mutation.User(); ok {
		_spec.SetField(project.FieldUser, field.TypeString, value)
	}
	if value, ok := puo.mutation.Group(); ok {
		_spec.SetField(project.FieldGroup, field.TypeString, value)
	}
	if value, ok := puo.mutation.RootPath(); ok {
		_spec.SetField(project.FieldRootPath, field.TypeString, value)
	}
	if value, ok := puo.mutation.ServiceName(); ok {
		_spec.SetField(project.FieldServiceName, field.TypeString, value)
	}
	if value, ok := puo.mutation.BinaryPath(); ok {
		_spec.SetField(project.FieldBinaryPath, field.TypeString, value)
	}
	if value, ok := puo.mutation.MoveToTarget(); ok {
		_spec.SetField(project.FieldMoveToTarget, field.TypeBool, value)
	}
	if value, ok := puo.mutation.BinaryTargetPath(); ok {
		_spec.SetField(project.FieldBinaryTargetPath, field.TypeString, value)
	}
	if puo.mutation.BinaryTargetPathCleared() {
		_spec.ClearField(project.FieldBinaryTargetPath, field.TypeString)
	}
	if puo.mutation.LogentriesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   project.LogentriesTable,
			Columns: []string{project.LogentriesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(logentry.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := puo.mutation.RemovedLogentriesIDs(); len(nodes) > 0 && !puo.mutation.LogentriesCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   project.LogentriesTable,
			Columns: []string{project.LogentriesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(logentry.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := puo.mutation.LogentriesIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2M,
			Inverse: false,
			Table:   project.LogentriesTable,
			Columns: []string{project.LogentriesColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(logentry.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Project{config: puo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, puo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{project.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	puo.mutation.done = true
	return _node, nil
}
