Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

18.5 KB
Raw Blame
// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/google/uuid"
	"github.com/sysadminsmedia/homebox/backend/internal/data/ent/export"
	"github.com/sysadminsmedia/homebox/backend/internal/data/ent/group"
	"github.com/sysadminsmedia/homebox/backend/internal/data/ent/predicate"
)

// ExportUpdate is the builder for updating Export entities.
type ExportUpdate struct {
	config
	hooks    []Hook
	mutation *ExportMutation
}

// Where appends a list predicates to the ExportUpdate builder.
func (_u *ExportUpdate) Where(ps ...predicate.Export) *ExportUpdate {
	_u.mutation.Where(ps...)
	return _u
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *ExportUpdate) SetUpdatedAt(v time.Time) *ExportUpdate {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetGroupID sets the "group_id" field.
func (_u *ExportUpdate) SetGroupID(v uuid.UUID) *ExportUpdate {
	_u.mutation.SetGroupID(v)
	return _u
}

// SetNillableGroupID sets the "group_id" field if the given value is not nil.
func (_u *ExportUpdate) SetNillableGroupID(v *uuid.UUID) *ExportUpdate {
	if v != nil {
		_u.SetGroupID(*v)
	}
	return _u
}

// SetKind sets the "kind" field.
func (_u *ExportUpdate) SetKind(v export.Kind) *ExportUpdate {
	_u.mutation.SetKind(v)
	return _u
}

// SetNillableKind sets the "kind" field if the given value is not nil.
func (_u *ExportUpdate) SetNillableKind(v *export.Kind) *ExportUpdate {
	if v != nil {
		_u.SetKind(*v)
	}
	return _u
}

// SetStatus sets the "status" field.
func (_u *ExportUpdate) SetStatus(v export.Status) *ExportUpdate {
	_u.mutation.SetStatus(v)
	return _u
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_u *ExportUpdate) SetNillableStatus(v *export.Status) *ExportUpdate {
	if v != nil {
		_u.SetStatus(*v)
	}
	return _u
}

// SetProgress sets the "progress" field.
func (_u *ExportUpdate) SetProgress(v int) *ExportUpdate {
	_u.mutation.ResetProgress()
	_u.mutation.SetProgress(v)
	return _u
}

// SetNillableProgress sets the "progress" field if the given value is not nil.
func (_u *ExportUpdate) SetNillableProgress(v *int) *ExportUpdate {
	if v != nil {
		_u.SetProgress(*v)
	}
	return _u
}

// AddProgress adds value to the "progress" field.
func (_u *ExportUpdate) AddProgress(v int) *ExportUpdate {
	_u.mutation.AddProgress(v)
	return _u
}

// SetArtifactPath sets the "artifact_path" field.
func (_u *ExportUpdate) SetArtifactPath(v string) *ExportUpdate {
	_u.mutation.SetArtifactPath(v)
	return _u
}

// SetNillableArtifactPath sets the "artifact_path" field if the given value is not nil.
func (_u *ExportUpdate) SetNillableArtifactPath(v *string) *ExportUpdate {
	if v != nil {
		_u.SetArtifactPath(*v)
	}
	return _u
}

// ClearArtifactPath clears the value of the "artifact_path" field.
func (_u *ExportUpdate) ClearArtifactPath() *ExportUpdate {
	_u.mutation.ClearArtifactPath()
	return _u
}

// SetSizeBytes sets the "size_bytes" field.
func (_u *ExportUpdate) SetSizeBytes(v int64) *ExportUpdate {
	_u.mutation.ResetSizeBytes()
	_u.mutation.SetSizeBytes(v)
	return _u
}

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

// AddSizeBytes adds value to the "size_bytes" field.
func (_u *ExportUpdate) AddSizeBytes(v int64) *ExportUpdate {
	_u.mutation.AddSizeBytes(v)
	return _u
}

// SetError sets the "error" field.
func (_u *ExportUpdate) SetError(v string) *ExportUpdate {
	_u.mutation.SetError(v)
	return _u
}

// SetNillableError sets the "error" field if the given value is not nil.
func (_u *ExportUpdate) SetNillableError(v *string) *ExportUpdate {
	if v != nil {
		_u.SetError(*v)
	}
	return _u
}

// ClearError clears the value of the "error" field.
func (_u *ExportUpdate) ClearError() *ExportUpdate {
	_u.mutation.ClearError()
	return _u
}

// SetGroup sets the "group" edge to the Group entity.
func (_u *ExportUpdate) SetGroup(v *Group) *ExportUpdate {
	return _u.SetGroupID(v.ID)
}

// Mutation returns the ExportMutation object of the builder.
func (_u *ExportUpdate) Mutation() *ExportMutation {
	return _u.mutation
}

// ClearGroup clears the "group" edge to the Group entity.
func (_u *ExportUpdate) ClearGroup() *ExportUpdate {
	_u.mutation.ClearGroup()
	return _u
}

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

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

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

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

// defaults sets the default values of the builder before save.
func (_u *ExportUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := export.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *ExportUpdate) check() error {
	if v, ok := _u.mutation.Kind(); ok {
		if err := export.KindValidator(v); err != nil {
			return &ValidationError{Name: "kind", err: fmt.Errorf(`ent: validator failed for field "Export.kind": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Status(); ok {
		if err := export.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Export.status": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Error(); ok {
		if err := export.ErrorValidator(v); err != nil {
			return &ValidationError{Name: "error", err: fmt.Errorf(`ent: validator failed for field "Export.error": %w`, err)}
		}
	}
	if _u.mutation.GroupCleared() && len(_u.mutation.GroupIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "Export.group"`)
	}
	return nil
}

func (_u *ExportUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(export.Table, export.Columns, sqlgraph.NewFieldSpec(export.FieldID, field.TypeUUID))
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(export.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Kind(); ok {
		_spec.SetField(export.FieldKind, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(export.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.Progress(); ok {
		_spec.SetField(export.FieldProgress, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedProgress(); ok {
		_spec.AddField(export.FieldProgress, field.TypeInt, value)
	}
	if value, ok := _u.mutation.ArtifactPath(); ok {
		_spec.SetField(export.FieldArtifactPath, field.TypeString, value)
	}
	if _u.mutation.ArtifactPathCleared() {
		_spec.ClearField(export.FieldArtifactPath, field.TypeString)
	}
	if value, ok := _u.mutation.SizeBytes(); ok {
		_spec.SetField(export.FieldSizeBytes, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedSizeBytes(); ok {
		_spec.AddField(export.FieldSizeBytes, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.Error(); ok {
		_spec.SetField(export.FieldError, field.TypeString, value)
	}
	if _u.mutation.ErrorCleared() {
		_spec.ClearField(export.FieldError, field.TypeString)
	}
	if _u.mutation.GroupCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   export.GroupTable,
			Columns: []string{export.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.GroupIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   export.GroupTable,
			Columns: []string{export.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{export.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// ExportUpdateOne is the builder for updating a single Export entity.
type ExportUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *ExportMutation
}

// SetUpdatedAt sets the "updated_at" field.
func (_u *ExportUpdateOne) SetUpdatedAt(v time.Time) *ExportUpdateOne {
	_u.mutation.SetUpdatedAt(v)
	return _u
}

// SetGroupID sets the "group_id" field.
func (_u *ExportUpdateOne) SetGroupID(v uuid.UUID) *ExportUpdateOne {
	_u.mutation.SetGroupID(v)
	return _u
}

// SetNillableGroupID sets the "group_id" field if the given value is not nil.
func (_u *ExportUpdateOne) SetNillableGroupID(v *uuid.UUID) *ExportUpdateOne {
	if v != nil {
		_u.SetGroupID(*v)
	}
	return _u
}

// SetKind sets the "kind" field.
func (_u *ExportUpdateOne) SetKind(v export.Kind) *ExportUpdateOne {
	_u.mutation.SetKind(v)
	return _u
}

// SetNillableKind sets the "kind" field if the given value is not nil.
func (_u *ExportUpdateOne) SetNillableKind(v *export.Kind) *ExportUpdateOne {
	if v != nil {
		_u.SetKind(*v)
	}
	return _u
}

// SetStatus sets the "status" field.
func (_u *ExportUpdateOne) SetStatus(v export.Status) *ExportUpdateOne {
	_u.mutation.SetStatus(v)
	return _u
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_u *ExportUpdateOne) SetNillableStatus(v *export.Status) *ExportUpdateOne {
	if v != nil {
		_u.SetStatus(*v)
	}
	return _u
}

// SetProgress sets the "progress" field.
func (_u *ExportUpdateOne) SetProgress(v int) *ExportUpdateOne {
	_u.mutation.ResetProgress()
	_u.mutation.SetProgress(v)
	return _u
}

// SetNillableProgress sets the "progress" field if the given value is not nil.
func (_u *ExportUpdateOne) SetNillableProgress(v *int) *ExportUpdateOne {
	if v != nil {
		_u.SetProgress(*v)
	}
	return _u
}

// AddProgress adds value to the "progress" field.
func (_u *ExportUpdateOne) AddProgress(v int) *ExportUpdateOne {
	_u.mutation.AddProgress(v)
	return _u
}

// SetArtifactPath sets the "artifact_path" field.
func (_u *ExportUpdateOne) SetArtifactPath(v string) *ExportUpdateOne {
	_u.mutation.SetArtifactPath(v)
	return _u
}

// SetNillableArtifactPath sets the "artifact_path" field if the given value is not nil.
func (_u *ExportUpdateOne) SetNillableArtifactPath(v *string) *ExportUpdateOne {
	if v != nil {
		_u.SetArtifactPath(*v)
	}
	return _u
}

// ClearArtifactPath clears the value of the "artifact_path" field.
func (_u *ExportUpdateOne) ClearArtifactPath() *ExportUpdateOne {
	_u.mutation.ClearArtifactPath()
	return _u
}

// SetSizeBytes sets the "size_bytes" field.
func (_u *ExportUpdateOne) SetSizeBytes(v int64) *ExportUpdateOne {
	_u.mutation.ResetSizeBytes()
	_u.mutation.SetSizeBytes(v)
	return _u
}

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

// AddSizeBytes adds value to the "size_bytes" field.
func (_u *ExportUpdateOne) AddSizeBytes(v int64) *ExportUpdateOne {
	_u.mutation.AddSizeBytes(v)
	return _u
}

// SetError sets the "error" field.
func (_u *ExportUpdateOne) SetError(v string) *ExportUpdateOne {
	_u.mutation.SetError(v)
	return _u
}

// SetNillableError sets the "error" field if the given value is not nil.
func (_u *ExportUpdateOne) SetNillableError(v *string) *ExportUpdateOne {
	if v != nil {
		_u.SetError(*v)
	}
	return _u
}

// ClearError clears the value of the "error" field.
func (_u *ExportUpdateOne) ClearError() *ExportUpdateOne {
	_u.mutation.ClearError()
	return _u
}

// SetGroup sets the "group" edge to the Group entity.
func (_u *ExportUpdateOne) SetGroup(v *Group) *ExportUpdateOne {
	return _u.SetGroupID(v.ID)
}

// Mutation returns the ExportMutation object of the builder.
func (_u *ExportUpdateOne) Mutation() *ExportMutation {
	return _u.mutation
}

// ClearGroup clears the "group" edge to the Group entity.
func (_u *ExportUpdateOne) ClearGroup() *ExportUpdateOne {
	_u.mutation.ClearGroup()
	return _u
}

// Where appends a list predicates to the ExportUpdate builder.
func (_u *ExportUpdateOne) Where(ps ...predicate.Export) *ExportUpdateOne {
	_u.mutation.Where(ps...)
	return _u
}

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

// Save executes the query and returns the updated Export entity.
func (_u *ExportUpdateOne) Save(ctx context.Context) (*Export, error) {
	_u.defaults()
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

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

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

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

// defaults sets the default values of the builder before save.
func (_u *ExportUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := export.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *ExportUpdateOne) check() error {
	if v, ok := _u.mutation.Kind(); ok {
		if err := export.KindValidator(v); err != nil {
			return &ValidationError{Name: "kind", err: fmt.Errorf(`ent: validator failed for field "Export.kind": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Status(); ok {
		if err := export.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Export.status": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Error(); ok {
		if err := export.ErrorValidator(v); err != nil {
			return &ValidationError{Name: "error", err: fmt.Errorf(`ent: validator failed for field "Export.error": %w`, err)}
		}
	}
	if _u.mutation.GroupCleared() && len(_u.mutation.GroupIDs()) > 0 {
		return errors.New(`ent: clearing a required unique edge "Export.group"`)
	}
	return nil
}

func (_u *ExportUpdateOne) sqlSave(ctx context.Context) (_node *Export, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(export.Table, export.Columns, sqlgraph.NewFieldSpec(export.FieldID, field.TypeUUID))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Export.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := _u.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, export.FieldID)
		for _, f := range fields {
			if !export.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != export.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.UpdatedAt(); ok {
		_spec.SetField(export.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.Kind(); ok {
		_spec.SetField(export.FieldKind, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.Status(); ok {
		_spec.SetField(export.FieldStatus, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.Progress(); ok {
		_spec.SetField(export.FieldProgress, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedProgress(); ok {
		_spec.AddField(export.FieldProgress, field.TypeInt, value)
	}
	if value, ok := _u.mutation.ArtifactPath(); ok {
		_spec.SetField(export.FieldArtifactPath, field.TypeString, value)
	}
	if _u.mutation.ArtifactPathCleared() {
		_spec.ClearField(export.FieldArtifactPath, field.TypeString)
	}
	if value, ok := _u.mutation.SizeBytes(); ok {
		_spec.SetField(export.FieldSizeBytes, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.AddedSizeBytes(); ok {
		_spec.AddField(export.FieldSizeBytes, field.TypeInt64, value)
	}
	if value, ok := _u.mutation.Error(); ok {
		_spec.SetField(export.FieldError, field.TypeString, value)
	}
	if _u.mutation.ErrorCleared() {
		_spec.ClearField(export.FieldError, field.TypeString)
	}
	if _u.mutation.GroupCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   export.GroupTable,
			Columns: []string{export.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.GroupIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   export.GroupTable,
			Columns: []string{export.GroupColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Export{config: _u.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{export.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}