Skip to content

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

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

package ent

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

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

// ExportCreate is the builder for creating a Export entity.
type ExportCreate struct {
	config
	mutation *ExportMutation
	hooks    []Hook
}

// SetCreatedAt sets the "created_at" field.
func (_c *ExportCreate) SetCreatedAt(v time.Time) *ExportCreate {
	_c.mutation.SetCreatedAt(v)
	return _c
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *ExportCreate) SetNillableCreatedAt(v *time.Time) *ExportCreate {
	if v != nil {
		_c.SetCreatedAt(*v)
	}
	return _c
}

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

// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (_c *ExportCreate) SetNillableUpdatedAt(v *time.Time) *ExportCreate {
	if v != nil {
		_c.SetUpdatedAt(*v)
	}
	return _c
}

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

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

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

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

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

// SetProgress sets the "progress" field.
func (_c *ExportCreate) SetProgress(v int) *ExportCreate {
	_c.mutation.SetProgress(v)
	return _c
}

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

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

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

// SetSizeBytes sets the "size_bytes" field.
func (_c *ExportCreate) SetSizeBytes(v int64) *ExportCreate {
	_c.mutation.SetSizeBytes(v)
	return _c
}

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

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

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

// SetID sets the "id" field.
func (_c *ExportCreate) SetID(v uuid.UUID) *ExportCreate {
	_c.mutation.SetID(v)
	return _c
}

// SetNillableID sets the "id" field if the given value is not nil.
func (_c *ExportCreate) SetNillableID(v *uuid.UUID) *ExportCreate {
	if v != nil {
		_c.SetID(*v)
	}
	return _c
}

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

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

// Save creates the Export in the database.
func (_c *ExportCreate) Save(ctx context.Context) (*Export, error) {
	_c.defaults()
	return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}

// SaveX calls Save and panics if Save returns an error.
func (_c *ExportCreate) SaveX(ctx context.Context) *Export {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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

// defaults sets the default values of the builder before save.
func (_c *ExportCreate) defaults() {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := export.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		v := export.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
	if _, ok := _c.mutation.Kind(); !ok {
		v := export.DefaultKind
		_c.mutation.SetKind(v)
	}
	if _, ok := _c.mutation.Status(); !ok {
		v := export.DefaultStatus
		_c.mutation.SetStatus(v)
	}
	if _, ok := _c.mutation.Progress(); !ok {
		v := export.DefaultProgress
		_c.mutation.SetProgress(v)
	}
	if _, ok := _c.mutation.SizeBytes(); !ok {
		v := export.DefaultSizeBytes
		_c.mutation.SetSizeBytes(v)
	}
	if _, ok := _c.mutation.ID(); !ok {
		v := export.DefaultID()
		_c.mutation.SetID(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *ExportCreate) check() error {
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Export.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Export.updated_at"`)}
	}
	if _, ok := _c.mutation.GroupID(); !ok {
		return &ValidationError{Name: "group_id", err: errors.New(`ent: missing required field "Export.group_id"`)}
	}
	if _, ok := _c.mutation.Kind(); !ok {
		return &ValidationError{Name: "kind", err: errors.New(`ent: missing required field "Export.kind"`)}
	}
	if v, ok := _c.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 _, ok := _c.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "Export.status"`)}
	}
	if v, ok := _c.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 _, ok := _c.mutation.Progress(); !ok {
		return &ValidationError{Name: "progress", err: errors.New(`ent: missing required field "Export.progress"`)}
	}
	if _, ok := _c.mutation.SizeBytes(); !ok {
		return &ValidationError{Name: "size_bytes", err: errors.New(`ent: missing required field "Export.size_bytes"`)}
	}
	if v, ok := _c.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 len(_c.mutation.GroupIDs()) == 0 {
		return &ValidationError{Name: "group", err: errors.New(`ent: missing required edge "Export.group"`)}
	}
	return nil
}

func (_c *ExportCreate) sqlSave(ctx context.Context) (*Export, error) {
	if err := _c.check(); err != nil {
		return nil, err
	}
	_node, _spec := _c.createSpec()
	if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	if _spec.ID.Value != nil {
		if id, ok := _spec.ID.Value.(*uuid.UUID); ok {
			_node.ID = *id
		} else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
			return nil, err
		}
	}
	_c.mutation.id = &_node.ID
	_c.mutation.done = true
	return _node, nil
}

func (_c *ExportCreate) createSpec() (*Export, *sqlgraph.CreateSpec) {
	var (
		_node = &Export{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(export.Table, sqlgraph.NewFieldSpec(export.FieldID, field.TypeUUID))
	)
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = &id
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(export.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(export.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := _c.mutation.Kind(); ok {
		_spec.SetField(export.FieldKind, field.TypeEnum, value)
		_node.Kind = value
	}
	if value, ok := _c.mutation.Status(); ok {
		_spec.SetField(export.FieldStatus, field.TypeEnum, value)
		_node.Status = value
	}
	if value, ok := _c.mutation.Progress(); ok {
		_spec.SetField(export.FieldProgress, field.TypeInt, value)
		_node.Progress = value
	}
	if value, ok := _c.mutation.ArtifactPath(); ok {
		_spec.SetField(export.FieldArtifactPath, field.TypeString, value)
		_node.ArtifactPath = value
	}
	if value, ok := _c.mutation.SizeBytes(); ok {
		_spec.SetField(export.FieldSizeBytes, field.TypeInt64, value)
		_node.SizeBytes = value
	}
	if value, ok := _c.mutation.Error(); ok {
		_spec.SetField(export.FieldError, field.TypeString, value)
		_node.Error = value
	}
	if nodes := _c.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)
		}
		_node.GroupID = nodes[0]
		_spec.Edges = append(_spec.Edges, edge)
	}
	return _node, _spec
}

// ExportCreateBulk is the builder for creating many Export entities in bulk.
type ExportCreateBulk struct {
	config
	err      error
	builders []*ExportCreate
}

// Save creates the Export entities in the database.
func (_c *ExportCreateBulk) Save(ctx context.Context) ([]*Export, error) {
	if _c.err != nil {
		return nil, _c.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
	nodes := make([]*Export, len(_c.builders))
	mutators := make([]Mutator, len(_c.builders))
	for i := range _c.builders {
		func(i int, root context.Context) {
			builder := _c.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*ExportMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				var err error
				nodes[i], specs[i] = builder.createSpec()
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil {
						if sqlgraph.IsConstraintError(err) {
							err = &ConstraintError{msg: err.Error(), wrap: err}
						}
					}
				}
				if err != nil {
					return nil, err
				}
				mutation.id = &nodes[i].ID
				mutation.done = true
				return nodes[i], nil
			})
			for i := len(builder.hooks) - 1; i >= 0; i-- {
				mut = builder.hooks[i](mut)
			}
			mutators[i] = mut
		}(i, ctx)
	}
	if len(mutators) > 0 {
		if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func (_c *ExportCreateBulk) SaveX(ctx context.Context) []*Export {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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