// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "tel-commerce/ent/business" "tel-commerce/ent/businesscategory" "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 } // SetCompany sets the "company" field. func (bc *BusinessCreate) SetCompany(s string) *BusinessCreate { bc.mutation.SetCompany(s) return bc } // SetNillableCompany sets the "company" field if the given value is not nil. func (bc *BusinessCreate) SetNillableCompany(s *string) *BusinessCreate { if s != nil { bc.SetCompany(*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 } // 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 } // AddBusinessCategoryIDs adds the "businessCategory" edge to the BusinessCategory entity by IDs. func (bc *BusinessCreate) AddBusinessCategoryIDs(ids ...int) *BusinessCreate { bc.mutation.AddBusinessCategoryIDs(ids...) return bc } // AddBusinessCategory adds the "businessCategory" edges to the BusinessCategory entity. func (bc *BusinessCreate) AddBusinessCategory(b ...*BusinessCategory) *BusinessCreate { ids := make([]int, len(b)) for i := range b { ids[i] = b[i].ID } return bc.AddBusinessCategoryIDs(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.Company(); !ok { v := business.DefaultCompany bc.mutation.SetCompany(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.Company(); !ok { return &ValidationError{Name: "company", err: errors.New(`ent: missing required field "Business.company"`)} } 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.Company(); ok { _spec.SetField(business.FieldCompany, field.TypeString, value) _node.Company = 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.BusinessCategoryIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, Inverse: true, Table: business.BusinessCategoryTable, Columns: business.BusinessCategoryPrimaryKey, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(businesscategory.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) } }