Skip to content

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

18.1 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/attachment"
	"github.com/sysadminsmedia/homebox/backend/internal/data/ent/entity"
	"github.com/sysadminsmedia/homebox/backend/internal/data/ent/predicate"
)

// AttachmentUpdate is the builder for updating Attachment entities.
type AttachmentUpdate struct {
	config
	hooks    []Hook
	mutation *AttachmentMutation
}

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

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

// SetType sets the "type" field.
func (_u *AttachmentUpdate) SetType(v attachment.Type) *AttachmentUpdate {
	_u.mutation.SetType(v)
	return _u
}

// SetNillableType sets the "type" field if the given value is not nil.
func (_u *AttachmentUpdate) SetNillableType(v *attachment.Type) *AttachmentUpdate {
	if v != nil {
		_u.SetType(*v)
	}
	return _u
}

// SetPrimary sets the "primary" field.
func (_u *AttachmentUpdate) SetPrimary(v bool) *AttachmentUpdate {
	_u.mutation.SetPrimary(v)
	return _u
}

// SetNillablePrimary sets the "primary" field if the given value is not nil.
func (_u *AttachmentUpdate) SetNillablePrimary(v *bool) *AttachmentUpdate {
	if v != nil {
		_u.SetPrimary(*v)
	}
	return _u
}

// SetTitle sets the "title" field.
func (_u *AttachmentUpdate) SetTitle(v string) *AttachmentUpdate {
	_u.mutation.SetTitle(v)
	return _u
}

// SetNillableTitle sets the "title" field if the given value is not nil.
func (_u *AttachmentUpdate) SetNillableTitle(v *string) *AttachmentUpdate {
	if v != nil {
		_u.SetTitle(*v)
	}
	return _u
}

// SetPath sets the "path" field.
func (_u *AttachmentUpdate) SetPath(v string) *AttachmentUpdate {
	_u.mutation.SetPath(v)
	return _u
}

// SetNillablePath sets the "path" field if the given value is not nil.
func (_u *AttachmentUpdate) SetNillablePath(v *string) *AttachmentUpdate {
	if v != nil {
		_u.SetPath(*v)
	}
	return _u
}

// SetMimeType sets the "mime_type" field.
func (_u *AttachmentUpdate) SetMimeType(v string) *AttachmentUpdate {
	_u.mutation.SetMimeType(v)
	return _u
}

// SetNillableMimeType sets the "mime_type" field if the given value is not nil.
func (_u *AttachmentUpdate) SetNillableMimeType(v *string) *AttachmentUpdate {
	if v != nil {
		_u.SetMimeType(*v)
	}
	return _u
}

// SetEntityID sets the "entity" edge to the Entity entity by ID.
func (_u *AttachmentUpdate) SetEntityID(id uuid.UUID) *AttachmentUpdate {
	_u.mutation.SetEntityID(id)
	return _u
}

// SetNillableEntityID sets the "entity" edge to the Entity entity by ID if the given value is not nil.
func (_u *AttachmentUpdate) SetNillableEntityID(id *uuid.UUID) *AttachmentUpdate {
	if id != nil {
		_u = _u.SetEntityID(*id)
	}
	return _u
}

// SetEntity sets the "entity" edge to the Entity entity.
func (_u *AttachmentUpdate) SetEntity(v *Entity) *AttachmentUpdate {
	return _u.SetEntityID(v.ID)
}

// SetThumbnailID sets the "thumbnail" edge to the Attachment entity by ID.
func (_u *AttachmentUpdate) SetThumbnailID(id uuid.UUID) *AttachmentUpdate {
	_u.mutation.SetThumbnailID(id)
	return _u
}

// SetNillableThumbnailID sets the "thumbnail" edge to the Attachment entity by ID if the given value is not nil.
func (_u *AttachmentUpdate) SetNillableThumbnailID(id *uuid.UUID) *AttachmentUpdate {
	if id != nil {
		_u = _u.SetThumbnailID(*id)
	}
	return _u
}

// SetThumbnail sets the "thumbnail" edge to the Attachment entity.
func (_u *AttachmentUpdate) SetThumbnail(v *Attachment) *AttachmentUpdate {
	return _u.SetThumbnailID(v.ID)
}

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

// ClearEntity clears the "entity" edge to the Entity entity.
func (_u *AttachmentUpdate) ClearEntity() *AttachmentUpdate {
	_u.mutation.ClearEntity()
	return _u
}

