// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"

	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"git.dcentral.systems/toolz/goplt/internal/ent/auditlog"
	"git.dcentral.systems/toolz/goplt/internal/ent/predicate"
)

// AuditLogUpdate is the builder for updating AuditLog entities.
type AuditLogUpdate struct {
	config
	hooks    []Hook
	mutation *AuditLogMutation
}

// Where appends a list predicates to the AuditLogUpdate builder.
func (_u *AuditLogUpdate) Where(ps ...predicate.AuditLog) *AuditLogUpdate {
	_u.mutation.Where(ps...)
	return _u
}

// SetUserID sets the "user_id" field.
func (_u *AuditLogUpdate) SetUserID(v string) *AuditLogUpdate {
	_u.mutation.SetUserID(v)
	return _u
}

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

// SetAction sets the "action" field.
func (_u *AuditLogUpdate) SetAction(v string) *AuditLogUpdate {
	_u.mutation.SetAction(v)
	return _u
}

// SetNillableAction sets the "action" field if the given value is not nil.
func (_u *AuditLogUpdate) SetNillableAction(v *string) *AuditLogUpdate {
	if v != nil {
		_u.SetAction(*v)
	}
	return _u
}

// SetResource sets the "resource" field.
func (_u *AuditLogUpdate) SetResource(v string) *AuditLogUpdate {
	_u.mutation.SetResource(v)
	return _u
}

// SetNillableResource sets the "resource" field if the given value is not nil.
func (_u *AuditLogUpdate) SetNillableResource(v *string) *AuditLogUpdate {
	if v != nil {
		_u.SetResource(*v)
	}
	return _u
}

// ClearResource clears the value of the "resource" field.
func (_u *AuditLogUpdate) ClearResource() *AuditLogUpdate {
	_u.mutation.ClearResource()
	return _u
}

// SetResourceID sets the "resource_id" field.
func (_u *AuditLogUpdate) SetResourceID(v string) *AuditLogUpdate {
	_u.mutation.SetResourceID(v)
	return _u
}

// SetNillableResourceID sets the "resource_id" field if the given value is not nil.
func (_u *AuditLogUpdate) SetNillableResourceID(v *string) *AuditLogUpdate {
	if v != nil {
		_u.SetResourceID(*v)
	}
	return _u
}

// ClearResourceID clears the value of the "resource_id" field.
func (_u *AuditLogUpdate) ClearResourceID() *AuditLogUpdate {
	_u.mutation.ClearResourceID()
	return _u
}

// SetIPAddress sets the "ip_address" field.
func (_u *AuditLogUpdate) SetIPAddress(v string) *AuditLogUpdate {
	_u.mutation.SetIPAddress(v)
	return _u
}

// SetNillableIPAddress sets the "ip_address" field if the given value is not nil.
func (_u *AuditLogUpdate) SetNillableIPAddress(v *string) *AuditLogUpdate {
	if v != nil {
		_u.SetIPAddress(*v)
	}
	return _u
}

// ClearIPAddress clears the value of the "ip_address" field.
func (_u *AuditLogUpdate) ClearIPAddress() *AuditLogUpdate {
	_u.mutation.ClearIPAddress()
	return _u
}

// SetUserAgent sets the "user_agent" field.
func (_u *AuditLogUpdate) SetUserAgent(v string) *AuditLogUpdate {
	_u.mutation.SetUserAgent(v)
	return _u
}

// SetNillableUserAgent sets the "user_agent" field if the given value is not nil.
func (_u *AuditLogUpdate) SetNillableUserAgent(v *string) *AuditLogUpdate {
	if v != nil {
		_u.SetUserAgent(*v)
	}
	return _u
}

// ClearUserAgent clears the value of the "user_agent" field.
func (_u *AuditLogUpdate) ClearUserAgent() *AuditLogUpdate {
	_u.mutation.ClearUserAgent()
	return _u
}

// SetMetadata sets the "metadata" field.
func (_u *AuditLogUpdate) SetMetadata(v map[string]interface{}) *AuditLogUpdate {
	_u.mutation.SetMetadata(v)
	return _u
}

// ClearMetadata clears the value of the "metadata" field.
func (_u *AuditLogUpdate) ClearMetadata() *AuditLogUpdate {
	_u.mutation.ClearMetadata()
	return _u
}

