// Code generated by ent, DO NOT EDIT.

package ent

import (
	"PasswordManager/ent/note"
	"PasswordManager/ent/predicate"
	"PasswordManager/ent/vault"
	"context"
	"errors"
	"fmt"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/google/uuid"
)

// NoteUpdate is the builder for updating Note entities.
type NoteUpdate struct {
	config
	hooks    []Hook
	mutation *NoteMutation
}

// Where appends a list predicates to the NoteUpdate builder.
func (nu *NoteUpdate) Where(ps ...predicate.Note) *NoteUpdate {
	nu.mutation.Where(ps...)
	return nu
}

// SetName sets the "name" field.
func (nu *NoteUpdate) SetName(b []byte) *NoteUpdate {
	nu.mutation.SetName(b)
	return nu
}

// SetNameIv sets the "nameIv" field.
func (nu *NoteUpdate) SetNameIv(b []byte) *NoteUpdate {
	nu.mutation.SetNameIv(b)
	return nu
}

// SetTitle sets the "title" field.
func (nu *NoteUpdate) SetTitle(b []byte) *NoteUpdate {
	nu.mutation.SetTitle(b)
	return nu
}

// SetTitleIv sets the "titleIv" field.
func (nu *NoteUpdate) SetTitleIv(b []byte) *NoteUpdate {
	nu.mutation.SetTitleIv(b)
	return nu
}

// SetContent sets the "content" field.
func (nu *NoteUpdate) SetContent(b []byte) *NoteUpdate {
	nu.mutation.SetContent(b)
	return nu
}

// SetContentIv sets the "contentIv" field.
func (nu *NoteUpdate) SetContentIv(b []byte) *NoteUpdate {
	nu.mutation.SetContentIv(b)
	return nu
}

// SetColour sets the "colour" field.
func (nu *NoteUpdate) SetColour(b []byte) *NoteUpdate {
	nu.mutation.SetColour(b)
	return nu
}

// SetColourIv sets the "colourIv" field.
func (nu *NoteUpdate) SetColourIv(b []byte) *NoteUpdate {
	nu.mutation.SetColourIv(b)
	return nu
}

// SetVaultID sets the "vault" edge to the Vault entity by ID.
func (nu *NoteUpdate) SetVaultID(id uuid.UUID) *NoteUpdate {
	nu.mutation.SetVaultID(id)
	return nu
}

// SetNillableVaultID sets the "vault" edge to the Vault entity by ID if the given value is not nil.
func (nu *NoteUpdate) SetNillableVaultID(id *uuid.UUID) *NoteUpdate {
	if id != nil {
		nu = nu.SetVaultID(*id)
	}
	return nu
}

// SetVault sets the "vault" edge to the Vault entity.
func (nu *NoteUpdate) SetVault(v *Vault) *NoteUpdate {
	return nu.SetVaultID(v.ID)
}

// Mutation returns the NoteMutation object of the builder.
func (nu *NoteUpdate) Mutation() *NoteMutation {
	return nu.mutation
}

// ClearVault clears the "vault" edge to the Vault entity.
func (nu *NoteUpdate) ClearVault() *NoteUpdate {
	nu.mutation.ClearVault()
	return nu
}

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (nu *NoteUpdate) check() error {
	if v, ok := nu.mutation.Name(); ok {
		if err := note.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Note.name": %w`, err)}
		}
	}
	if v, ok := nu.mutation.NameIv(); ok {
		if err := note.NameIvValidator(v); err != nil {
			return &ValidationError{Name: "nameIv", err: fmt.Errorf(`ent: validator failed for field "Note.nameIv": %w`, err)}
		}
	}
	if v, ok := nu.mutation.Title(); ok {
		if err := note.TitleValidator(v); err != nil {
			return &ValidationError{Name: "title", err: fmt.Errorf(`ent: validator failed for field "Note.title": %w`, err)}
		}
	}
	if v, ok := nu.mutation.TitleIv(); ok {
		if err := note.TitleIvValidator(v); err != nil {
			return &ValidationError{Name: "titleIv", err: fmt.Errorf(`ent: validator failed for field "Note.titleIv": %w`, err)}
		}
	}
	if v, ok := nu.mutation.Content(); ok {
		if err := note.ContentValidator(v); err != nil {
			return &ValidationError{Name: "content", err: fmt.Errorf(`ent: validator failed for field "Note.content": %w`, err)}
		}
	}
	if v, ok := nu.mutation.ContentIv(); ok {
		if err := note.ContentIvValidator(v); err != nil {
			return &ValidationError{Name: "contentIv", err: fmt.Errorf(`ent: validator failed for field "Note.contentIv": %w`, err)}
		}
	}
	if v, ok := nu.mutation.Colour(); ok {
		if err := note.ColourValidator(v); err != nil {
			return &ValidationError{Name: "colour", err: fmt.Errorf(`ent: validator failed for field "Note.colour": %w`, err)}
		}
	}
	if v, ok := nu.mutation.ColourIv(); ok {
		if err := note.ColourIvValidator(v); err != nil {
			return &ValidationError{Name: "colourIv", err: fmt.Errorf(`ent: validator failed for field "Note.colourIv": %w`, err)}
		}
	}
	return nil
}