// ClearThumbnail clears the "thumbnail" edge to the Attachment entity.
func (_u *AttachmentUpdate) ClearThumbnail() *AttachmentUpdate {
	_u.mutation.ClearThumbnail()
	return _u
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *AttachmentUpdate) 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 *AttachmentUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *AttachmentUpdate) 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 *AttachmentUpdate) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := attachment.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *AttachmentUpdate) check() error {
	if v, ok := _u.mutation.GetType(); ok {
		if err := attachment.TypeValidator(v); err != nil {
			return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "Attachment.type": %w`, err)}
		}
	}
	return nil
}

func (_u *AttachmentUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(attachment.Table, attachment.Columns, sqlgraph.NewFieldSpec(attachment.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(attachment.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.GetType(); ok {
		_spec.SetField(attachment.FieldType, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.Primary(); ok {
		_spec.SetField(attachment.FieldPrimary, field.TypeBool, value)
	}
	if value, ok := _u.mutation.Title(); ok {
		_spec.SetField(attachment.FieldTitle, field.TypeString, value)
	}
	if value, ok := _u.mutation.Path(); ok {
		_spec.SetField(attachment.FieldPath, field.TypeString, value)
	}
	if value, ok := _u.mutation.MimeType(); ok {
		_spec.SetField(attachment.FieldMimeType, field.TypeString, value)
	}
	if _u.mutation.EntityCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   attachment.EntityTable,
			Columns: []string{attachment.EntityColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(entity.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.EntityIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   attachment.EntityTable,
			Columns: []string{attachment.EntityColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(entity.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.ThumbnailCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   attachment.ThumbnailTable,
			Columns: []string{attachment.ThumbnailColumn},
			Bidi:    true,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(attachment.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.ThumbnailIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   attachment.ThumbnailTable,
			Columns: []string{attachment.ThumbnailColumn},
			Bidi:    true,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(attachment.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{attachment.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// AttachmentUpdateOne is the builder for updating a single Attachment entity.
type AttachmentUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *AttachmentMutation
}

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

// SetType sets the "type" field.
func (_u *AttachmentUpdateOne) SetType(v attachment.Type) *AttachmentUpdateOne {
	_u.mutation.SetType(v)
	return _u
}

// SetNillableType sets the "type" field if the given value is not nil.
func (_u *AttachmentUpdateOne) SetNillableType(v *attachment.Type) *AttachmentUpdateOne {
	if v != nil {
		_u.SetType(*v)
	}
	return _u
}

// SetPrimary sets the "primary" field.
func (_u *AttachmentUpdateOne) SetPrimary(v bool) *AttachmentUpdateOne {
	_u.mutation.SetPrimary(v)
	return _u
}

// SetNillablePrimary sets the "primary" field if the given value is not nil.
func (_u *AttachmentUpdateOne) SetNillablePrimary(v *bool) *AttachmentUpdateOne {
	if v != nil {
		_u.SetPrimary(*v)
	}
	return _u
}

// SetTitle sets the "title" field.
func (_u *AttachmentUpdateOne) SetTitle(v string) *AttachmentUpdateOne {
	_u.mutation.SetTitle(v)
	return _u
}

// SetNillableTitle sets the "title" field if the given value is not nil.
func (_u *AttachmentUpdateOne) SetNillableTitle(v *string) *AttachmentUpdateOne {
	if v != nil {
		_u.SetTitle(*v)
	}
	return _u
}

// SetPath sets the "path" field.
func (_u *AttachmentUpdateOne) SetPath(v string) *AttachmentUpdateOne {
	_u.mutation.SetPath(v)
	return _u
}

// SetNillablePath sets the "path" field if the given value is not nil.
func (_u *AttachmentUpdateOne) SetNillablePath(v *string) *AttachmentUpdateOne {
	if v != nil {
		_u.SetPath(*v)
	}
	return _u
}

// SetMimeType sets the "mime_type" field.
func (_u *AttachmentUpdateOne) SetMimeType(v string) *AttachmentUpdateOne {
	_u.mutation.SetMimeType(v)
	return _u
}

// SetNillableMimeType sets the "mime_type" field if the given value is not nil.
func (_u *AttachmentUpdateOne) SetNillableMimeType(v *string) *AttachmentUpdateOne {
	if v != nil {
		_u.SetMimeType(*v)
	}
	return _u
}

// SetEntityID sets the "entity" edge to the Entity entity by ID.
func (_u *AttachmentUpdateOne) SetEntityID(id uuid.UUID) *AttachmentUpdateOne {
	_u.mutation.SetEntityID(id)
	return _u
}

// SetNillableEntityID sets the "entity" edge to the Entity entity by ID if the given value is not nil.
func (_u *AttachmentUpdateOne) SetNillableEntityID(id *uuid.UUID) *AttachmentUpdateOne {
	if id != nil {
		_u = _u.SetEntityID(*id)
	}
	return _u
}

// SetEntity sets the "entity" edge to the Entity entity.
func (_u *AttachmentUpdateOne) SetEntity(v *Entity) *AttachmentUpdateOne {
	return _u.SetEntityID(v.ID)
}

// SetThumbnailID sets the "thumbnail" edge to the Attachment entity by ID.
func (_u *AttachmentUpdateOne) SetThumbnailID(id uuid.UUID) *AttachmentUpdateOne {
	_u.mutation.SetThumbnailID(id)
	return _u
}

// SetNillableThumbnailID sets the "thumbnail" edge to the Attachment entity by ID if the given value is not nil.
func (_u *AttachmentUpdateOne) SetNillableThumbnailID(id *uuid.UUID) *AttachmentUpdateOne {
	if id != nil {
		_u = _u.SetThumbnailID(*id)
	}
	return _u
}

// SetThumbnail sets the "thumbnail" edge to the Attachment entity.
func (_u *AttachmentUpdateOne) SetThumbnail(v *Attachment) *AttachmentUpdateOne {
	return _u.SetThumbnailID(v.ID)
}

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

// ClearEntity clears the "entity" edge to the Entity entity.
func (_u *AttachmentUpdateOne) ClearEntity() *AttachmentUpdateOne {
	_u.mutation.ClearEntity()
	return _u
}

// ClearThumbnail clears the "thumbnail" edge to the Attachment entity.
func (_u *AttachmentUpdateOne) ClearThumbnail() *AttachmentUpdateOne {
	_u.mutation.ClearThumbnail()
	return _u
}

// Where appends a list predicates to the AttachmentUpdate builder.
func (_u *AttachmentUpdateOne) Where(ps ...predicate.Attachment) *AttachmentUpdateOne {
	_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 *AttachmentUpdateOne) Select(field string, fields ...string) *AttachmentUpdateOne {
	_u.fields = append([]string{field}, fields...)
	return _u
}

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_u *AttachmentUpdateOne) 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 *AttachmentUpdateOne) defaults() {
	if _, ok := _u.mutation.UpdatedAt(); !ok {
		v := attachment.UpdateDefaultUpdatedAt()
		_u.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_u *AttachmentUpdateOne) check() error {
	if v, ok := _u.mutation.GetType(); ok {
		if err := attachment.TypeValidator(v); err != nil {
			return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "Attachment.type": %w`, err)}
		}
	}
	return nil
}

