2022-09-12 22:47:27 +00:00
|
|
|
// 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"
|
2022-10-30 04:05:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/attachment"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/document"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/item"
|
2022-09-12 22:47:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// AttachmentCreate is the builder for creating a Attachment entity.
|
|
|
|
type AttachmentCreate struct {
|
|
|
|
config
|
|
|
|
mutation *AttachmentMutation
|
|
|
|
hooks []Hook
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCreatedAt sets the "created_at" field.
|
|
|
|
func (ac *AttachmentCreate) SetCreatedAt(t time.Time) *AttachmentCreate {
|
|
|
|
ac.mutation.SetCreatedAt(t)
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
|
|
|
|
func (ac *AttachmentCreate) SetNillableCreatedAt(t *time.Time) *AttachmentCreate {
|
|
|
|
if t != nil {
|
|
|
|
ac.SetCreatedAt(*t)
|
|
|
|
}
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetUpdatedAt sets the "updated_at" field.
|
|
|
|
func (ac *AttachmentCreate) SetUpdatedAt(t time.Time) *AttachmentCreate {
|
|
|
|
ac.mutation.SetUpdatedAt(t)
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
|
|
|
|
func (ac *AttachmentCreate) SetNillableUpdatedAt(t *time.Time) *AttachmentCreate {
|
|
|
|
if t != nil {
|
|
|
|
ac.SetUpdatedAt(*t)
|
|
|
|
}
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetType sets the "type" field.
|
|
|
|
func (ac *AttachmentCreate) SetType(a attachment.Type) *AttachmentCreate {
|
|
|
|
ac.mutation.SetType(a)
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNillableType sets the "type" field if the given value is not nil.
|
|
|
|
func (ac *AttachmentCreate) SetNillableType(a *attachment.Type) *AttachmentCreate {
|
|
|
|
if a != nil {
|
|
|
|
ac.SetType(*a)
|
|
|
|
}
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
2023-10-07 02:51:08 +00:00
|
|
|
// SetPrimary sets the "primary" field.
|
|
|
|
func (ac *AttachmentCreate) SetPrimary(b bool) *AttachmentCreate {
|
|
|
|
ac.mutation.SetPrimary(b)
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNillablePrimary sets the "primary" field if the given value is not nil.
|
|
|
|
func (ac *AttachmentCreate) SetNillablePrimary(b *bool) *AttachmentCreate {
|
|
|
|
if b != nil {
|
|
|
|
ac.SetPrimary(*b)
|
|
|
|
}
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
// SetID sets the "id" field.
|
|
|
|
func (ac *AttachmentCreate) SetID(u uuid.UUID) *AttachmentCreate {
|
|
|
|
ac.mutation.SetID(u)
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNillableID sets the "id" field if the given value is not nil.
|
|
|
|
func (ac *AttachmentCreate) SetNillableID(u *uuid.UUID) *AttachmentCreate {
|
|
|
|
if u != nil {
|
|
|
|
ac.SetID(*u)
|
|
|
|
}
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetItemID sets the "item" edge to the Item entity by ID.
|
|
|
|
func (ac *AttachmentCreate) SetItemID(id uuid.UUID) *AttachmentCreate {
|
|
|
|
ac.mutation.SetItemID(id)
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetItem sets the "item" edge to the Item entity.
|
|
|
|
func (ac *AttachmentCreate) SetItem(i *Item) *AttachmentCreate {
|
|
|
|
return ac.SetItemID(i.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDocumentID sets the "document" edge to the Document entity by ID.
|
|
|
|
func (ac *AttachmentCreate) SetDocumentID(id uuid.UUID) *AttachmentCreate {
|
|
|
|
ac.mutation.SetDocumentID(id)
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDocument sets the "document" edge to the Document entity.
|
|
|
|
func (ac *AttachmentCreate) SetDocument(d *Document) *AttachmentCreate {
|
|
|
|
return ac.SetDocumentID(d.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mutation returns the AttachmentMutation object of the builder.
|
|
|
|
func (ac *AttachmentCreate) Mutation() *AttachmentMutation {
|
|
|
|
return ac.mutation
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save creates the Attachment in the database.
|
|
|
|
func (ac *AttachmentCreate) Save(ctx context.Context) (*Attachment, error) {
|
|
|
|
ac.defaults()
|
2023-07-23 03:57:51 +00:00
|
|
|
return withHooks(ctx, ac.sqlSave, ac.mutation, ac.hooks)
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
|
|
func (ac *AttachmentCreate) SaveX(ctx context.Context) *Attachment {
|
|
|
|
v, err := ac.Save(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exec executes the query.
|
|
|
|
func (ac *AttachmentCreate) Exec(ctx context.Context) error {
|
|
|
|
_, err := ac.Save(ctx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
|
|
func (ac *AttachmentCreate) ExecX(ctx context.Context) {
|
|
|
|
if err := ac.Exec(ctx); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// defaults sets the default values of the builder before save.
|
|
|
|
func (ac *AttachmentCreate) defaults() {
|
|
|
|
if _, ok := ac.mutation.CreatedAt(); !ok {
|
|
|
|
v := attachment.DefaultCreatedAt()
|
|
|
|
ac.mutation.SetCreatedAt(v)
|
|
|
|
}
|
|
|
|
if _, ok := ac.mutation.UpdatedAt(); !ok {
|
|
|
|
v := attachment.DefaultUpdatedAt()
|
|
|
|
ac.mutation.SetUpdatedAt(v)
|
|
|
|
}
|
|
|
|
if _, ok := ac.mutation.GetType(); !ok {
|
|
|
|
v := attachment.DefaultType
|
|
|
|
ac.mutation.SetType(v)
|
|
|
|
}
|
2023-10-07 02:51:08 +00:00
|
|
|
if _, ok := ac.mutation.Primary(); !ok {
|
|
|
|
v := attachment.DefaultPrimary
|
|
|
|
ac.mutation.SetPrimary(v)
|
|
|
|
}
|
2022-09-12 22:47:27 +00:00
|
|
|
if _, ok := ac.mutation.ID(); !ok {
|
|
|
|
v := attachment.DefaultID()
|
|
|
|
ac.mutation.SetID(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
|
|
func (ac *AttachmentCreate) check() error {
|
|
|
|
if _, ok := ac.mutation.CreatedAt(); !ok {
|
|
|
|
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Attachment.created_at"`)}
|
|
|
|
}
|
|
|
|
if _, ok := ac.mutation.UpdatedAt(); !ok {
|
|
|
|
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Attachment.updated_at"`)}
|
|
|
|
}
|
|
|
|
if _, ok := ac.mutation.GetType(); !ok {
|
|
|
|
return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "Attachment.type"`)}
|
|
|
|
}
|
|
|
|
if v, ok := ac.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)}
|
|
|
|
}
|
|
|
|
}
|
2023-10-07 02:51:08 +00:00
|
|
|
if _, ok := ac.mutation.Primary(); !ok {
|
|
|
|
return &ValidationError{Name: "primary", err: errors.New(`ent: missing required field "Attachment.primary"`)}
|
|
|
|
}
|
2022-09-12 22:47:27 +00:00
|
|
|
if _, ok := ac.mutation.ItemID(); !ok {
|
|
|
|
return &ValidationError{Name: "item", err: errors.New(`ent: missing required edge "Attachment.item"`)}
|
|
|
|
}
|
|
|
|
if _, ok := ac.mutation.DocumentID(); !ok {
|
|
|
|
return &ValidationError{Name: "document", err: errors.New(`ent: missing required edge "Attachment.document"`)}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *AttachmentCreate) sqlSave(ctx context.Context) (*Attachment, error) {
|
2023-01-19 05:44:06 +00:00
|
|
|
if err := ac.check(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-12 22:47:27 +00:00
|
|
|
_node, _spec := ac.createSpec()
|
|
|
|
if err := sqlgraph.CreateNode(ctx, ac.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
|
|
|
|
}
|
|
|
|
}
|
2023-01-19 05:44:06 +00:00
|
|
|
ac.mutation.id = &_node.ID
|
|
|
|
ac.mutation.done = true
|
2022-09-12 22:47:27 +00:00
|
|
|
return _node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *AttachmentCreate) createSpec() (*Attachment, *sqlgraph.CreateSpec) {
|
|
|
|
var (
|
|
|
|
_node = &Attachment{config: ac.config}
|
2023-02-18 06:41:01 +00:00
|
|
|
_spec = sqlgraph.NewCreateSpec(attachment.Table, sqlgraph.NewFieldSpec(attachment.FieldID, field.TypeUUID))
|
2022-09-12 22:47:27 +00:00
|
|
|
)
|
|
|
|
if id, ok := ac.mutation.ID(); ok {
|
|
|
|
_node.ID = id
|
|
|
|
_spec.ID.Value = &id
|
|
|
|
}
|
|
|
|
if value, ok := ac.mutation.CreatedAt(); ok {
|
2022-12-02 01:45:28 +00:00
|
|
|
_spec.SetField(attachment.FieldCreatedAt, field.TypeTime, value)
|
2022-09-12 22:47:27 +00:00
|
|
|
_node.CreatedAt = value
|
|
|
|
}
|
|
|
|
if value, ok := ac.mutation.UpdatedAt(); ok {
|
2022-12-02 01:45:28 +00:00
|
|
|
_spec.SetField(attachment.FieldUpdatedAt, field.TypeTime, value)
|
2022-09-12 22:47:27 +00:00
|
|
|
_node.UpdatedAt = value
|
|
|
|
}
|
|
|
|
if value, ok := ac.mutation.GetType(); ok {
|
2022-12-02 01:45:28 +00:00
|
|
|
_spec.SetField(attachment.FieldType, field.TypeEnum, value)
|
2022-09-12 22:47:27 +00:00
|
|
|
_node.Type = value
|
|
|
|
}
|
2023-10-07 02:51:08 +00:00
|
|
|
if value, ok := ac.mutation.Primary(); ok {
|
|
|
|
_spec.SetField(attachment.FieldPrimary, field.TypeBool, value)
|
|
|
|
_node.Primary = value
|
|
|
|
}
|
2022-09-12 22:47:27 +00:00
|
|
|
if nodes := ac.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{
|
2023-03-23 04:49:49 +00:00
|
|
|
IDSpec: sqlgraph.NewFieldSpec(item.FieldID, field.TypeUUID),
|
2022-09-12 22:47:27 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
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 := ac.mutation.DocumentIDs(); len(nodes) > 0 {
|
|
|
|
edge := &sqlgraph.EdgeSpec{
|
|
|
|
Rel: sqlgraph.M2O,
|
|
|
|
Inverse: true,
|
|
|
|
Table: attachment.DocumentTable,
|
|
|
|
Columns: []string{attachment.DocumentColumn},
|
|
|
|
Bidi: false,
|
|
|
|
Target: &sqlgraph.EdgeTarget{
|
2023-03-23 04:49:49 +00:00
|
|
|
IDSpec: sqlgraph.NewFieldSpec(document.FieldID, field.TypeUUID),
|
2022-09-12 22:47:27 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, k := range nodes {
|
|
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
|
|
}
|
|
|
|
_node.document_attachments = &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
|
2023-11-16 02:30:49 +00:00
|
|
|
err error
|
2022-09-12 22:47:27 +00:00
|
|
|
builders []*AttachmentCreate
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save creates the Attachment entities in the database.
|
|
|
|
func (acb *AttachmentCreateBulk) Save(ctx context.Context) ([]*Attachment, error) {
|
2023-11-16 02:30:49 +00:00
|
|
|
if acb.err != nil {
|
|
|
|
return nil, acb.err
|
|
|
|
}
|
2022-09-12 22:47:27 +00:00
|
|
|
specs := make([]*sqlgraph.CreateSpec, len(acb.builders))
|
|
|
|
nodes := make([]*Attachment, len(acb.builders))
|
|
|
|
mutators := make([]Mutator, len(acb.builders))
|
|
|
|
for i := range acb.builders {
|
|
|
|
func(i int, root context.Context) {
|
|
|
|
builder := acb.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
|
2023-07-23 03:57:51 +00:00
|
|
|
nodes[i], specs[i] = builder.createSpec()
|
2022-09-12 22:47:27 +00:00
|
|
|
if i < len(mutators)-1 {
|
|
|
|
_, err = mutators[i+1].Mutate(root, acb.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, acb.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, acb.builders[0].mutation); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
|
|
func (acb *AttachmentCreateBulk) SaveX(ctx context.Context) []*Attachment {
|
|
|
|
v, err := acb.Save(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exec executes the query.
|
|
|
|
func (acb *AttachmentCreateBulk) Exec(ctx context.Context) error {
|
|
|
|
_, err := acb.Save(ctx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
|
|
func (acb *AttachmentCreateBulk) ExecX(ctx context.Context) {
|
|
|
|
if err := acb.Exec(ctx); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|