accountserver/ent/account_create.go

352 lines
9.5 KiB
Go
Raw Permalink Normal View History

2024-08-19 13:55:36 +02:00
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"code.icod.de/auth/accountserver/ent/account"
"code.icod.de/auth/accountserver/ent/email"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/google/uuid"
)
// AccountCreate is the builder for creating a Account entity.
type AccountCreate struct {
config
mutation *AccountMutation
hooks []Hook
}
// SetCreatedAt sets the "created_at" field.
func (ac *AccountCreate) SetCreatedAt(t time.Time) *AccountCreate {
ac.mutation.SetCreatedAt(t)
return ac
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (ac *AccountCreate) SetNillableCreatedAt(t *time.Time) *AccountCreate {
if t != nil {
ac.SetCreatedAt(*t)
}
return ac
}
// SetUpdatedAt sets the "updated_at" field.
func (ac *AccountCreate) SetUpdatedAt(t time.Time) *AccountCreate {
ac.mutation.SetUpdatedAt(t)
return ac
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (ac *AccountCreate) SetNillableUpdatedAt(t *time.Time) *AccountCreate {
if t != nil {
ac.SetUpdatedAt(*t)
}
return ac
}
// SetNickname sets the "nickname" field.
func (ac *AccountCreate) SetNickname(s string) *AccountCreate {
ac.mutation.SetNickname(s)
return ac
}
2024-09-03 15:56:34 +02:00
// SetNillableNickname sets the "nickname" field if the given value is not nil.
func (ac *AccountCreate) SetNillableNickname(s *string) *AccountCreate {
if s != nil {
ac.SetNickname(*s)
}
return ac
}
2024-08-19 13:55:36 +02:00
// SetName sets the "name" field.
func (ac *AccountCreate) SetName(s string) *AccountCreate {
ac.mutation.SetName(s)
return ac
}
2024-09-03 15:56:34 +02:00
// SetNillableName sets the "name" field if the given value is not nil.
func (ac *AccountCreate) SetNillableName(s *string) *AccountCreate {
if s != nil {
ac.SetName(*s)
}
return ac
}
2024-08-19 13:55:36 +02:00
// SetSecret sets the "secret" field.
func (ac *AccountCreate) SetSecret(b []byte) *AccountCreate {
ac.mutation.SetSecret(b)
return ac
}
// SetAes sets the "aes" field.
func (ac *AccountCreate) SetAes(b []byte) *AccountCreate {
ac.mutation.SetAes(b)
return ac
}
// SetX509 sets the "x509" field.
func (ac *AccountCreate) SetX509(b []byte) *AccountCreate {
ac.mutation.SetX509(b)
return ac
}
// SetID sets the "id" field.
func (ac *AccountCreate) SetID(u uuid.UUID) *AccountCreate {
ac.mutation.SetID(u)
return ac
}
// AddEmailIDs adds the "emails" edge to the Email entity by IDs.
func (ac *AccountCreate) AddEmailIDs(ids ...uuid.UUID) *AccountCreate {
ac.mutation.AddEmailIDs(ids...)
return ac
}
// AddEmails adds the "emails" edges to the Email entity.
func (ac *AccountCreate) AddEmails(e ...*Email) *AccountCreate {
ids := make([]uuid.UUID, len(e))
for i := range e {
ids[i] = e[i].ID
}
return ac.AddEmailIDs(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) {
ac.defaults()
return withHooks(ctx, ac.sqlSave, ac.mutation, ac.hooks)
}
// 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.CreatedAt(); !ok {
v := account.DefaultCreatedAt()
ac.mutation.SetCreatedAt(v)
}
if _, ok := ac.mutation.UpdatedAt(); !ok {
v := account.DefaultUpdatedAt()
ac.mutation.SetUpdatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (ac *AccountCreate) check() error {
if _, ok := ac.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Account.created_at"`)}
}
if _, ok := ac.mutation.UpdatedAt(); !ok {
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Account.updated_at"`)}
}
if _, ok := ac.mutation.Secret(); !ok {
return &ValidationError{Name: "secret", err: errors.New(`ent: missing required field "Account.secret"`)}
}
if _, ok := ac.mutation.Aes(); !ok {
return &ValidationError{Name: "aes", err: errors.New(`ent: missing required field "Account.aes"`)}
}
if v, ok := ac.mutation.Aes(); ok {
if err := account.AesValidator(v); err != nil {
return &ValidationError{Name: "aes", err: fmt.Errorf(`ent: validator failed for field "Account.aes": %w`, err)}
}
}
if _, ok := ac.mutation.X509(); !ok {
return &ValidationError{Name: "x509", err: errors.New(`ent: missing required field "Account.x509"`)}
}
return nil
}
func (ac *AccountCreate) sqlSave(ctx context.Context) (*Account, error) {
if err := ac.check(); err != nil {
return nil, err
}
_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
}
}
ac.mutation.id = &_node.ID
ac.mutation.done = true
return _node, nil
}
func (ac *AccountCreate) createSpec() (*Account, *sqlgraph.CreateSpec) {
var (
_node = &Account{config: ac.config}
_spec = sqlgraph.NewCreateSpec(account.Table, sqlgraph.NewFieldSpec(account.FieldID, field.TypeUUID))
)
if id, ok := ac.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = &id
}
if value, ok := ac.mutation.CreatedAt(); ok {
_spec.SetField(account.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := ac.mutation.UpdatedAt(); ok {
_spec.SetField(account.FieldUpdatedAt, field.TypeTime, value)
_node.UpdatedAt = value
}
if value, ok := ac.mutation.Nickname(); ok {
_spec.SetField(account.FieldNickname, field.TypeString, value)
_node.Nickname = value
}
if value, ok := ac.mutation.Name(); ok {
_spec.SetField(account.FieldName, field.TypeString, value)
_node.Name = value
}
if value, ok := ac.mutation.Secret(); ok {
_spec.SetField(account.FieldSecret, field.TypeBytes, value)
_node.Secret = value
}
if value, ok := ac.mutation.Aes(); ok {
_spec.SetField(account.FieldAes, field.TypeBytes, value)
_node.Aes = value
}
if value, ok := ac.mutation.X509(); ok {
_spec.SetField(account.FieldX509, field.TypeBytes, value)
_node.X509 = value
}
if nodes := ac.mutation.EmailsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: account.EmailsTable,
Columns: []string{account.EmailsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(email.FieldID, field.TypeUUID),
},
}
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
err error
builders []*AccountCreate
}
// Save creates the Account entities in the database.
func (acb *AccountCreateBulk) Save(ctx context.Context) ([]*Account, error) {
if acb.err != nil {
return nil, acb.err
}
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
var err error
nodes[i], specs[i] = builder.createSpec()
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 *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)
}
}