saasitone/ent/user_create.go

320 lines
8.7 KiB
Go
Raw Normal View History

2023-11-14 03:46:57 -08:00
// Code generated by ent, DO NOT EDIT.
2021-12-10 17:44:23 -08:00
package ent
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
2022-01-08 12:32:18 -08:00
"github.com/mikestefanello/pagoda/ent/passwordtoken"
"github.com/mikestefanello/pagoda/ent/user"
2021-12-10 17:44:23 -08:00
)
// UserCreate is the builder for creating a User entity.
type UserCreate struct {
config
mutation *UserMutation
hooks []Hook
}
2021-12-14 18:16:48 -08:00
// SetName sets the "name" field.
func (uc *UserCreate) SetName(s string) *UserCreate {
uc.mutation.SetName(s)
return uc
}
// SetEmail sets the "email" field.
func (uc *UserCreate) SetEmail(s string) *UserCreate {
uc.mutation.SetEmail(s)
2021-12-10 17:44:23 -08:00
return uc
}
// SetPassword sets the "password" field.
func (uc *UserCreate) SetPassword(s string) *UserCreate {
uc.mutation.SetPassword(s)
return uc
}
2022-01-08 12:32:18 -08:00
// SetVerified sets the "verified" field.
func (uc *UserCreate) SetVerified(b bool) *UserCreate {
uc.mutation.SetVerified(b)
return uc
}
// SetNillableVerified sets the "verified" field if the given value is not nil.
func (uc *UserCreate) SetNillableVerified(b *bool) *UserCreate {
if b != nil {
uc.SetVerified(*b)
}
return uc
}
2021-12-10 17:44:23 -08:00
// SetCreatedAt sets the "created_at" field.
func (uc *UserCreate) SetCreatedAt(t time.Time) *UserCreate {
uc.mutation.SetCreatedAt(t)
return uc
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (uc *UserCreate) SetNillableCreatedAt(t *time.Time) *UserCreate {
if t != nil {
uc.SetCreatedAt(*t)
}
return uc
}
2021-12-15 05:48:51 -08:00
// AddOwnerIDs adds the "owner" edge to the PasswordToken entity by IDs.
func (uc *UserCreate) AddOwnerIDs(ids ...int) *UserCreate {
uc.mutation.AddOwnerIDs(ids...)
return uc
}
// AddOwner adds the "owner" edges to the PasswordToken entity.
func (uc *UserCreate) AddOwner(p ...*PasswordToken) *UserCreate {
ids := make([]int, len(p))
for i := range p {
ids[i] = p[i].ID
}
return uc.AddOwnerIDs(ids...)
}
2021-12-10 17:44:23 -08:00
// Mutation returns the UserMutation object of the builder.
func (uc *UserCreate) Mutation() *UserMutation {
return uc.mutation
}
// Save creates the User in the database.
func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
if err := uc.defaults(); err != nil {
return nil, err
}
2023-11-14 03:46:57 -08:00
return withHooks(ctx, uc.sqlSave, uc.mutation, uc.hooks)
2021-12-10 17:44:23 -08:00
}
// SaveX calls Save and panics if Save returns an error.
func (uc *UserCreate) SaveX(ctx context.Context) *User {
v, err := uc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (uc *UserCreate) Exec(ctx context.Context) error {
_, err := uc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (uc *UserCreate) ExecX(ctx context.Context) {
if err := uc.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (uc *UserCreate) defaults() error {
2022-01-08 12:32:18 -08:00
if _, ok := uc.mutation.Verified(); !ok {
v := user.DefaultVerified
uc.mutation.SetVerified(v)
}
2021-12-10 17:44:23 -08:00
if _, ok := uc.mutation.CreatedAt(); !ok {
if user.DefaultCreatedAt == nil {
return fmt.Errorf("ent: uninitialized user.DefaultCreatedAt (forgotten import ent/runtime?)")
}
2021-12-10 17:44:23 -08:00
v := user.DefaultCreatedAt()
uc.mutation.SetCreatedAt(v)
}
return nil
2021-12-10 17:44:23 -08:00
}
// check runs all checks and user-defined validators on the builder.
func (uc *UserCreate) check() error {
2021-12-14 18:16:48 -08:00
if _, ok := uc.mutation.Name(); !ok {
2022-01-28 05:45:16 -08:00
return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "User.name"`)}
2021-12-14 18:16:48 -08:00
}
if v, ok := uc.mutation.Name(); ok {
if err := user.NameValidator(v); err != nil {
2022-01-28 05:45:16 -08:00
return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "User.name": %w`, err)}
2021-12-14 18:16:48 -08:00
}
}
if _, ok := uc.mutation.Email(); !ok {
2022-01-28 05:45:16 -08:00
return &ValidationError{Name: "email", err: errors.New(`ent: missing required field "User.email"`)}
2021-12-10 17:44:23 -08:00
}
2021-12-14 18:16:48 -08:00
if v, ok := uc.mutation.Email(); ok {
if err := user.EmailValidator(v); err != nil {
2022-01-28 05:45:16 -08:00
return &ValidationError{Name: "email", err: fmt.Errorf(`ent: validator failed for field "User.email": %w`, err)}
}
}
2021-12-10 17:44:23 -08:00
if _, ok := uc.mutation.Password(); !ok {
2022-01-28 05:45:16 -08:00
return &ValidationError{Name: "password", err: errors.New(`ent: missing required field "User.password"`)}
2021-12-10 17:44:23 -08:00
}
if v, ok := uc.mutation.Password(); ok {
if err := user.PasswordValidator(v); err != nil {
2022-01-28 05:45:16 -08:00
return &ValidationError{Name: "password", err: fmt.Errorf(`ent: validator failed for field "User.password": %w`, err)}
}
}
2022-01-08 12:32:18 -08:00
if _, ok := uc.mutation.Verified(); !ok {
2022-01-28 05:45:16 -08:00
return &ValidationError{Name: "verified", err: errors.New(`ent: missing required field "User.verified"`)}
2022-01-08 12:32:18 -08:00
}
2021-12-10 17:44:23 -08:00
if _, ok := uc.mutation.CreatedAt(); !ok {
2022-01-28 05:45:16 -08:00
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "User.created_at"`)}
2021-12-10 17:44:23 -08:00
}
return nil
}
func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
2023-11-14 03:46:57 -08:00
if err := uc.check(); err != nil {
return nil, err
}
2021-12-10 17:44:23 -08:00
_node, _spec := uc.createSpec()
if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil {
if sqlgraph.IsConstraintError(err) {
2023-11-14 03:46:57 -08:00
err = &ConstraintError{msg: err.Error(), wrap: err}
2021-12-10 17:44:23 -08:00
}
return nil, err
}
id := _spec.ID.Value.(int64)
_node.ID = int(id)
2023-11-14 03:46:57 -08:00
uc.mutation.id = &_node.ID
uc.mutation.done = true
2021-12-10 17:44:23 -08:00
return _node, nil
}
func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) {
var (
_node = &User{config: uc.config}
2023-11-14 03:46:57 -08:00
_spec = sqlgraph.NewCreateSpec(user.Table, sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt))
2021-12-10 17:44:23 -08:00
)
2021-12-14 18:16:48 -08:00
if value, ok := uc.mutation.Name(); ok {
2023-11-14 03:46:57 -08:00
_spec.SetField(user.FieldName, field.TypeString, value)
2021-12-14 18:16:48 -08:00
_node.Name = value
}
if value, ok := uc.mutation.Email(); ok {
2023-11-14 03:46:57 -08:00
_spec.SetField(user.FieldEmail, field.TypeString, value)
2021-12-14 18:16:48 -08:00
_node.Email = value
2021-12-10 17:44:23 -08:00
}
if value, ok := uc.mutation.Password(); ok {
2023-11-14 03:46:57 -08:00
_spec.SetField(user.FieldPassword, field.TypeString, value)
2021-12-10 17:44:23 -08:00
_node.Password = value
}
2022-01-08 12:32:18 -08:00
if value, ok := uc.mutation.Verified(); ok {
2023-11-14 03:46:57 -08:00
_spec.SetField(user.FieldVerified, field.TypeBool, value)
2022-01-08 12:32:18 -08:00
_node.Verified = value
}
2021-12-10 17:44:23 -08:00
if value, ok := uc.mutation.CreatedAt(); ok {
2023-11-14 03:46:57 -08:00
_spec.SetField(user.FieldCreatedAt, field.TypeTime, value)
2021-12-10 17:44:23 -08:00
_node.CreatedAt = value
}
2021-12-15 05:48:51 -08:00
if nodes := uc.mutation.OwnerIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: true,
Table: user.OwnerTable,
Columns: []string{user.OwnerColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
2023-11-14 03:46:57 -08:00
IDSpec: sqlgraph.NewFieldSpec(passwordtoken.FieldID, field.TypeInt),
2021-12-15 05:48:51 -08:00
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
2021-12-10 17:44:23 -08:00
return _node, _spec
}
// UserCreateBulk is the builder for creating many User entities in bulk.
type UserCreateBulk struct {
config
2023-11-14 03:46:57 -08:00
err error
2021-12-10 17:44:23 -08:00
builders []*UserCreate
}
// Save creates the User entities in the database.
func (ucb *UserCreateBulk) Save(ctx context.Context) ([]*User, error) {
2023-11-14 03:46:57 -08:00
if ucb.err != nil {
return nil, ucb.err
}
2021-12-10 17:44:23 -08:00
specs := make([]*sqlgraph.CreateSpec, len(ucb.builders))
nodes := make([]*User, len(ucb.builders))
mutators := make([]Mutator, len(ucb.builders))
for i := range ucb.builders {
func(i int, root context.Context) {
builder := ucb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*UserMutation)
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
2023-11-14 03:46:57 -08:00
nodes[i], specs[i] = builder.createSpec()
2021-12-10 17:44:23 -08:00
if i < len(mutators)-1 {
_, err = mutators[i+1].Mutate(root, ucb.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, ucb.driver, spec); err != nil {
if sqlgraph.IsConstraintError(err) {
2023-11-14 03:46:57 -08:00
err = &ConstraintError{msg: err.Error(), wrap: err}
2021-12-10 17:44:23 -08:00
}
}
}
if err != nil {
return nil, err
}
mutation.id = &nodes[i].ID
if specs[i].ID.Value != nil {
id := specs[i].ID.Value.(int64)
nodes[i].ID = int(id)
}
2023-11-14 03:46:57 -08:00
mutation.done = true
2021-12-10 17:44:23 -08:00
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, ucb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (ucb *UserCreateBulk) SaveX(ctx context.Context) []*User {
v, err := ucb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (ucb *UserCreateBulk) Exec(ctx context.Context) error {
_, err := ucb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (ucb *UserCreateBulk) ExecX(ctx context.Context) {
if err := ucb.Exec(ctx); err != nil {
panic(err)
}
}