telegram-commerce/ent/productcategory_update.go
2023-10-26 01:24:32 +03:30

548 lines
17 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"tel-commerce/ent/predicate"
"tel-commerce/ent/product"
"tel-commerce/ent/productcategory"
"time"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
)
// ProductCategoryUpdate is the builder for updating ProductCategory entities.
type ProductCategoryUpdate struct {
config
hooks []Hook
mutation *ProductCategoryMutation
}
// Where appends a list predicates to the ProductCategoryUpdate builder.
func (pcu *ProductCategoryUpdate) Where(ps ...predicate.ProductCategory) *ProductCategoryUpdate {
pcu.mutation.Where(ps...)
return pcu
}
// SetName sets the "name" field.
func (pcu *ProductCategoryUpdate) SetName(s string) *ProductCategoryUpdate {
pcu.mutation.SetName(s)
return pcu
}
// SetNillableName sets the "name" field if the given value is not nil.
func (pcu *ProductCategoryUpdate) SetNillableName(s *string) *ProductCategoryUpdate {
if s != nil {
pcu.SetName(*s)
}
return pcu
}
// SetSlug sets the "slug" field.
func (pcu *ProductCategoryUpdate) SetSlug(s string) *ProductCategoryUpdate {
pcu.mutation.SetSlug(s)
return pcu
}
// SetDescription sets the "description" field.
func (pcu *ProductCategoryUpdate) SetDescription(s string) *ProductCategoryUpdate {
pcu.mutation.SetDescription(s)
return pcu
}
// SetNillableDescription sets the "description" field if the given value is not nil.
func (pcu *ProductCategoryUpdate) SetNillableDescription(s *string) *ProductCategoryUpdate {
if s != nil {
pcu.SetDescription(*s)
}
return pcu
}
// ClearDescription clears the value of the "description" field.
func (pcu *ProductCategoryUpdate) ClearDescription() *ProductCategoryUpdate {
pcu.mutation.ClearDescription()
return pcu
}
// SetStatus sets the "status" field.
func (pcu *ProductCategoryUpdate) SetStatus(b bool) *ProductCategoryUpdate {
pcu.mutation.SetStatus(b)
return pcu
}
// SetNillableStatus sets the "status" field if the given value is not nil.
func (pcu *ProductCategoryUpdate) SetNillableStatus(b *bool) *ProductCategoryUpdate {
if b != nil {
pcu.SetStatus(*b)
}
return pcu
}
// SetCreatedAt sets the "created_at" field.
func (pcu *ProductCategoryUpdate) SetCreatedAt(t time.Time) *ProductCategoryUpdate {
pcu.mutation.SetCreatedAt(t)
return pcu
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (pcu *ProductCategoryUpdate) SetNillableCreatedAt(t *time.Time) *ProductCategoryUpdate {
if t != nil {
pcu.SetCreatedAt(*t)
}
return pcu
}
// SetUpdatedAt sets the "updated_at" field.
func (pcu *ProductCategoryUpdate) SetUpdatedAt(t time.Time) *ProductCategoryUpdate {
pcu.mutation.SetUpdatedAt(t)
return pcu
}
// AddProductIDs adds the "products" edge to the Product entity by IDs.
func (pcu *ProductCategoryUpdate) AddProductIDs(ids ...int) *ProductCategoryUpdate {
pcu.mutation.AddProductIDs(ids...)
return pcu
}
// AddProducts adds the "products" edges to the Product entity.
func (pcu *ProductCategoryUpdate) AddProducts(p ...*Product) *ProductCategoryUpdate {
ids := make([]int, len(p))
for i := range p {
ids[i] = p[i].ID
}
return pcu.AddProductIDs(ids...)
}
// Mutation returns the ProductCategoryMutation object of the builder.
func (pcu *ProductCategoryUpdate) Mutation() *ProductCategoryMutation {
return pcu.mutation
}
// ClearProducts clears all "products" edges to the Product entity.
func (pcu *ProductCategoryUpdate) ClearProducts() *ProductCategoryUpdate {
pcu.mutation.ClearProducts()
return pcu
}
// RemoveProductIDs removes the "products" edge to Product entities by IDs.
func (pcu *ProductCategoryUpdate) RemoveProductIDs(ids ...int) *ProductCategoryUpdate {
pcu.mutation.RemoveProductIDs(ids...)
return pcu
}
// RemoveProducts removes "products" edges to Product entities.
func (pcu *ProductCategoryUpdate) RemoveProducts(p ...*Product) *ProductCategoryUpdate {
ids := make([]int, len(p))
for i := range p {
ids[i] = p[i].ID
}
return pcu.RemoveProductIDs(ids...)
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (pcu *ProductCategoryUpdate) Save(ctx context.Context) (int, error) {
pcu.defaults()
return withHooks(ctx, pcu.sqlSave, pcu.mutation, pcu.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (pcu *ProductCategoryUpdate) SaveX(ctx context.Context) int {
affected, err := pcu.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (pcu *ProductCategoryUpdate) Exec(ctx context.Context) error {
_, err := pcu.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (pcu *ProductCategoryUpdate) ExecX(ctx context.Context) {
if err := pcu.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (pcu *ProductCategoryUpdate) defaults() {
if _, ok := pcu.mutation.UpdatedAt(); !ok {
v := productcategory.UpdateDefaultUpdatedAt()
pcu.mutation.SetUpdatedAt(v)
}
}
func (pcu *ProductCategoryUpdate) sqlSave(ctx context.Context) (n int, err error) {
_spec := sqlgraph.NewUpdateSpec(productcategory.Table, productcategory.Columns, sqlgraph.NewFieldSpec(productcategory.FieldID, field.TypeInt))
if ps := pcu.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := pcu.mutation.Name(); ok {
_spec.SetField(productcategory.FieldName, field.TypeString, value)
}
if value, ok := pcu.mutation.Slug(); ok {
_spec.SetField(productcategory.FieldSlug, field.TypeString, value)
}
if value, ok := pcu.mutation.Description(); ok {
_spec.SetField(productcategory.FieldDescription, field.TypeString, value)
}
if pcu.mutation.DescriptionCleared() {
_spec.ClearField(productcategory.FieldDescription, field.TypeString)
}
if value, ok := pcu.mutation.Status(); ok {
_spec.SetField(productcategory.FieldStatus, field.TypeBool, value)
}
if value, ok := pcu.mutation.CreatedAt(); ok {
_spec.SetField(productcategory.FieldCreatedAt, field.TypeTime, value)
}
if value, ok := pcu.mutation.UpdatedAt(); ok {
_spec.SetField(productcategory.FieldUpdatedAt, field.TypeTime, value)
}
if pcu.mutation.ProductsCleared() {
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),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := pcu.mutation.RemovedProductsIDs(); len(nodes) > 0 && !pcu.mutation.ProductsCleared() {
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.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := pcu.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.Add = append(_spec.Edges.Add, edge)
}
if n, err = sqlgraph.UpdateNodes(ctx, pcu.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{productcategory.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
pcu.mutation.done = true
return n, nil
}
// ProductCategoryUpdateOne is the builder for updating a single ProductCategory entity.
type ProductCategoryUpdateOne struct {
config
fields []string
hooks []Hook
mutation *ProductCategoryMutation
}
// SetName sets the "name" field.
func (pcuo *ProductCategoryUpdateOne) SetName(s string) *ProductCategoryUpdateOne {
pcuo.mutation.SetName(s)
return pcuo
}
// SetNillableName sets the "name" field if the given value is not nil.
func (pcuo *ProductCategoryUpdateOne) SetNillableName(s *string) *ProductCategoryUpdateOne {
if s != nil {
pcuo.SetName(*s)
}
return pcuo
}
// SetSlug sets the "slug" field.
func (pcuo *ProductCategoryUpdateOne) SetSlug(s string) *ProductCategoryUpdateOne {
pcuo.mutation.SetSlug(s)
return pcuo
}
// SetDescription sets the "description" field.
func (pcuo *ProductCategoryUpdateOne) SetDescription(s string) *ProductCategoryUpdateOne {
pcuo.mutation.SetDescription(s)
return pcuo
}
// SetNillableDescription sets the "description" field if the given value is not nil.
func (pcuo *ProductCategoryUpdateOne) SetNillableDescription(s *string) *ProductCategoryUpdateOne {
if s != nil {
pcuo.SetDescription(*s)
}
return pcuo
}
// ClearDescription clears the value of the "description" field.
func (pcuo *ProductCategoryUpdateOne) ClearDescription() *ProductCategoryUpdateOne {
pcuo.mutation.ClearDescription()
return pcuo
}
// SetStatus sets the "status" field.
func (pcuo *ProductCategoryUpdateOne) SetStatus(b bool) *ProductCategoryUpdateOne {
pcuo.mutation.SetStatus(b)
return pcuo
}
// SetNillableStatus sets the "status" field if the given value is not nil.
func (pcuo *ProductCategoryUpdateOne) SetNillableStatus(b *bool) *ProductCategoryUpdateOne {
if b != nil {
pcuo.SetStatus(*b)
}
return pcuo
}
// SetCreatedAt sets the "created_at" field.
func (pcuo *ProductCategoryUpdateOne) SetCreatedAt(t time.Time) *ProductCategoryUpdateOne {
pcuo.mutation.SetCreatedAt(t)
return pcuo
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (pcuo *ProductCategoryUpdateOne) SetNillableCreatedAt(t *time.Time) *ProductCategoryUpdateOne {
if t != nil {
pcuo.SetCreatedAt(*t)
}
return pcuo
}
// SetUpdatedAt sets the "updated_at" field.
func (pcuo *ProductCategoryUpdateOne) SetUpdatedAt(t time.Time) *ProductCategoryUpdateOne {
pcuo.mutation.SetUpdatedAt(t)
return pcuo
}
// AddProductIDs adds the "products" edge to the Product entity by IDs.
func (pcuo *ProductCategoryUpdateOne) AddProductIDs(ids ...int) *ProductCategoryUpdateOne {
pcuo.mutation.AddProductIDs(ids...)
return pcuo
}
// AddProducts adds the "products" edges to the Product entity.
func (pcuo *ProductCategoryUpdateOne) AddProducts(p ...*Product) *ProductCategoryUpdateOne {
ids := make([]int, len(p))
for i := range p {
ids[i] = p[i].ID
}
return pcuo.AddProductIDs(ids...)
}
// Mutation returns the ProductCategoryMutation object of the builder.
func (pcuo *ProductCategoryUpdateOne) Mutation() *ProductCategoryMutation {
return pcuo.mutation
}
// ClearProducts clears all "products" edges to the Product entity.
func (pcuo *ProductCategoryUpdateOne) ClearProducts() *ProductCategoryUpdateOne {
pcuo.mutation.ClearProducts()
return pcuo
}
// RemoveProductIDs removes the "products" edge to Product entities by IDs.
func (pcuo *ProductCategoryUpdateOne) RemoveProductIDs(ids ...int) *ProductCategoryUpdateOne {
pcuo.mutation.RemoveProductIDs(ids...)
return pcuo
}
// RemoveProducts removes "products" edges to Product entities.
func (pcuo *ProductCategoryUpdateOne) RemoveProducts(p ...*Product) *ProductCategoryUpdateOne {
ids := make([]int, len(p))
for i := range p {
ids[i] = p[i].ID
}
return pcuo.RemoveProductIDs(ids...)
}
// Where appends a list predicates to the ProductCategoryUpdate builder.
func (pcuo *ProductCategoryUpdateOne) Where(ps ...predicate.ProductCategory) *ProductCategoryUpdateOne {
pcuo.mutation.Where(ps...)
return pcuo
}
// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (pcuo *ProductCategoryUpdateOne) Select(field string, fields ...string) *ProductCategoryUpdateOne {
pcuo.fields = append([]string{field}, fields...)
return pcuo
}
// Save executes the query and returns the updated ProductCategory entity.
func (pcuo *ProductCategoryUpdateOne) Save(ctx context.Context) (*ProductCategory, error) {
pcuo.defaults()
return withHooks(ctx, pcuo.sqlSave, pcuo.mutation, pcuo.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (pcuo *ProductCategoryUpdateOne) SaveX(ctx context.Context) *ProductCategory {
node, err := pcuo.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (pcuo *ProductCategoryUpdateOne) Exec(ctx context.Context) error {
_, err := pcuo.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (pcuo *ProductCategoryUpdateOne) ExecX(ctx context.Context) {
if err := pcuo.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (pcuo *ProductCategoryUpdateOne) defaults() {
if _, ok := pcuo.mutation.UpdatedAt(); !ok {
v := productcategory.UpdateDefaultUpdatedAt()
pcuo.mutation.SetUpdatedAt(v)
}
}
func (pcuo *ProductCategoryUpdateOne) sqlSave(ctx context.Context) (_node *ProductCategory, err error) {
_spec := sqlgraph.NewUpdateSpec(productcategory.Table, productcategory.Columns, sqlgraph.NewFieldSpec(productcategory.FieldID, field.TypeInt))
id, ok := pcuo.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "ProductCategory.id" for update`)}
}
_spec.Node.ID.Value = id
if fields := pcuo.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, productcategory.FieldID)
for _, f := range fields {
if !productcategory.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != productcategory.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, f)
}
}
}
if ps := pcuo.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := pcuo.mutation.Name(); ok {
_spec.SetField(productcategory.FieldName, field.TypeString, value)
}
if value, ok := pcuo.mutation.Slug(); ok {
_spec.SetField(productcategory.FieldSlug, field.TypeString, value)
}
if value, ok := pcuo.mutation.Description(); ok {
_spec.SetField(productcategory.FieldDescription, field.TypeString, value)
}
if pcuo.mutation.DescriptionCleared() {
_spec.ClearField(productcategory.FieldDescription, field.TypeString)
}
if value, ok := pcuo.mutation.Status(); ok {
_spec.SetField(productcategory.FieldStatus, field.TypeBool, value)
}
if value, ok := pcuo.mutation.CreatedAt(); ok {
_spec.SetField(productcategory.FieldCreatedAt, field.TypeTime, value)
}
if value, ok := pcuo.mutation.UpdatedAt(); ok {
_spec.SetField(productcategory.FieldUpdatedAt, field.TypeTime, value)
}
if pcuo.mutation.ProductsCleared() {
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),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := pcuo.mutation.RemovedProductsIDs(); len(nodes) > 0 && !pcuo.mutation.ProductsCleared() {
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.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := pcuo.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.Add = append(_spec.Edges.Add, edge)
}
_node = &ProductCategory{config: pcuo.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues
if err = sqlgraph.UpdateNode(ctx, pcuo.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{productcategory.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
pcuo.mutation.done = true
return _node, nil
}