online-order/ent/product_update.go

1074 lines
31 KiB
Go
Raw Permalink Normal View History

2023-10-27 09:51:58 +00:00
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
2023-10-28 23:12:07 +00:00
"online-order/ent/business"
2023-10-27 09:51:58 +00:00
"online-order/ent/predicate"
"online-order/ent/product"
"online-order/ent/productcategory"
2023-10-28 23:12:07 +00:00
"online-order/ent/user"
2023-10-27 09:51:58 +00:00
"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
}
2023-10-28 23:12:07 +00:00
// SetSummary sets the "summary" field.
func (pu *ProductUpdate) SetSummary(s string) *ProductUpdate {
pu.mutation.SetSummary(s)
return pu
}
// SetNillableSummary sets the "summary" field if the given value is not nil.
func (pu *ProductUpdate) SetNillableSummary(s *string) *ProductUpdate {
if s != nil {
pu.SetSummary(*s)
}
return pu
}
// ClearSummary clears the value of the "summary" field.
func (pu *ProductUpdate) ClearSummary() *ProductUpdate {
pu.mutation.ClearSummary()
return pu
}
2023-10-27 09:51:58 +00:00
// 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
}
2023-10-28 23:12:07 +00:00
// SetProductCategoryID sets the "product_category_id" field.
func (pu *ProductUpdate) SetProductCategoryID(i int) *ProductUpdate {
pu.mutation.SetProductCategoryID(i)
return pu
}
// SetNillableProductCategoryID sets the "product_category_id" field if the given value is not nil.
func (pu *ProductUpdate) SetNillableProductCategoryID(i *int) *ProductUpdate {
if i != nil {
pu.SetProductCategoryID(*i)
}
return pu
}
// ClearProductCategoryID clears the value of the "product_category_id" field.
func (pu *ProductUpdate) ClearProductCategoryID() *ProductUpdate {
pu.mutation.ClearProductCategoryID()
return pu
}
// SetBusinessID sets the "business_id" field.
func (pu *ProductUpdate) SetBusinessID(i int) *ProductUpdate {
pu.mutation.SetBusinessID(i)
return pu
}
// SetNillableBusinessID sets the "business_id" field if the given value is not nil.
func (pu *ProductUpdate) SetNillableBusinessID(i *int) *ProductUpdate {
if i != nil {
pu.SetBusinessID(*i)
}
return pu
}
// ClearBusinessID clears the value of the "business_id" field.
func (pu *ProductUpdate) ClearBusinessID() *ProductUpdate {
pu.mutation.ClearBusinessID()
return pu
}
// SetUserID sets the "user_id" field.
func (pu *ProductUpdate) SetUserID(i int) *ProductUpdate {
pu.mutation.SetUserID(i)
return pu
}
// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (pu *ProductUpdate) SetNillableUserID(i *int) *ProductUpdate {
if i != nil {
pu.SetUserID(*i)
}
return pu
}
// ClearUserID clears the value of the "user_id" field.
func (pu *ProductUpdate) ClearUserID() *ProductUpdate {
pu.mutation.ClearUserID()
return pu
}
2023-10-27 09:51:58 +00:00
// 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
}
2023-10-28 23:12:07 +00:00
// SetProductCategoriesID sets the "product_categories" edge to the ProductCategory entity by ID.
func (pu *ProductUpdate) SetProductCategoriesID(id int) *ProductUpdate {
pu.mutation.SetProductCategoriesID(id)
2023-10-27 09:51:58 +00:00
return pu
}
2023-10-28 23:12:07 +00:00
// SetNillableProductCategoriesID sets the "product_categories" edge to the ProductCategory entity by ID if the given value is not nil.
func (pu *ProductUpdate) SetNillableProductCategoriesID(id *int) *ProductUpdate {
if id != nil {
pu = pu.SetProductCategoriesID(*id)
2023-10-27 09:51:58 +00:00
}
2023-10-28 23:12:07 +00:00
return pu
}
// SetProductCategories sets the "product_categories" edge to the ProductCategory entity.
func (pu *ProductUpdate) SetProductCategories(p *ProductCategory) *ProductUpdate {
return pu.SetProductCategoriesID(p.ID)
}
// SetBusinessesID sets the "businesses" edge to the Business entity by ID.
func (pu *ProductUpdate) SetBusinessesID(id int) *ProductUpdate {
pu.mutation.SetBusinessesID(id)
return pu
}
// SetNillableBusinessesID sets the "businesses" edge to the Business entity by ID if the given value is not nil.
func (pu *ProductUpdate) SetNillableBusinessesID(id *int) *ProductUpdate {
if id != nil {
pu = pu.SetBusinessesID(*id)
}
return pu
}
// SetBusinesses sets the "businesses" edge to the Business entity.
func (pu *ProductUpdate) SetBusinesses(b *Business) *ProductUpdate {
return pu.SetBusinessesID(b.ID)
}
// SetUsersID sets the "users" edge to the User entity by ID.
func (pu *ProductUpdate) SetUsersID(id int) *ProductUpdate {
pu.mutation.SetUsersID(id)
return pu
}
// SetNillableUsersID sets the "users" edge to the User entity by ID if the given value is not nil.
func (pu *ProductUpdate) SetNillableUsersID(id *int) *ProductUpdate {
if id != nil {
pu = pu.SetUsersID(*id)
}
return pu
}
// SetUsers sets the "users" edge to the User entity.
func (pu *ProductUpdate) SetUsers(u *User) *ProductUpdate {
return pu.SetUsersID(u.ID)
2023-10-27 09:51:58 +00:00
}
// Mutation returns the ProductMutation object of the builder.
func (pu *ProductUpdate) Mutation() *ProductMutation {
return pu.mutation
}
2023-10-28 23:12:07 +00:00
// ClearProductCategories clears the "product_categories" edge to the ProductCategory entity.
func (pu *ProductUpdate) ClearProductCategories() *ProductUpdate {
pu.mutation.ClearProductCategories()
2023-10-27 09:51:58 +00:00
return pu
}
2023-10-28 23:12:07 +00:00
// ClearBusinesses clears the "businesses" edge to the Business entity.
func (pu *ProductUpdate) ClearBusinesses() *ProductUpdate {
pu.mutation.ClearBusinesses()
2023-10-27 09:51:58 +00:00
return pu
}
2023-10-28 23:12:07 +00:00
// ClearUsers clears the "users" edge to the User entity.
func (pu *ProductUpdate) ClearUsers() *ProductUpdate {
pu.mutation.ClearUsers()
return pu
2023-10-27 09:51:58 +00:00
}
// 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)
}
2023-10-28 23:12:07 +00:00
if value, ok := pu.mutation.Summary(); ok {
_spec.SetField(product.FieldSummary, field.TypeString, value)
}
if pu.mutation.SummaryCleared() {
_spec.ClearField(product.FieldSummary, field.TypeString)
}
2023-10-27 09:51:58 +00:00
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)
}
2023-10-28 23:12:07 +00:00
if pu.mutation.ProductCategoriesCleared() {
2023-10-27 09:51:58 +00:00
edge := &sqlgraph.EdgeSpec{
2023-10-28 23:12:07 +00:00
Rel: sqlgraph.M2O,
2023-10-27 09:51:58 +00:00
Inverse: true,
2023-10-28 23:12:07 +00:00
Table: product.ProductCategoriesTable,
Columns: []string{product.ProductCategoriesColumn},
2023-10-27 09:51:58 +00:00
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(productcategory.FieldID, field.TypeInt),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
2023-10-28 23:12:07 +00:00
if nodes := pu.mutation.ProductCategoriesIDs(); len(nodes) > 0 {
2023-10-27 09:51:58 +00:00
edge := &sqlgraph.EdgeSpec{
2023-10-28 23:12:07 +00:00
Rel: sqlgraph.M2O,
2023-10-27 09:51:58 +00:00
Inverse: true,
2023-10-28 23:12:07 +00:00
Table: product.ProductCategoriesTable,
Columns: []string{product.ProductCategoriesColumn},
2023-10-27 09:51:58 +00:00
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(productcategory.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
2023-10-28 23:12:07 +00:00
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if pu.mutation.BusinessesCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: product.BusinessesTable,
Columns: []string{product.BusinessesColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(business.FieldID, field.TypeInt),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := pu.mutation.BusinessesIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: product.BusinessesTable,
Columns: []string{product.BusinessesColumn},
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.Add = append(_spec.Edges.Add, edge)
}
if pu.mutation.UsersCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: product.UsersTable,
Columns: []string{product.UsersColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
},
}
2023-10-27 09:51:58 +00:00
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
2023-10-28 23:12:07 +00:00
if nodes := pu.mutation.UsersIDs(); len(nodes) > 0 {
2023-10-27 09:51:58 +00:00
edge := &sqlgraph.EdgeSpec{
2023-10-28 23:12:07 +00:00
Rel: sqlgraph.M2O,
2023-10-27 09:51:58 +00:00
Inverse: true,
2023-10-28 23:12:07 +00:00
Table: product.UsersTable,
Columns: []string{product.UsersColumn},
2023-10-27 09:51:58 +00:00
Bidi: false,
Target: &sqlgraph.EdgeTarget{
2023-10-28 23:12:07 +00:00
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
2023-10-27 09:51:58 +00:00
},
}
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
}
2023-10-28 23:12:07 +00:00
// SetSummary sets the "summary" field.
func (puo *ProductUpdateOne) SetSummary(s string) *ProductUpdateOne {
puo.mutation.SetSummary(s)
return puo
}
// SetNillableSummary sets the "summary" field if the given value is not nil.
func (puo *ProductUpdateOne) SetNillableSummary(s *string) *ProductUpdateOne {
if s != nil {
puo.SetSummary(*s)
}
return puo
}
// ClearSummary clears the value of the "summary" field.
func (puo *ProductUpdateOne) ClearSummary() *ProductUpdateOne {
puo.mutation.ClearSummary()
return puo
}
2023-10-27 09:51:58 +00:00
// 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
}
2023-10-28 23:12:07 +00:00
// SetProductCategoryID sets the "product_category_id" field.
func (puo *ProductUpdateOne) SetProductCategoryID(i int) *ProductUpdateOne {
puo.mutation.SetProductCategoryID(i)
return puo
}
// SetNillableProductCategoryID sets the "product_category_id" field if the given value is not nil.
func (puo *ProductUpdateOne) SetNillableProductCategoryID(i *int) *ProductUpdateOne {
if i != nil {
puo.SetProductCategoryID(*i)
}
return puo
}
// ClearProductCategoryID clears the value of the "product_category_id" field.
func (puo *ProductUpdateOne) ClearProductCategoryID() *ProductUpdateOne {
puo.mutation.ClearProductCategoryID()
return puo
}
// SetBusinessID sets the "business_id" field.
func (puo *ProductUpdateOne) SetBusinessID(i int) *ProductUpdateOne {
puo.mutation.SetBusinessID(i)
return puo
}
// SetNillableBusinessID sets the "business_id" field if the given value is not nil.
func (puo *ProductUpdateOne) SetNillableBusinessID(i *int) *ProductUpdateOne {
if i != nil {
puo.SetBusinessID(*i)
}
return puo
}
// ClearBusinessID clears the value of the "business_id" field.
func (puo *ProductUpdateOne) ClearBusinessID() *ProductUpdateOne {
puo.mutation.ClearBusinessID()
return puo
}
// SetUserID sets the "user_id" field.
func (puo *ProductUpdateOne) SetUserID(i int) *ProductUpdateOne {
puo.mutation.SetUserID(i)
return puo
}
// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (puo *ProductUpdateOne) SetNillableUserID(i *int) *ProductUpdateOne {
if i != nil {
puo.SetUserID(*i)
}
return puo
}
// ClearUserID clears the value of the "user_id" field.
func (puo *ProductUpdateOne) ClearUserID() *ProductUpdateOne {
puo.mutation.ClearUserID()
return puo
}
2023-10-27 09:51:58 +00:00
// 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
}
2023-10-28 23:12:07 +00:00
// SetProductCategoriesID sets the "product_categories" edge to the ProductCategory entity by ID.
func (puo *ProductUpdateOne) SetProductCategoriesID(id int) *ProductUpdateOne {
puo.mutation.SetProductCategoriesID(id)
2023-10-27 09:51:58 +00:00
return puo
}
2023-10-28 23:12:07 +00:00
// SetNillableProductCategoriesID sets the "product_categories" edge to the ProductCategory entity by ID if the given value is not nil.
func (puo *ProductUpdateOne) SetNillableProductCategoriesID(id *int) *ProductUpdateOne {
if id != nil {
puo = puo.SetProductCategoriesID(*id)
2023-10-27 09:51:58 +00:00
}
2023-10-28 23:12:07 +00:00
return puo
}
// SetProductCategories sets the "product_categories" edge to the ProductCategory entity.
func (puo *ProductUpdateOne) SetProductCategories(p *ProductCategory) *ProductUpdateOne {
return puo.SetProductCategoriesID(p.ID)
}
// SetBusinessesID sets the "businesses" edge to the Business entity by ID.
func (puo *ProductUpdateOne) SetBusinessesID(id int) *ProductUpdateOne {
puo.mutation.SetBusinessesID(id)
return puo
}
// SetNillableBusinessesID sets the "businesses" edge to the Business entity by ID if the given value is not nil.
func (puo *ProductUpdateOne) SetNillableBusinessesID(id *int) *ProductUpdateOne {
if id != nil {
puo = puo.SetBusinessesID(*id)
}
return puo
}
// SetBusinesses sets the "businesses" edge to the Business entity.
func (puo *ProductUpdateOne) SetBusinesses(b *Business) *ProductUpdateOne {
return puo.SetBusinessesID(b.ID)
}
// SetUsersID sets the "users" edge to the User entity by ID.
func (puo *ProductUpdateOne) SetUsersID(id int) *ProductUpdateOne {
puo.mutation.SetUsersID(id)
return puo
}
// SetNillableUsersID sets the "users" edge to the User entity by ID if the given value is not nil.
func (puo *ProductUpdateOne) SetNillableUsersID(id *int) *ProductUpdateOne {
if id != nil {
puo = puo.SetUsersID(*id)
}
return puo
}
// SetUsers sets the "users" edge to the User entity.
func (puo *ProductUpdateOne) SetUsers(u *User) *ProductUpdateOne {
return puo.SetUsersID(u.ID)
2023-10-27 09:51:58 +00:00
}
// Mutation returns the ProductMutation object of the builder.
func (puo *ProductUpdateOne) Mutation() *ProductMutation {
return puo.mutation
}
2023-10-28 23:12:07 +00:00
// ClearProductCategories clears the "product_categories" edge to the ProductCategory entity.
func (puo *ProductUpdateOne) ClearProductCategories() *ProductUpdateOne {
puo.mutation.ClearProductCategories()
2023-10-27 09:51:58 +00:00
return puo
}
2023-10-28 23:12:07 +00:00
// ClearBusinesses clears the "businesses" edge to the Business entity.
func (puo *ProductUpdateOne) ClearBusinesses() *ProductUpdateOne {
puo.mutation.ClearBusinesses()
2023-10-27 09:51:58 +00:00
return puo
}
2023-10-28 23:12:07 +00:00
// ClearUsers clears the "users" edge to the User entity.
func (puo *ProductUpdateOne) ClearUsers() *ProductUpdateOne {
puo.mutation.ClearUsers()
return puo
2023-10-27 09:51:58 +00:00
}
// 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)
}
2023-10-28 23:12:07 +00:00
if value, ok := puo.mutation.Summary(); ok {
_spec.SetField(product.FieldSummary, field.TypeString, value)
}
if puo.mutation.SummaryCleared() {
_spec.ClearField(product.FieldSummary, field.TypeString)
}
2023-10-27 09:51:58 +00:00
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)
}
2023-10-28 23:12:07 +00:00
if puo.mutation.ProductCategoriesCleared() {
2023-10-27 09:51:58 +00:00
edge := &sqlgraph.EdgeSpec{
2023-10-28 23:12:07 +00:00
Rel: sqlgraph.M2O,
2023-10-27 09:51:58 +00:00
Inverse: true,
2023-10-28 23:12:07 +00:00
Table: product.ProductCategoriesTable,
Columns: []string{product.ProductCategoriesColumn},
2023-10-27 09:51:58 +00:00
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(productcategory.FieldID, field.TypeInt),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
2023-10-28 23:12:07 +00:00
if nodes := puo.mutation.ProductCategoriesIDs(); len(nodes) > 0 {
2023-10-27 09:51:58 +00:00
edge := &sqlgraph.EdgeSpec{
2023-10-28 23:12:07 +00:00
Rel: sqlgraph.M2O,
2023-10-27 09:51:58 +00:00
Inverse: true,
2023-10-28 23:12:07 +00:00
Table: product.ProductCategoriesTable,
Columns: []string{product.ProductCategoriesColumn},
2023-10-27 09:51:58 +00:00
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(productcategory.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
2023-10-28 23:12:07 +00:00
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if puo.mutation.BusinessesCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: product.BusinessesTable,
Columns: []string{product.BusinessesColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(business.FieldID, field.TypeInt),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := puo.mutation.BusinessesIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: product.BusinessesTable,
Columns: []string{product.BusinessesColumn},
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.Add = append(_spec.Edges.Add, edge)
}
if puo.mutation.UsersCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: product.UsersTable,
Columns: []string{product.UsersColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
},
}
2023-10-27 09:51:58 +00:00
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
2023-10-28 23:12:07 +00:00
if nodes := puo.mutation.UsersIDs(); len(nodes) > 0 {
2023-10-27 09:51:58 +00:00
edge := &sqlgraph.EdgeSpec{
2023-10-28 23:12:07 +00:00
Rel: sqlgraph.M2O,
2023-10-27 09:51:58 +00:00
Inverse: true,
2023-10-28 23:12:07 +00:00
Table: product.UsersTable,
Columns: []string{product.UsersColumn},
2023-10-27 09:51:58 +00:00
Bidi: false,
Target: &sqlgraph.EdgeTarget{
2023-10-28 23:12:07 +00:00
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
2023-10-27 09:51:58 +00:00
},
}
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
}