// Code generated by entc, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "time" "code.icod.de/postfix/manager/ent/domain" "code.icod.de/postfix/manager/ent/mailbox" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" ) // MailboxCreate is the builder for creating a Mailbox entity. type MailboxCreate struct { config mutation *MailboxMutation hooks []Hook } // SetActive sets the "active" field. func (mc *MailboxCreate) SetActive(b bool) *MailboxCreate { mc.mutation.SetActive(b) return mc } // SetCreated sets the "created" field. func (mc *MailboxCreate) SetCreated(t time.Time) *MailboxCreate { mc.mutation.SetCreated(t) return mc } // SetNillableCreated sets the "created" field if the given value is not nil. func (mc *MailboxCreate) SetNillableCreated(t *time.Time) *MailboxCreate { if t != nil { mc.SetCreated(*t) } return mc } // SetModified sets the "modified" field. func (mc *MailboxCreate) SetModified(t time.Time) *MailboxCreate { mc.mutation.SetModified(t) return mc } // SetNillableModified sets the "modified" field if the given value is not nil. func (mc *MailboxCreate) SetNillableModified(t *time.Time) *MailboxCreate { if t != nil { mc.SetModified(*t) } return mc } // SetDomainID sets the "domain_id" field. func (mc *MailboxCreate) SetDomainID(i int64) *MailboxCreate { mc.mutation.SetDomainID(i) return mc } // SetNillableDomainID sets the "domain_id" field if the given value is not nil. func (mc *MailboxCreate) SetNillableDomainID(i *int64) *MailboxCreate { if i != nil { mc.SetDomainID(*i) } return mc } // SetUsername sets the "username" field. func (mc *MailboxCreate) SetUsername(s string) *MailboxCreate { mc.mutation.SetUsername(s) return mc } // SetPassword sets the "password" field. func (mc *MailboxCreate) SetPassword(b []byte) *MailboxCreate { mc.mutation.SetPassword(b) return mc } // SetName sets the "name" field. func (mc *MailboxCreate) SetName(s string) *MailboxCreate { mc.mutation.SetName(s) return mc } // SetNillableName sets the "name" field if the given value is not nil. func (mc *MailboxCreate) SetNillableName(s *string) *MailboxCreate { if s != nil { mc.SetName(*s) } return mc } // SetQuota sets the "quota" field. func (mc *MailboxCreate) SetQuota(i int64) *MailboxCreate { mc.mutation.SetQuota(i) return mc } // SetLocalPart sets the "local_part" field. func (mc *MailboxCreate) SetLocalPart(s string) *MailboxCreate { mc.mutation.SetLocalPart(s) return mc } // SetHomedir sets the "homedir" field. func (mc *MailboxCreate) SetHomedir(s string) *MailboxCreate { mc.mutation.SetHomedir(s) return mc } // SetNillableHomedir sets the "homedir" field if the given value is not nil. func (mc *MailboxCreate) SetNillableHomedir(s *string) *MailboxCreate { if s != nil { mc.SetHomedir(*s) } return mc } // SetMaildir sets the "maildir" field. func (mc *MailboxCreate) SetMaildir(s string) *MailboxCreate { mc.mutation.SetMaildir(s) return mc } // SetNillableMaildir sets the "maildir" field if the given value is not nil. func (mc *MailboxCreate) SetNillableMaildir(s *string) *MailboxCreate { if s != nil { mc.SetMaildir(*s) } return mc } // SetUID sets the "uid" field. func (mc *MailboxCreate) SetUID(i int32) *MailboxCreate { mc.mutation.SetUID(i) return mc } // SetNillableUID sets the "uid" field if the given value is not nil. func (mc *MailboxCreate) SetNillableUID(i *int32) *MailboxCreate { if i != nil { mc.SetUID(*i) } return mc } // SetGid sets the "gid" field. func (mc *MailboxCreate) SetGid(i int32) *MailboxCreate { mc.mutation.SetGid(i) return mc } // SetNillableGid sets the "gid" field if the given value is not nil. func (mc *MailboxCreate) SetNillableGid(i *int32) *MailboxCreate { if i != nil { mc.SetGid(*i) } return mc } // SetID sets the "id" field. func (mc *MailboxCreate) SetID(i int64) *MailboxCreate { mc.mutation.SetID(i) return mc } // SetDomain sets the "domain" edge to the Domain entity. func (mc *MailboxCreate) SetDomain(d *Domain) *MailboxCreate { return mc.SetDomainID(d.ID) } // Mutation returns the MailboxMutation object of the builder. func (mc *MailboxCreate) Mutation() *MailboxMutation { return mc.mutation } // Save creates the Mailbox in the database. func (mc *MailboxCreate) Save(ctx context.Context) (*Mailbox, error) { var ( err error node *Mailbox ) mc.defaults() if len(mc.hooks) == 0 { if err = mc.check(); err != nil { return nil, err } node, err = mc.sqlSave(ctx) } else { var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*MailboxMutation) if !ok { return nil, fmt.Errorf("unexpected mutation type %T", m) } if err = mc.check(); err != nil { return nil, err } mc.mutation = mutation if node, err = mc.sqlSave(ctx); err != nil { return nil, err } mutation.id = &node.ID mutation.done = true return node, err }) for i := len(mc.hooks) - 1; i >= 0; i-- { if mc.hooks[i] == nil { return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") } mut = mc.hooks[i](mut) } if _, err := mut.Mutate(ctx, mc.mutation); err != nil { return nil, err } } return node, err } // SaveX calls Save and panics if Save returns an error. func (mc *MailboxCreate) SaveX(ctx context.Context) *Mailbox { v, err := mc.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (mc *MailboxCreate) Exec(ctx context.Context) error { _, err := mc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (mc *MailboxCreate) ExecX(ctx context.Context) { if err := mc.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (mc *MailboxCreate) defaults() { if _, ok := mc.mutation.Created(); !ok { v := mailbox.DefaultCreated() mc.mutation.SetCreated(v) } if _, ok := mc.mutation.Modified(); !ok { v := mailbox.DefaultModified() mc.mutation.SetModified(v) } } // check runs all checks and user-defined validators on the builder. func (mc *MailboxCreate) check() error { if _, ok := mc.mutation.Active(); !ok { return &ValidationError{Name: "active", err: errors.New(`ent: missing required field "Mailbox.active"`)} } if _, ok := mc.mutation.Created(); !ok { return &ValidationError{Name: "created", err: errors.New(`ent: missing required field "Mailbox.created"`)} } if _, ok := mc.mutation.Username(); !ok { return &ValidationError{Name: "username", err: errors.New(`ent: missing required field "Mailbox.username"`)} } if _, ok := mc.mutation.Password(); !ok { return &ValidationError{Name: "password", err: errors.New(`ent: missing required field "Mailbox.password"`)} } if _, ok := mc.mutation.Quota(); !ok { return &ValidationError{Name: "quota", err: errors.New(`ent: missing required field "Mailbox.quota"`)} } if _, ok := mc.mutation.LocalPart(); !ok { return &ValidationError{Name: "local_part", err: errors.New(`ent: missing required field "Mailbox.local_part"`)} } return nil } func (mc *MailboxCreate) sqlSave(ctx context.Context) (*Mailbox, error) { _node, _spec := mc.createSpec() if err := sqlgraph.CreateNode(ctx, mc.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 (mc *MailboxCreate) createSpec() (*Mailbox, *sqlgraph.CreateSpec) { var ( _node = &Mailbox{config: mc.config} _spec = &sqlgraph.CreateSpec{ Table: mailbox.Table, ID: &sqlgraph.FieldSpec{ Type: field.TypeInt64, Column: mailbox.FieldID, }, } ) if id, ok := mc.mutation.ID(); ok { _node.ID = id _spec.ID.Value = id } if value, ok := mc.mutation.Active(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeBool, Value: value, Column: mailbox.FieldActive, }) _node.Active = value } if value, ok := mc.mutation.Created(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeTime, Value: value, Column: mailbox.FieldCreated, }) _node.Created = value } if value, ok := mc.mutation.Modified(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeTime, Value: value, Column: mailbox.FieldModified, }) _node.Modified = &value } if value, ok := mc.mutation.Username(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeString, Value: value, Column: mailbox.FieldUsername, }) _node.Username = value } if value, ok := mc.mutation.Password(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeBytes, Value: value, Column: mailbox.FieldPassword, }) _node.Password = value } if value, ok := mc.mutation.Name(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeString, Value: value, Column: mailbox.FieldName, }) _node.Name = &value } if value, ok := mc.mutation.Quota(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeInt64, Value: value, Column: mailbox.FieldQuota, }) _node.Quota = value } if value, ok := mc.mutation.LocalPart(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeString, Value: value, Column: mailbox.FieldLocalPart, }) _node.LocalPart = value } if value, ok := mc.mutation.Homedir(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeString, Value: value, Column: mailbox.FieldHomedir, }) _node.Homedir = &value } if value, ok := mc.mutation.Maildir(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeString, Value: value, Column: mailbox.FieldMaildir, }) _node.Maildir = &value } if value, ok := mc.mutation.UID(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeInt32, Value: value, Column: mailbox.FieldUID, }) _node.UID = &value } if value, ok := mc.mutation.Gid(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeInt32, Value: value, Column: mailbox.FieldGid, }) _node.Gid = &value } if nodes := mc.mutation.DomainIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: mailbox.DomainTable, Columns: []string{mailbox.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 } // MailboxCreateBulk is the builder for creating many Mailbox entities in bulk. type MailboxCreateBulk struct { config builders []*MailboxCreate } // Save creates the Mailbox entities in the database. func (mcb *MailboxCreateBulk) Save(ctx context.Context) ([]*Mailbox, error) { specs := make([]*sqlgraph.CreateSpec, len(mcb.builders)) nodes := make([]*Mailbox, len(mcb.builders)) mutators := make([]Mutator, len(mcb.builders)) for i := range mcb.builders { func(i int, root context.Context) { builder := mcb.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*MailboxMutation) 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, mcb.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, mcb.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, mcb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (mcb *MailboxCreateBulk) SaveX(ctx context.Context) []*Mailbox { v, err := mcb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (mcb *MailboxCreateBulk) Exec(ctx context.Context) error { _, err := mcb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (mcb *MailboxCreateBulk) ExecX(ctx context.Context) { if err := mcb.Exec(ctx); err != nil { panic(err) } }