// 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/attachment" "github.com/sysadminsmedia/homebox/backend/internal/data/ent/item" ) // AttachmentCreate is the builder for creating a Attachment entity. type AttachmentCreate struct { config mutation *AttachmentMutation hooks []Hook } // SetCreatedAt sets the "created_at" field. func (_c *AttachmentCreate) SetCreatedAt(v time.Time) *AttachmentCreate { _c.mutation.SetCreatedAt(v) return _c } // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. func (_c *AttachmentCreate) SetNillableCreatedAt(v *time.Time) *AttachmentCreate { if v != nil { _c.SetCreatedAt(*v) } return _c } // SetUpdatedAt sets the "updated_at" field. func (_c *AttachmentCreate) SetUpdatedAt(v time.Time) *AttachmentCreate { _c.mutation.SetUpdatedAt(v) return _c } // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. func (_c *AttachmentCreate) SetNillableUpdatedAt(v *time.Time) *AttachmentCreate { if v != nil { _c.SetUpdatedAt(*v) } return _c } // SetType sets the "type" field. func (_c *AttachmentCreate) SetType(v attachment.Type) *AttachmentCreate { _c.mutation.SetType(v) return _c } // SetNillableType sets the "type" field if the given value is not nil. func (_c *AttachmentCreate) SetNillableType(v *attachment.Type) *AttachmentCreate { if v != nil { _c.SetType(*v) } return _c } // SetPrimary sets the "primary" field. func (_c *AttachmentCreate) SetPrimary(v bool) *AttachmentCreate { _c.mutation.SetPrimary(v) return _c } // SetNillablePrimary sets the "primary" field if the given value is not nil. func (_c *AttachmentCreate) SetNillablePrimary(v *bool) *AttachmentCreate { if v != nil { _c.SetPrimary(*v) } return _c } // SetTitle sets the "title" field. func (_c *AttachmentCreate) SetTitle(v string) *AttachmentCreate { _c.mutation.SetTitle(v) return _c } // SetNillableTitle sets the "title" field if the given value is not nil. func (_c *AttachmentCreate) SetNillableTitle(v *string) *AttachmentCreate { if v != nil { _c.SetTitle(*v) } return _c } // SetPath sets the "path" field. func (_c *AttachmentCreate) SetPath(v string) *AttachmentCreate { _c.mutation.SetPath(v) return _c } // SetNillablePath sets the "path" field if the given value is not nil. func (_c *AttachmentCreate) SetNillablePath(v *string) *AttachmentCreate { if v != nil { _c.SetPath(*v) } return _c } // SetMimeType sets the "mime_type" field. func (_c *AttachmentCreate) SetMimeType(v string) *AttachmentCreate { _c.mutation.SetMimeType(v) return _c } // SetNillableMimeType sets the "mime_type" field if the given value is not nil. func (_c *AttachmentCreate) SetNillableMimeType(v *string) *AttachmentCreate { if v != nil { _c.SetMimeType(*v) } return _c } // SetID sets the "id" field. func (_c *AttachmentCreate) SetID(v uuid.UUID) *AttachmentCreate { _c.mutation.SetID(v) return _c } // SetNillableID sets the "id" field if the given value is not nil. func (_c *AttachmentCreate) SetNillableID(v *uuid.UUID) *AttachmentCreate { if v != nil { _c.SetID(*v) } return _c } // SetItemID sets the "item" edge to the Item entity by ID. func (_c *AttachmentCreate) SetItemID(id uuid.UUID) *AttachmentCreate { _c.mutation.SetItemID(id) return _c } // SetNillableItemID sets the "item" edge to the Item entity by ID if the given value is not nil. func (_c *AttachmentCreate) SetNillableItemID(id *uuid.UUID) *AttachmentCreate { if id != nil { _c = _c.SetItemID(*id) } return _c } // SetItem sets the "item" edge to the Item entity. func (_c *AttachmentCreate) SetItem(v *Item) *AttachmentCreate { return _c.SetItemID(v.ID) } // SetThumbnailID sets the "thumbnail" edge to the Attachment entity by ID. func (_c *AttachmentCreate) SetThumbnailID(id uuid.UUID) *AttachmentCreate { _c.mutation.SetThumbnailID(id) return _c } // SetNillableThumbnailID sets the "thumbnail" edge to the Attachment entity by ID if the given value is not nil. func (_c *AttachmentCreate) SetNillableThumbnailID(id *uuid.UUID) *AttachmentCreate { if id != nil { _c = _c.SetThumbnailID(*id) } return _c } // SetThumbnail sets the "thumbnail" edge to the Attachment entity. func (_c *AttachmentCreate) SetThumbnail(v *Attachment) *AttachmentCreate { return _c.SetThumbnailID(v.ID) } // Mutation returns the AttachmentMutation object of the builder. func (_c *AttachmentCreate) Mutation() *AttachmentMutation { return _c.mutation } // Save creates the Attachment in the database. func (_c *AttachmentCreate) Save(ctx context.Context) (*Attachment, error) { _c.defaults() return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) } // SaveX calls Save and panics if Save returns an error. func (_c *AttachmentCreate) SaveX(ctx context.Context) *Attachment { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *AttachmentCreate) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *AttachmentCreate) 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 *AttachmentCreate) defaults() { if _, ok := _c.mutation.CreatedAt(); !ok { v := attachment.DefaultCreatedAt() _c.mutation.SetCreatedAt(v) } if _, ok := _c.mutation.UpdatedAt(); !ok { v := attachment.DefaultUpdatedAt() _c.mutation.SetUpdatedAt(v) } if _, ok := _c.mutation.GetType(); !ok { v := attachment.DefaultType _c.mutation.SetType(v) } if _, ok := _c.mutation.Primary(); !ok { v := attachment.DefaultPrimary _c.mutation.SetPrimary(v) } if _, ok := _c.mutation.Title(); !ok { v := attachment.DefaultTitle _c.mutation.SetTitle(v) } if _, ok := _c.mutation.Path(); !ok { v := attachment.DefaultPath _c.mutation.SetPath(v) } if _, ok := _c.mutation.MimeType(); !ok { v := attachment.DefaultMimeType _c.mutation.SetMimeType(v) } if _, ok := _c.mutation.ID(); !ok { v := attachment.DefaultID() _c.mutation.SetID(v) } } // check runs all checks and user-defined validators on the builder. func (_c *AttachmentCreate) check() error { if _, ok := _c.mutation.CreatedAt(); !ok { return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Attachment.created_at"`)} } if _, ok := _c.mutation.UpdatedAt(); !ok { return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Attachment.updated_at"`)} } if _, ok := _c.mutation.GetType(); !ok { return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "Attachment.type"`)} } if v, ok := _c.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)} } } if _, ok := _c.mutation.Primary(); !ok { return &ValidationError{Name: "primary", err: errors.New(`ent: missing required field "Attachment.primary"`)} } if _, ok := _c.mutation.Title(); !ok { return &ValidationError{Name: "title", err: errors.New(`ent: missing required field "Attachment.title"`)} } if _, ok := _c.mutation.Path(); !ok { return &ValidationError{Name: "path", err: errors.New(`ent: missing required field "Attachment.path"`)} } if _, ok := _c.mutation.MimeType(); !ok { return &ValidationError{Name: "mime_type", err: errors.New(`ent: missing required field "Attachment.mime_type"`)} } return nil } func (_c *AttachmentCreate) sqlSave(ctx context.Context) (*Attachment, 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 *AttachmentCreate) createSpec() (*Attachment, *sqlgraph.CreateSpec) { var ( _node = &Attachment{config: _c.config} _spec = sqlgraph.NewCreateSpec(attachment.Table, sqlgraph.NewFieldSpec(attachment.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(attachment.FieldCreatedAt, field.TypeTime, value) _node.CreatedAt = value } if value, ok := _c.mutation.UpdatedAt(); ok { _spec.SetField(attachment.FieldUpdatedAt, field.TypeTime, value) _node.UpdatedAt = value } if value, ok := _c.mutation.GetType(); ok { _spec.SetField(attachment.FieldType, field.TypeEnum, value) _node.Type = value } if value, ok := _c.mutation.Primary(); ok { _spec.SetField(attachment.FieldPrimary, field.TypeBool, value) _node.Primary = value } if value, ok := _c.mutation.Title(); ok { _spec.SetField(attachment.FieldTitle, field.TypeString, value) _node.Title = value } if value, ok := _c.mutation.Path(); ok { _spec.SetField(attachment.FieldPath, field.TypeString, value) _node.Path = value } if value, ok := _c.mutation.MimeType(); ok { _spec.SetField(attachment.FieldMimeType, field.TypeString, value) _node.MimeType = value } if nodes := _c.mutation.ItemIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: attachment.ItemTable, Columns: []string{attachment.ItemColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(item.FieldID, field.TypeUUID), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.item_attachments = &nodes[0] _spec.Edges = append(_spec.Edges, edge) } if nodes := _c.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) } _node.attachment_thumbnail = &nodes[0] _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // AttachmentCreateBulk is the builder for creating many Attachment entities in bulk. type AttachmentCreateBulk struct { config err error builders []*AttachmentCreate } // Save creates the Attachment entities in the database. func (_c *AttachmentCreateBulk) Save(ctx context.Context) ([]*Attachment, error) { if _c.err != nil { return nil, _c.err } specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) nodes := make([]*Attachment, 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.(*AttachmentMutation) 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 *AttachmentCreateBulk) SaveX(ctx context.Context) []*Attachment { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *AttachmentCreateBulk) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *AttachmentCreateBulk) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } }