online-order/ent/product_create.go

418 lines
12 KiB
Go
Raw Normal View History

2023-10-27 09:51:58 +00:00
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"online-order/ent/product"
"online-order/ent/productcategory"
"time"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
)
// ProductCreate is the builder for creating a Product entity.
type ProductCreate struct {
config
mutation *ProductMutation
hooks []Hook
}
// SetName sets the "name" field.
func (pc *ProductCreate) SetName(s string) *ProductCreate {
pc.mutation.SetName(s)
return pc
}
// SetNillableName sets the "name" field if the given value is not nil.
func (pc *ProductCreate) SetNillableName(s *string) *ProductCreate {
if s != nil {
pc.SetName(*s)
}
return pc
}
// SetDescription sets the "description" field.
func (pc *ProductCreate) SetDescription(s string) *ProductCreate {
pc.mutation.SetDescription(s)
return pc
}
// SetNillableDescription sets the "description" field if the given value is not nil.
func (pc *ProductCreate) SetNillableDescription(s *string) *ProductCreate {
if s != nil {
pc.SetDescription(*s)
}
return pc
}
// SetPrice sets the "price" field.
func (pc *ProductCreate) SetPrice(f float64) *ProductCreate {
pc.mutation.SetPrice(f)
return pc
}
// SetNillablePrice sets the "price" field if the given value is not nil.
func (pc *ProductCreate) SetNillablePrice(f *float64) *ProductCreate {
if f != nil {
pc.SetPrice(*f)
}
return pc
}
// SetOriginalPrice sets the "original_price" field.
func (pc *ProductCreate) SetOriginalPrice(f float64) *ProductCreate {
pc.mutation.SetOriginalPrice(f)
return pc
}
// SetNillableOriginalPrice sets the "original_price" field if the given value is not nil.
func (pc *ProductCreate) SetNillableOriginalPrice(f *float64) *ProductCreate {
if f != nil {
pc.SetOriginalPrice(*f)
}
return pc
}
// SetQuantity sets the "quantity" field.
func (pc *ProductCreate) SetQuantity(i int) *ProductCreate {
pc.mutation.SetQuantity(i)
return pc
}
// SetNillableQuantity sets the "quantity" field if the given value is not nil.
func (pc *ProductCreate) SetNillableQuantity(i *int) *ProductCreate {
if i != nil {
pc.SetQuantity(*i)
}
return pc
}
// SetStatus sets the "status" field.
func (pc *ProductCreate) SetStatus(b bool) *ProductCreate {
pc.mutation.SetStatus(b)
return pc
}
// SetNillableStatus sets the "status" field if the given value is not nil.
func (pc *ProductCreate) SetNillableStatus(b *bool) *ProductCreate {
if b != nil {
pc.SetStatus(*b)
}
return pc
}
// SetCreatedAt sets the "created_at" field.
func (pc *ProductCreate) SetCreatedAt(t time.Time) *ProductCreate {
pc.mutation.SetCreatedAt(t)
return pc
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (pc *ProductCreate) SetNillableCreatedAt(t *time.Time) *ProductCreate {
if t != nil {
pc.SetCreatedAt(*t)
}
return pc
}
// SetUpdatedAt sets the "updated_at" field.
func (pc *ProductCreate) SetUpdatedAt(t time.Time) *ProductCreate {
pc.mutation.SetUpdatedAt(t)
return pc
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (pc *ProductCreate) SetNillableUpdatedAt(t *time.Time) *ProductCreate {
if t != nil {
pc.SetUpdatedAt(*t)
}
return pc
}
// AddProductCategoryIDs adds the "productCategory" edge to the ProductCategory entity by IDs.
func (pc *ProductCreate) AddProductCategoryIDs(ids ...int) *ProductCreate {
pc.mutation.AddProductCategoryIDs(ids...)
return pc
}
// AddProductCategory adds the "productCategory" edges to the ProductCategory entity.
func (pc *ProductCreate) AddProductCategory(p ...*ProductCategory) *ProductCreate {
ids := make([]int, len(p))
for i := range p {
ids[i] = p[i].ID
}
return pc.AddProductCategoryIDs(ids...)
}
// Mutation returns the ProductMutation object of the builder.
func (pc *ProductCreate) Mutation() *ProductMutation {
return pc.mutation
}
// Save creates the Product in the database.
func (pc *ProductCreate) Save(ctx context.Context) (*Product, error) {
pc.defaults()
return withHooks(ctx, pc.sqlSave, pc.mutation, pc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (pc *ProductCreate) SaveX(ctx context.Context) *Product {
v, err := pc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (pc *ProductCreate) Exec(ctx context.Context) error {
_, err := pc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (pc *ProductCreate) ExecX(ctx context.Context) {
if err := pc.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (pc *ProductCreate) defaults() {
if _, ok := pc.mutation.Name(); !ok {
v := product.DefaultName
pc.mutation.SetName(v)
}
if _, ok := pc.mutation.Price(); !ok {
v := product.DefaultPrice
pc.mutation.SetPrice(v)
}
if _, ok := pc.mutation.OriginalPrice(); !ok {
v := product.DefaultOriginalPrice
pc.mutation.SetOriginalPrice(v)
}
if _, ok := pc.mutation.Quantity(); !ok {
v := product.DefaultQuantity
pc.mutation.SetQuantity(v)
}
if _, ok := pc.mutation.Status(); !ok {
v := product.DefaultStatus
pc.mutation.SetStatus(v)
}
if _, ok := pc.mutation.CreatedAt(); !ok {
v := product.DefaultCreatedAt
pc.mutation.SetCreatedAt(v)
}
if _, ok := pc.mutation.UpdatedAt(); !ok {
v := product.DefaultUpdatedAt
pc.mutation.SetUpdatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (pc *ProductCreate) check() error {
if _, ok := pc.mutation.Name(); !ok {
return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Product.name"`)}
}
if _, ok := pc.mutation.Price(); !ok {
return &ValidationError{Name: "price", err: errors.New(`ent: missing required field "Product.price"`)}
}
if v, ok := pc.mutation.Price(); ok {
if err := product.PriceValidator(v); err != nil {
return &ValidationError{Name: "price", err: fmt.Errorf(`ent: validator failed for field "Product.price": %w`, err)}
}
}
if _, ok := pc.mutation.OriginalPrice(); !ok {
return &ValidationError{Name: "original_price", err: errors.New(`ent: missing required field "Product.original_price"`)}
}
if v, ok := pc.mutation.OriginalPrice(); ok {
if err := product.OriginalPriceValidator(v); err != nil {
return &ValidationError{Name: "original_price", err: fmt.Errorf(`ent: validator failed for field "Product.original_price": %w`, err)}
}
}
if _, ok := pc.mutation.Quantity(); !ok {
return &ValidationError{Name: "quantity", err: errors.New(`ent: missing required field "Product.quantity"`)}
}
if v, ok := pc.mutation.Quantity(); ok {
if err := product.QuantityValidator(v); err != nil {
return &ValidationError{Name: "quantity", err: fmt.Errorf(`ent: validator failed for field "Product.quantity": %w`, err)}
}
}
if _, ok := pc.mutation.Status(); !ok {
return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "Product.status"`)}
}
if _, ok := pc.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Product.created_at"`)}
}
if _, ok := pc.mutation.UpdatedAt(); !ok {
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Product.updated_at"`)}
}
return nil
}
func (pc *ProductCreate) sqlSave(ctx context.Context) (*Product, error) {
if err := pc.check(); err != nil {
return nil, err
}
_node, _spec := pc.createSpec()
if err := sqlgraph.CreateNode(ctx, pc.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)
pc.mutation.id = &_node.ID
pc.mutation.done = true
return _node, nil
}
func (pc *ProductCreate) createSpec() (*Product, *sqlgraph.CreateSpec) {
var (
_node = &Product{config: pc.config}
_spec = sqlgraph.NewCreateSpec(product.Table, sqlgraph.NewFieldSpec(product.FieldID, field.TypeInt))
)
if value, ok := pc.mutation.Name(); ok {
_spec.SetField(product.FieldName, field.TypeString, value)
_node.Name = value
}
if value, ok := pc.mutation.Description(); ok {
_spec.SetField(product.FieldDescription, field.TypeString, value)
_node.Description = &value
}
if value, ok := pc.mutation.Price(); ok {
_spec.SetField(product.FieldPrice, field.TypeFloat64, value)
_node.Price = value
}
if value, ok := pc.mutation.OriginalPrice(); ok {
_spec.SetField(product.FieldOriginalPrice, field.TypeFloat64, value)
_node.OriginalPrice = value
}
if value, ok := pc.mutation.Quantity(); ok {
_spec.SetField(product.FieldQuantity, field.TypeInt, value)
_node.Quantity = value
}
if value, ok := pc.mutation.Status(); ok {
_spec.SetField(product.FieldStatus, field.TypeBool, value)
_node.Status = value
}
if value, ok := pc.mutation.CreatedAt(); ok {
_spec.SetField(product.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := pc.mutation.UpdatedAt(); ok {
_spec.SetField(product.FieldUpdatedAt, field.TypeTime, value)
_node.UpdatedAt = value
}
if nodes := pc.mutation.ProductCategoryIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: true,
Table: product.ProductCategoryTable,
Columns: product.ProductCategoryPrimaryKey,
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)
}
return _node, _spec
}
// ProductCreateBulk is the builder for creating many Product entities in bulk.
type ProductCreateBulk struct {
config
err error
builders []*ProductCreate
}
// Save creates the Product entities in the database.
func (pcb *ProductCreateBulk) Save(ctx context.Context) ([]*Product, error) {
if pcb.err != nil {
return nil, pcb.err
}
specs := make([]*sqlgraph.CreateSpec, len(pcb.builders))
nodes := make([]*Product, len(pcb.builders))
mutators := make([]Mutator, len(pcb.builders))
for i := range pcb.builders {
func(i int, root context.Context) {
builder := pcb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*ProductMutation)
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, pcb.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, pcb.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, pcb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (pcb *ProductCreateBulk) SaveX(ctx context.Context) []*Product {
v, err := pcb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (pcb *ProductCreateBulk) Exec(ctx context.Context) error {
_, err := pcb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (pcb *ProductCreateBulk) ExecX(ctx context.Context) {
if err := pcb.Exec(ctx); err != nil {
panic(err)
}
}