// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/buildbarn/bb-portal/ent/gen/ent/artifactmetrics"
	"github.com/buildbarn/bb-portal/ent/gen/ent/filesmetric"
	"github.com/buildbarn/bb-portal/ent/gen/ent/predicate"
)

// FilesMetricUpdate is the builder for updating FilesMetric entities.
type FilesMetricUpdate struct {
	config
	hooks    []Hook
	mutation *FilesMetricMutation
}

// Where appends a list predicates to the FilesMetricUpdate builder.
func (fmu *FilesMetricUpdate) Where(ps ...predicate.FilesMetric) *FilesMetricUpdate {
	fmu.mutation.Where(ps...)
	return fmu
}

// SetSizeInBytes sets the "size_in_bytes" field.
func (fmu *FilesMetricUpdate) SetSizeInBytes(i int64) *FilesMetricUpdate {
	fmu.mutation.ResetSizeInBytes()
	fmu.mutation.SetSizeInBytes(i)
	return fmu
}

// SetNillableSizeInBytes sets the "size_in_bytes" field if the given value is not nil.
func (fmu *FilesMetricUpdate) SetNillableSizeInBytes(i *int64) *FilesMetricUpdate {
	if i != nil {
		fmu.SetSizeInBytes(*i)
	}
	return fmu
}

// AddSizeInBytes adds i to the "size_in_bytes" field.
func (fmu *FilesMetricUpdate) AddSizeInBytes(i int64) *FilesMetricUpdate {
	fmu.mutation.AddSizeInBytes(i)
	return fmu
}

// ClearSizeInBytes clears the value of the "size_in_bytes" field.
func (fmu *FilesMetricUpdate) ClearSizeInBytes() *FilesMetricUpdate {
	fmu.mutation.ClearSizeInBytes()
	return fmu
}

// SetCount sets the "count" field.
func (fmu *FilesMetricUpdate) SetCount(i int32) *FilesMetricUpdate {
	fmu.mutation.ResetCount()
	fmu.mutation.SetCount(i)
	return fmu
}

// SetNillableCount sets the "count" field if the given value is not nil.
func (fmu *FilesMetricUpdate) SetNillableCount(i *int32) *FilesMetricUpdate {
	if i != nil {
		fmu.SetCount(*i)
	}
	return fmu
}

// AddCount adds i to the "count" field.
func (fmu *FilesMetricUpdate) AddCount(i int32) *FilesMetricUpdate {
	fmu.mutation.AddCount(i)
	return fmu
}

// ClearCount clears the value of the "count" field.
func (fmu *FilesMetricUpdate) ClearCount() *FilesMetricUpdate {
	fmu.mutation.ClearCount()
	return fmu
}

// SetArtifactMetricsID sets the "artifact_metrics" edge to the ArtifactMetrics entity by ID.
func (fmu *FilesMetricUpdate) SetArtifactMetricsID(id int) *FilesMetricUpdate {
	fmu.mutation.SetArtifactMetricsID(id)
	return fmu
}

// SetNillableArtifactMetricsID sets the "artifact_metrics" edge to the ArtifactMetrics entity by ID if the given value is not nil.
func (fmu *FilesMetricUpdate) SetNillableArtifactMetricsID(id *int) *FilesMetricUpdate {
	if id != nil {
		fmu = fmu.SetArtifactMetricsID(*id)
	}
	return fmu
}

// SetArtifactMetrics sets the "artifact_metrics" edge to the ArtifactMetrics entity.
func (fmu *FilesMetricUpdate) SetArtifactMetrics(a *ArtifactMetrics) *FilesMetricUpdate {
	return fmu.SetArtifactMetricsID(a.ID)
}

// Mutation returns the FilesMetricMutation object of the builder.
func (fmu *FilesMetricUpdate) Mutation() *FilesMetricMutation {
	return fmu.mutation
}

// ClearArtifactMetrics clears the "artifact_metrics" edge to the ArtifactMetrics entity.
func (fmu *FilesMetricUpdate) ClearArtifactMetrics() *FilesMetricUpdate {
	fmu.mutation.ClearArtifactMetrics()
	return fmu
}

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

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

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

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

