// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "tel-commerce/ent/business" "tel-commerce/ent/businesscategory" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" ) // BusinessCategoryCreate is the builder for creating a BusinessCategory entity. type BusinessCategoryCreate struct { config mutation *BusinessCategoryMutation hooks []Hook } // SetSlug sets the "slug" field. func (bcc *BusinessCategoryCreate) SetSlug(s string) *BusinessCategoryCreate { bcc.mutation.SetSlug(s) return bcc } // SetName sets the "name" field. func (bcc *BusinessCategoryCreate) SetName(s string) *BusinessCategoryCreate { bcc.mutation.SetName(s) return bcc } // SetNillableName sets the "name" field if the given value is not nil. func (bcc *BusinessCategoryCreate) SetNillableName(s *string) *BusinessCategoryCreate { if s != nil { bcc.SetName(*s) } return bcc } // SetDescription sets the "description" field. func (bcc *BusinessCategoryCreate) SetDescription(s string) *BusinessCategoryCreate { bcc.mutation.SetDescription(s) return bcc } // SetNillableDescription sets the "description" field if the given value is not nil. func (bcc *BusinessCategoryCreate) SetNillableDescription(s *string) *BusinessCategoryCreate { if s != nil { bcc.SetDescription(*s) } return bcc } // AddBusinessIDs adds the "businesses" edge to the Business entity by IDs. func (bcc *BusinessCategoryCreate) AddBusinessIDs(ids ...int) *BusinessCategoryCreate { bcc.mutation.AddBusinessIDs(ids...) return bcc } // AddBusinesses adds the "businesses" edges to the Business entity. func (bcc *BusinessCategoryCreate) AddBusinesses(b ...*Business) *BusinessCategoryCreate { ids := make([]int, len(b)) for i := range b { ids[i] = b[i].ID } return bcc.AddBusinessIDs(ids...) } // Mutation returns the BusinessCategoryMutation object of the builder. func (bcc *BusinessCategoryCreate) Mutation() *BusinessCategoryMutation { return bcc.mutation } // Save creates the BusinessCategory in the database. func (bcc *BusinessCategoryCreate) Save(ctx context.Context) (*BusinessCategory, error) { bcc.defaults() return withHooks(ctx, bcc.sqlSave, bcc.mutation, bcc.hooks) } // SaveX calls Save and panics if Save returns an error. func (bcc *BusinessCategoryCreate) SaveX(ctx context.Context) *BusinessCategory { v, err := bcc.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (bcc *BusinessCategoryCreate) Exec(ctx context.Context) error { _, err := bcc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (bcc *BusinessCategoryCreate) ExecX(ctx context.Context) { if err := bcc.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (bcc *BusinessCategoryCreate) defaults() { if _, ok := bcc.mutation.Name(); !ok { v := businesscategory.DefaultName bcc.mutation.SetName(v) } if _, ok := bcc.mutation.Description(); !ok { v := businesscategory.DefaultDescription bcc.mutation.SetDescription(v) } } // check runs all checks and user-defined validators on the builder. func (bcc *BusinessCategoryCreate) check() error { if _, ok := bcc.mutation.Slug(); !ok { return &ValidationError{Name: "slug", err: errors.New(`ent: missing required field "BusinessCategory.slug"`)} } if _, ok := bcc.mutation.Name(); !ok { return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "BusinessCategory.name"`)} } if _, ok := bcc.mutation.Description(); !ok { return &ValidationError{Name: "description", err: errors.New(`ent: missing required field "BusinessCategory.description"`)} } return nil } func (bcc *BusinessCategoryCreate) sqlSave(ctx context.Context) (*BusinessCategory, error) { if err := bcc.check(); err != nil { return nil, err } _node, _spec := bcc.createSpec() if err := sqlgraph.CreateNode(ctx, bcc.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) bcc.mutation.id = &_node.ID bcc.mutation.done = true return _node, nil } func (bcc *BusinessCategoryCreate) createSpec() (*BusinessCategory, *sqlgraph.CreateSpec) { var ( _node = &BusinessCategory{config: bcc.config} _spec = sqlgraph.NewCreateSpec(businesscategory.Table, sqlgraph.NewFieldSpec(businesscategory.FieldID, field.TypeInt)) ) if value, ok := bcc.mutation.Slug(); ok { _spec.SetField(businesscategory.FieldSlug, field.TypeString, value) _node.Slug = value } if value, ok := bcc.mutation.Name(); ok { _spec.SetField(businesscategory.FieldName, field.TypeString, value) _node.Name = value } if value, ok := bcc.mutation.Description(); ok { _spec.SetField(businesscategory.FieldDescription, field.TypeString, value) _node.Description = value } if nodes := bcc.mutation.BusinessesIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, Inverse: false, Table: businesscategory.BusinessesTable, Columns: businesscategory.BusinessesPrimaryKey, 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 } // BusinessCategoryCreateBulk is the builder for creating many BusinessCategory entities in bulk. type BusinessCategoryCreateBulk struct { config err error builders []*BusinessCategoryCreate } // Save creates the BusinessCategory entities in the database. func (bccb *BusinessCategoryCreateBulk) Save(ctx context.Context) ([]*BusinessCategory, error) { if bccb.err != nil { return nil, bccb.err } specs := make([]*sqlgraph.CreateSpec, len(bccb.builders)) nodes := make([]*BusinessCategory, len(bccb.builders)) mutators := make([]Mutator, len(bccb.builders)) for i := range bccb.builders { func(i int, root context.Context) { builder := bccb.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*BusinessCategoryMutation) 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, bccb.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, bccb.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, bccb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (bccb *BusinessCategoryCreateBulk) SaveX(ctx context.Context) []*BusinessCategory { v, err := bccb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (bccb *BusinessCategoryCreateBulk) Exec(ctx context.Context) error { _, err := bccb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (bccb *BusinessCategoryCreateBulk) ExecX(ctx context.Context) { if err := bccb.Exec(ctx); err != nil { panic(err) } }