// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"registry-backend/ent/node"
	"registry-backend/ent/nodereview"
	"registry-backend/ent/predicate"
	"registry-backend/ent/user"

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

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

// Where appends a list predicates to the NodeReviewUpdate builder.
func (nru *NodeReviewUpdate) Where(ps ...predicate.NodeReview) *NodeReviewUpdate {
	nru.mutation.Where(ps...)
	return nru
}

// SetNodeID sets the "node_id" field.
func (nru *NodeReviewUpdate) SetNodeID(s string) *NodeReviewUpdate {
	nru.mutation.SetNodeID(s)
	return nru
}

// SetNillableNodeID sets the "node_id" field if the given value is not nil.
func (nru *NodeReviewUpdate) SetNillableNodeID(s *string) *NodeReviewUpdate {
	if s != nil {
		nru.SetNodeID(*s)
	}
	return nru
}

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

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

// SetStar sets the "star" field.
func (nru *NodeReviewUpdate) SetStar(i int) *NodeReviewUpdate {
	nru.mutation.ResetStar()
	nru.mutation.SetStar(i)
	return nru
}

// SetNillableStar sets the "star" field if the given value is not nil.
func (nru *NodeReviewUpdate) SetNillableStar(i *int) *NodeReviewUpdate {
	if i != nil {
		nru.SetStar(*i)
	}
	return nru
}

// AddStar adds i to the "star" field.
func (nru *NodeReviewUpdate) AddStar(i int) *NodeReviewUpdate {
	nru.mutation.AddStar(i)
	return nru
}

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

// SetNode sets the "node" edge to the Node entity.
func (nru *NodeReviewUpdate) SetNode(n *Node) *NodeReviewUpdate {
	return nru.SetNodeID(n.ID)
}

// Mutation returns the NodeReviewMutation object of the builder.
func (nru *NodeReviewUpdate) Mutation() *NodeReviewMutation {
	return nru.mutation
}

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

// ClearNode clears the "node" edge to the Node entity.
func (nru *NodeReviewUpdate) ClearNode() *NodeReviewUpdate {
	nru.mutation.ClearNode()
	return nru
}

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (nru *NodeReviewUpdate) check() error {
	if _, ok := nru.mutation.UserID(); nru.mutation.UserCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "NodeReview.user"`)
	}
	if _, ok := nru.mutation.NodeID(); nru.mutation.NodeCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "NodeReview.node"`)
	}
	return nil
}

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

func (nru *NodeReviewUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := nru.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(nodereview.Table, nodereview.Columns, sqlgraph.NewFieldSpec(nodereview.FieldID, field.TypeUUID))
	if ps := nru.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := nru.mutation.Star(); ok {
		_spec.SetField(nodereview.FieldStar, field.TypeInt, value)
	}
	if value, ok := nru.mutation.AddedStar(); ok {
		_spec.AddField(nodereview.FieldStar, field.TypeInt, value)
	}
	if nru.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   nodereview.UserTable,
			Columns: []string{nodereview.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := nru.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   nodereview.UserTable,
			Columns: []string{nodereview.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 nru.mutation.NodeCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   nodereview.NodeTable,
			Columns: []string{nodereview.NodeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(node.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := nru.mutation.NodeIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   nodereview.NodeTable,
			Columns: []string{nodereview.NodeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(node.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(nru.modifiers...)
	if n, err = sqlgraph.UpdateNodes(ctx, nru.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{nodereview.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	nru.mutation.done = true
	return n, nil
}

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

// SetNodeID sets the "node_id" field.
func (nruo *NodeReviewUpdateOne) SetNodeID(s string) *NodeReviewUpdateOne {
	nruo.mutation.SetNodeID(s)
	return nruo
}

// SetNillableNodeID sets the "node_id" field if the given value is not nil.
func (nruo *NodeReviewUpdateOne) SetNillableNodeID(s *string) *NodeReviewUpdateOne {
	if s != nil {
		nruo.SetNodeID(*s)
	}
	return nruo
}

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

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

// SetStar sets the "star" field.
func (nruo *NodeReviewUpdateOne) SetStar(i int) *NodeReviewUpdateOne {
	nruo.mutation.ResetStar()
	nruo.mutation.SetStar(i)
	return nruo
}

// SetNillableStar sets the "star" field if the given value is not nil.
func (nruo *NodeReviewUpdateOne) SetNillableStar(i *int) *NodeReviewUpdateOne {
	if i != nil {
		nruo.SetStar(*i)
	}
	return nruo
}

// AddStar adds i to the "star" field.
func (nruo *NodeReviewUpdateOne) AddStar(i int) *NodeReviewUpdateOne {
	nruo.mutation.AddStar(i)
	return nruo
}

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

// SetNode sets the "node" edge to the Node entity.
func (nruo *NodeReviewUpdateOne) SetNode(n *Node) *NodeReviewUpdateOne {
	return nruo.SetNodeID(n.ID)
}

// Mutation returns the NodeReviewMutation object of the builder.
func (nruo *NodeReviewUpdateOne) Mutation() *NodeReviewMutation {
	return nruo.mutation
}

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

// ClearNode clears the "node" edge to the Node entity.
func (nruo *NodeReviewUpdateOne) ClearNode() *NodeReviewUpdateOne {
	nruo.mutation.ClearNode()
	return nruo
}

// Where appends a list predicates to the NodeReviewUpdate builder.
func (nruo *NodeReviewUpdateOne) Where(ps ...predicate.NodeReview) *NodeReviewUpdateOne {
	nruo.mutation.Where(ps...)
	return nruo
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (nruo *NodeReviewUpdateOne) check() error {
	if _, ok := nruo.mutation.UserID(); nruo.mutation.UserCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "NodeReview.user"`)
	}
	if _, ok := nruo.mutation.NodeID(); nruo.mutation.NodeCleared() && !ok {
		return errors.New(`ent: clearing a required unique edge "NodeReview.node"`)
	}
	return nil
}

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

func (nruo *NodeReviewUpdateOne) sqlSave(ctx context.Context) (_node *NodeReview, err error) {
	if err := nruo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(nodereview.Table, nodereview.Columns, sqlgraph.NewFieldSpec(nodereview.FieldID, field.TypeUUID))
	id, ok := nruo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "NodeReview.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := nruo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, nodereview.FieldID)
		for _, f := range fields {
			if !nodereview.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != nodereview.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := nruo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := nruo.mutation.Star(); ok {
		_spec.SetField(nodereview.FieldStar, field.TypeInt, value)
	}
	if value, ok := nruo.mutation.AddedStar(); ok {
		_spec.AddField(nodereview.FieldStar, field.TypeInt, value)
	}
	if nruo.mutation.UserCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   nodereview.UserTable,
			Columns: []string{nodereview.UserColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := nruo.mutation.UserIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   nodereview.UserTable,
			Columns: []string{nodereview.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 nruo.mutation.NodeCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   nodereview.NodeTable,
			Columns: []string{nodereview.NodeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(node.FieldID, field.TypeString),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := nruo.mutation.NodeIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   nodereview.NodeTable,
			Columns: []string{nodereview.NodeColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(node.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(nruo.modifiers...)
	_node = &NodeReview{config: nruo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, nruo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{nodereview.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	nruo.mutation.done = true
	return _node, nil
}