func (_u *AttachmentUpdateOne) sqlSave(ctx context.Context) (_node *Attachment, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(attachment.Table, attachment.Columns, sqlgraph.NewFieldSpec(attachment.FieldID, field.TypeUUID))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Attachment.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, attachment.FieldID)
		for _, f := range fields {
			if !attachment.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != attachment.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(attachment.FieldUpdatedAt, field.TypeTime, value)
	}
	if value, ok := _u.mutation.GetType(); ok {
		_spec.SetField(attachment.FieldType, field.TypeEnum, value)
	}
	if value, ok := _u.mutation.Primary(); ok {
		_spec.SetField(attachment.FieldPrimary, field.TypeBool, value)
	}
	if value, ok := _u.mutation.Title(); ok {
		_spec.SetField(attachment.FieldTitle, field.TypeString, value)
	}
	if value, ok := _u.mutation.Path(); ok {
		_spec.SetField(attachment.FieldPath, field.TypeString, value)
	}
	if value, ok := _u.mutation.MimeType(); ok {
		_spec.SetField(attachment.FieldMimeType, field.TypeString, value)
	}
	if _u.mutation.EntityCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   attachment.EntityTable,
			Columns: []string{attachment.EntityColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(entity.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.EntityIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   attachment.EntityTable,
			Columns: []string{attachment.EntityColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(entity.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if _u.mutation.ThumbnailCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   attachment.ThumbnailTable,
			Columns: []string{attachment.ThumbnailColumn},
			Bidi:    true,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(attachment.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := _u.mutation.ThumbnailIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.O2O,
			Inverse: false,
			Table:   attachment.ThumbnailTable,
			Columns: []string{attachment.ThumbnailColumn},
			Bidi:    true,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(attachment.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Attachment{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{attachment.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}