// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "tel-commerce/ent/product" "tel-commerce/ent/productcategory" "time" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" ) // ProductCategoryCreate is the builder for creating a ProductCategory entity. type ProductCategoryCreate struct { config mutation *ProductCategoryMutation hooks []Hook } // SetName sets the "name" field. func (pcc *ProductCategoryCreate) SetName(s string) *ProductCategoryCreate { pcc.mutation.SetName(s) return pcc } // SetNillableName sets the "name" field if the given value is not nil. func (pcc *ProductCategoryCreate) SetNillableName(s *string) *ProductCategoryCreate { if s != nil { pcc.SetName(*s) } return pcc } // SetSlug sets the "slug" field. func (pcc *ProductCategoryCreate) SetSlug(s string) *ProductCategoryCreate { pcc.mutation.SetSlug(s) return pcc } // SetDescription sets the "description" field. func (pcc *ProductCategoryCreate) SetDescription(s string) *ProductCategoryCreate { pcc.mutation.SetDescription(s) return pcc } // SetNillableDescription sets the "description" field if the given value is not nil. func (pcc *ProductCategoryCreate) SetNillableDescription(s *string) *ProductCategoryCreate { if s != nil { pcc.SetDescription(*s) } return pcc } // SetStatus sets the "status" field. func (pcc *ProductCategoryCreate) SetStatus(b bool) *ProductCategoryCreate { pcc.mutation.SetStatus(b) return pcc } // SetNillableStatus sets the "status" field if the given value is not nil. func (pcc *ProductCategoryCreate) SetNillableStatus(b *bool) *ProductCategoryCreate { if b != nil { pcc.SetStatus(*b) } return pcc } // SetCreatedAt sets the "created_at" field. func (pcc *ProductCategoryCreate) SetCreatedAt(t time.Time) *ProductCategoryCreate { pcc.mutation.SetCreatedAt(t) return pcc } // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. func (pcc *ProductCategoryCreate) SetNillableCreatedAt(t *time.Time) *ProductCategoryCreate { if t != nil { pcc.SetCreatedAt(*t) } return pcc } // SetUpdatedAt sets the "updated_at" field. func (pcc *ProductCategoryCreate) SetUpdatedAt(t time.Time) *ProductCategoryCreate { pcc.mutation.SetUpdatedAt(t) return pcc } // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. func (pcc *ProductCategoryCreate) SetNillableUpdatedAt(t *time.Time) *ProductCategoryCreate { if t != nil { pcc.SetUpdatedAt(*t) } return pcc } // AddProductIDs adds the "products" edge to the Product entity by IDs. func (pcc *ProductCategoryCreate) AddProductIDs(ids ...int) *ProductCategoryCreate { pcc.mutation.AddProductIDs(ids...) return pcc } // AddProducts adds the "products" edges to the Product entity. func (pcc *ProductCategoryCreate) AddProducts(p ...*Product) *ProductCategoryCreate { ids := make([]int, len(p)) for i := range p { ids[i] = p[i].ID } return pcc.AddProductIDs(ids...) } // Mutation returns the ProductCategoryMutation object of the builder. func (pcc *ProductCategoryCreate) Mutation() *ProductCategoryMutation { return pcc.mutation } // Save creates the ProductCategory in the database. func (pcc *ProductCategoryCreate) Save(ctx context.Context) (*ProductCategory, error) { pcc.defaults() return withHooks(ctx, pcc.sqlSave, pcc.mutation, pcc.hooks) } // SaveX calls Save and panics if Save returns an error. func (pcc *ProductCategoryCreate) SaveX(ctx context.Context) *ProductCategory { v, err := pcc.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (pcc *ProductCategoryCreate) Exec(ctx context.Context) error { _, err := pcc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (pcc *ProductCategoryCreate) ExecX(ctx context.Context) { if err := pcc.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (pcc *ProductCategoryCreate) defaults() { if _, ok := pcc.mutation.Name(); !ok { v := productcategory.DefaultName pcc.mutation.SetName(v) } if _, ok := pcc.mutation.Status(); !ok { v := productcategory.DefaultStatus pcc.mutation.SetStatus(v) } if _, ok := pcc.mutation.CreatedAt(); !ok { v := productcategory.DefaultCreatedAt pcc.mutation.SetCreatedAt(v) } if _, ok := pcc.mutation.UpdatedAt(); !ok { v := productcategory.DefaultUpdatedAt pcc.mutation.SetUpdatedAt(v) } } // check runs all checks and user-defined validators on the builder. func (pcc *ProductCategoryCreate) check() error { if _, ok := pcc.mutation.Name(); !ok { return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "ProductCategory.name"`)} } if _, ok := pcc.mutation.Slug(); !ok { return &ValidationError{Name: "slug", err: errors.New(`ent: missing required field "ProductCategory.slug"`)} } if _, ok := pcc.mutation.Status(); !ok { return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "ProductCategory.status"`)} } if _, ok := pcc.mutation.CreatedAt(); !ok { return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "ProductCategory.created_at"`)} } if _, ok := pcc.mutation.UpdatedAt(); !ok { return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "ProductCategory.updated_at"`)} } return nil } func (pcc *ProductCategoryCreate) sqlSave(ctx context.Context) (*ProductCategory, error) { if err := pcc.check(); err != nil { return nil, err } _node, _spec := pcc.createSpec() if err := sqlgraph.CreateNode(ctx, pcc.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) pcc.mutation.id = &_node.ID pcc.mutation.done = true return _node, nil } func (pcc *ProductCategoryCreate) createSpec() (*ProductCategory, *sqlgraph.CreateSpec) { var ( _node = &ProductCategory{config: pcc.config} _spec = sqlgraph.NewCreateSpec(productcategory.Table, sqlgraph.NewFieldSpec(productcategory.FieldID, field.TypeInt)) ) if value, ok := pcc.mutation.Name(); ok { _spec.SetField(productcategory.FieldName, field.TypeString, value) _node.Name = value } if value, ok := pcc.mutation.Slug(); ok { _spec.SetField(productcategory.FieldSlug, field.TypeString, value) _node.Slug = value } if value, ok := pcc.mutation.Description(); ok { _spec.SetField(productcategory.FieldDescription, field.TypeString, value) _node.Description = &value } if value, ok := pcc.mutation.Status(); ok { _spec.SetField(productcategory.FieldStatus, field.TypeBool, value) _node.Status = value } if value, ok := pcc.mutation.CreatedAt(); ok { _spec.SetField(productcategory.FieldCreatedAt, field.TypeTime, value) _node.CreatedAt = value } if value, ok := pcc.mutation.UpdatedAt(); ok { _spec.SetField(productcategory.FieldUpdatedAt, field.TypeTime, value) _node.UpdatedAt = value } if nodes := pcc.mutation.ProductsIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, Inverse: false, Table: productcategory.ProductsTable, Columns: productcategory.ProductsPrimaryKey, 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) } return _node, _spec } // ProductCategoryCreateBulk is the builder for creating many ProductCategory entities in bulk. type ProductCategoryCreateBulk struct { config err error builders []*ProductCategoryCreate } // Save creates the ProductCategory entities in the database. func (pccb *ProductCategoryCreateBulk) Save(ctx context.Context) ([]*ProductCategory, error) { if pccb.err != nil { return nil, pccb.err } specs := make([]*sqlgraph.CreateSpec, len(pccb.builders)) nodes := make([]*ProductCategory, len(pccb.builders)) mutators := make([]Mutator, len(pccb.builders)) for i := range pccb.builders { func(i int, root context.Context) { builder := pccb.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*ProductCategoryMutation) 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, pccb.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, pccb.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, pccb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (pccb *ProductCategoryCreateBulk) SaveX(ctx context.Context) []*ProductCategory { v, err := pccb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (pccb *ProductCategoryCreateBulk) Exec(ctx context.Context) error { _, err := pccb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (pccb *ProductCategoryCreateBulk) ExecX(ctx context.Context) { if err := pccb.Exec(ctx); err != nil { panic(err) } }