func (nu *NoteUpdate) sqlSave(ctx context.Context) (n int, err error) {
	if err := nu.check(); err != nil {
		return n, err
	}
	_spec := sqlgraph.NewUpdateSpec(note.Table, note.Columns, sqlgraph.NewFieldSpec(note.FieldID, field.TypeUUID))
	if ps := nu.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := nu.mutation.Name(); ok {
		_spec.SetField(note.FieldName, field.TypeBytes, value)
	}
	if value, ok := nu.mutation.NameIv(); ok {
		_spec.SetField(note.FieldNameIv, field.TypeBytes, value)
	}
	if value, ok := nu.mutation.Title(); ok {
		_spec.SetField(note.FieldTitle, field.TypeBytes, value)
	}
	if value, ok := nu.mutation.TitleIv(); ok {
		_spec.SetField(note.FieldTitleIv, field.TypeBytes, value)
	}
	if value, ok := nu.mutation.Content(); ok {
		_spec.SetField(note.FieldContent, field.TypeBytes, value)
	}
	if value, ok := nu.mutation.ContentIv(); ok {
		_spec.SetField(note.FieldContentIv, field.TypeBytes, value)
	}
	if value, ok := nu.mutation.Colour(); ok {
		_spec.SetField(note.FieldColour, field.TypeBytes, value)
	}
	if value, ok := nu.mutation.ColourIv(); ok {
		_spec.SetField(note.FieldColourIv, field.TypeBytes, value)
	}
	if nu.mutation.VaultCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   note.VaultTable,
			Columns: []string{note.VaultColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(vault.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := nu.mutation.VaultIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   note.VaultTable,
			Columns: []string{note.VaultColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(vault.FieldID, field.TypeUUID),
			},
		}
		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, nu.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{note.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	nu.mutation.done = true
	return n, nil
}

// NoteUpdateOne is the builder for updating a single Note entity.
type NoteUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *NoteMutation
}

// SetName sets the "name" field.
func (nuo *NoteUpdateOne) SetName(b []byte) *NoteUpdateOne {
	nuo.mutation.SetName(b)
	return nuo
}

// SetNameIv sets the "nameIv" field.
func (nuo *NoteUpdateOne) SetNameIv(b []byte) *NoteUpdateOne {
	nuo.mutation.SetNameIv(b)
	return nuo
}

// SetTitle sets the "title" field.
func (nuo *NoteUpdateOne) SetTitle(b []byte) *NoteUpdateOne {
	nuo.mutation.SetTitle(b)
	return nuo
}

// SetTitleIv sets the "titleIv" field.
func (nuo *NoteUpdateOne) SetTitleIv(b []byte) *NoteUpdateOne {
	nuo.mutation.SetTitleIv(b)
	return nuo
}

// SetContent sets the "content" field.
func (nuo *NoteUpdateOne) SetContent(b []byte) *NoteUpdateOne {
	nuo.mutation.SetContent(b)
	return nuo
}

// SetContentIv sets the "contentIv" field.
func (nuo *NoteUpdateOne) SetContentIv(b []byte) *NoteUpdateOne {
	nuo.mutation.SetContentIv(b)
	return nuo
}

// SetColour sets the "colour" field.
func (nuo *NoteUpdateOne) SetColour(b []byte) *NoteUpdateOne {
	nuo.mutation.SetColour(b)
	return nuo
}

// SetColourIv sets the "colourIv" field.
func (nuo *NoteUpdateOne) SetColourIv(b []byte) *NoteUpdateOne {
	nuo.mutation.SetColourIv(b)
	return nuo
}

// SetVaultID sets the "vault" edge to the Vault entity by ID.
func (nuo *NoteUpdateOne) SetVaultID(id uuid.UUID) *NoteUpdateOne {
	nuo.mutation.SetVaultID(id)
	return nuo
}

// SetNillableVaultID sets the "vault" edge to the Vault entity by ID if the given value is not nil.
func (nuo *NoteUpdateOne) SetNillableVaultID(id *uuid.UUID) *NoteUpdateOne {
	if id != nil {
		nuo = nuo.SetVaultID(*id)
	}
	return nuo
}

// SetVault sets the "vault" edge to the Vault entity.
func (nuo *NoteUpdateOne) SetVault(v *Vault) *NoteUpdateOne {
	return nuo.SetVaultID(v.ID)
}

// Mutation returns the NoteMutation object of the builder.
func (nuo *NoteUpdateOne) Mutation() *NoteMutation {
	return nuo.mutation
}

// ClearVault clears the "vault" edge to the Vault entity.
func (nuo *NoteUpdateOne) ClearVault() *NoteUpdateOne {
	nuo.mutation.ClearVault()
	return nuo
}

// Where appends a list predicates to the NoteUpdate builder.
func (nuo *NoteUpdateOne) Where(ps ...predicate.Note) *NoteUpdateOne {
	nuo.mutation.Where(ps...)
	return nuo
}

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

// Save executes the query and returns the updated Note entity.
func (nuo *NoteUpdateOne) Save(ctx context.Context) (*Note, error) {
	return withHooks[*Note, NoteMutation](ctx, nuo.sqlSave, nuo.mutation, nuo.hooks)
}

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

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

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

// check runs all checks and user-defined validators on the builder.
func (nuo *NoteUpdateOne) check() error {
	if v, ok := nuo.mutation.Name(); ok {
		if err := note.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Note.name": %w`, err)}
		}
	}
	if v, ok := nuo.mutation.NameIv(); ok {
		if err := note.NameIvValidator(v); err != nil {
			return &ValidationError{Name: "nameIv", err: fmt.Errorf(`ent: validator failed for field "Note.nameIv": %w`, err)}
		}
	}
	if v, ok := nuo.mutation.Title(); ok {
		if err := note.TitleValidator(v); err != nil {
			return &ValidationError{Name: "title", err: fmt.Errorf(`ent: validator failed for field "Note.title": %w`, err)}
		}
	}
	if v, ok := nuo.mutation.TitleIv(); ok {
		if err := note.TitleIvValidator(v); err != nil {
			return &ValidationError{Name: "titleIv", err: fmt.Errorf(`ent: validator failed for field "Note.titleIv": %w`, err)}
		}
	}
	if v, ok := nuo.mutation.Content(); ok {
		if err := note.ContentValidator(v); err != nil {
			return &ValidationError{Name: "content", err: fmt.Errorf(`ent: validator failed for field "Note.content": %w`, err)}
		}
	}
	if v, ok := nuo.mutation.ContentIv(); ok {
		if err := note.ContentIvValidator(v); err != nil {
			return &ValidationError{Name: "contentIv", err: fmt.Errorf(`ent: validator failed for field "Note.contentIv": %w`, err)}
		}
	}
	if v, ok := nuo.mutation.Colour(); ok {
		if err := note.ColourValidator(v); err != nil {
			return &ValidationError{Name: "colour", err: fmt.Errorf(`ent: validator failed for field "Note.colour": %w`, err)}
		}
	}
	if v, ok := nuo.mutation.ColourIv(); ok {
		if err := note.ColourIvValidator(v); err != nil {
			return &ValidationError{Name: "colourIv", err: fmt.Errorf(`ent: validator failed for field "Note.colourIv": %w`, err)}
		}
	}
	return nil
}

