// 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"
)

// LogentryUpdate is the builder for updating Logentry entities.
type LogentryUpdate struct {
	config
	hooks    []Hook
	mutation *LogentryMutation
}

// Where appends a list predicates to the LogentryUpdate builder.
func (lu *LogentryUpdate) Where(ps ...predicate.Logentry) *LogentryUpdate {
	lu.mutation.Where(ps...)
	return lu
}

// SetContent sets the "content" field.
func (lu *LogentryUpdate) SetContent(s string) *LogentryUpdate {
	lu.mutation.SetContent(s)
	return lu
}

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

// SetProjectID sets the "project" edge to the Project entity by ID.
func (lu *LogentryUpdate) SetProjectID(id int) *LogentryUpdate {
	lu.mutation.SetProjectID(id)
	return lu
}

// SetNillableProjectID sets the "project" edge to the Project entity by ID if the given value is not nil.
func (lu *LogentryUpdate) SetNillableProjectID(id *int) *LogentryUpdate {
	if id != nil {
		lu = lu.SetProjectID(*id)
	}
	return lu
}

// SetProject sets the "project" edge to the Project entity.
func (lu *LogentryUpdate) SetProject(p *Project) *LogentryUpdate {
	return lu.SetProjectID(p.ID)
}

// Mutation returns the LogentryMutation object of the builder.
func (lu *LogentryUpdate) Mutation() *LogentryMutation {
	return lu.mutation
}

// ClearProject clears the "project" edge to the Project entity.
func (lu *LogentryUpdate) ClearProject() *LogentryUpdate {
	lu.mutation.ClearProject()
	return lu
}

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

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

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

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

func (lu *LogentryUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(logentry.Table, logentry.Columns, sqlgraph.NewFieldSpec(logentry.FieldID, field.TypeInt))
	if ps := lu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := lu.mutation.Content(); ok {
		_spec.SetField(logentry.FieldContent, field.TypeString, value)
	}
	if lu.mutation.ProjectCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   logentry.ProjectTable,
			Columns: []string{logentry.ProjectColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(project.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := lu.mutation.ProjectIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   logentry.ProjectTable,
			Columns: []string{logentry.ProjectColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(project.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, lu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{logentry.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	lu.mutation.done = true
	return n, nil
}

// LogentryUpdateOne is the builder for updating a single Logentry entity.
type LogentryUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *LogentryMutation
}

// SetContent sets the "content" field.
func (luo *LogentryUpdateOne) SetContent(s string) *LogentryUpdateOne {
	luo.mutation.SetContent(s)
	return luo
}

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

// SetProjectID sets the "project" edge to the Project entity by ID.
func (luo *LogentryUpdateOne) SetProjectID(id int) *LogentryUpdateOne {
	luo.mutation.SetProjectID(id)
	return luo
}

// SetNillableProjectID sets the "project" edge to the Project entity by ID if the given value is not nil.
func (luo *LogentryUpdateOne) SetNillableProjectID(id *int) *LogentryUpdateOne {
	if id != nil {
		luo = luo.SetProjectID(*id)
	}
	return luo
}

// SetProject sets the "project" edge to the Project entity.
func (luo *LogentryUpdateOne) SetProject(p *Project) *LogentryUpdateOne {
	return luo.SetProjectID(p.ID)
}

// Mutation returns the LogentryMutation object of the builder.
func (luo *LogentryUpdateOne) Mutation() *LogentryMutation {
	return luo.mutation
}

// ClearProject clears the "project" edge to the Project entity.
func (luo *LogentryUpdateOne) ClearProject() *LogentryUpdateOne {
	luo.mutation.ClearProject()
	return luo
}

// Where appends a list predicates to the LogentryUpdate builder.
func (luo *LogentryUpdateOne) Where(ps ...predicate.Logentry) *LogentryUpdateOne {
	luo.mutation.Where(ps...)
	return luo
}

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

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

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

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

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

func (luo *LogentryUpdateOne) sqlSave(ctx context.Context) (_node *Logentry, err error) {
	_spec := sqlgraph.NewUpdateSpec(logentry.Table, logentry.Columns, sqlgraph.NewFieldSpec(logentry.FieldID, field.TypeInt))
	id, ok := luo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Logentry.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := luo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, logentry.FieldID)
		for _, f := range fields {
			if !logentry.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != logentry.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := luo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := luo.mutation.Content(); ok {
		_spec.SetField(logentry.FieldContent, field.TypeString, value)
	}
	if luo.mutation.ProjectCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   logentry.ProjectTable,
			Columns: []string{logentry.ProjectColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(project.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := luo.mutation.ProjectIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   logentry.ProjectTable,
			Columns: []string{logentry.ProjectColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(project.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Logentry{config: luo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, luo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{logentry.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	luo.mutation.done = true
	return _node, nil
}
