738 lines
22 KiB
Go
738 lines
22 KiB
Go
|
// Code generated by ent, DO NOT EDIT.
|
||
|
|
||
|
package ent
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"online-order/ent/predicate"
|
||
|
"online-order/ent/product"
|
||
|
"online-order/ent/productcategory"
|
||
|
"time"
|
||
|
|
||
|
"entgo.io/ent/dialect/sql"
|
||
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
||
|
"entgo.io/ent/schema/field"
|
||
|
)
|
||
|
|
||
|
// ProductUpdate is the builder for updating Product entities.
|
||
|
type ProductUpdate struct {
|
||
|
config
|
||
|
hooks []Hook
|
||
|
mutation *ProductMutation
|
||
|
}
|
||
|
|
||
|
// Where appends a list predicates to the ProductUpdate builder.
|
||
|
func (pu *ProductUpdate) Where(ps ...predicate.Product) *ProductUpdate {
|
||
|
pu.mutation.Where(ps...)
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// SetName sets the "name" field.
|
||
|
func (pu *ProductUpdate) SetName(s string) *ProductUpdate {
|
||
|
pu.mutation.SetName(s)
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// SetNillableName sets the "name" field if the given value is not nil.
|
||
|
func (pu *ProductUpdate) SetNillableName(s *string) *ProductUpdate {
|
||
|
if s != nil {
|
||
|
pu.SetName(*s)
|
||
|
}
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// SetDescription sets the "description" field.
|
||
|
func (pu *ProductUpdate) SetDescription(s string) *ProductUpdate {
|
||
|
pu.mutation.SetDescription(s)
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// SetNillableDescription sets the "description" field if the given value is not nil.
|
||
|
func (pu *ProductUpdate) SetNillableDescription(s *string) *ProductUpdate {
|
||
|
if s != nil {
|
||
|
pu.SetDescription(*s)
|
||
|
}
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// ClearDescription clears the value of the "description" field.
|
||
|
func (pu *ProductUpdate) ClearDescription() *ProductUpdate {
|
||
|
pu.mutation.ClearDescription()
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// SetPrice sets the "price" field.
|
||
|
func (pu *ProductUpdate) SetPrice(f float64) *ProductUpdate {
|
||
|
pu.mutation.ResetPrice()
|
||
|
pu.mutation.SetPrice(f)
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// SetNillablePrice sets the "price" field if the given value is not nil.
|
||
|
func (pu *ProductUpdate) SetNillablePrice(f *float64) *ProductUpdate {
|
||
|
if f != nil {
|
||
|
pu.SetPrice(*f)
|
||
|
}
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// AddPrice adds f to the "price" field.
|
||
|
func (pu *ProductUpdate) AddPrice(f float64) *ProductUpdate {
|
||
|
pu.mutation.AddPrice(f)
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// SetOriginalPrice sets the "original_price" field.
|
||
|
func (pu *ProductUpdate) SetOriginalPrice(f float64) *ProductUpdate {
|
||
|
pu.mutation.ResetOriginalPrice()
|
||
|
pu.mutation.SetOriginalPrice(f)
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// SetNillableOriginalPrice sets the "original_price" field if the given value is not nil.
|
||
|
func (pu *ProductUpdate) SetNillableOriginalPrice(f *float64) *ProductUpdate {
|
||
|
if f != nil {
|
||
|
pu.SetOriginalPrice(*f)
|
||
|
}
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// AddOriginalPrice adds f to the "original_price" field.
|
||
|
func (pu *ProductUpdate) AddOriginalPrice(f float64) *ProductUpdate {
|
||
|
pu.mutation.AddOriginalPrice(f)
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// SetQuantity sets the "quantity" field.
|
||
|
func (pu *ProductUpdate) SetQuantity(i int) *ProductUpdate {
|
||
|
pu.mutation.ResetQuantity()
|
||
|
pu.mutation.SetQuantity(i)
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// SetNillableQuantity sets the "quantity" field if the given value is not nil.
|
||
|
func (pu *ProductUpdate) SetNillableQuantity(i *int) *ProductUpdate {
|
||
|
if i != nil {
|
||
|
pu.SetQuantity(*i)
|
||
|
}
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// AddQuantity adds i to the "quantity" field.
|
||
|
func (pu *ProductUpdate) AddQuantity(i int) *ProductUpdate {
|
||
|
pu.mutation.AddQuantity(i)
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// SetStatus sets the "status" field.
|
||
|
func (pu *ProductUpdate) SetStatus(b bool) *ProductUpdate {
|
||
|
pu.mutation.SetStatus(b)
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// SetNillableStatus sets the "status" field if the given value is not nil.
|
||
|
func (pu *ProductUpdate) SetNillableStatus(b *bool) *ProductUpdate {
|
||
|
if b != nil {
|
||
|
pu.SetStatus(*b)
|
||
|
}
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// SetCreatedAt sets the "created_at" field.
|
||
|
func (pu *ProductUpdate) SetCreatedAt(t time.Time) *ProductUpdate {
|
||
|
pu.mutation.SetCreatedAt(t)
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
|
||
|
func (pu *ProductUpdate) SetNillableCreatedAt(t *time.Time) *ProductUpdate {
|
||
|
if t != nil {
|
||
|
pu.SetCreatedAt(*t)
|
||
|
}
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// SetUpdatedAt sets the "updated_at" field.
|
||
|
func (pu *ProductUpdate) SetUpdatedAt(t time.Time) *ProductUpdate {
|
||
|
pu.mutation.SetUpdatedAt(t)
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// AddProductCategoryIDs adds the "productCategory" edge to the ProductCategory entity by IDs.
|
||
|
func (pu *ProductUpdate) AddProductCategoryIDs(ids ...int) *ProductUpdate {
|
||
|
pu.mutation.AddProductCategoryIDs(ids...)
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// AddProductCategory adds the "productCategory" edges to the ProductCategory entity.
|
||
|
func (pu *ProductUpdate) AddProductCategory(p ...*ProductCategory) *ProductUpdate {
|
||
|
ids := make([]int, len(p))
|
||
|
for i := range p {
|
||
|
ids[i] = p[i].ID
|
||
|
}
|
||
|
return pu.AddProductCategoryIDs(ids...)
|
||
|
}
|
||
|
|
||
|
// Mutation returns the ProductMutation object of the builder.
|
||
|
func (pu *ProductUpdate) Mutation() *ProductMutation {
|
||
|
return pu.mutation
|
||
|
}
|
||
|
|
||
|
// ClearProductCategory clears all "productCategory" edges to the ProductCategory entity.
|
||
|
func (pu *ProductUpdate) ClearProductCategory() *ProductUpdate {
|
||
|
pu.mutation.ClearProductCategory()
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// RemoveProductCategoryIDs removes the "productCategory" edge to ProductCategory entities by IDs.
|
||
|
func (pu *ProductUpdate) RemoveProductCategoryIDs(ids ...int) *ProductUpdate {
|
||
|
pu.mutation.RemoveProductCategoryIDs(ids...)
|
||
|
return pu
|
||
|
}
|
||
|
|
||
|
// RemoveProductCategory removes "productCategory" edges to ProductCategory entities.
|
||
|
func (pu *ProductUpdate) RemoveProductCategory(p ...*ProductCategory) *ProductUpdate {
|
||
|
ids := make([]int, len(p))
|
||
|
for i := range p {
|
||
|
ids[i] = p[i].ID
|
||
|
}
|
||
|
return pu.RemoveProductCategoryIDs(ids...)
|
||
|
}
|
||
|
|
||
|
// Save executes the query and returns the number of nodes affected by the update operation.
|
||
|
func (pu *ProductUpdate) Save(ctx context.Context) (int, error) {
|
||
|
pu.defaults()
|
||
|
return withHooks(ctx, pu.sqlSave, pu.mutation, pu.hooks)
|
||
|
}
|
||
|
|
||
|
// SaveX is like Save, but panics if an error occurs.
|
||
|
func (pu *ProductUpdate) SaveX(ctx context.Context) int {
|
||
|
affected, err := pu.Save(ctx)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return affected
|
||
|
}
|
||
|
|
||
|
// Exec executes the query.
|
||
|
func (pu *ProductUpdate) Exec(ctx context.Context) error {
|
||
|
_, err := pu.Save(ctx)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// ExecX is like Exec, but panics if an error occurs.
|
||
|
func (pu *ProductUpdate) ExecX(ctx context.Context) {
|
||
|
if err := pu.Exec(ctx); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// defaults sets the default values of the builder before save.
|
||
|
func (pu *ProductUpdate) defaults() {
|
||
|
if _, ok := pu.mutation.UpdatedAt(); !ok {
|
||
|
v := product.UpdateDefaultUpdatedAt()
|
||
|
pu.mutation.SetUpdatedAt(v)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// check runs all checks and user-defined validators on the builder.
|
||
|
func (pu *ProductUpdate) check() error {
|
||
|
if v, ok := pu.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 v, ok := pu.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 v, ok := pu.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)}
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (pu *ProductUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
||
|
if err := pu.check(); err != nil {
|
||
|
return n, err
|
||
|
}
|
||
|
_spec := sqlgraph.NewUpdateSpec(product.Table, product.Columns, sqlgraph.NewFieldSpec(product.FieldID, field.TypeInt))
|
||
|
if ps := pu.mutation.predicates; len(ps) > 0 {
|
||
|
_spec.Predicate = func(selector *sql.Selector) {
|
||
|
for i := range ps {
|
||
|
ps[i](selector)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
if value, ok := pu.mutation.Name(); ok {
|
||
|
_spec.SetField(product.FieldName, field.TypeString, value)
|
||
|
}
|
||
|
if value, ok := pu.mutation.Description(); ok {
|
||
|
_spec.SetField(product.FieldDescription, field.TypeString, value)
|
||
|
}
|
||
|
if pu.mutation.DescriptionCleared() {
|
||
|
_spec.ClearField(product.FieldDescription, field.TypeString)
|
||
|
}
|
||
|
if value, ok := pu.mutation.Price(); ok {
|
||
|
_spec.SetField(product.FieldPrice, field.TypeFloat64, value)
|
||
|
}
|
||
|
if value, ok := pu.mutation.AddedPrice(); ok {
|
||
|
_spec.AddField(product.FieldPrice, field.TypeFloat64, value)
|
||
|
}
|
||
|
if value, ok := pu.mutation.OriginalPrice(); ok {
|
||
|
_spec.SetField(product.FieldOriginalPrice, field.TypeFloat64, value)
|
||
|
}
|
||
|
if value, ok := pu.mutation.AddedOriginalPrice(); ok {
|
||
|
_spec.AddField(product.FieldOriginalPrice, field.TypeFloat64, value)
|
||
|
}
|
||
|
if value, ok := pu.mutation.Quantity(); ok {
|
||
|
_spec.SetField(product.FieldQuantity, field.TypeInt, value)
|
||
|
}
|
||
|
if value, ok := pu.mutation.AddedQuantity(); ok {
|
||
|
_spec.AddField(product.FieldQuantity, field.TypeInt, value)
|
||
|
}
|
||
|
if value, ok := pu.mutation.Status(); ok {
|
||
|
_spec.SetField(product.FieldStatus, field.TypeBool, value)
|
||
|
}
|
||
|
if value, ok := pu.mutation.CreatedAt(); ok {
|
||
|
_spec.SetField(product.FieldCreatedAt, field.TypeTime, value)
|
||
|
}
|
||
|
if value, ok := pu.mutation.UpdatedAt(); ok {
|
||
|
_spec.SetField(product.FieldUpdatedAt, field.TypeTime, value)
|
||
|
}
|
||
|
if pu.mutation.ProductCategoryCleared() {
|
||
|
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),
|
||
|
},
|
||
|
}
|
||
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
||
|
}
|
||
|
if nodes := pu.mutation.RemovedProductCategoryIDs(); len(nodes) > 0 && !pu.mutation.ProductCategoryCleared() {
|
||
|
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.Clear = append(_spec.Edges.Clear, edge)
|
||
|
}
|
||
|
if nodes := pu.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.Add = append(_spec.Edges.Add, edge)
|
||
|
}
|
||
|
if n, err = sqlgraph.UpdateNodes(ctx, pu.driver, _spec); err != nil {
|
||
|
if _, ok := err.(*sqlgraph.NotFoundError); ok {
|
||
|
err = &NotFoundError{product.Label}
|
||
|
} else if sqlgraph.IsConstraintError(err) {
|
||
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
||
|
}
|
||
|
return 0, err
|
||
|
}
|
||
|
pu.mutation.done = true
|
||
|
return n, nil
|
||
|
}
|
||
|
|
||
|
// ProductUpdateOne is the builder for updating a single Product entity.
|
||
|
type ProductUpdateOne struct {
|
||
|
config
|
||
|
fields []string
|
||
|
hooks []Hook
|
||
|
mutation *ProductMutation
|
||
|
}
|
||
|
|
||
|
// SetName sets the "name" field.
|
||
|
func (puo *ProductUpdateOne) SetName(s string) *ProductUpdateOne {
|
||
|
puo.mutation.SetName(s)
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// SetNillableName sets the "name" field if the given value is not nil.
|
||
|
func (puo *ProductUpdateOne) SetNillableName(s *string) *ProductUpdateOne {
|
||
|
if s != nil {
|
||
|
puo.SetName(*s)
|
||
|
}
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// SetDescription sets the "description" field.
|
||
|
func (puo *ProductUpdateOne) SetDescription(s string) *ProductUpdateOne {
|
||
|
puo.mutation.SetDescription(s)
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// SetNillableDescription sets the "description" field if the given value is not nil.
|
||
|
func (puo *ProductUpdateOne) SetNillableDescription(s *string) *ProductUpdateOne {
|
||
|
if s != nil {
|
||
|
puo.SetDescription(*s)
|
||
|
}
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// ClearDescription clears the value of the "description" field.
|
||
|
func (puo *ProductUpdateOne) ClearDescription() *ProductUpdateOne {
|
||
|
puo.mutation.ClearDescription()
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// SetPrice sets the "price" field.
|
||
|
func (puo *ProductUpdateOne) SetPrice(f float64) *ProductUpdateOne {
|
||
|
puo.mutation.ResetPrice()
|
||
|
puo.mutation.SetPrice(f)
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// SetNillablePrice sets the "price" field if the given value is not nil.
|
||
|
func (puo *ProductUpdateOne) SetNillablePrice(f *float64) *ProductUpdateOne {
|
||
|
if f != nil {
|
||
|
puo.SetPrice(*f)
|
||
|
}
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// AddPrice adds f to the "price" field.
|
||
|
func (puo *ProductUpdateOne) AddPrice(f float64) *ProductUpdateOne {
|
||
|
puo.mutation.AddPrice(f)
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// SetOriginalPrice sets the "original_price" field.
|
||
|
func (puo *ProductUpdateOne) SetOriginalPrice(f float64) *ProductUpdateOne {
|
||
|
puo.mutation.ResetOriginalPrice()
|
||
|
puo.mutation.SetOriginalPrice(f)
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// SetNillableOriginalPrice sets the "original_price" field if the given value is not nil.
|
||
|
func (puo *ProductUpdateOne) SetNillableOriginalPrice(f *float64) *ProductUpdateOne {
|
||
|
if f != nil {
|
||
|
puo.SetOriginalPrice(*f)
|
||
|
}
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// AddOriginalPrice adds f to the "original_price" field.
|
||
|
func (puo *ProductUpdateOne) AddOriginalPrice(f float64) *ProductUpdateOne {
|
||
|
puo.mutation.AddOriginalPrice(f)
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// SetQuantity sets the "quantity" field.
|
||
|
func (puo *ProductUpdateOne) SetQuantity(i int) *ProductUpdateOne {
|
||
|
puo.mutation.ResetQuantity()
|
||
|
puo.mutation.SetQuantity(i)
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// SetNillableQuantity sets the "quantity" field if the given value is not nil.
|
||
|
func (puo *ProductUpdateOne) SetNillableQuantity(i *int) *ProductUpdateOne {
|
||
|
if i != nil {
|
||
|
puo.SetQuantity(*i)
|
||
|
}
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// AddQuantity adds i to the "quantity" field.
|
||
|
func (puo *ProductUpdateOne) AddQuantity(i int) *ProductUpdateOne {
|
||
|
puo.mutation.AddQuantity(i)
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// SetStatus sets the "status" field.
|
||
|
func (puo *ProductUpdateOne) SetStatus(b bool) *ProductUpdateOne {
|
||
|
puo.mutation.SetStatus(b)
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// SetNillableStatus sets the "status" field if the given value is not nil.
|
||
|
func (puo *ProductUpdateOne) SetNillableStatus(b *bool) *ProductUpdateOne {
|
||
|
if b != nil {
|
||
|
puo.SetStatus(*b)
|
||
|
}
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// SetCreatedAt sets the "created_at" field.
|
||
|
func (puo *ProductUpdateOne) SetCreatedAt(t time.Time) *ProductUpdateOne {
|
||
|
puo.mutation.SetCreatedAt(t)
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
|
||
|
func (puo *ProductUpdateOne) SetNillableCreatedAt(t *time.Time) *ProductUpdateOne {
|
||
|
if t != nil {
|
||
|
puo.SetCreatedAt(*t)
|
||
|
}
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// SetUpdatedAt sets the "updated_at" field.
|
||
|
func (puo *ProductUpdateOne) SetUpdatedAt(t time.Time) *ProductUpdateOne {
|
||
|
puo.mutation.SetUpdatedAt(t)
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// AddProductCategoryIDs adds the "productCategory" edge to the ProductCategory entity by IDs.
|
||
|
func (puo *ProductUpdateOne) AddProductCategoryIDs(ids ...int) *ProductUpdateOne {
|
||
|
puo.mutation.AddProductCategoryIDs(ids...)
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// AddProductCategory adds the "productCategory" edges to the ProductCategory entity.
|
||
|
func (puo *ProductUpdateOne) AddProductCategory(p ...*ProductCategory) *ProductUpdateOne {
|
||
|
ids := make([]int, len(p))
|
||
|
for i := range p {
|
||
|
ids[i] = p[i].ID
|
||
|
}
|
||
|
return puo.AddProductCategoryIDs(ids...)
|
||
|
}
|
||
|
|
||
|
// Mutation returns the ProductMutation object of the builder.
|
||
|
func (puo *ProductUpdateOne) Mutation() *ProductMutation {
|
||
|
return puo.mutation
|
||
|
}
|
||
|
|
||
|
// ClearProductCategory clears all "productCategory" edges to the ProductCategory entity.
|
||
|
func (puo *ProductUpdateOne) ClearProductCategory() *ProductUpdateOne {
|
||
|
puo.mutation.ClearProductCategory()
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// RemoveProductCategoryIDs removes the "productCategory" edge to ProductCategory entities by IDs.
|
||
|
func (puo *ProductUpdateOne) RemoveProductCategoryIDs(ids ...int) *ProductUpdateOne {
|
||
|
puo.mutation.RemoveProductCategoryIDs(ids...)
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// RemoveProductCategory removes "productCategory" edges to ProductCategory entities.
|
||
|
func (puo *ProductUpdateOne) RemoveProductCategory(p ...*ProductCategory) *ProductUpdateOne {
|
||
|
ids := make([]int, len(p))
|
||
|
for i := range p {
|
||
|
ids[i] = p[i].ID
|
||
|
}
|
||
|
return puo.RemoveProductCategoryIDs(ids...)
|
||
|
}
|
||
|
|
||
|
// Where appends a list predicates to the ProductUpdate builder.
|
||
|
func (puo *ProductUpdateOne) Where(ps ...predicate.Product) *ProductUpdateOne {
|
||
|
puo.mutation.Where(ps...)
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// Select allows selecting one or more fields (columns) of the returned entity.
|
||
|
// The default is selecting all fields defined in the entity schema.
|
||
|
func (puo *ProductUpdateOne) Select(field string, fields ...string) *ProductUpdateOne {
|
||
|
puo.fields = append([]string{field}, fields...)
|
||
|
return puo
|
||
|
}
|
||
|
|
||
|
// Save executes the query and returns the updated Product entity.
|
||
|
func (puo *ProductUpdateOne) Save(ctx context.Context) (*Product, error) {
|
||
|
puo.defaults()
|
||
|
return withHooks(ctx, puo.sqlSave, puo.mutation, puo.hooks)
|
||
|
}
|
||
|
|
||
|
// SaveX is like Save, but panics if an error occurs.
|
||
|
func (puo *ProductUpdateOne) SaveX(ctx context.Context) *Product {
|
||
|
node, err := puo.Save(ctx)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return node
|
||
|
}
|
||
|
|
||
|
// Exec executes the query on the entity.
|
||
|
func (puo *ProductUpdateOne) Exec(ctx context.Context) error {
|
||
|
_, err := puo.Save(ctx)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// ExecX is like Exec, but panics if an error occurs.
|
||
|
func (puo *ProductUpdateOne) ExecX(ctx context.Context) {
|
||
|
if err := puo.Exec(ctx); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// defaults sets the default values of the builder before save.
|
||
|
func (puo *ProductUpdateOne) defaults() {
|
||
|
if _, ok := puo.mutation.UpdatedAt(); !ok {
|
||
|
v := product.UpdateDefaultUpdatedAt()
|
||
|
puo.mutation.SetUpdatedAt(v)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// check runs all checks and user-defined validators on the builder.
|
||
|
func (puo *ProductUpdateOne) check() error {
|
||
|
if v, ok := puo.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 v, ok := puo.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 v, ok := puo.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)}
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (puo *ProductUpdateOne) sqlSave(ctx context.Context) (_node *Product, err error) {
|
||
|
if err := puo.check(); err != nil {
|
||
|
return _node, err
|
||
|
}
|
||
|
_spec := sqlgraph.NewUpdateSpec(product.Table, product.Columns, sqlgraph.NewFieldSpec(product.FieldID, field.TypeInt))
|
||
|
id, ok := puo.mutation.ID()
|
||
|
if !ok {
|
||
|
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Product.id" for update`)}
|
||
|
}
|
||
|
_spec.Node.ID.Value = id
|
||
|
if fields := puo.fields; len(fields) > 0 {
|
||
|
_spec.Node.Columns = make([]string, 0, len(fields))
|
||
|
_spec.Node.Columns = append(_spec.Node.Columns, product.FieldID)
|
||
|
for _, f := range fields {
|
||
|
if !product.ValidColumn(f) {
|
||
|
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
|
||
|
}
|
||
|
if f != product.FieldID {
|
||
|
_spec.Node.Columns = append(_spec.Node.Columns, f)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
if ps := puo.mutation.predicates; len(ps) > 0 {
|
||
|
_spec.Predicate = func(selector *sql.Selector) {
|
||
|
for i := range ps {
|
||
|
ps[i](selector)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
if value, ok := puo.mutation.Name(); ok {
|
||
|
_spec.SetField(product.FieldName, field.TypeString, value)
|
||
|
}
|
||
|
if value, ok := puo.mutation.Description(); ok {
|
||
|
_spec.SetField(product.FieldDescription, field.TypeString, value)
|
||
|
}
|
||
|
if puo.mutation.DescriptionCleared() {
|
||
|
_spec.ClearField(product.FieldDescription, field.TypeString)
|
||
|
}
|
||
|
if value, ok := puo.mutation.Price(); ok {
|
||
|
_spec.SetField(product.FieldPrice, field.TypeFloat64, value)
|
||
|
}
|
||
|
if value, ok := puo.mutation.AddedPrice(); ok {
|
||
|
_spec.AddField(product.FieldPrice, field.TypeFloat64, value)
|
||
|
}
|
||
|
if value, ok := puo.mutation.OriginalPrice(); ok {
|
||
|
_spec.SetField(product.FieldOriginalPrice, field.TypeFloat64, value)
|
||
|
}
|
||
|
if value, ok := puo.mutation.AddedOriginalPrice(); ok {
|
||
|
_spec.AddField(product.FieldOriginalPrice, field.TypeFloat64, value)
|
||
|
}
|
||
|
if value, ok := puo.mutation.Quantity(); ok {
|
||
|
_spec.SetField(product.FieldQuantity, field.TypeInt, value)
|
||
|
}
|
||
|
if value, ok := puo.mutation.AddedQuantity(); ok {
|
||
|
_spec.AddField(product.FieldQuantity, field.TypeInt, value)
|
||
|
}
|
||
|
if value, ok := puo.mutation.Status(); ok {
|
||
|
_spec.SetField(product.FieldStatus, field.TypeBool, value)
|
||
|
}
|
||
|
if value, ok := puo.mutation.CreatedAt(); ok {
|
||
|
_spec.SetField(product.FieldCreatedAt, field.TypeTime, value)
|
||
|
}
|
||
|
if value, ok := puo.mutation.UpdatedAt(); ok {
|
||
|
_spec.SetField(product.FieldUpdatedAt, field.TypeTime, value)
|
||
|
}
|
||
|
if puo.mutation.ProductCategoryCleared() {
|
||
|
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),
|
||
|
},
|
||
|
}
|
||
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
||
|
}
|
||
|
if nodes := puo.mutation.RemovedProductCategoryIDs(); len(nodes) > 0 && !puo.mutation.ProductCategoryCleared() {
|
||
|
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.Clear = append(_spec.Edges.Clear, edge)
|
||
|
}
|
||
|
if nodes := puo.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.Add = append(_spec.Edges.Add, edge)
|
||
|
}
|
||
|
_node = &Product{config: puo.config}
|
||
|
_spec.Assign = _node.assignValues
|
||
|
_spec.ScanValues = _node.scanValues
|
||
|
if err = sqlgraph.UpdateNode(ctx, puo.driver, _spec); err != nil {
|
||
|
if _, ok := err.(*sqlgraph.NotFoundError); ok {
|
||
|
err = &NotFoundError{product.Label}
|
||
|
} else if sqlgraph.IsConstraintError(err) {
|
||
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
||
|
}
|
||
|
return nil, err
|
||
|
}
|
||
|
puo.mutation.done = true
|
||
|
return _node, nil
|
||
|
}
|