func (nuo *NoteUpdateOne) sqlSave(ctx context.Context) (_node *Note, err error) {
	if err := nuo.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(note.Table, note.Columns, sqlgraph.NewFieldSpec(note.FieldID, field.TypeUUID))
	id, ok := nuo.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Note.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := nuo.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, note.FieldID)
		for _, f := range fields {
			if !note.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != note.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := nuo.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := nuo.mutation.Name(); ok {
		_spec.SetField(note.FieldName, field.TypeBytes, value)
	}
	if value, ok := nuo.mutation.NameIv(); ok {
		_spec.SetField(note.FieldNameIv, field.TypeBytes, value)
	}
	if value, ok := nuo.mutation.Title(); ok {
		_spec.SetField(note.FieldTitle, field.TypeBytes, value)
	}
	if value, ok := nuo.mutation.TitleIv(); ok {
		_spec.SetField(note.FieldTitleIv, field.TypeBytes, value)
	}
	if value, ok := nuo.mutation.Content(); ok {
		_spec.SetField(note.FieldContent, field.TypeBytes, value)
	}
	if value, ok := nuo.mutation.ContentIv(); ok {
		_spec.SetField(note.FieldContentIv, field.TypeBytes, value)
	}
	if value, ok := nuo.mutation.Colour(); ok {
		_spec.SetField(note.FieldColour, field.TypeBytes, value)
	}
	if value, ok := nuo.mutation.ColourIv(); ok {
		_spec.SetField(note.FieldColourIv, field.TypeBytes, value)
	}
	if nuo.mutation.VaultCleared() {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   note.VaultTable,
			Columns: []string{note.VaultColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(vault.FieldID, field.TypeUUID),
			},
		}
		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
	}
	if nodes := nuo.mutation.VaultIDs(); len(nodes) > 0 {
		edge := &sqlgraph.EdgeSpec{
			Rel:     sqlgraph.M2O,
			Inverse: true,
			Table:   note.VaultTable,
			Columns: []string{note.VaultColumn},
			Bidi:    false,
			Target: &sqlgraph.EdgeTarget{
				IDSpec: sqlgraph.NewFieldSpec(vault.FieldID, field.TypeUUID),
			},
		}
		for _, k := range nodes {
			edge.Target.Nodes = append(edge.Target.Nodes, k)
		}
		_spec.Edges.Add = append(_spec.Edges.Add, edge)
	}
	_node = &Note{config: nuo.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, nuo.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{note.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	nuo.mutation.done = true
	return _node, nil
}
