// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "time" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" "github.com/google/uuid" "github.com/hay-kot/content/backend/ent/document" "github.com/hay-kot/content/backend/ent/documenttoken" "github.com/hay-kot/content/backend/ent/predicate" ) // DocumentTokenUpdate is the builder for updating DocumentToken entities. type DocumentTokenUpdate struct { config hooks []Hook mutation *DocumentTokenMutation } // Where appends a list predicates to the DocumentTokenUpdate builder. func (dtu *DocumentTokenUpdate) Where(ps ...predicate.DocumentToken) *DocumentTokenUpdate { dtu.mutation.Where(ps...) return dtu } // SetUpdatedAt sets the "updated_at" field. func (dtu *DocumentTokenUpdate) SetUpdatedAt(t time.Time) *DocumentTokenUpdate { dtu.mutation.SetUpdatedAt(t) return dtu } // SetToken sets the "token" field. func (dtu *DocumentTokenUpdate) SetToken(b []byte) *DocumentTokenUpdate { dtu.mutation.SetToken(b) return dtu } // SetUses sets the "uses" field. func (dtu *DocumentTokenUpdate) SetUses(i int) *DocumentTokenUpdate { dtu.mutation.ResetUses() dtu.mutation.SetUses(i) return dtu } // SetNillableUses sets the "uses" field if the given value is not nil. func (dtu *DocumentTokenUpdate) SetNillableUses(i *int) *DocumentTokenUpdate { if i != nil { dtu.SetUses(*i) } return dtu } // AddUses adds i to the "uses" field. func (dtu *DocumentTokenUpdate) AddUses(i int) *DocumentTokenUpdate { dtu.mutation.AddUses(i) return dtu } // SetExpiresAt sets the "expires_at" field. func (dtu *DocumentTokenUpdate) SetExpiresAt(t time.Time) *DocumentTokenUpdate { dtu.mutation.SetExpiresAt(t) return dtu } // SetNillableExpiresAt sets the "expires_at" field if the given value is not nil. func (dtu *DocumentTokenUpdate) SetNillableExpiresAt(t *time.Time) *DocumentTokenUpdate { if t != nil { dtu.SetExpiresAt(*t) } return dtu } // SetDocumentID sets the "document" edge to the Document entity by ID. func (dtu *DocumentTokenUpdate) SetDocumentID(id uuid.UUID) *DocumentTokenUpdate { dtu.mutation.SetDocumentID(id) return dtu } // SetNillableDocumentID sets the "document" edge to the Document entity by ID if the given value is not nil. func (dtu *DocumentTokenUpdate) SetNillableDocumentID(id *uuid.UUID) *DocumentTokenUpdate { if id != nil { dtu = dtu.SetDocumentID(*id) } return dtu } // SetDocument sets the "document" edge to the Document entity. func (dtu *DocumentTokenUpdate) SetDocument(d *Document) *DocumentTokenUpdate { return dtu.SetDocumentID(d.ID) } // Mutation returns the DocumentTokenMutation object of the builder. func (dtu *DocumentTokenUpdate) Mutation() *DocumentTokenMutation { return dtu.mutation } // ClearDocument clears the "document" edge to the Document entity. func (dtu *DocumentTokenUpdate) ClearDocument() *DocumentTokenUpdate { dtu.mutation.ClearDocument() return dtu } // Save executes the query and returns the number of nodes affected by the update operation. func (dtu *DocumentTokenUpdate) Save(ctx context.Context) (int, error) { var ( err error affected int ) dtu.defaults() if len(dtu.hooks) == 0 { if err = dtu.check(); err != nil { return 0, err } affected, err = dtu.sqlSave(ctx) } else { var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*DocumentTokenMutation) if !ok { return nil, fmt.Errorf("unexpected mutation type %T", m) } if err = dtu.check(); err != nil { return 0, err } dtu.mutation = mutation affected, err = dtu.sqlSave(ctx) mutation.done = true return affected, err }) for i := len(dtu.hooks) - 1; i >= 0; i-- { if dtu.hooks[i] == nil { return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") } mut = dtu.hooks[i](mut) } if _, err := mut.Mutate(ctx, dtu.mutation); err != nil { return 0, err } } return affected, err } // SaveX is like Save, but panics if an error occurs. func (dtu *DocumentTokenUpdate) SaveX(ctx context.Context) int { affected, err := dtu.Save(ctx) if err != nil { panic(err) } return affected } // Exec executes the query. func (dtu *DocumentTokenUpdate) Exec(ctx context.Context) error { _, err := dtu.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (dtu *DocumentTokenUpdate) ExecX(ctx context.Context) { if err := dtu.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (dtu *DocumentTokenUpdate) defaults() { if _, ok := dtu.mutation.UpdatedAt(); !ok { v := documenttoken.UpdateDefaultUpdatedAt() dtu.mutation.SetUpdatedAt(v) } } // check runs all checks and user-defined validators on the builder. func (dtu *DocumentTokenUpdate) check() error { if v, ok := dtu.mutation.Token(); ok { if err := documenttoken.TokenValidator(v); err != nil { return &ValidationError{Name: "token", err: fmt.Errorf(`ent: validator failed for field "DocumentToken.token": %w`, err)} } } return nil } func (dtu *DocumentTokenUpdate) sqlSave(ctx context.Context) (n int, err error) { _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: documenttoken.Table, Columns: documenttoken.Columns, ID: &sqlgraph.FieldSpec{ Type: field.TypeUUID, Column: documenttoken.FieldID, }, }, } if ps := dtu.mutation.predicates; len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { for i := range ps { ps[i](selector) } } } if value, ok := dtu.mutation.UpdatedAt(); ok { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeTime, Value: value, Column: documenttoken.FieldUpdatedAt, }) } if value, ok := dtu.mutation.Token(); ok { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeBytes, Value: value, Column: documenttoken.FieldToken, }) } if value, ok := dtu.mutation.Uses(); ok { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeInt, Value: value, Column: documenttoken.FieldUses, }) } if value, ok := dtu.mutation.AddedUses(); ok { _spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{ Type: field.TypeInt, Value: value, Column: documenttoken.FieldUses, }) } if value, ok := dtu.mutation.ExpiresAt(); ok { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeTime, Value: value, Column: documenttoken.FieldExpiresAt, }) } if dtu.mutation.DocumentCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: documenttoken.DocumentTable, Columns: []string{documenttoken.DocumentColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ Type: field.TypeUUID, Column: document.FieldID, }, }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := dtu.mutation.DocumentIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: documenttoken.DocumentTable, Columns: []string{documenttoken.DocumentColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ Type: field.TypeUUID, Column: document.FieldID, }, }, } 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, dtu.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { err = &NotFoundError{documenttoken.Label} } else if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return 0, err } return n, nil } // DocumentTokenUpdateOne is the builder for updating a single DocumentToken entity. type DocumentTokenUpdateOne struct { config fields []string hooks []Hook mutation *DocumentTokenMutation } // SetUpdatedAt sets the "updated_at" field. func (dtuo *DocumentTokenUpdateOne) SetUpdatedAt(t time.Time) *DocumentTokenUpdateOne { dtuo.mutation.SetUpdatedAt(t) return dtuo } // SetToken sets the "token" field. func (dtuo *DocumentTokenUpdateOne) SetToken(b []byte) *DocumentTokenUpdateOne { dtuo.mutation.SetToken(b) return dtuo } // SetUses sets the "uses" field. func (dtuo *DocumentTokenUpdateOne) SetUses(i int) *DocumentTokenUpdateOne { dtuo.mutation.ResetUses() dtuo.mutation.SetUses(i) return dtuo } // SetNillableUses sets the "uses" field if the given value is not nil. func (dtuo *DocumentTokenUpdateOne) SetNillableUses(i *int) *DocumentTokenUpdateOne { if i != nil { dtuo.SetUses(*i) } return dtuo } // AddUses adds i to the "uses" field. func (dtuo *DocumentTokenUpdateOne) AddUses(i int) *DocumentTokenUpdateOne { dtuo.mutation.AddUses(i) return dtuo } // SetExpiresAt sets the "expires_at" field. func (dtuo *DocumentTokenUpdateOne) SetExpiresAt(t time.Time) *DocumentTokenUpdateOne { dtuo.mutation.SetExpiresAt(t) return dtuo } // SetNillableExpiresAt sets the "expires_at" field if the given value is not nil. func (dtuo *DocumentTokenUpdateOne) SetNillableExpiresAt(t *time.Time) *DocumentTokenUpdateOne { if t != nil { dtuo.SetExpiresAt(*t) } return dtuo } // SetDocumentID sets the "document" edge to the Document entity by ID. func (dtuo *DocumentTokenUpdateOne) SetDocumentID(id uuid.UUID) *DocumentTokenUpdateOne { dtuo.mutation.SetDocumentID(id) return dtuo } // SetNillableDocumentID sets the "document" edge to the Document entity by ID if the given value is not nil. func (dtuo *DocumentTokenUpdateOne) SetNillableDocumentID(id *uuid.UUID) *DocumentTokenUpdateOne { if id != nil { dtuo = dtuo.SetDocumentID(*id) } return dtuo } // SetDocument sets the "document" edge to the Document entity. func (dtuo *DocumentTokenUpdateOne) SetDocument(d *Document) *DocumentTokenUpdateOne { return dtuo.SetDocumentID(d.ID) } // Mutation returns the DocumentTokenMutation object of the builder. func (dtuo *DocumentTokenUpdateOne) Mutation() *DocumentTokenMutation { return dtuo.mutation } // ClearDocument clears the "document" edge to the Document entity. func (dtuo *DocumentTokenUpdateOne) ClearDocument() *DocumentTokenUpdateOne { dtuo.mutation.ClearDocument() return dtuo } // Select allows selecting one or more fields (columns) of the returned entity. // The default is selecting all fields defined in the entity schema. func (dtuo *DocumentTokenUpdateOne) Select(field string, fields ...string) *DocumentTokenUpdateOne { dtuo.fields = append([]string{field}, fields...) return dtuo } // Save executes the query and returns the updated DocumentToken entity. func (dtuo *DocumentTokenUpdateOne) Save(ctx context.Context) (*DocumentToken, error) { var ( err error node *DocumentToken ) dtuo.defaults() if len(dtuo.hooks) == 0 { if err = dtuo.check(); err != nil { return nil, err } node, err = dtuo.sqlSave(ctx) } else { var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*DocumentTokenMutation) if !ok { return nil, fmt.Errorf("unexpected mutation type %T", m) } if err = dtuo.check(); err != nil { return nil, err } dtuo.mutation = mutation node, err = dtuo.sqlSave(ctx) mutation.done = true return node, err }) for i := len(dtuo.hooks) - 1; i >= 0; i-- { if dtuo.hooks[i] == nil { return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") } mut = dtuo.hooks[i](mut) } v, err := mut.Mutate(ctx, dtuo.mutation) if err != nil { return nil, err } nv, ok := v.(*DocumentToken) if !ok { return nil, fmt.Errorf("unexpected node type %T returned from DocumentTokenMutation", v) } node = nv } return node, err } // SaveX is like Save, but panics if an error occurs. func (dtuo *DocumentTokenUpdateOne) SaveX(ctx context.Context) *DocumentToken { node, err := dtuo.Save(ctx) if err != nil { panic(err) } return node } // Exec executes the query on the entity. func (dtuo *DocumentTokenUpdateOne) Exec(ctx context.Context) error { _, err := dtuo.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (dtuo *DocumentTokenUpdateOne) ExecX(ctx context.Context) { if err := dtuo.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (dtuo *DocumentTokenUpdateOne) defaults() { if _, ok := dtuo.mutation.UpdatedAt(); !ok { v := documenttoken.UpdateDefaultUpdatedAt() dtuo.mutation.SetUpdatedAt(v) } } // check runs all checks and user-defined validators on the builder. func (dtuo *DocumentTokenUpdateOne) check() error { if v, ok := dtuo.mutation.Token(); ok { if err := documenttoken.TokenValidator(v); err != nil { return &ValidationError{Name: "token", err: fmt.Errorf(`ent: validator failed for field "DocumentToken.token": %w`, err)} } } return nil } func (dtuo *DocumentTokenUpdateOne) sqlSave(ctx context.Context) (_node *DocumentToken, err error) { _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: documenttoken.Table, Columns: documenttoken.Columns, ID: &sqlgraph.FieldSpec{ Type: field.TypeUUID, Column: documenttoken.FieldID, }, }, } id, ok := dtuo.mutation.ID() if !ok { return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "DocumentToken.id" for update`)} } _spec.Node.ID.Value = id if fields := dtuo.fields; len(fields) > 0 { _spec.Node.Columns = make([]string, 0, len(fields)) _spec.Node.Columns = append(_spec.Node.Columns, documenttoken.FieldID) for _, f := range fields { if !documenttoken.ValidColumn(f) { return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} } if f != documenttoken.FieldID { _spec.Node.Columns = append(_spec.Node.Columns, f) } } } if ps := dtuo.mutation.predicates; len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { for i := range ps { ps[i](selector) } } } if value, ok := dtuo.mutation.UpdatedAt(); ok { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeTime, Value: value, Column: documenttoken.FieldUpdatedAt, }) } if value, ok := dtuo.mutation.Token(); ok { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeBytes, Value: value, Column: documenttoken.FieldToken, }) } if value, ok := dtuo.mutation.Uses(); ok { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeInt, Value: value, Column: documenttoken.FieldUses, }) } if value, ok := dtuo.mutation.AddedUses(); ok { _spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{ Type: field.TypeInt, Value: value, Column: documenttoken.FieldUses, }) } if value, ok := dtuo.mutation.ExpiresAt(); ok { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeTime, Value: value, Column: documenttoken.FieldExpiresAt, }) } if dtuo.mutation.DocumentCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: documenttoken.DocumentTable, Columns: []string{documenttoken.DocumentColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ Type: field.TypeUUID, Column: document.FieldID, }, }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := dtuo.mutation.DocumentIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: documenttoken.DocumentTable, Columns: []string{documenttoken.DocumentColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ Type: field.TypeUUID, Column: document.FieldID, }, }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges.Add = append(_spec.Edges.Add, edge) } _node = &DocumentToken{config: dtuo.config} _spec.Assign = _node.assignValues _spec.ScanValues = _node.scanValues if err = sqlgraph.UpdateNode(ctx, dtuo.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { err = &NotFoundError{documenttoken.Label} } else if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return nil, err } return _node, nil }