Files
homebox/backend/internal/data/ent/entityfield_create.go
2025-05-07 20:41:29 -04:00

439 lines
13 KiB
Go

// 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/entity"
"github.com/sysadminsmedia/homebox/backend/internal/data/ent/entityfield"
)
// EntityFieldCreate is the builder for creating a EntityField entity.
type EntityFieldCreate struct {
config
mutation *EntityFieldMutation
hooks []Hook
}
// SetCreatedAt sets the "created_at" field.
func (efc *EntityFieldCreate) SetCreatedAt(t time.Time) *EntityFieldCreate {
efc.mutation.SetCreatedAt(t)
return efc
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (efc *EntityFieldCreate) SetNillableCreatedAt(t *time.Time) *EntityFieldCreate {
if t != nil {
efc.SetCreatedAt(*t)
}
return efc
}
// SetUpdatedAt sets the "updated_at" field.
func (efc *EntityFieldCreate) SetUpdatedAt(t time.Time) *EntityFieldCreate {
efc.mutation.SetUpdatedAt(t)
return efc
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (efc *EntityFieldCreate) SetNillableUpdatedAt(t *time.Time) *EntityFieldCreate {
if t != nil {
efc.SetUpdatedAt(*t)
}
return efc
}
// SetName sets the "name" field.
func (efc *EntityFieldCreate) SetName(s string) *EntityFieldCreate {
efc.mutation.SetName(s)
return efc
}
// SetDescription sets the "description" field.
func (efc *EntityFieldCreate) SetDescription(s string) *EntityFieldCreate {
efc.mutation.SetDescription(s)
return efc
}
// SetNillableDescription sets the "description" field if the given value is not nil.
func (efc *EntityFieldCreate) SetNillableDescription(s *string) *EntityFieldCreate {
if s != nil {
efc.SetDescription(*s)
}
return efc
}
// SetType sets the "type" field.
func (efc *EntityFieldCreate) SetType(e entityfield.Type) *EntityFieldCreate {
efc.mutation.SetType(e)
return efc
}
// SetTextValue sets the "text_value" field.
func (efc *EntityFieldCreate) SetTextValue(s string) *EntityFieldCreate {
efc.mutation.SetTextValue(s)
return efc
}
// SetNillableTextValue sets the "text_value" field if the given value is not nil.
func (efc *EntityFieldCreate) SetNillableTextValue(s *string) *EntityFieldCreate {
if s != nil {
efc.SetTextValue(*s)
}
return efc
}
// SetNumberValue sets the "number_value" field.
func (efc *EntityFieldCreate) SetNumberValue(i int) *EntityFieldCreate {
efc.mutation.SetNumberValue(i)
return efc
}
// SetNillableNumberValue sets the "number_value" field if the given value is not nil.
func (efc *EntityFieldCreate) SetNillableNumberValue(i *int) *EntityFieldCreate {
if i != nil {
efc.SetNumberValue(*i)
}
return efc
}
// SetBooleanValue sets the "boolean_value" field.
func (efc *EntityFieldCreate) SetBooleanValue(b bool) *EntityFieldCreate {
efc.mutation.SetBooleanValue(b)
return efc
}
// SetNillableBooleanValue sets the "boolean_value" field if the given value is not nil.
func (efc *EntityFieldCreate) SetNillableBooleanValue(b *bool) *EntityFieldCreate {
if b != nil {
efc.SetBooleanValue(*b)
}
return efc
}
// SetTimeValue sets the "time_value" field.
func (efc *EntityFieldCreate) SetTimeValue(t time.Time) *EntityFieldCreate {
efc.mutation.SetTimeValue(t)
return efc
}
// SetNillableTimeValue sets the "time_value" field if the given value is not nil.
func (efc *EntityFieldCreate) SetNillableTimeValue(t *time.Time) *EntityFieldCreate {
if t != nil {
efc.SetTimeValue(*t)
}
return efc
}
// SetID sets the "id" field.
func (efc *EntityFieldCreate) SetID(u uuid.UUID) *EntityFieldCreate {
efc.mutation.SetID(u)
return efc
}
// SetNillableID sets the "id" field if the given value is not nil.
func (efc *EntityFieldCreate) SetNillableID(u *uuid.UUID) *EntityFieldCreate {
if u != nil {
efc.SetID(*u)
}
return efc
}
// SetEntityID sets the "entity" edge to the Entity entity by ID.
func (efc *EntityFieldCreate) SetEntityID(id uuid.UUID) *EntityFieldCreate {
efc.mutation.SetEntityID(id)
return efc
}
// SetNillableEntityID sets the "entity" edge to the Entity entity by ID if the given value is not nil.
func (efc *EntityFieldCreate) SetNillableEntityID(id *uuid.UUID) *EntityFieldCreate {
if id != nil {
efc = efc.SetEntityID(*id)
}
return efc
}
// SetEntity sets the "entity" edge to the Entity entity.
func (efc *EntityFieldCreate) SetEntity(e *Entity) *EntityFieldCreate {
return efc.SetEntityID(e.ID)
}
// Mutation returns the EntityFieldMutation object of the builder.
func (efc *EntityFieldCreate) Mutation() *EntityFieldMutation {
return efc.mutation
}
// Save creates the EntityField in the database.
func (efc *EntityFieldCreate) Save(ctx context.Context) (*EntityField, error) {
efc.defaults()
return withHooks(ctx, efc.sqlSave, efc.mutation, efc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (efc *EntityFieldCreate) SaveX(ctx context.Context) *EntityField {
v, err := efc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (efc *EntityFieldCreate) Exec(ctx context.Context) error {
_, err := efc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (efc *EntityFieldCreate) ExecX(ctx context.Context) {
if err := efc.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (efc *EntityFieldCreate) defaults() {
if _, ok := efc.mutation.CreatedAt(); !ok {
v := entityfield.DefaultCreatedAt()
efc.mutation.SetCreatedAt(v)
}
if _, ok := efc.mutation.UpdatedAt(); !ok {
v := entityfield.DefaultUpdatedAt()
efc.mutation.SetUpdatedAt(v)
}
if _, ok := efc.mutation.BooleanValue(); !ok {
v := entityfield.DefaultBooleanValue
efc.mutation.SetBooleanValue(v)
}
if _, ok := efc.mutation.TimeValue(); !ok {
v := entityfield.DefaultTimeValue()
efc.mutation.SetTimeValue(v)
}
if _, ok := efc.mutation.ID(); !ok {
v := entityfield.DefaultID()
efc.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (efc *EntityFieldCreate) check() error {
if _, ok := efc.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "EntityField.created_at"`)}
}
if _, ok := efc.mutation.UpdatedAt(); !ok {
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "EntityField.updated_at"`)}
}
if _, ok := efc.mutation.Name(); !ok {
return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "EntityField.name"`)}
}
if v, ok := efc.mutation.Name(); ok {
if err := entityfield.NameValidator(v); err != nil {
return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "EntityField.name": %w`, err)}
}
}
if v, ok := efc.mutation.Description(); ok {
if err := entityfield.DescriptionValidator(v); err != nil {
return &ValidationError{Name: "description", err: fmt.Errorf(`ent: validator failed for field "EntityField.description": %w`, err)}
}
}
if _, ok := efc.mutation.GetType(); !ok {
return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "EntityField.type"`)}
}
if v, ok := efc.mutation.GetType(); ok {
if err := entityfield.TypeValidator(v); err != nil {
return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "EntityField.type": %w`, err)}
}
}
if v, ok := efc.mutation.TextValue(); ok {
if err := entityfield.TextValueValidator(v); err != nil {
return &ValidationError{Name: "text_value", err: fmt.Errorf(`ent: validator failed for field "EntityField.text_value": %w`, err)}
}
}
if _, ok := efc.mutation.BooleanValue(); !ok {
return &ValidationError{Name: "boolean_value", err: errors.New(`ent: missing required field "EntityField.boolean_value"`)}
}
if _, ok := efc.mutation.TimeValue(); !ok {
return &ValidationError{Name: "time_value", err: errors.New(`ent: missing required field "EntityField.time_value"`)}
}
return nil
}
func (efc *EntityFieldCreate) sqlSave(ctx context.Context) (*EntityField, error) {
if err := efc.check(); err != nil {
return nil, err
}
_node, _spec := efc.createSpec()
if err := sqlgraph.CreateNode(ctx, efc.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
}
}
efc.mutation.id = &_node.ID
efc.mutation.done = true
return _node, nil
}
func (efc *EntityFieldCreate) createSpec() (*EntityField, *sqlgraph.CreateSpec) {
var (
_node = &EntityField{config: efc.config}
_spec = sqlgraph.NewCreateSpec(entityfield.Table, sqlgraph.NewFieldSpec(entityfield.FieldID, field.TypeUUID))
)
if id, ok := efc.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = &id
}
if value, ok := efc.mutation.CreatedAt(); ok {
_spec.SetField(entityfield.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := efc.mutation.UpdatedAt(); ok {
_spec.SetField(entityfield.FieldUpdatedAt, field.TypeTime, value)
_node.UpdatedAt = value
}
if value, ok := efc.mutation.Name(); ok {
_spec.SetField(entityfield.FieldName, field.TypeString, value)
_node.Name = value
}
if value, ok := efc.mutation.Description(); ok {
_spec.SetField(entityfield.FieldDescription, field.TypeString, value)
_node.Description = value
}
if value, ok := efc.mutation.GetType(); ok {
_spec.SetField(entityfield.FieldType, field.TypeEnum, value)
_node.Type = value
}
if value, ok := efc.mutation.TextValue(); ok {
_spec.SetField(entityfield.FieldTextValue, field.TypeString, value)
_node.TextValue = value
}
if value, ok := efc.mutation.NumberValue(); ok {
_spec.SetField(entityfield.FieldNumberValue, field.TypeInt, value)
_node.NumberValue = value
}
if value, ok := efc.mutation.BooleanValue(); ok {
_spec.SetField(entityfield.FieldBooleanValue, field.TypeBool, value)
_node.BooleanValue = value
}
if value, ok := efc.mutation.TimeValue(); ok {
_spec.SetField(entityfield.FieldTimeValue, field.TypeTime, value)
_node.TimeValue = value
}
if nodes := efc.mutation.EntityIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: entityfield.EntityTable,
Columns: []string{entityfield.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)
}
_node.entity_fields = &nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// EntityFieldCreateBulk is the builder for creating many EntityField entities in bulk.
type EntityFieldCreateBulk struct {
config
err error
builders []*EntityFieldCreate
}
// Save creates the EntityField entities in the database.
func (efcb *EntityFieldCreateBulk) Save(ctx context.Context) ([]*EntityField, error) {
if efcb.err != nil {
return nil, efcb.err
}
specs := make([]*sqlgraph.CreateSpec, len(efcb.builders))
nodes := make([]*EntityField, len(efcb.builders))
mutators := make([]Mutator, len(efcb.builders))
for i := range efcb.builders {
func(i int, root context.Context) {
builder := efcb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*EntityFieldMutation)
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, efcb.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, efcb.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, efcb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (efcb *EntityFieldCreateBulk) SaveX(ctx context.Context) []*EntityField {
v, err := efcb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (efcb *EntityFieldCreateBulk) Exec(ctx context.Context) error {
_, err := efcb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (efcb *EntityFieldCreateBulk) ExecX(ctx context.Context) {
if err := efcb.Exec(ctx); err != nil {
panic(err)
}
}