func (fmu *FilesMetricUpdate) sqlSave(ctx context.Context) (n int, err error) {
	_spec := sqlgraph.NewUpdateSpec(filesmetric.Table, filesmetric.Columns, sqlgraph.NewFieldSpec(filesmetric.FieldID, field.TypeInt))
	if ps := fmu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := fmu.mutation.SizeInBytes(); ok {
		_spec.SetField(filesmetric.FieldSizeInBytes, field.TypeInt64, value)
	}
	if value, ok := fmu.mutation.AddedSizeInBytes(); ok {
		_spec.AddField(filesmetric.FieldSizeInBytes, field.TypeInt64, value)
	}
	if fmu.mutation.SizeInBytesCleared() {
		_spec.ClearField(filesmetric.FieldSizeInBytes, field.TypeInt64)
	}
	if value, ok := fmu.mutation.Count(); ok {
		_spec.SetField(filesmetric.FieldCount, field.TypeInt32, value)
	}
	if value, ok := fmu.mutation.AddedCount(); ok {
		_spec.AddField(filesmetric.FieldCount, field.TypeInt32, value)
	}
	if fmu.mutation.CountCleared() {
		_spec.ClearField(filesmetric.FieldCount, field.TypeInt32)
	}
	if fmu.mutation.ArtifactMetricsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   filesmetric.ArtifactMetricsTable,
			Columns: []string{filesmetric.ArtifactMetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(artifactmetrics.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := fmu.mutation.ArtifactMetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   filesmetric.ArtifactMetricsTable,
			Columns: []string{filesmetric.ArtifactMetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(artifactmetrics.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, fmu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{filesmetric.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	fmu.mutation.done = true
	return n, nil
}

// FilesMetricUpdateOne is the builder for updating a single FilesMetric entity.
type FilesMetricUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *FilesMetricMutation
}

// SetSizeInBytes sets the "size_in_bytes" field.
func (fmuo *FilesMetricUpdateOne) SetSizeInBytes(i int64) *FilesMetricUpdateOne {
	fmuo.mutation.ResetSizeInBytes()
	fmuo.mutation.SetSizeInBytes(i)
	return fmuo
}

// SetNillableSizeInBytes sets the "size_in_bytes" field if the given value is not nil.
func (fmuo *FilesMetricUpdateOne) SetNillableSizeInBytes(i *int64) *FilesMetricUpdateOne {
	if i != nil {
		fmuo.SetSizeInBytes(*i)
	}
	return fmuo
}

// AddSizeInBytes adds i to the "size_in_bytes" field.
func (fmuo *FilesMetricUpdateOne) AddSizeInBytes(i int64) *FilesMetricUpdateOne {
	fmuo.mutation.AddSizeInBytes(i)
	return fmuo
}

// ClearSizeInBytes clears the value of the "size_in_bytes" field.
func (fmuo *FilesMetricUpdateOne) ClearSizeInBytes() *FilesMetricUpdateOne {
	fmuo.mutation.ClearSizeInBytes()
	return fmuo
}

// SetCount sets the "count" field.
func (fmuo *FilesMetricUpdateOne) SetCount(i int32) *FilesMetricUpdateOne {
	fmuo.mutation.ResetCount()
	fmuo.mutation.SetCount(i)
	return fmuo
}

// SetNillableCount sets the "count" field if the given value is not nil.
func (fmuo *FilesMetricUpdateOne) SetNillableCount(i *int32) *FilesMetricUpdateOne {
	if i != nil {
		fmuo.SetCount(*i)
	}
	return fmuo
}

// AddCount adds i to the "count" field.
func (fmuo *FilesMetricUpdateOne) AddCount(i int32) *FilesMetricUpdateOne {
	fmuo.mutation.AddCount(i)
	return fmuo
}

// ClearCount clears the value of the "count" field.
func (fmuo *FilesMetricUpdateOne) ClearCount() *FilesMetricUpdateOne {
	fmuo.mutation.ClearCount()
	return fmuo
}

// SetArtifactMetricsID sets the "artifact_metrics" edge to the ArtifactMetrics entity by ID.
func (fmuo *FilesMetricUpdateOne) SetArtifactMetricsID(id int) *FilesMetricUpdateOne {
	fmuo.mutation.SetArtifactMetricsID(id)
	return fmuo
}

// SetNillableArtifactMetricsID sets the "artifact_metrics" edge to the ArtifactMetrics entity by ID if the given value is not nil.
func (fmuo *FilesMetricUpdateOne) SetNillableArtifactMetricsID(id *int) *FilesMetricUpdateOne {
	if id != nil {
		fmuo = fmuo.SetArtifactMetricsID(*id)
	}
	return fmuo
}

// SetArtifactMetrics sets the "artifact_metrics" edge to the ArtifactMetrics entity.
func (fmuo *FilesMetricUpdateOne) SetArtifactMetrics(a *ArtifactMetrics) *FilesMetricUpdateOne {
	return fmuo.SetArtifactMetricsID(a.ID)
}

// Mutation returns the FilesMetricMutation object of the builder.
func (fmuo *FilesMetricUpdateOne) Mutation() *FilesMetricMutation {
	return fmuo.mutation
}

// ClearArtifactMetrics clears the "artifact_metrics" edge to the ArtifactMetrics entity.
func (fmuo *FilesMetricUpdateOne) ClearArtifactMetrics() *FilesMetricUpdateOne {
	fmuo.mutation.ClearArtifactMetrics()
	return fmuo
}

// Where appends a list predicates to the FilesMetricUpdate builder.
func (fmuo *FilesMetricUpdateOne) Where(ps ...predicate.FilesMetric) *FilesMetricUpdateOne {
	fmuo.mutation.Where(ps...)
	return fmuo
}

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

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

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

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

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

func (fmuo *FilesMetricUpdateOne) sqlSave(ctx context.Context) (_node *FilesMetric, err error) {
	_spec := sqlgraph.NewUpdateSpec(filesmetric.Table, filesmetric.Columns, sqlgraph.NewFieldSpec(filesmetric.FieldID, field.TypeInt))
	id, ok := fmuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "FilesMetric.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := fmuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, filesmetric.FieldID)
		for _, f := range fields {
			if !filesmetric.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != filesmetric.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := fmuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := fmuo.mutation.SizeInBytes(); ok {
		_spec.SetField(filesmetric.FieldSizeInBytes, field.TypeInt64, value)
	}
	if value, ok := fmuo.mutation.AddedSizeInBytes(); ok {
		_spec.AddField(filesmetric.FieldSizeInBytes, field.TypeInt64, value)
	}
	if fmuo.mutation.SizeInBytesCleared() {
		_spec.ClearField(filesmetric.FieldSizeInBytes, field.TypeInt64)
	}
	if value, ok := fmuo.mutation.Count(); ok {
		_spec.SetField(filesmetric.FieldCount, field.TypeInt32, value)
	}
	if value, ok := fmuo.mutation.AddedCount(); ok {
		_spec.AddField(filesmetric.FieldCount, field.TypeInt32, value)
	}
	if fmuo.mutation.CountCleared() {
		_spec.ClearField(filesmetric.FieldCount, field.TypeInt32)
	}
	if fmuo.mutation.ArtifactMetricsCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   filesmetric.ArtifactMetricsTable,
			Columns: []string{filesmetric.ArtifactMetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(artifactmetrics.FieldID, field.TypeInt),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := fmuo.mutation.ArtifactMetricsIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: true,
			Table:   filesmetric.ArtifactMetricsTable,
			Columns: []string{filesmetric.ArtifactMetricsColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(artifactmetrics.FieldID, field.TypeInt),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &FilesMetric{config: fmuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, fmuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{filesmetric.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	fmuo.mutation.done = true
	return _node, nil
}
