manager/ent/account_create.go

416 lines
11 KiB
Go

// Code generated by entc, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"code.icod.de/postfix/manager/ent/account"
"code.icod.de/postfix/manager/ent/domain"
"code.icod.de/postfix/manager/ent/logentry"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
)
// AccountCreate is the builder for creating a Account entity.
type AccountCreate struct {
config
mutation *AccountMutation
hooks []Hook
}
// SetCreated sets the "created" field.
func (ac *AccountCreate) SetCreated(t time.Time) *AccountCreate {
ac.mutation.SetCreated(t)
return ac
}
// SetNillableCreated sets the "created" field if the given value is not nil.
func (ac *AccountCreate) SetNillableCreated(t *time.Time) *AccountCreate {
if t != nil {
ac.SetCreated(*t)
}
return ac
}
// SetModified sets the "modified" field.
func (ac *AccountCreate) SetModified(t time.Time) *AccountCreate {
ac.mutation.SetModified(t)
return ac
}
// SetNillableModified sets the "modified" field if the given value is not nil.
func (ac *AccountCreate) SetNillableModified(t *time.Time) *AccountCreate {
if t != nil {
ac.SetModified(*t)
}
return ac
}
// SetUsername sets the "username" field.
func (ac *AccountCreate) SetUsername(s string) *AccountCreate {
ac.mutation.SetUsername(s)
return ac
}
// SetPassword sets the "password" field.
func (ac *AccountCreate) SetPassword(b []byte) *AccountCreate {
ac.mutation.SetPassword(b)
return ac
}
// SetSuper sets the "super" field.
func (ac *AccountCreate) SetSuper(b bool) *AccountCreate {
ac.mutation.SetSuper(b)
return ac
}
// SetActive sets the "active" field.
func (ac *AccountCreate) SetActive(b bool) *AccountCreate {
ac.mutation.SetActive(b)
return ac
}
// SetID sets the "id" field.
func (ac *AccountCreate) SetID(i int64) *AccountCreate {
ac.mutation.SetID(i)
return ac
}
// AddDomainIDs adds the "domains" edge to the Domain entity by IDs.
func (ac *AccountCreate) AddDomainIDs(ids ...int64) *AccountCreate {
ac.mutation.AddDomainIDs(ids...)
return ac
}
// AddDomains adds the "domains" edges to the Domain entity.
func (ac *AccountCreate) AddDomains(d ...*Domain) *AccountCreate {
ids := make([]int64, len(d))
for i := range d {
ids[i] = d[i].ID
}
return ac.AddDomainIDs(ids...)
}
// AddLogIDs adds the "logs" edge to the Logentry entity by IDs.
func (ac *AccountCreate) AddLogIDs(ids ...int64) *AccountCreate {
ac.mutation.AddLogIDs(ids...)
return ac
}
// AddLogs adds the "logs" edges to the Logentry entity.
func (ac *AccountCreate) AddLogs(l ...*Logentry) *AccountCreate {
ids := make([]int64, len(l))
for i := range l {
ids[i] = l[i].ID
}
return ac.AddLogIDs(ids...)
}
// Mutation returns the AccountMutation object of the builder.
func (ac *AccountCreate) Mutation() *AccountMutation {
return ac.mutation
}
// Save creates the Account in the database.
func (ac *AccountCreate) Save(ctx context.Context) (*Account, error) {
var (
err error
node *Account
)
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.(*AccountMutation)
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 *AccountCreate) SaveX(ctx context.Context) *Account {
v, err := ac.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (ac *AccountCreate) Exec(ctx context.Context) error {
_, err := ac.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (ac *AccountCreate) 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 *AccountCreate) defaults() {
if _, ok := ac.mutation.Created(); !ok {
v := account.DefaultCreated()
ac.mutation.SetCreated(v)
}
if _, ok := ac.mutation.Modified(); !ok {
v := account.DefaultModified()
ac.mutation.SetModified(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (ac *AccountCreate) check() error {
if _, ok := ac.mutation.Created(); !ok {
return &ValidationError{Name: "created", err: errors.New(`ent: missing required field "Account.created"`)}
}
if _, ok := ac.mutation.Username(); !ok {
return &ValidationError{Name: "username", err: errors.New(`ent: missing required field "Account.username"`)}
}
if _, ok := ac.mutation.Password(); !ok {
return &ValidationError{Name: "password", err: errors.New(`ent: missing required field "Account.password"`)}
}
if _, ok := ac.mutation.Super(); !ok {
return &ValidationError{Name: "super", err: errors.New(`ent: missing required field "Account.super"`)}
}
if _, ok := ac.mutation.Active(); !ok {
return &ValidationError{Name: "active", err: errors.New(`ent: missing required field "Account.active"`)}
}
return nil
}
func (ac *AccountCreate) sqlSave(ctx context.Context) (*Account, 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 *AccountCreate) createSpec() (*Account, *sqlgraph.CreateSpec) {
var (
_node = &Account{config: ac.config}
_spec = &sqlgraph.CreateSpec{
Table: account.Table,
ID: &sqlgraph.FieldSpec{
Type: field.TypeInt64,
Column: account.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: account.FieldCreated,
})
_node.Created = value
}
if value, ok := ac.mutation.Modified(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeTime,
Value: value,
Column: account.FieldModified,
})
_node.Modified = value
}
if value, ok := ac.mutation.Username(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: account.FieldUsername,
})
_node.Username = value
}
if value, ok := ac.mutation.Password(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeBytes,
Value: value,
Column: account.FieldPassword,
})
_node.Password = value
}
if value, ok := ac.mutation.Super(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeBool,
Value: value,
Column: account.FieldSuper,
})
_node.Super = value
}
if value, ok := ac.mutation.Active(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeBool,
Value: value,
Column: account.FieldActive,
})
_node.Active = value
}
if nodes := ac.mutation.DomainsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: false,
Table: account.DomainsTable,
Columns: account.DomainsPrimaryKey,
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)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := ac.mutation.LogsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: account.LogsTable,
Columns: []string{account.LogsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt64,
Column: logentry.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// AccountCreateBulk is the builder for creating many Account entities in bulk.
type AccountCreateBulk struct {
config
builders []*AccountCreate
}
// Save creates the Account entities in the database.
func (acb *AccountCreateBulk) Save(ctx context.Context) ([]*Account, error) {
specs := make([]*sqlgraph.CreateSpec, len(acb.builders))
nodes := make([]*Account, 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.(*AccountMutation)
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 *AccountCreateBulk) SaveX(ctx context.Context) []*Account {
v, err := acb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (acb *AccountCreateBulk) Exec(ctx context.Context) error {
_, err := acb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (acb *AccountCreateBulk) ExecX(ctx context.Context) {
if err := acb.Exec(ctx); err != nil {
panic(err)
}
}