online-order/ent/user_create.go

482 lines
13 KiB
Go
Raw Permalink Normal View History

2023-10-28 23:12:07 +00:00
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"online-order/ent/business"
"online-order/ent/domain"
"online-order/ent/product"
"online-order/ent/user"
"time"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
)
// UserCreate is the builder for creating a User entity.
type UserCreate struct {
config
mutation *UserMutation
hooks []Hook
}
// SetEmail sets the "email" field.
func (uc *UserCreate) SetEmail(s string) *UserCreate {
uc.mutation.SetEmail(s)
return uc
}
// SetNillableEmail sets the "email" field if the given value is not nil.
func (uc *UserCreate) SetNillableEmail(s *string) *UserCreate {
if s != nil {
uc.SetEmail(*s)
}
return uc
}
// SetCellPhone sets the "cell_phone" field.
func (uc *UserCreate) SetCellPhone(s string) *UserCreate {
uc.mutation.SetCellPhone(s)
return uc
}
// SetFirstName sets the "first_name" field.
func (uc *UserCreate) SetFirstName(s string) *UserCreate {
uc.mutation.SetFirstName(s)
return uc
}
// SetLastName sets the "last_name" field.
func (uc *UserCreate) SetLastName(s string) *UserCreate {
uc.mutation.SetLastName(s)
return uc
}
// SetPassword sets the "password" field.
func (uc *UserCreate) SetPassword(s string) *UserCreate {
uc.mutation.SetPassword(s)
return uc
}
// SetNillablePassword sets the "password" field if the given value is not nil.
func (uc *UserCreate) SetNillablePassword(s *string) *UserCreate {
if s != nil {
uc.SetPassword(*s)
}
return uc
}
// SetIsActive sets the "is_active" field.
func (uc *UserCreate) SetIsActive(b bool) *UserCreate {
uc.mutation.SetIsActive(b)
return uc
}
// SetNillableIsActive sets the "is_active" field if the given value is not nil.
func (uc *UserCreate) SetNillableIsActive(b *bool) *UserCreate {
if b != nil {
uc.SetIsActive(*b)
}
return uc
}
// SetIsAdmin sets the "is_admin" field.
func (uc *UserCreate) SetIsAdmin(b bool) *UserCreate {
uc.mutation.SetIsAdmin(b)
return uc
}
// SetNillableIsAdmin sets the "is_admin" field if the given value is not nil.
func (uc *UserCreate) SetNillableIsAdmin(b *bool) *UserCreate {
if b != nil {
uc.SetIsAdmin(*b)
}
return uc
}
// 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
}
// SetUpdatedAt sets the "updated_at" field.
func (uc *UserCreate) SetUpdatedAt(t time.Time) *UserCreate {
uc.mutation.SetUpdatedAt(t)
return uc
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (uc *UserCreate) SetNillableUpdatedAt(t *time.Time) *UserCreate {
if t != nil {
uc.SetUpdatedAt(*t)
}
return uc
}
// SetLastAuthenticationAt sets the "last_authentication_at" field.
func (uc *UserCreate) SetLastAuthenticationAt(t time.Time) *UserCreate {
uc.mutation.SetLastAuthenticationAt(t)
return uc
}
// SetNillableLastAuthenticationAt sets the "last_authentication_at" field if the given value is not nil.
func (uc *UserCreate) SetNillableLastAuthenticationAt(t *time.Time) *UserCreate {
if t != nil {
uc.SetLastAuthenticationAt(*t)
}
return uc
}
// AddProductIDs adds the "products" edge to the Product entity by IDs.
func (uc *UserCreate) AddProductIDs(ids ...int) *UserCreate {
uc.mutation.AddProductIDs(ids...)
return uc
}
// AddProducts adds the "products" edges to the Product entity.
func (uc *UserCreate) AddProducts(p ...*Product) *UserCreate {
ids := make([]int, len(p))
for i := range p {
ids[i] = p[i].ID
}
return uc.AddProductIDs(ids...)
}
// AddDomainIDs adds the "domains" edge to the Domain entity by IDs.
func (uc *UserCreate) AddDomainIDs(ids ...int) *UserCreate {
uc.mutation.AddDomainIDs(ids...)
return uc
}
// AddDomains adds the "domains" edges to the Domain entity.
func (uc *UserCreate) AddDomains(d ...*Domain) *UserCreate {
ids := make([]int, len(d))
for i := range d {
ids[i] = d[i].ID
}
return uc.AddDomainIDs(ids...)
}
// AddBusinessIDs adds the "businesses" edge to the Business entity by IDs.
func (uc *UserCreate) AddBusinessIDs(ids ...int) *UserCreate {
uc.mutation.AddBusinessIDs(ids...)
return uc
}
// AddBusinesses adds the "businesses" edges to the Business entity.
func (uc *UserCreate) AddBusinesses(b ...*Business) *UserCreate {
ids := make([]int, len(b))
for i := range b {
ids[i] = b[i].ID
}
return uc.AddBusinessIDs(ids...)
}
// 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) {
uc.defaults()
return withHooks(ctx, uc.sqlSave, uc.mutation, uc.hooks)
}
// 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() {
if _, ok := uc.mutation.IsActive(); !ok {
v := user.DefaultIsActive
uc.mutation.SetIsActive(v)
}
if _, ok := uc.mutation.IsAdmin(); !ok {
v := user.DefaultIsAdmin
uc.mutation.SetIsAdmin(v)
}
if _, ok := uc.mutation.CreatedAt(); !ok {
v := user.DefaultCreatedAt()
uc.mutation.SetCreatedAt(v)
}
if _, ok := uc.mutation.UpdatedAt(); !ok {
v := user.DefaultUpdatedAt()
uc.mutation.SetUpdatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (uc *UserCreate) check() error {
if v, ok := uc.mutation.Email(); ok {
if err := user.EmailValidator(v); err != nil {
return &ValidationError{Name: "email", err: fmt.Errorf(`ent: validator failed for field "User.email": %w`, err)}
}
}
if _, ok := uc.mutation.CellPhone(); !ok {
return &ValidationError{Name: "cell_phone", err: errors.New(`ent: missing required field "User.cell_phone"`)}
}
if _, ok := uc.mutation.FirstName(); !ok {
return &ValidationError{Name: "first_name", err: errors.New(`ent: missing required field "User.first_name"`)}
}
if v, ok := uc.mutation.FirstName(); ok {
if err := user.FirstNameValidator(v); err != nil {
return &ValidationError{Name: "first_name", err: fmt.Errorf(`ent: validator failed for field "User.first_name": %w`, err)}
}
}
if _, ok := uc.mutation.LastName(); !ok {
return &ValidationError{Name: "last_name", err: errors.New(`ent: missing required field "User.last_name"`)}
}
if v, ok := uc.mutation.LastName(); ok {
if err := user.LastNameValidator(v); err != nil {
return &ValidationError{Name: "last_name", err: fmt.Errorf(`ent: validator failed for field "User.last_name": %w`, err)}
}
}
if _, ok := uc.mutation.IsActive(); !ok {
return &ValidationError{Name: "is_active", err: errors.New(`ent: missing required field "User.is_active"`)}
}
if _, ok := uc.mutation.IsAdmin(); !ok {
return &ValidationError{Name: "is_admin", err: errors.New(`ent: missing required field "User.is_admin"`)}
}
if _, ok := uc.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "User.created_at"`)}
}
if _, ok := uc.mutation.UpdatedAt(); !ok {
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "User.updated_at"`)}
}
return nil
}
func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
if err := uc.check(); err != nil {
return nil, err
}
_node, _spec := uc.createSpec()
if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
id := _spec.ID.Value.(int64)
_node.ID = int(id)
uc.mutation.id = &_node.ID
uc.mutation.done = true
return _node, nil
}
func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) {
var (
_node = &User{config: uc.config}
_spec = sqlgraph.NewCreateSpec(user.Table, sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt))
)
if value, ok := uc.mutation.Email(); ok {
_spec.SetField(user.FieldEmail, field.TypeString, value)
_node.Email = &value
}
if value, ok := uc.mutation.CellPhone(); ok {
_spec.SetField(user.FieldCellPhone, field.TypeString, value)
_node.CellPhone = &value
}
if value, ok := uc.mutation.FirstName(); ok {
_spec.SetField(user.FieldFirstName, field.TypeString, value)
_node.FirstName = value
}
if value, ok := uc.mutation.LastName(); ok {
_spec.SetField(user.FieldLastName, field.TypeString, value)
_node.LastName = value
}
if value, ok := uc.mutation.Password(); ok {
_spec.SetField(user.FieldPassword, field.TypeString, value)
_node.Password = &value
}
if value, ok := uc.mutation.IsActive(); ok {
_spec.SetField(user.FieldIsActive, field.TypeBool, value)
_node.IsActive = value
}
if value, ok := uc.mutation.IsAdmin(); ok {
_spec.SetField(user.FieldIsAdmin, field.TypeBool, value)
_node.IsAdmin = value
}
if value, ok := uc.mutation.CreatedAt(); ok {
_spec.SetField(user.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := uc.mutation.UpdatedAt(); ok {
_spec.SetField(user.FieldUpdatedAt, field.TypeTime, value)
_node.UpdatedAt = value
}
if value, ok := uc.mutation.LastAuthenticationAt(); ok {
_spec.SetField(user.FieldLastAuthenticationAt, field.TypeTime, value)
_node.LastAuthenticationAt = value
}
if nodes := uc.mutation.ProductsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: user.ProductsTable,
Columns: []string{user.ProductsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(product.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := uc.mutation.DomainsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: user.DomainsTable,
Columns: []string{user.DomainsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(domain.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := uc.mutation.BusinessesIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: user.BusinessesTable,
Columns: []string{user.BusinessesColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(business.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// UserCreateBulk is the builder for creating many User entities in bulk.
type UserCreateBulk struct {
config
err error
builders []*UserCreate
}
// Save creates the User entities in the database.
func (ucb *UserCreateBulk) Save(ctx context.Context) ([]*User, error) {
if ucb.err != nil {
return nil, ucb.err
}
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
nodes[i], specs[i] = builder.createSpec()
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) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
}
}
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)
}
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, 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)
}
}