1
0
Fork 1
mirror of https://github.com/hay-kot/homebox.git synced 2024-12-29 10:06:37 +00:00
homebox/backend/ent/documenttoken_create.go

419 lines
12 KiB
Go
Raw Normal View History

// 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/hay-kot/homebox/backend/ent/document"
"github.com/hay-kot/homebox/backend/ent/documenttoken"
)
// DocumentTokenCreate is the builder for creating a DocumentToken entity.
type DocumentTokenCreate struct {
config
mutation *DocumentTokenMutation
hooks []Hook
}
// SetCreatedAt sets the "created_at" field.
func (dtc *DocumentTokenCreate) SetCreatedAt(t time.Time) *DocumentTokenCreate {
dtc.mutation.SetCreatedAt(t)
return dtc
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (dtc *DocumentTokenCreate) SetNillableCreatedAt(t *time.Time) *DocumentTokenCreate {
if t != nil {
dtc.SetCreatedAt(*t)
}
return dtc
}
// SetUpdatedAt sets the "updated_at" field.
func (dtc *DocumentTokenCreate) SetUpdatedAt(t time.Time) *DocumentTokenCreate {
dtc.mutation.SetUpdatedAt(t)
return dtc
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (dtc *DocumentTokenCreate) SetNillableUpdatedAt(t *time.Time) *DocumentTokenCreate {
if t != nil {
dtc.SetUpdatedAt(*t)
}
return dtc
}
// SetToken sets the "token" field.
func (dtc *DocumentTokenCreate) SetToken(b []byte) *DocumentTokenCreate {
dtc.mutation.SetToken(b)
return dtc
}
// SetUses sets the "uses" field.
func (dtc *DocumentTokenCreate) SetUses(i int) *DocumentTokenCreate {
dtc.mutation.SetUses(i)
return dtc
}
// SetNillableUses sets the "uses" field if the given value is not nil.
func (dtc *DocumentTokenCreate) SetNillableUses(i *int) *DocumentTokenCreate {
if i != nil {
dtc.SetUses(*i)
}
return dtc
}
// SetExpiresAt sets the "expires_at" field.
func (dtc *DocumentTokenCreate) SetExpiresAt(t time.Time) *DocumentTokenCreate {
dtc.mutation.SetExpiresAt(t)
return dtc
}
// SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
func (dtc *DocumentTokenCreate) SetNillableExpiresAt(t *time.Time) *DocumentTokenCreate {
if t != nil {
dtc.SetExpiresAt(*t)
}
return dtc
}
// SetID sets the "id" field.
func (dtc *DocumentTokenCreate) SetID(u uuid.UUID) *DocumentTokenCreate {
dtc.mutation.SetID(u)
return dtc
}
// SetNillableID sets the "id" field if the given value is not nil.
func (dtc *DocumentTokenCreate) SetNillableID(u *uuid.UUID) *DocumentTokenCreate {
if u != nil {
dtc.SetID(*u)
}
return dtc
}
// SetDocumentID sets the "document" edge to the Document entity by ID.
func (dtc *DocumentTokenCreate) SetDocumentID(id uuid.UUID) *DocumentTokenCreate {
dtc.mutation.SetDocumentID(id)
return dtc
}
// SetNillableDocumentID sets the "document" edge to the Document entity by ID if the given value is not nil.
func (dtc *DocumentTokenCreate) SetNillableDocumentID(id *uuid.UUID) *DocumentTokenCreate {
if id != nil {
dtc = dtc.SetDocumentID(*id)
}
return dtc
}
// SetDocument sets the "document" edge to the Document entity.
func (dtc *DocumentTokenCreate) SetDocument(d *Document) *DocumentTokenCreate {
return dtc.SetDocumentID(d.ID)
}
// Mutation returns the DocumentTokenMutation object of the builder.
func (dtc *DocumentTokenCreate) Mutation() *DocumentTokenMutation {
return dtc.mutation
}
// Save creates the DocumentToken in the database.
func (dtc *DocumentTokenCreate) Save(ctx context.Context) (*DocumentToken, error) {
var (
err error
node *DocumentToken
)
dtc.defaults()
if len(dtc.hooks) == 0 {
if err = dtc.check(); err != nil {
return nil, err
}
node, err = dtc.sqlSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*DocumentTokenMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err = dtc.check(); err != nil {
return nil, err
}
dtc.mutation = mutation
if node, err = dtc.sqlSave(ctx); err != nil {
return nil, err
}
mutation.id = &node.ID
mutation.done = true
return node, err
})
for i := len(dtc.hooks) - 1; i >= 0; i-- {
if dtc.hooks[i] == nil {
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
}
mut = dtc.hooks[i](mut)
}
v, err := mut.Mutate(ctx, dtc.mutation)
if err != nil {
return nil, err
}
nv, ok := v.(*DocumentToken)
if !ok {
return nil, fmt.Errorf("unexpected node type %T returned from DocumentTokenMutation", v)
}
node = nv
}
return node, err
}
// SaveX calls Save and panics if Save returns an error.
func (dtc *DocumentTokenCreate) SaveX(ctx context.Context) *DocumentToken {
v, err := dtc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (dtc *DocumentTokenCreate) Exec(ctx context.Context) error {
_, err := dtc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (dtc *DocumentTokenCreate) ExecX(ctx context.Context) {
if err := dtc.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (dtc *DocumentTokenCreate) defaults() {
if _, ok := dtc.mutation.CreatedAt(); !ok {
v := documenttoken.DefaultCreatedAt()
dtc.mutation.SetCreatedAt(v)
}
if _, ok := dtc.mutation.UpdatedAt(); !ok {
v := documenttoken.DefaultUpdatedAt()
dtc.mutation.SetUpdatedAt(v)
}
if _, ok := dtc.mutation.Uses(); !ok {
v := documenttoken.DefaultUses
dtc.mutation.SetUses(v)
}
if _, ok := dtc.mutation.ExpiresAt(); !ok {
v := documenttoken.DefaultExpiresAt()
dtc.mutation.SetExpiresAt(v)
}
if _, ok := dtc.mutation.ID(); !ok {
v := documenttoken.DefaultID()
dtc.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (dtc *DocumentTokenCreate) check() error {
if _, ok := dtc.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "DocumentToken.created_at"`)}
}
if _, ok := dtc.mutation.UpdatedAt(); !ok {
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "DocumentToken.updated_at"`)}
}
if _, ok := dtc.mutation.Token(); !ok {
return &ValidationError{Name: "token", err: errors.New(`ent: missing required field "DocumentToken.token"`)}
}
if v, ok := dtc.mutation.Token(); ok {
if err := documenttoken.TokenValidator(v); err != nil {
return &ValidationError{Name: "token", err: fmt.Errorf(`ent: validator failed for field "DocumentToken.token": %w`, err)}
}
}
if _, ok := dtc.mutation.Uses(); !ok {
return &ValidationError{Name: "uses", err: errors.New(`ent: missing required field "DocumentToken.uses"`)}
}
if _, ok := dtc.mutation.ExpiresAt(); !ok {
return &ValidationError{Name: "expires_at", err: errors.New(`ent: missing required field "DocumentToken.expires_at"`)}
}
return nil
}
func (dtc *DocumentTokenCreate) sqlSave(ctx context.Context) (*DocumentToken, error) {
_node, _spec := dtc.createSpec()
if err := sqlgraph.CreateNode(ctx, dtc.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
}
}
return _node, nil
}
func (dtc *DocumentTokenCreate) createSpec() (*DocumentToken, *sqlgraph.CreateSpec) {
var (
_node = &DocumentToken{config: dtc.config}
_spec = &sqlgraph.CreateSpec{
Table: documenttoken.Table,
ID: &sqlgraph.FieldSpec{
Type: field.TypeUUID,
Column: documenttoken.FieldID,
},
}
)
if id, ok := dtc.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = &id
}
if value, ok := dtc.mutation.CreatedAt(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeTime,
Value: value,
Column: documenttoken.FieldCreatedAt,
})
_node.CreatedAt = value
}
if value, ok := dtc.mutation.UpdatedAt(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeTime,
Value: value,
Column: documenttoken.FieldUpdatedAt,
})
_node.UpdatedAt = value
}
if value, ok := dtc.mutation.Token(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeBytes,
Value: value,
Column: documenttoken.FieldToken,
})
_node.Token = value
}
if value, ok := dtc.mutation.Uses(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeInt,
Value: value,
Column: documenttoken.FieldUses,
})
_node.Uses = value
}
if value, ok := dtc.mutation.ExpiresAt(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeTime,
Value: value,
Column: documenttoken.FieldExpiresAt,
})
_node.ExpiresAt = value
}
if nodes := dtc.mutation.DocumentIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: documenttoken.DocumentTable,
Columns: []string{documenttoken.DocumentColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeUUID,
Column: document.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.document_document_tokens = &nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// DocumentTokenCreateBulk is the builder for creating many DocumentToken entities in bulk.
type DocumentTokenCreateBulk struct {
config
builders []*DocumentTokenCreate
}
// Save creates the DocumentToken entities in the database.
func (dtcb *DocumentTokenCreateBulk) Save(ctx context.Context) ([]*DocumentToken, error) {
specs := make([]*sqlgraph.CreateSpec, len(dtcb.builders))
nodes := make([]*DocumentToken, len(dtcb.builders))
mutators := make([]Mutator, len(dtcb.builders))
for i := range dtcb.builders {
func(i int, root context.Context) {
builder := dtcb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*DocumentTokenMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err := builder.check(); err != nil {
return nil, err
}
builder.mutation = mutation
nodes[i], specs[i] = builder.createSpec()
var err error
if i < len(mutators)-1 {
_, err = mutators[i+1].Mutate(root, dtcb.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, dtcb.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, dtcb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (dtcb *DocumentTokenCreateBulk) SaveX(ctx context.Context) []*DocumentToken {
v, err := dtcb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (dtcb *DocumentTokenCreateBulk) Exec(ctx context.Context) error {
_, err := dtcb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (dtcb *DocumentTokenCreateBulk) ExecX(ctx context.Context) {
if err := dtcb.Exec(ctx); err != nil {
panic(err)
}
}