manager/ent/alias_create.go

352 lines
8.7 KiB
Go
Raw Normal View History

2022-04-08 21:26:25 +02:00
// Code generated by entc, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"code.icod.de/postfix/manager/ent/alias"
"code.icod.de/postfix/manager/ent/domain"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
)
// AliasCreate is the builder for creating a Alias entity.
type AliasCreate struct {
config
mutation *AliasMutation
hooks []Hook
}
// SetCreated sets the "created" field.
func (ac *AliasCreate) SetCreated(t time.Time) *AliasCreate {
ac.mutation.SetCreated(t)
return ac
}
// SetNillableCreated sets the "created" field if the given value is not nil.
func (ac *AliasCreate) SetNillableCreated(t *time.Time) *AliasCreate {
if t != nil {
ac.SetCreated(*t)
}
return ac
}
// SetModified sets the "modified" field.
func (ac *AliasCreate) SetModified(t time.Time) *AliasCreate {
ac.mutation.SetModified(t)
return ac
}
// SetNillableModified sets the "modified" field if the given value is not nil.
func (ac *AliasCreate) SetNillableModified(t *time.Time) *AliasCreate {
if t != nil {
ac.SetModified(*t)
}
return ac
}
// SetDomainID sets the "domain_id" field.
func (ac *AliasCreate) SetDomainID(i int64) *AliasCreate {
ac.mutation.SetDomainID(i)
return ac
}
// SetNillableDomainID sets the "domain_id" field if the given value is not nil.
func (ac *AliasCreate) SetNillableDomainID(i *int64) *AliasCreate {
if i != nil {
ac.SetDomainID(*i)
}
return ac
}
// SetGoto sets the "goto" field.
func (ac *AliasCreate) SetGoto(s string) *AliasCreate {
ac.mutation.SetGoto(s)
return ac
}
// SetActive sets the "active" field.
func (ac *AliasCreate) SetActive(b bool) *AliasCreate {
ac.mutation.SetActive(b)
return ac
}
// SetID sets the "id" field.
func (ac *AliasCreate) SetID(i int64) *AliasCreate {
ac.mutation.SetID(i)
return ac
}
// SetDomain sets the "domain" edge to the Domain entity.
func (ac *AliasCreate) SetDomain(d *Domain) *AliasCreate {
return ac.SetDomainID(d.ID)
}
// Mutation returns the AliasMutation object of the builder.
func (ac *AliasCreate) Mutation() *AliasMutation {
return ac.mutation
}
// Save creates the Alias in the database.
func (ac *AliasCreate) Save(ctx context.Context) (*Alias, error) {
var (
err error
node *Alias
)
ac.defaults()
if len(ac.hooks) == 0 {
if err = ac.check(); err != nil {
return nil, err
}
node, err = ac.sqlSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*AliasMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err = ac.check(); err != nil {
return nil, err
}
ac.mutation = mutation
if node, err = ac.sqlSave(ctx); err != nil {
return nil, err
}
mutation.id = &node.ID
mutation.done = true
return node, err
})
for i := len(ac.hooks) - 1; i >= 0; i-- {
if ac.hooks[i] == nil {
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
}
mut = ac.hooks[i](mut)
}
if _, err := mut.Mutate(ctx, ac.mutation); err != nil {
return nil, err
}
}
return node, err
}
// SaveX calls Save and panics if Save returns an error.
func (ac *AliasCreate) SaveX(ctx context.Context) *Alias {
v, err := ac.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (ac *AliasCreate) Exec(ctx context.Context) error {
_, err := ac.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (ac *AliasCreate) 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 *AliasCreate) defaults() {
if _, ok := ac.mutation.Created(); !ok {
v := alias.DefaultCreated()
ac.mutation.SetCreated(v)
}
if _, ok := ac.mutation.Modified(); !ok {
v := alias.DefaultModified()
ac.mutation.SetModified(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (ac *AliasCreate) check() error {
if _, ok := ac.mutation.Created(); !ok {
return &ValidationError{Name: "created", err: errors.New(`ent: missing required field "Alias.created"`)}
}
if _, ok := ac.mutation.Goto(); !ok {
return &ValidationError{Name: "goto", err: errors.New(`ent: missing required field "Alias.goto"`)}
}
if _, ok := ac.mutation.Active(); !ok {
return &ValidationError{Name: "active", err: errors.New(`ent: missing required field "Alias.active"`)}
}
return nil
}
func (ac *AliasCreate) sqlSave(ctx context.Context) (*Alias, error) {
_node, _spec := ac.createSpec()
if err := sqlgraph.CreateNode(ctx, ac.driver, _spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{err.Error(), err}
}
return nil, err
}
if _spec.ID.Value != _node.ID {
id := _spec.ID.Value.(int64)
_node.ID = int64(id)
}
return _node, nil
}
func (ac *AliasCreate) createSpec() (*Alias, *sqlgraph.CreateSpec) {
var (
_node = &Alias{config: ac.config}
_spec = &sqlgraph.CreateSpec{
Table: alias.Table,
ID: &sqlgraph.FieldSpec{
Type: field.TypeInt64,
Column: alias.FieldID,
},
}
)
if id, ok := ac.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = id
}
if value, ok := ac.mutation.Created(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeTime,
Value: value,
Column: alias.FieldCreated,
})
_node.Created = value
}
if value, ok := ac.mutation.Modified(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeTime,
Value: value,
Column: alias.FieldModified,
})
_node.Modified = &value
}
if value, ok := ac.mutation.Goto(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: alias.FieldGoto,
})
_node.Goto = value
}
if value, ok := ac.mutation.Active(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeBool,
Value: value,
Column: alias.FieldActive,
})
_node.Active = value
}
if nodes := ac.mutation.DomainIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: alias.DomainTable,
Columns: []string{alias.DomainColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt64,
Column: domain.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.DomainID = nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// AliasCreateBulk is the builder for creating many Alias entities in bulk.
type AliasCreateBulk struct {
config
builders []*AliasCreate
}
// Save creates the Alias entities in the database.
func (acb *AliasCreateBulk) Save(ctx context.Context) ([]*Alias, error) {
specs := make([]*sqlgraph.CreateSpec, len(acb.builders))
nodes := make([]*Alias, 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.(*AliasMutation)
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, 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{err.Error(), err}
}
}
}
if err != nil {
return nil, err
}
mutation.id = &nodes[i].ID
mutation.done = true
if specs[i].ID.Value != nil && nodes[i].ID == 0 {
id := specs[i].ID.Value.(int64)
nodes[i].ID = int64(id)
}
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 *AliasCreateBulk) SaveX(ctx context.Context) []*Alias {
v, err := acb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (acb *AliasCreateBulk) Exec(ctx context.Context) error {
_, err := acb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (acb *AliasCreateBulk) ExecX(ctx context.Context) {
if err := acb.Exec(ctx); err != nil {
panic(err)
}
}