// 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/blob"
	"github.com/buildbarn/bb-portal/ent/gen/ent/predicate"
)

// BlobUpdate is the builder for updating Blob entities.
type BlobUpdate struct {
	config
	hooks    []Hook
	mutation *BlobMutation
}

// Where appends a list predicates to the BlobUpdate builder.
func (bu *BlobUpdate) Where(ps ...predicate.Blob) *BlobUpdate {
	bu.mutation.Where(ps...)
	return bu
}

// SetSizeBytes sets the "size_bytes" field.
func (bu *BlobUpdate) SetSizeBytes(i int64) *BlobUpdate {
	bu.mutation.ResetSizeBytes()
	bu.mutation.SetSizeBytes(i)
	return bu
}

// SetNillableSizeBytes sets the "size_bytes" field if the given value is not nil.
func (bu *BlobUpdate) SetNillableSizeBytes(i *int64) *BlobUpdate {
	if i != nil {
		bu.SetSizeBytes(*i)
	}
	return bu
}

// AddSizeBytes adds i to the "size_bytes" field.
func (bu *BlobUpdate) AddSizeBytes(i int64) *BlobUpdate {
	bu.mutation.AddSizeBytes(i)
	return bu
}

// ClearSizeBytes clears the value of the "size_bytes" field.
func (bu *BlobUpdate) ClearSizeBytes() *BlobUpdate {
	bu.mutation.ClearSizeBytes()
	return bu
}

// SetArchivingStatus sets the "archiving_status" field.
func (bu *BlobUpdate) SetArchivingStatus(bs blob.ArchivingStatus) *BlobUpdate {
	bu.mutation.SetArchivingStatus(bs)
	return bu
}

// SetNillableArchivingStatus sets the "archiving_status" field if the given value is not nil.
func (bu *BlobUpdate) SetNillableArchivingStatus(bs *blob.ArchivingStatus) *BlobUpdate {
	if bs != nil {
		bu.SetArchivingStatus(*bs)
	}
	return bu
}

// SetReason sets the "reason" field.
func (bu *BlobUpdate) SetReason(s string) *BlobUpdate {
	bu.mutation.SetReason(s)
	return bu
}

// SetNillableReason sets the "reason" field if the given value is not nil.
func (bu *BlobUpdate) SetNillableReason(s *string) *BlobUpdate {
	if s != nil {
		bu.SetReason(*s)
	}
	return bu
}

// ClearReason clears the value of the "reason" field.
func (bu *BlobUpdate) ClearReason() *BlobUpdate {
	bu.mutation.ClearReason()
	return bu
}

// SetArchiveURL sets the "archive_url" field.
func (bu *BlobUpdate) SetArchiveURL(s string) *BlobUpdate {
	bu.mutation.SetArchiveURL(s)
	return bu
}

// SetNillableArchiveURL sets the "archive_url" field if the given value is not nil.
func (bu *BlobUpdate) SetNillableArchiveURL(s *string) *BlobUpdate {
	if s != nil {
		bu.SetArchiveURL(*s)
	}
	return bu
}

// ClearArchiveURL clears the value of the "archive_url" field.
func (bu *BlobUpdate) ClearArchiveURL() *BlobUpdate {
	bu.mutation.ClearArchiveURL()
	return bu
}

// SetInstanceName sets the "instance_name" field.
func (bu *BlobUpdate) SetInstanceName(s string) *BlobUpdate {
	bu.mutation.SetInstanceName(s)
	return bu
}

// SetNillableInstanceName sets the "instance_name" field if the given value is not nil.
func (bu *BlobUpdate) SetNillableInstanceName(s *string) *BlobUpdate {
	if s != nil {
		bu.SetInstanceName(*s)
	}
	return bu
}

