// Code generated by entc, 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/content/backend/ent/authtokens" "github.com/hay-kot/content/backend/ent/user" ) // AuthTokensCreate is the builder for creating a AuthTokens entity. type AuthTokensCreate struct { config mutation *AuthTokensMutation hooks []Hook } // SetToken sets the "token" field. func (atc *AuthTokensCreate) SetToken(b []byte) *AuthTokensCreate { atc.mutation.SetToken(b) return atc } // SetExpiresAt sets the "expires_at" field. func (atc *AuthTokensCreate) SetExpiresAt(t time.Time) *AuthTokensCreate { atc.mutation.SetExpiresAt(t) return atc } // SetNillableExpiresAt sets the "expires_at" field if the given value is not nil. func (atc *AuthTokensCreate) SetNillableExpiresAt(t *time.Time) *AuthTokensCreate { if t != nil { atc.SetExpiresAt(*t) } return atc } // SetCreatedAt sets the "created_at" field. func (atc *AuthTokensCreate) SetCreatedAt(t time.Time) *AuthTokensCreate { atc.mutation.SetCreatedAt(t) return atc } // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. func (atc *AuthTokensCreate) SetNillableCreatedAt(t *time.Time) *AuthTokensCreate { if t != nil { atc.SetCreatedAt(*t) } return atc } // SetUserID sets the "user" edge to the User entity by ID. func (atc *AuthTokensCreate) SetUserID(id uuid.UUID) *AuthTokensCreate { atc.mutation.SetUserID(id) return atc } // SetNillableUserID sets the "user" edge to the User entity by ID if the given value is not nil. func (atc *AuthTokensCreate) SetNillableUserID(id *uuid.UUID) *AuthTokensCreate { if id != nil { atc = atc.SetUserID(*id) } return atc } // SetUser sets the "user" edge to the User entity. func (atc *AuthTokensCreate) SetUser(u *User) *AuthTokensCreate { return atc.SetUserID(u.ID) } // Mutation returns the AuthTokensMutation object of the builder. func (atc *AuthTokensCreate) Mutation() *AuthTokensMutation { return atc.mutation } // Save creates the AuthTokens in the database. func (atc *AuthTokensCreate) Save(ctx context.Context) (*AuthTokens, error) { var ( err error node *AuthTokens ) atc.defaults() if len(atc.hooks) == 0 { if err = atc.check(); err != nil { return nil, err } node, err = atc.sqlSave(ctx) } else { var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*AuthTokensMutation) if !ok { return nil, fmt.Errorf("unexpected mutation type %T", m) } if err = atc.check(); err != nil { return nil, err } atc.mutation = mutation if node, err = atc.sqlSave(ctx); err != nil { return nil, err } mutation.id = &node.ID mutation.done = true return node, err }) for i := len(atc.hooks) - 1; i >= 0; i-- { if atc.hooks[i] == nil { return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") } mut = atc.hooks[i](mut) } if _, err := mut.Mutate(ctx, atc.mutation); err != nil { return nil, err } } return node, err } // SaveX calls Save and panics if Save returns an error. func (atc *AuthTokensCreate) SaveX(ctx context.Context) *AuthTokens { v, err := atc.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (atc *AuthTokensCreate) Exec(ctx context.Context) error { _, err := atc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (atc *AuthTokensCreate) ExecX(ctx context.Context) { if err := atc.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (atc *AuthTokensCreate) defaults() { if _, ok := atc.mutation.ExpiresAt(); !ok { v := authtokens.DefaultExpiresAt() atc.mutation.SetExpiresAt(v) } if _, ok := atc.mutation.CreatedAt(); !ok { v := authtokens.DefaultCreatedAt() atc.mutation.SetCreatedAt(v) } } // check runs all checks and user-defined validators on the builder. func (atc *AuthTokensCreate) check() error { if _, ok := atc.mutation.Token(); !ok { return &ValidationError{Name: "token", err: errors.New(`ent: missing required field "AuthTokens.token"`)} } if _, ok := atc.mutation.ExpiresAt(); !ok { return &ValidationError{Name: "expires_at", err: errors.New(`ent: missing required field "AuthTokens.expires_at"`)} } if _, ok := atc.mutation.CreatedAt(); !ok { return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "AuthTokens.created_at"`)} } return nil } func (atc *AuthTokensCreate) sqlSave(ctx context.Context) (*AuthTokens, error) { _node, _spec := atc.createSpec() if err := sqlgraph.CreateNode(ctx, atc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{err.Error(), err} } return nil, err } id := _spec.ID.Value.(int64) _node.ID = int(id) return _node, nil } func (atc *AuthTokensCreate) createSpec() (*AuthTokens, *sqlgraph.CreateSpec) { var ( _node = &AuthTokens{config: atc.config} _spec = &sqlgraph.CreateSpec{ Table: authtokens.Table, ID: &sqlgraph.FieldSpec{ Type: field.TypeInt, Column: authtokens.FieldID, }, } ) if value, ok := atc.mutation.Token(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeBytes, Value: value, Column: authtokens.FieldToken, }) _node.Token = value } if value, ok := atc.mutation.ExpiresAt(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeTime, Value: value, Column: authtokens.FieldExpiresAt, }) _node.ExpiresAt = value } if value, ok := atc.mutation.CreatedAt(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeTime, Value: value, Column: authtokens.FieldCreatedAt, }) _node.CreatedAt = value } if nodes := atc.mutation.UserIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: authtokens.UserTable, Columns: []string{authtokens.UserColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ Type: field.TypeUUID, Column: user.FieldID, }, }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.user_auth_tokens = &nodes[0] _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // AuthTokensCreateBulk is the builder for creating many AuthTokens entities in bulk. type AuthTokensCreateBulk struct { config builders []*AuthTokensCreate } // Save creates the AuthTokens entities in the database. func (atcb *AuthTokensCreateBulk) Save(ctx context.Context) ([]*AuthTokens, error) { specs := make([]*sqlgraph.CreateSpec, len(atcb.builders)) nodes := make([]*AuthTokens, len(atcb.builders)) mutators := make([]Mutator, len(atcb.builders)) for i := range atcb.builders { func(i int, root context.Context) { builder := atcb.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*AuthTokensMutation) 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, atcb.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, atcb.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 { id := specs[i].ID.Value.(int64) nodes[i].ID = int(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, atcb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (atcb *AuthTokensCreateBulk) SaveX(ctx context.Context) []*AuthTokens { v, err := atcb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (atcb *AuthTokensCreateBulk) Exec(ctx context.Context) error { _, err := atcb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (atcb *AuthTokensCreateBulk) ExecX(ctx context.Context) { if err := atcb.Exec(ctx); err != nil { panic(err) } }