// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"registry-backend/ent/predicate"
	"registry-backend/ent/publisher"
	"registry-backend/ent/publisherpermission"
	"registry-backend/ent/schema"
	"registry-backend/ent/user"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// PublisherPermissionUpdate is the builder for updating PublisherPermission entities.
type PublisherPermissionUpdate struct {
	config
	hooks     []Hook
	mutation  *PublisherPermissionMutation
	modifiers []func(*sql.UpdateBuilder)
}

// Where appends a list predicates to the PublisherPermissionUpdate builder.
func (ppu *PublisherPermissionUpdate) Where(ps ...predicate.PublisherPermission) *PublisherPermissionUpdate {
	ppu.mutation.Where(ps...)
	return ppu
}

// SetPermission sets the "permission" field.
func (ppu *PublisherPermissionUpdate) SetPermission(spt schema.PublisherPermissionType) *PublisherPermissionUpdate {
	ppu.mutation.SetPermission(spt)
	return ppu
}

// SetNillablePermission sets the "permission" field if the given value is not nil.
func (ppu *PublisherPermissionUpdate) SetNillablePermission(spt *schema.PublisherPermissionType) *PublisherPermissionUpdate {
	if spt != nil {
		ppu.SetPermission(*spt)
	}
	return ppu
}

// SetUserID sets the "user_id" field.
func (ppu *PublisherPermissionUpdate) SetUserID(s string) *PublisherPermissionUpdate {
	ppu.mutation.SetUserID(s)
	return ppu
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (ppu *PublisherPermissionUpdate) SetNillableUserID(s *string) *PublisherPermissionUpdate {
	if s != nil {
		ppu.SetUserID(*s)
	}
	return ppu
}

// SetPublisherID sets the "publisher_id" field.
func (ppu *PublisherPermissionUpdate) SetPublisherID(s string) *PublisherPermissionUpdate {
	ppu.mutation.SetPublisherID(s)
	return ppu
}

// SetNillablePublisherID sets the "publisher_id" field if the given value is not nil.
func (ppu *PublisherPermissionUpdate) SetNillablePublisherID(s *string) *PublisherPermissionUpdate {
	if s != nil {
		ppu.SetPublisherID(*s)
	}
	return ppu
}

// SetUser sets the "user" edge to the User entity.
func (ppu *PublisherPermissionUpdate) SetUser(u *User) *PublisherPermissionUpdate {
	return ppu.SetUserID(u.ID)
}

// SetPublisher sets the "publisher" edge to the Publisher entity.
func (ppu *PublisherPermissionUpdate) SetPublisher(p *Publisher) *PublisherPermissionUpdate {
	return ppu.SetPublisherID(p.ID)
}

// Mutation returns the PublisherPermissionMutation object of the builder.
func (ppu *PublisherPermissionUpdate) Mutation() *PublisherPermissionMutation {
	return ppu.mutation
}

// ClearUser clears the "user" edge to the User entity.
func (ppu *PublisherPermissionUpdate) ClearUser() *PublisherPermissionUpdate {
	ppu.mutation.ClearUser()
	return ppu
}

// ClearPublisher clears the "publisher" edge to the Publisher entity.
func (ppu *PublisherPermissionUpdate) ClearPublisher() *PublisherPermissionUpdate {
	ppu.mutation.ClearPublisher()
	return ppu
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (ppu *PublisherPermissionUpdate) Save(ctx context.Context) (int, error) {
	return withHooks(ctx, ppu.sqlSave, ppu.mutation, ppu.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (ppu *PublisherPermissionUpdate) SaveX(ctx context.Context) int {
	affected, err := ppu.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

// Exec executes the query.
func (ppu *PublisherPermissionUpdate) Exec(ctx context.Context) error {
	_, err := ppu.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (ppu *PublisherPermissionUpdate) ExecX(ctx context.Context) {
	if err := ppu.Exec(ctx); err != nil {
		panic(err)
	}
}

// check runs all checks and user-defined validators on the builder.
func (ppu *PublisherPermissionUpdate) check() error {
	if v, ok := ppu.mutation.Permission(); ok {
		if err := publisherpermission.PermissionValidator(v); err != nil {
			return &ValidationError{Name: "permission", err: fmt.Errorf(`ent: validator failed for field "PublisherPermission.permission": %w`, err)}
		}
	}
	if _, ok := ppu.mutation.UserID(); ppu.mutation.UserCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "PublisherPermission.user"`)
	}
	if _, ok := ppu.mutation.PublisherID(); ppu.mutation.PublisherCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "PublisherPermission.publisher"`)
	}
	return nil
}

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (ppu *PublisherPermissionUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *PublisherPermissionUpdate {
	ppu.modifiers = append(ppu.modifiers, modifiers...)
	return ppu
}

func (ppu *PublisherPermissionUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := ppu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(publisherpermission.Table, publisherpermission.Columns, sqlgraph.NewFieldSpec(publisherpermission.FieldID, field.TypeInt))
	if ps := ppu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := ppu.mutation.Permission(); ok {
		_spec.SetField(publisherpermission.FieldPermission, field.TypeEnum, value)
	}
	if ppu.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   publisherpermission.UserTable,
			Columns: []string{publisherpermission.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := ppu.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   publisherpermission.UserTable,
			Columns: []string{publisherpermission.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if ppu.mutation.PublisherCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   publisherpermission.PublisherTable,
			Columns: []string{publisherpermission.PublisherColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(publisher.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := ppu.mutation.PublisherIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   publisherpermission.PublisherTable,
			Columns: []string{publisherpermission.PublisherColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(publisher.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_spec.AddModifiers(ppu.modifiers...)
	if n, err = sqlgraph.UpdateNodes(ctx, ppu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{publisherpermission.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	ppu.mutation.done = true
	return n, nil
}

// PublisherPermissionUpdateOne is the builder for updating a single PublisherPermission entity.
type PublisherPermissionUpdateOne struct {
	config
	fields    []string
	hooks     []Hook
	mutation  *PublisherPermissionMutation
	modifiers []func(*sql.UpdateBuilder)
}

// SetPermission sets the "permission" field.
func (ppuo *PublisherPermissionUpdateOne) SetPermission(spt schema.PublisherPermissionType) *PublisherPermissionUpdateOne {
	ppuo.mutation.SetPermission(spt)
	return ppuo
}

// SetNillablePermission sets the "permission" field if the given value is not nil.
func (ppuo *PublisherPermissionUpdateOne) SetNillablePermission(spt *schema.PublisherPermissionType) *PublisherPermissionUpdateOne {
	if spt != nil {
		ppuo.SetPermission(*spt)
	}
	return ppuo
}

// SetUserID sets the "user_id" field.
func (ppuo *PublisherPermissionUpdateOne) SetUserID(s string) *PublisherPermissionUpdateOne {
	ppuo.mutation.SetUserID(s)
	return ppuo
}

// SetNillableUserID sets the "user_id" field if the given value is not nil.
func (ppuo *PublisherPermissionUpdateOne) SetNillableUserID(s *string) *PublisherPermissionUpdateOne {
	if s != nil {
		ppuo.SetUserID(*s)
	}
	return ppuo
}

// SetPublisherID sets the "publisher_id" field.
func (ppuo *PublisherPermissionUpdateOne) SetPublisherID(s string) *PublisherPermissionUpdateOne {
	ppuo.mutation.SetPublisherID(s)
	return ppuo
}

// SetNillablePublisherID sets the "publisher_id" field if the given value is not nil.
func (ppuo *PublisherPermissionUpdateOne) SetNillablePublisherID(s *string) *PublisherPermissionUpdateOne {
	if s != nil {
		ppuo.SetPublisherID(*s)
	}
	return ppuo
}

// SetUser sets the "user" edge to the User entity.
func (ppuo *PublisherPermissionUpdateOne) SetUser(u *User) *PublisherPermissionUpdateOne {
	return ppuo.SetUserID(u.ID)
}

// SetPublisher sets the "publisher" edge to the Publisher entity.
func (ppuo *PublisherPermissionUpdateOne) SetPublisher(p *Publisher) *PublisherPermissionUpdateOne {
	return ppuo.SetPublisherID(p.ID)
}

// Mutation returns the PublisherPermissionMutation object of the builder.
func (ppuo *PublisherPermissionUpdateOne) Mutation() *PublisherPermissionMutation {
	return ppuo.mutation
}

// ClearUser clears the "user" edge to the User entity.
func (ppuo *PublisherPermissionUpdateOne) ClearUser() *PublisherPermissionUpdateOne {
	ppuo.mutation.ClearUser()
	return ppuo
}

// ClearPublisher clears the "publisher" edge to the Publisher entity.
func (ppuo *PublisherPermissionUpdateOne) ClearPublisher() *PublisherPermissionUpdateOne {
	ppuo.mutation.ClearPublisher()
	return ppuo
}

// Where appends a list predicates to the PublisherPermissionUpdate builder.
func (ppuo *PublisherPermissionUpdateOne) Where(ps ...predicate.PublisherPermission) *PublisherPermissionUpdateOne {
	ppuo.mutation.Where(ps...)
	return ppuo
}

// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (ppuo *PublisherPermissionUpdateOne) Select(field string, fields ...string) *PublisherPermissionUpdateOne {
	ppuo.fields = append([]string{field}, fields...)
	return ppuo
}

// Save executes the query and returns the updated PublisherPermission entity.
func (ppuo *PublisherPermissionUpdateOne) Save(ctx context.Context) (*PublisherPermission, error) {
	return withHooks(ctx, ppuo.sqlSave, ppuo.mutation, ppuo.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (ppuo *PublisherPermissionUpdateOne) SaveX(ctx context.Context) *PublisherPermission {
	node, err := ppuo.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (ppuo *PublisherPermissionUpdateOne) Exec(ctx context.Context) error {
	_, err := ppuo.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (ppuo *PublisherPermissionUpdateOne) ExecX(ctx context.Context) {
	if err := ppuo.Exec(ctx); err != nil {
		panic(err)
	}
}

// check runs all checks and user-defined validators on the builder.
func (ppuo *PublisherPermissionUpdateOne) check() error {
	if v, ok := ppuo.mutation.Permission(); ok {
		if err := publisherpermission.PermissionValidator(v); err != nil {
			return &ValidationError{Name: "permission", err: fmt.Errorf(`ent: validator failed for field "PublisherPermission.permission": %w`, err)}
		}
	}
	if _, ok := ppuo.mutation.UserID(); ppuo.mutation.UserCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "PublisherPermission.user"`)
	}
	if _, ok := ppuo.mutation.PublisherID(); ppuo.mutation.PublisherCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "PublisherPermission.publisher"`)
	}
	return nil
}

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (ppuo *PublisherPermissionUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *PublisherPermissionUpdateOne {
	ppuo.modifiers = append(ppuo.modifiers, modifiers...)
	return ppuo
}

func (ppuo *PublisherPermissionUpdateOne) sqlSave(ctx context.Context) (_node *PublisherPermission, err error) {
	if err := ppuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(publisherpermission.Table, publisherpermission.Columns, sqlgraph.NewFieldSpec(publisherpermission.FieldID, field.TypeInt))
	id, ok := ppuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "PublisherPermission.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := ppuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, publisherpermission.FieldID)
		for _, f := range fields {
			if !publisherpermission.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != publisherpermission.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := ppuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := ppuo.mutation.Permission(); ok {
		_spec.SetField(publisherpermission.FieldPermission, field.TypeEnum, value)
	}
	if ppuo.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   publisherpermission.UserTable,
			Columns: []string{publisherpermission.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := ppuo.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   publisherpermission.UserTable,
			Columns: []string{publisherpermission.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	if ppuo.mutation.PublisherCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   publisherpermission.PublisherTable,
			Columns: []string{publisherpermission.PublisherColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(publisher.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := ppuo.mutation.PublisherIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   publisherpermission.PublisherTable,
			Columns: []string{publisherpermission.PublisherColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(publisher.FieldID, field.TypeString),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_spec.AddModifiers(ppuo.modifiers...)
	_node = &PublisherPermission{config: ppuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, ppuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{publisherpermission.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	ppuo.mutation.done = true
	return _node, nil
}