// Mutation returns the BlobMutation object of the builder.
func (bu *BlobUpdate) Mutation() *BlobMutation {
	return bu.mutation
}

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (bu *BlobUpdate) check() error {
	if v, ok := bu.mutation.ArchivingStatus(); ok {
		if err := blob.ArchivingStatusValidator(v); err != nil {
			return &ValidationError{Name: "archiving_status", err: fmt.Errorf(`ent: validator failed for field "Blob.archiving_status": %w`, err)}
		}
	}
	return nil
}

func (bu *BlobUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := bu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(blob.Table, blob.Columns, sqlgraph.NewFieldSpec(blob.FieldID, field.TypeInt))
	if ps := bu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := bu.mutation.SizeBytes(); ok {
		_spec.SetField(blob.FieldSizeBytes, field.TypeInt64, value)
	}
	if value, ok := bu.mutation.AddedSizeBytes(); ok {
		_spec.AddField(blob.FieldSizeBytes, field.TypeInt64, value)
	}
	if bu.mutation.SizeBytesCleared() {
		_spec.ClearField(blob.FieldSizeBytes, field.TypeInt64)
	}
	if value, ok := bu.mutation.ArchivingStatus(); ok {
		_spec.SetField(blob.FieldArchivingStatus, field.TypeEnum, value)
	}
	if value, ok := bu.mutation.Reason(); ok {
		_spec.SetField(blob.FieldReason, field.TypeString, value)
	}
	if bu.mutation.ReasonCleared() {
		_spec.ClearField(blob.FieldReason, field.TypeString)
	}
	if value, ok := bu.mutation.ArchiveURL(); ok {
		_spec.SetField(blob.FieldArchiveURL, field.TypeString, value)
	}
	if bu.mutation.ArchiveURLCleared() {
		_spec.ClearField(blob.FieldArchiveURL, field.TypeString)
	}
	if value, ok := bu.mutation.InstanceName(); ok {
		_spec.SetField(blob.FieldInstanceName, field.TypeString, value)
	}
	if n, err = sqlgraph.UpdateNodes(ctx, bu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{blob.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	bu.mutation.done = true
	return n, nil
}

// BlobUpdateOne is the builder for updating a single Blob entity.
type BlobUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *BlobMutation
}

// SetSizeBytes sets the "size_bytes" field.
func (buo *BlobUpdateOne) SetSizeBytes(i int64) *BlobUpdateOne {
	buo.mutation.ResetSizeBytes()
	buo.mutation.SetSizeBytes(i)
	return buo
}

// SetNillableSizeBytes sets the "size_bytes" field if the given value is not nil.
func (buo *BlobUpdateOne) SetNillableSizeBytes(i *int64) *BlobUpdateOne {
	if i != nil {
		buo.SetSizeBytes(*i)
	}
	return buo
}

// AddSizeBytes adds i to the "size_bytes" field.
func (buo *BlobUpdateOne) AddSizeBytes(i int64) *BlobUpdateOne {
	buo.mutation.AddSizeBytes(i)
	return buo
}

// ClearSizeBytes clears the value of the "size_bytes" field.
func (buo *BlobUpdateOne) ClearSizeBytes() *BlobUpdateOne {
	buo.mutation.ClearSizeBytes()
	return buo
}

// SetArchivingStatus sets the "archiving_status" field.
func (buo *BlobUpdateOne) SetArchivingStatus(bs blob.ArchivingStatus) *BlobUpdateOne {
	buo.mutation.SetArchivingStatus(bs)
	return buo
}

// SetNillableArchivingStatus sets the "archiving_status" field if the given value is not nil.
func (buo *BlobUpdateOne) SetNillableArchivingStatus(bs *blob.ArchivingStatus) *BlobUpdateOne {
	if bs != nil {
		buo.SetArchivingStatus(*bs)
	}
	return buo
}

// SetReason sets the "reason" field.
func (buo *BlobUpdateOne) SetReason(s string) *BlobUpdateOne {
	buo.mutation.SetReason(s)
	return buo
}

// SetNillableReason sets the "reason" field if the given value is not nil.
func (buo *BlobUpdateOne) SetNillableReason(s *string) *BlobUpdateOne {
	if s != nil {
		buo.SetReason(*s)
	}
	return buo
}

// ClearReason clears the value of the "reason" field.
func (buo *BlobUpdateOne) ClearReason() *BlobUpdateOne {
	buo.mutation.ClearReason()
	return buo
}

// SetArchiveURL sets the "archive_url" field.
func (buo *BlobUpdateOne) SetArchiveURL(s string) *BlobUpdateOne {
	buo.mutation.SetArchiveURL(s)
	return buo
}

// SetNillableArchiveURL sets the "archive_url" field if the given value is not nil.
func (buo *BlobUpdateOne) SetNillableArchiveURL(s *string) *BlobUpdateOne {
	if s != nil {
		buo.SetArchiveURL(*s)
	}
	return buo
}

// ClearArchiveURL clears the value of the "archive_url" field.
func (buo *BlobUpdateOne) ClearArchiveURL() *BlobUpdateOne {
	buo.mutation.ClearArchiveURL()
	return buo
}

// SetInstanceName sets the "instance_name" field.
func (buo *BlobUpdateOne) SetInstanceName(s string) *BlobUpdateOne {
	buo.mutation.SetInstanceName(s)
	return buo
}

// SetNillableInstanceName sets the "instance_name" field if the given value is not nil.
func (buo *BlobUpdateOne) SetNillableInstanceName(s *string) *BlobUpdateOne {
	if s != nil {
		buo.SetInstanceName(*s)
	}
	return buo
}

// Mutation returns the BlobMutation object of the builder.
func (buo *BlobUpdateOne) Mutation() *BlobMutation {
	return buo.mutation
}

// Where appends a list predicates to the BlobUpdate builder.
func (buo *BlobUpdateOne) Where(ps ...predicate.Blob) *BlobUpdateOne {
	buo.mutation.Where(ps...)
	return buo
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (buo *BlobUpdateOne) check() error {
	if v, ok := buo.mutation.ArchivingStatus(); ok {
		if err := blob.ArchivingStatusValidator(v); err != nil {
			return &ValidationError{Name: "archiving_status", err: fmt.Errorf(`ent: validator failed for field "Blob.archiving_status": %w`, err)}
		}
	}
	return nil
}

func (buo *BlobUpdateOne) sqlSave(ctx context.Context) (_node *Blob, err error) {
	if err := buo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(blob.Table, blob.Columns, sqlgraph.NewFieldSpec(blob.FieldID, field.TypeInt))
	id, ok := buo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Blob.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := buo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, blob.FieldID)
		for _, f := range fields {
			if !blob.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != blob.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := buo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := buo.mutation.SizeBytes(); ok {
		_spec.SetField(blob.FieldSizeBytes, field.TypeInt64, value)
	}
	if value, ok := buo.mutation.AddedSizeBytes(); ok {
		_spec.AddField(blob.FieldSizeBytes, field.TypeInt64, value)
	}
	if buo.mutation.SizeBytesCleared() {
		_spec.ClearField(blob.FieldSizeBytes, field.TypeInt64)
	}
	if value, ok := buo.mutation.ArchivingStatus(); ok {
		_spec.SetField(blob.FieldArchivingStatus, field.TypeEnum, value)
	}
	if value, ok := buo.mutation.Reason(); ok {
		_spec.SetField(blob.FieldReason, field.TypeString, value)
	}
	if buo.mutation.ReasonCleared() {
		_spec.ClearField(blob.FieldReason, field.TypeString)
	}
	if value, ok := buo.mutation.ArchiveURL(); ok {
		_spec.SetField(blob.FieldArchiveURL, field.TypeString, value)
	}
	if buo.mutation.ArchiveURLCleared() {
		_spec.ClearField(blob.FieldArchiveURL, field.TypeString)
	}
	if value, ok := buo.mutation.InstanceName(); ok {
		_spec.SetField(blob.FieldInstanceName, field.TypeString, value)
	}
	_node = &Blob{config: buo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, buo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{blob.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	buo.mutation.done = true
	return _node, nil
}
