online-order/ent/business_create.go
2023-10-29 02:42:07 +03:30

545 lines
16 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"online-order/ent/business"
"online-order/ent/businesscategory"
"online-order/ent/businessconfig"
"online-order/ent/domain"
"online-order/ent/product"
"online-order/ent/productcategory"
"online-order/ent/user"
"time"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
)
// BusinessCreate is the builder for creating a Business entity.
type BusinessCreate struct {
config
mutation *BusinessMutation
hooks []Hook
}
// SetName sets the "name" field.
func (bc *BusinessCreate) SetName(s string) *BusinessCreate {
bc.mutation.SetName(s)
return bc
}
// SetNillableName sets the "name" field if the given value is not nil.
func (bc *BusinessCreate) SetNillableName(s *string) *BusinessCreate {
if s != nil {
bc.SetName(*s)
}
return bc
}
// SetSlug sets the "slug" field.
func (bc *BusinessCreate) SetSlug(s string) *BusinessCreate {
bc.mutation.SetSlug(s)
return bc
}
// SetBotID sets the "bot_id" field.
func (bc *BusinessCreate) SetBotID(s string) *BusinessCreate {
bc.mutation.SetBotID(s)
return bc
}
// SetDescription sets the "description" field.
func (bc *BusinessCreate) SetDescription(s string) *BusinessCreate {
bc.mutation.SetDescription(s)
return bc
}
// SetNillableDescription sets the "description" field if the given value is not nil.
func (bc *BusinessCreate) SetNillableDescription(s *string) *BusinessCreate {
if s != nil {
bc.SetDescription(*s)
}
return bc
}
// SetAboutUs sets the "about_us" field.
func (bc *BusinessCreate) SetAboutUs(s string) *BusinessCreate {
bc.mutation.SetAboutUs(s)
return bc
}
// SetNillableAboutUs sets the "about_us" field if the given value is not nil.
func (bc *BusinessCreate) SetNillableAboutUs(s *string) *BusinessCreate {
if s != nil {
bc.SetAboutUs(*s)
}
return bc
}
// SetBusinessCategoryID sets the "business_category_id" field.
func (bc *BusinessCreate) SetBusinessCategoryID(i int) *BusinessCreate {
bc.mutation.SetBusinessCategoryID(i)
return bc
}
// SetNillableBusinessCategoryID sets the "business_category_id" field if the given value is not nil.
func (bc *BusinessCreate) SetNillableBusinessCategoryID(i *int) *BusinessCreate {
if i != nil {
bc.SetBusinessCategoryID(*i)
}
return bc
}
// SetUserID sets the "user_id" field.
func (bc *BusinessCreate) SetUserID(i int) *BusinessCreate {
bc.mutation.SetUserID(i)
return bc
}
// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (bc *BusinessCreate) SetNillableUserID(i *int) *BusinessCreate {
if i != nil {
bc.SetUserID(*i)
}
return bc
}
// SetCreatedAt sets the "created_at" field.
func (bc *BusinessCreate) SetCreatedAt(t time.Time) *BusinessCreate {
bc.mutation.SetCreatedAt(t)
return bc
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (bc *BusinessCreate) SetNillableCreatedAt(t *time.Time) *BusinessCreate {
if t != nil {
bc.SetCreatedAt(*t)
}
return bc
}
// SetUpdatedAt sets the "updated_at" field.
func (bc *BusinessCreate) SetUpdatedAt(t time.Time) *BusinessCreate {
bc.mutation.SetUpdatedAt(t)
return bc
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (bc *BusinessCreate) SetNillableUpdatedAt(t *time.Time) *BusinessCreate {
if t != nil {
bc.SetUpdatedAt(*t)
}
return bc
}
// SetBusinessCategoriesID sets the "business_categories" edge to the BusinessCategory entity by ID.
func (bc *BusinessCreate) SetBusinessCategoriesID(id int) *BusinessCreate {
bc.mutation.SetBusinessCategoriesID(id)
return bc
}
// SetNillableBusinessCategoriesID sets the "business_categories" edge to the BusinessCategory entity by ID if the given value is not nil.
func (bc *BusinessCreate) SetNillableBusinessCategoriesID(id *int) *BusinessCreate {
if id != nil {
bc = bc.SetBusinessCategoriesID(*id)
}
return bc
}
// SetBusinessCategories sets the "business_categories" edge to the BusinessCategory entity.
func (bc *BusinessCreate) SetBusinessCategories(b *BusinessCategory) *BusinessCreate {
return bc.SetBusinessCategoriesID(b.ID)
}
// SetUsersID sets the "users" edge to the User entity by ID.
func (bc *BusinessCreate) SetUsersID(id int) *BusinessCreate {
bc.mutation.SetUsersID(id)
return bc
}
// SetNillableUsersID sets the "users" edge to the User entity by ID if the given value is not nil.
func (bc *BusinessCreate) SetNillableUsersID(id *int) *BusinessCreate {
if id != nil {
bc = bc.SetUsersID(*id)
}
return bc
}
// SetUsers sets the "users" edge to the User entity.
func (bc *BusinessCreate) SetUsers(u *User) *BusinessCreate {
return bc.SetUsersID(u.ID)
}
// AddProductIDs adds the "products" edge to the Product entity by IDs.
func (bc *BusinessCreate) AddProductIDs(ids ...int) *BusinessCreate {
bc.mutation.AddProductIDs(ids...)
return bc
}
// AddProducts adds the "products" edges to the Product entity.
func (bc *BusinessCreate) AddProducts(p ...*Product) *BusinessCreate {
ids := make([]int, len(p))
for i := range p {
ids[i] = p[i].ID
}
return bc.AddProductIDs(ids...)
}
// AddProductCategoryIDs adds the "product_categories" edge to the ProductCategory entity by IDs.
func (bc *BusinessCreate) AddProductCategoryIDs(ids ...int) *BusinessCreate {
bc.mutation.AddProductCategoryIDs(ids...)
return bc
}
// AddProductCategories adds the "product_categories" edges to the ProductCategory entity.
func (bc *BusinessCreate) AddProductCategories(p ...*ProductCategory) *BusinessCreate {
ids := make([]int, len(p))
for i := range p {
ids[i] = p[i].ID
}
return bc.AddProductCategoryIDs(ids...)
}
// AddDomainIDs adds the "domains" edge to the Domain entity by IDs.
func (bc *BusinessCreate) AddDomainIDs(ids ...int) *BusinessCreate {
bc.mutation.AddDomainIDs(ids...)
return bc
}
// AddDomains adds the "domains" edges to the Domain entity.
func (bc *BusinessCreate) AddDomains(d ...*Domain) *BusinessCreate {
ids := make([]int, len(d))
for i := range d {
ids[i] = d[i].ID
}
return bc.AddDomainIDs(ids...)
}
// AddBusinessConfigIDs adds the "business_configs" edge to the BusinessConfig entity by IDs.
func (bc *BusinessCreate) AddBusinessConfigIDs(ids ...int) *BusinessCreate {
bc.mutation.AddBusinessConfigIDs(ids...)
return bc
}
// AddBusinessConfigs adds the "business_configs" edges to the BusinessConfig entity.
func (bc *BusinessCreate) AddBusinessConfigs(b ...*BusinessConfig) *BusinessCreate {
ids := make([]int, len(b))
for i := range b {
ids[i] = b[i].ID
}
return bc.AddBusinessConfigIDs(ids...)
}
// Mutation returns the BusinessMutation object of the builder.
func (bc *BusinessCreate) Mutation() *BusinessMutation {
return bc.mutation
}
// Save creates the Business in the database.
func (bc *BusinessCreate) Save(ctx context.Context) (*Business, error) {
bc.defaults()
return withHooks(ctx, bc.sqlSave, bc.mutation, bc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (bc *BusinessCreate) SaveX(ctx context.Context) *Business {
v, err := bc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (bc *BusinessCreate) Exec(ctx context.Context) error {
_, err := bc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (bc *BusinessCreate) ExecX(ctx context.Context) {
if err := bc.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (bc *BusinessCreate) defaults() {
if _, ok := bc.mutation.Name(); !ok {
v := business.DefaultName
bc.mutation.SetName(v)
}
if _, ok := bc.mutation.CreatedAt(); !ok {
v := business.DefaultCreatedAt()
bc.mutation.SetCreatedAt(v)
}
if _, ok := bc.mutation.UpdatedAt(); !ok {
v := business.DefaultUpdatedAt()
bc.mutation.SetUpdatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (bc *BusinessCreate) check() error {
if _, ok := bc.mutation.Name(); !ok {
return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Business.name"`)}
}
if _, ok := bc.mutation.Slug(); !ok {
return &ValidationError{Name: "slug", err: errors.New(`ent: missing required field "Business.slug"`)}
}
if _, ok := bc.mutation.BotID(); !ok {
return &ValidationError{Name: "bot_id", err: errors.New(`ent: missing required field "Business.bot_id"`)}
}
if _, ok := bc.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Business.created_at"`)}
}
if _, ok := bc.mutation.UpdatedAt(); !ok {
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Business.updated_at"`)}
}
return nil
}
func (bc *BusinessCreate) sqlSave(ctx context.Context) (*Business, error) {
if err := bc.check(); err != nil {
return nil, err
}
_node, _spec := bc.createSpec()
if err := sqlgraph.CreateNode(ctx, bc.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)
bc.mutation.id = &_node.ID
bc.mutation.done = true
return _node, nil
}
func (bc *BusinessCreate) createSpec() (*Business, *sqlgraph.CreateSpec) {
var (
_node = &Business{config: bc.config}
_spec = sqlgraph.NewCreateSpec(business.Table, sqlgraph.NewFieldSpec(business.FieldID, field.TypeInt))
)
if value, ok := bc.mutation.Name(); ok {
_spec.SetField(business.FieldName, field.TypeString, value)
_node.Name = value
}
if value, ok := bc.mutation.Slug(); ok {
_spec.SetField(business.FieldSlug, field.TypeString, value)
_node.Slug = value
}
if value, ok := bc.mutation.BotID(); ok {
_spec.SetField(business.FieldBotID, field.TypeString, value)
_node.BotID = &value
}
if value, ok := bc.mutation.Description(); ok {
_spec.SetField(business.FieldDescription, field.TypeString, value)
_node.Description = &value
}
if value, ok := bc.mutation.AboutUs(); ok {
_spec.SetField(business.FieldAboutUs, field.TypeString, value)
_node.AboutUs = &value
}
if value, ok := bc.mutation.CreatedAt(); ok {
_spec.SetField(business.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := bc.mutation.UpdatedAt(); ok {
_spec.SetField(business.FieldUpdatedAt, field.TypeTime, value)
_node.UpdatedAt = value
}
if nodes := bc.mutation.BusinessCategoriesIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: business.BusinessCategoriesTable,
Columns: []string{business.BusinessCategoriesColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(businesscategory.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.BusinessCategoryID = &nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := bc.mutation.UsersIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: business.UsersTable,
Columns: []string{business.UsersColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.UserID = &nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := bc.mutation.ProductsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: business.ProductsTable,
Columns: []string{business.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 := bc.mutation.ProductCategoriesIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: business.ProductCategoriesTable,
Columns: []string{business.ProductCategoriesColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(productcategory.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := bc.mutation.DomainsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: business.DomainsTable,
Columns: []string{business.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 := bc.mutation.BusinessConfigsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: business.BusinessConfigsTable,
Columns: []string{business.BusinessConfigsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(businessconfig.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// BusinessCreateBulk is the builder for creating many Business entities in bulk.
type BusinessCreateBulk struct {
config
err error
builders []*BusinessCreate
}
// Save creates the Business entities in the database.
func (bcb *BusinessCreateBulk) Save(ctx context.Context) ([]*Business, error) {
if bcb.err != nil {
return nil, bcb.err
}
specs := make([]*sqlgraph.CreateSpec, len(bcb.builders))
nodes := make([]*Business, len(bcb.builders))
mutators := make([]Mutator, len(bcb.builders))
for i := range bcb.builders {
func(i int, root context.Context) {
builder := bcb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*BusinessMutation)
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, bcb.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, bcb.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, bcb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (bcb *BusinessCreateBulk) SaveX(ctx context.Context) []*Business {
v, err := bcb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (bcb *BusinessCreateBulk) Exec(ctx context.Context) error {
_, err := bcb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (bcb *BusinessCreateBulk) ExecX(ctx context.Context) {
if err := bcb.Exec(ctx); err != nil {
panic(err)
}
}