// Mutation returns the AuditLogMutation object of the builder.
func (_u *AuditLogUpdate) Mutation() *AuditLogMutation {
	return _u.mutation
}

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *AuditLogUpdate) check() error {
	if v, ok := _u.mutation.UserID(); ok {
		if err := auditlog.UserIDValidator(v); err != nil {
			return &ValidationError{Name: "user_id", err: fmt.Errorf(`ent: validator failed for field "AuditLog.user_id": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Action(); ok {
		if err := auditlog.ActionValidator(v); err != nil {
			return &ValidationError{Name: "action", err: fmt.Errorf(`ent: validator failed for field "AuditLog.action": %w`, err)}
		}
	}
	return nil
}

func (_u *AuditLogUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(auditlog.Table, auditlog.Columns, sqlgraph.NewFieldSpec(auditlog.FieldID, field.TypeString))
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.UserID(); ok {
		_spec.SetField(auditlog.FieldUserID, field.TypeString, value)
	}
	if value, ok := _u.mutation.Action(); ok {
		_spec.SetField(auditlog.FieldAction, field.TypeString, value)
	}
	if value, ok := _u.mutation.Resource(); ok {
		_spec.SetField(auditlog.FieldResource, field.TypeString, value)
	}
	if _u.mutation.ResourceCleared() {
		_spec.ClearField(auditlog.FieldResource, field.TypeString)
	}
	if value, ok := _u.mutation.ResourceID(); ok {
		_spec.SetField(auditlog.FieldResourceID, field.TypeString, value)
	}
	if _u.mutation.ResourceIDCleared() {
		_spec.ClearField(auditlog.FieldResourceID, field.TypeString)
	}
	if value, ok := _u.mutation.IPAddress(); ok {
		_spec.SetField(auditlog.FieldIPAddress, field.TypeString, value)
	}
	if _u.mutation.IPAddressCleared() {
		_spec.ClearField(auditlog.FieldIPAddress, field.TypeString)
	}
	if value, ok := _u.mutation.UserAgent(); ok {
		_spec.SetField(auditlog.FieldUserAgent, field.TypeString, value)
	}
	if _u.mutation.UserAgentCleared() {
		_spec.ClearField(auditlog.FieldUserAgent, field.TypeString)
	}
	if value, ok := _u.mutation.Metadata(); ok {
		_spec.SetField(auditlog.FieldMetadata, field.TypeJSON, value)
	}
	if _u.mutation.MetadataCleared() {
		_spec.ClearField(auditlog.FieldMetadata, field.TypeJSON)
	}
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{auditlog.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// AuditLogUpdateOne is the builder for updating a single AuditLog entity.
type AuditLogUpdateOne struct {
	config
	fields   []string
	hooks    []Hook
	mutation *AuditLogMutation
}

// SetUserID sets the "user_id" field.
func (_u *AuditLogUpdateOne) SetUserID(v string) *AuditLogUpdateOne {
	_u.mutation.SetUserID(v)
	return _u
}

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

// SetAction sets the "action" field.
func (_u *AuditLogUpdateOne) SetAction(v string) *AuditLogUpdateOne {
	_u.mutation.SetAction(v)
	return _u
}

// SetNillableAction sets the "action" field if the given value is not nil.
func (_u *AuditLogUpdateOne) SetNillableAction(v *string) *AuditLogUpdateOne {
	if v != nil {
		_u.SetAction(*v)
	}
	return _u
}

// SetResource sets the "resource" field.
func (_u *AuditLogUpdateOne) SetResource(v string) *AuditLogUpdateOne {
	_u.mutation.SetResource(v)
	return _u
}

// SetNillableResource sets the "resource" field if the given value is not nil.
func (_u *AuditLogUpdateOne) SetNillableResource(v *string) *AuditLogUpdateOne {
	if v != nil {
		_u.SetResource(*v)
	}
	return _u
}

// ClearResource clears the value of the "resource" field.
func (_u *AuditLogUpdateOne) ClearResource() *AuditLogUpdateOne {
	_u.mutation.ClearResource()
	return _u
}

// SetResourceID sets the "resource_id" field.
func (_u *AuditLogUpdateOne) SetResourceID(v string) *AuditLogUpdateOne {
	_u.mutation.SetResourceID(v)
	return _u
}

// SetNillableResourceID sets the "resource_id" field if the given value is not nil.
func (_u *AuditLogUpdateOne) SetNillableResourceID(v *string) *AuditLogUpdateOne {
	if v != nil {
		_u.SetResourceID(*v)
	}
	return _u
}

// ClearResourceID clears the value of the "resource_id" field.
func (_u *AuditLogUpdateOne) ClearResourceID() *AuditLogUpdateOne {
	_u.mutation.ClearResourceID()
	return _u
}

// SetIPAddress sets the "ip_address" field.
func (_u *AuditLogUpdateOne) SetIPAddress(v string) *AuditLogUpdateOne {
	_u.mutation.SetIPAddress(v)
	return _u
}

// SetNillableIPAddress sets the "ip_address" field if the given value is not nil.
func (_u *AuditLogUpdateOne) SetNillableIPAddress(v *string) *AuditLogUpdateOne {
	if v != nil {
		_u.SetIPAddress(*v)
	}
	return _u
}

// ClearIPAddress clears the value of the "ip_address" field.
func (_u *AuditLogUpdateOne) ClearIPAddress() *AuditLogUpdateOne {
	_u.mutation.ClearIPAddress()
	return _u
}

// SetUserAgent sets the "user_agent" field.
func (_u *AuditLogUpdateOne) SetUserAgent(v string) *AuditLogUpdateOne {
	_u.mutation.SetUserAgent(v)
	return _u
}

// SetNillableUserAgent sets the "user_agent" field if the given value is not nil.
func (_u *AuditLogUpdateOne) SetNillableUserAgent(v *string) *AuditLogUpdateOne {
	if v != nil {
		_u.SetUserAgent(*v)
	}
	return _u
}

// ClearUserAgent clears the value of the "user_agent" field.
func (_u *AuditLogUpdateOne) ClearUserAgent() *AuditLogUpdateOne {
	_u.mutation.ClearUserAgent()
	return _u
}

// SetMetadata sets the "metadata" field.
func (_u *AuditLogUpdateOne) SetMetadata(v map[string]interface{}) *AuditLogUpdateOne {
	_u.mutation.SetMetadata(v)
	return _u
}

// ClearMetadata clears the value of the "metadata" field.
func (_u *AuditLogUpdateOne) ClearMetadata() *AuditLogUpdateOne {
	_u.mutation.ClearMetadata()
	return _u
}

// Mutation returns the AuditLogMutation object of the builder.
func (_u *AuditLogUpdateOne) Mutation() *AuditLogMutation {
	return _u.mutation
}

// Where appends a list predicates to the AuditLogUpdate builder.
func (_u *AuditLogUpdateOne) Where(ps ...predicate.AuditLog) *AuditLogUpdateOne {
	_u.mutation.Where(ps...)
	return _u
}

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

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

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

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

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

// check runs all checks and user-defined validators on the builder.
func (_u *AuditLogUpdateOne) check() error {
	if v, ok := _u.mutation.UserID(); ok {
		if err := auditlog.UserIDValidator(v); err != nil {
			return &ValidationError{Name: "user_id", err: fmt.Errorf(`ent: validator failed for field "AuditLog.user_id": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Action(); ok {
		if err := auditlog.ActionValidator(v); err != nil {
			return &ValidationError{Name: "action", err: fmt.Errorf(`ent: validator failed for field "AuditLog.action": %w`, err)}
		}
	}
	return nil
}

func (_u *AuditLogUpdateOne) sqlSave(ctx context.Context) (_node *AuditLog, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(auditlog.Table, auditlog.Columns, sqlgraph.NewFieldSpec(auditlog.FieldID, field.TypeString))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "AuditLog.id" for update`)}
	}
	_spec.Node.ID.Value = id
	if fields := _u.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, auditlog.FieldID)
		for _, f := range fields {
			if !auditlog.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != auditlog.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, f)
			}
		}
	}
	if ps := _u.mutation.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if value, ok := _u.mutation.UserID(); ok {
		_spec.SetField(auditlog.FieldUserID, field.TypeString, value)
	}
	if value, ok := _u.mutation.Action(); ok {
		_spec.SetField(auditlog.FieldAction, field.TypeString, value)
	}
	if value, ok := _u.mutation.Resource(); ok {
		_spec.SetField(auditlog.FieldResource, field.TypeString, value)
	}
	if _u.mutation.ResourceCleared() {
		_spec.ClearField(auditlog.FieldResource, field.TypeString)
	}
	if value, ok := _u.mutation.ResourceID(); ok {
		_spec.SetField(auditlog.FieldResourceID, field.TypeString, value)
	}
	if _u.mutation.ResourceIDCleared() {
		_spec.ClearField(auditlog.FieldResourceID, field.TypeString)
	}
	if value, ok := _u.mutation.IPAddress(); ok {
		_spec.SetField(auditlog.FieldIPAddress, field.TypeString, value)
	}
	if _u.mutation.IPAddressCleared() {
		_spec.ClearField(auditlog.FieldIPAddress, field.TypeString)
	}
	if value, ok := _u.mutation.UserAgent(); ok {
		_spec.SetField(auditlog.FieldUserAgent, field.TypeString, value)
	}
	if _u.mutation.UserAgentCleared() {
		_spec.ClearField(auditlog.FieldUserAgent, field.TypeString)
	}
	if value, ok := _u.mutation.Metadata(); ok {
		_spec.SetField(auditlog.FieldMetadata, field.TypeJSON, value)
	}
	if _u.mutation.MetadataCleared() {
		_spec.ClearField(auditlog.FieldMetadata, field.TypeJSON)
	}
	_node = &AuditLog{config: _u.config}
	_spec.Assign = _node.assignValues
	_spec.ScanValues = _node.scanValues
	if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{auditlog.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
