/ ent / token_update.go
token_update.go
  1  // Code generated by ent, DO NOT EDIT.
  2  
  3  package ent
  4  
  5  import (
  6  	"context"
  7  	"errors"
  8  	"fmt"
  9  	"time"
 10  
 11  	"entgo.io/ent/dialect/sql"
 12  	"entgo.io/ent/dialect/sql/sqlgraph"
 13  	"entgo.io/ent/schema/field"
 14  	"github.com/google/uuid"
 15  	"github.com/mrusme/journalist/ent/predicate"
 16  	"github.com/mrusme/journalist/ent/token"
 17  	"github.com/mrusme/journalist/ent/user"
 18  )
 19  
 20  // TokenUpdate is the builder for updating Token entities.
 21  type TokenUpdate struct {
 22  	config
 23  	hooks    []Hook
 24  	mutation *TokenMutation
 25  }
 26  
 27  // Where appends a list predicates to the TokenUpdate builder.
 28  func (tu *TokenUpdate) Where(ps ...predicate.Token) *TokenUpdate {
 29  	tu.mutation.Where(ps...)
 30  	return tu
 31  }
 32  
 33  // SetType sets the "type" field.
 34  func (tu *TokenUpdate) SetType(s string) *TokenUpdate {
 35  	tu.mutation.SetType(s)
 36  	return tu
 37  }
 38  
 39  // SetNillableType sets the "type" field if the given value is not nil.
 40  func (tu *TokenUpdate) SetNillableType(s *string) *TokenUpdate {
 41  	if s != nil {
 42  		tu.SetType(*s)
 43  	}
 44  	return tu
 45  }
 46  
 47  // SetName sets the "name" field.
 48  func (tu *TokenUpdate) SetName(s string) *TokenUpdate {
 49  	tu.mutation.SetName(s)
 50  	return tu
 51  }
 52  
 53  // SetNillableName sets the "name" field if the given value is not nil.
 54  func (tu *TokenUpdate) SetNillableName(s *string) *TokenUpdate {
 55  	if s != nil {
 56  		tu.SetName(*s)
 57  	}
 58  	return tu
 59  }
 60  
 61  // SetToken sets the "token" field.
 62  func (tu *TokenUpdate) SetToken(s string) *TokenUpdate {
 63  	tu.mutation.SetToken(s)
 64  	return tu
 65  }
 66  
 67  // SetNillableToken sets the "token" field if the given value is not nil.
 68  func (tu *TokenUpdate) SetNillableToken(s *string) *TokenUpdate {
 69  	if s != nil {
 70  		tu.SetToken(*s)
 71  	}
 72  	return tu
 73  }
 74  
 75  // SetCreatedAt sets the "created_at" field.
 76  func (tu *TokenUpdate) SetCreatedAt(t time.Time) *TokenUpdate {
 77  	tu.mutation.SetCreatedAt(t)
 78  	return tu
 79  }
 80  
 81  // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
 82  func (tu *TokenUpdate) SetNillableCreatedAt(t *time.Time) *TokenUpdate {
 83  	if t != nil {
 84  		tu.SetCreatedAt(*t)
 85  	}
 86  	return tu
 87  }
 88  
 89  // SetUpdatedAt sets the "updated_at" field.
 90  func (tu *TokenUpdate) SetUpdatedAt(t time.Time) *TokenUpdate {
 91  	tu.mutation.SetUpdatedAt(t)
 92  	return tu
 93  }
 94  
 95  // SetDeletedAt sets the "deleted_at" field.
 96  func (tu *TokenUpdate) SetDeletedAt(t time.Time) *TokenUpdate {
 97  	tu.mutation.SetDeletedAt(t)
 98  	return tu
 99  }
100  
101  // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
102  func (tu *TokenUpdate) SetNillableDeletedAt(t *time.Time) *TokenUpdate {
103  	if t != nil {
104  		tu.SetDeletedAt(*t)
105  	}
106  	return tu
107  }
108  
109  // ClearDeletedAt clears the value of the "deleted_at" field.
110  func (tu *TokenUpdate) ClearDeletedAt() *TokenUpdate {
111  	tu.mutation.ClearDeletedAt()
112  	return tu
113  }
114  
115  // SetOwnerID sets the "owner" edge to the User entity by ID.
116  func (tu *TokenUpdate) SetOwnerID(id uuid.UUID) *TokenUpdate {
117  	tu.mutation.SetOwnerID(id)
118  	return tu
119  }
120  
121  // SetNillableOwnerID sets the "owner" edge to the User entity by ID if the given value is not nil.
122  func (tu *TokenUpdate) SetNillableOwnerID(id *uuid.UUID) *TokenUpdate {
123  	if id != nil {
124  		tu = tu.SetOwnerID(*id)
125  	}
126  	return tu
127  }
128  
129  // SetOwner sets the "owner" edge to the User entity.
130  func (tu *TokenUpdate) SetOwner(u *User) *TokenUpdate {
131  	return tu.SetOwnerID(u.ID)
132  }
133  
134  // Mutation returns the TokenMutation object of the builder.
135  func (tu *TokenUpdate) Mutation() *TokenMutation {
136  	return tu.mutation
137  }
138  
139  // ClearOwner clears the "owner" edge to the User entity.
140  func (tu *TokenUpdate) ClearOwner() *TokenUpdate {
141  	tu.mutation.ClearOwner()
142  	return tu
143  }
144  
145  // Save executes the query and returns the number of nodes affected by the update operation.
146  func (tu *TokenUpdate) Save(ctx context.Context) (int, error) {
147  	tu.defaults()
148  	return withHooks(ctx, tu.sqlSave, tu.mutation, tu.hooks)
149  }
150  
151  // SaveX is like Save, but panics if an error occurs.
152  func (tu *TokenUpdate) SaveX(ctx context.Context) int {
153  	affected, err := tu.Save(ctx)
154  	if err != nil {
155  		panic(err)
156  	}
157  	return affected
158  }
159  
160  // Exec executes the query.
161  func (tu *TokenUpdate) Exec(ctx context.Context) error {
162  	_, err := tu.Save(ctx)
163  	return err
164  }
165  
166  // ExecX is like Exec, but panics if an error occurs.
167  func (tu *TokenUpdate) ExecX(ctx context.Context) {
168  	if err := tu.Exec(ctx); err != nil {
169  		panic(err)
170  	}
171  }
172  
173  // defaults sets the default values of the builder before save.
174  func (tu *TokenUpdate) defaults() {
175  	if _, ok := tu.mutation.UpdatedAt(); !ok {
176  		v := token.UpdateDefaultUpdatedAt()
177  		tu.mutation.SetUpdatedAt(v)
178  	}
179  }
180  
181  // check runs all checks and user-defined validators on the builder.
182  func (tu *TokenUpdate) check() error {
183  	if v, ok := tu.mutation.GetType(); ok {
184  		if err := token.TypeValidator(v); err != nil {
185  			return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "Token.type": %w`, err)}
186  		}
187  	}
188  	if v, ok := tu.mutation.Name(); ok {
189  		if err := token.NameValidator(v); err != nil {
190  			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Token.name": %w`, err)}
191  		}
192  	}
193  	return nil
194  }
195  
196  func (tu *TokenUpdate) sqlSave(ctx context.Context) (n int, err error) {
197  	if err := tu.check(); err != nil {
198  		return n, err
199  	}
200  	_spec := sqlgraph.NewUpdateSpec(token.Table, token.Columns, sqlgraph.NewFieldSpec(token.FieldID, field.TypeUUID))
201  	if ps := tu.mutation.predicates; len(ps) > 0 {
202  		_spec.Predicate = func(selector *sql.Selector) {
203  			for i := range ps {
204  				ps[i](selector)
205  			}
206  		}
207  	}
208  	if value, ok := tu.mutation.GetType(); ok {
209  		_spec.SetField(token.FieldType, field.TypeString, value)
210  	}
211  	if value, ok := tu.mutation.Name(); ok {
212  		_spec.SetField(token.FieldName, field.TypeString, value)
213  	}
214  	if value, ok := tu.mutation.Token(); ok {
215  		_spec.SetField(token.FieldToken, field.TypeString, value)
216  	}
217  	if value, ok := tu.mutation.CreatedAt(); ok {
218  		_spec.SetField(token.FieldCreatedAt, field.TypeTime, value)
219  	}
220  	if value, ok := tu.mutation.UpdatedAt(); ok {
221  		_spec.SetField(token.FieldUpdatedAt, field.TypeTime, value)
222  	}
223  	if value, ok := tu.mutation.DeletedAt(); ok {
224  		_spec.SetField(token.FieldDeletedAt, field.TypeTime, value)
225  	}
226  	if tu.mutation.DeletedAtCleared() {
227  		_spec.ClearField(token.FieldDeletedAt, field.TypeTime)
228  	}
229  	if tu.mutation.OwnerCleared() {
230  		edge := &sqlgraph.EdgeSpec{
231  			Rel:     sqlgraph.M2O,
232  			Inverse: true,
233  			Table:   token.OwnerTable,
234  			Columns: []string{token.OwnerColumn},
235  			Bidi:    false,
236  			Target: &sqlgraph.EdgeTarget{
237  				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID),
238  			},
239  		}
240  		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
241  	}
242  	if nodes := tu.mutation.OwnerIDs(); len(nodes) > 0 {
243  		edge := &sqlgraph.EdgeSpec{
244  			Rel:     sqlgraph.M2O,
245  			Inverse: true,
246  			Table:   token.OwnerTable,
247  			Columns: []string{token.OwnerColumn},
248  			Bidi:    false,
249  			Target: &sqlgraph.EdgeTarget{
250  				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID),
251  			},
252  		}
253  		for _, k := range nodes {
254  			edge.Target.Nodes = append(edge.Target.Nodes, k)
255  		}
256  		_spec.Edges.Add = append(_spec.Edges.Add, edge)
257  	}
258  	if n, err = sqlgraph.UpdateNodes(ctx, tu.driver, _spec); err != nil {
259  		if _, ok := err.(*sqlgraph.NotFoundError); ok {
260  			err = &NotFoundError{token.Label}
261  		} else if sqlgraph.IsConstraintError(err) {
262  			err = &ConstraintError{msg: err.Error(), wrap: err}
263  		}
264  		return 0, err
265  	}
266  	tu.mutation.done = true
267  	return n, nil
268  }
269  
270  // TokenUpdateOne is the builder for updating a single Token entity.
271  type TokenUpdateOne struct {
272  	config
273  	fields   []string
274  	hooks    []Hook
275  	mutation *TokenMutation
276  }
277  
278  // SetType sets the "type" field.
279  func (tuo *TokenUpdateOne) SetType(s string) *TokenUpdateOne {
280  	tuo.mutation.SetType(s)
281  	return tuo
282  }
283  
284  // SetNillableType sets the "type" field if the given value is not nil.
285  func (tuo *TokenUpdateOne) SetNillableType(s *string) *TokenUpdateOne {
286  	if s != nil {
287  		tuo.SetType(*s)
288  	}
289  	return tuo
290  }
291  
292  // SetName sets the "name" field.
293  func (tuo *TokenUpdateOne) SetName(s string) *TokenUpdateOne {
294  	tuo.mutation.SetName(s)
295  	return tuo
296  }
297  
298  // SetNillableName sets the "name" field if the given value is not nil.
299  func (tuo *TokenUpdateOne) SetNillableName(s *string) *TokenUpdateOne {
300  	if s != nil {
301  		tuo.SetName(*s)
302  	}
303  	return tuo
304  }
305  
306  // SetToken sets the "token" field.
307  func (tuo *TokenUpdateOne) SetToken(s string) *TokenUpdateOne {
308  	tuo.mutation.SetToken(s)
309  	return tuo
310  }
311  
312  // SetNillableToken sets the "token" field if the given value is not nil.
313  func (tuo *TokenUpdateOne) SetNillableToken(s *string) *TokenUpdateOne {
314  	if s != nil {
315  		tuo.SetToken(*s)
316  	}
317  	return tuo
318  }
319  
320  // SetCreatedAt sets the "created_at" field.
321  func (tuo *TokenUpdateOne) SetCreatedAt(t time.Time) *TokenUpdateOne {
322  	tuo.mutation.SetCreatedAt(t)
323  	return tuo
324  }
325  
326  // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
327  func (tuo *TokenUpdateOne) SetNillableCreatedAt(t *time.Time) *TokenUpdateOne {
328  	if t != nil {
329  		tuo.SetCreatedAt(*t)
330  	}
331  	return tuo
332  }
333  
334  // SetUpdatedAt sets the "updated_at" field.
335  func (tuo *TokenUpdateOne) SetUpdatedAt(t time.Time) *TokenUpdateOne {
336  	tuo.mutation.SetUpdatedAt(t)
337  	return tuo
338  }
339  
340  // SetDeletedAt sets the "deleted_at" field.
341  func (tuo *TokenUpdateOne) SetDeletedAt(t time.Time) *TokenUpdateOne {
342  	tuo.mutation.SetDeletedAt(t)
343  	return tuo
344  }
345  
346  // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
347  func (tuo *TokenUpdateOne) SetNillableDeletedAt(t *time.Time) *TokenUpdateOne {
348  	if t != nil {
349  		tuo.SetDeletedAt(*t)
350  	}
351  	return tuo
352  }
353  
354  // ClearDeletedAt clears the value of the "deleted_at" field.
355  func (tuo *TokenUpdateOne) ClearDeletedAt() *TokenUpdateOne {
356  	tuo.mutation.ClearDeletedAt()
357  	return tuo
358  }
359  
360  // SetOwnerID sets the "owner" edge to the User entity by ID.
361  func (tuo *TokenUpdateOne) SetOwnerID(id uuid.UUID) *TokenUpdateOne {
362  	tuo.mutation.SetOwnerID(id)
363  	return tuo
364  }
365  
366  // SetNillableOwnerID sets the "owner" edge to the User entity by ID if the given value is not nil.
367  func (tuo *TokenUpdateOne) SetNillableOwnerID(id *uuid.UUID) *TokenUpdateOne {
368  	if id != nil {
369  		tuo = tuo.SetOwnerID(*id)
370  	}
371  	return tuo
372  }
373  
374  // SetOwner sets the "owner" edge to the User entity.
375  func (tuo *TokenUpdateOne) SetOwner(u *User) *TokenUpdateOne {
376  	return tuo.SetOwnerID(u.ID)
377  }
378  
379  // Mutation returns the TokenMutation object of the builder.
380  func (tuo *TokenUpdateOne) Mutation() *TokenMutation {
381  	return tuo.mutation
382  }
383  
384  // ClearOwner clears the "owner" edge to the User entity.
385  func (tuo *TokenUpdateOne) ClearOwner() *TokenUpdateOne {
386  	tuo.mutation.ClearOwner()
387  	return tuo
388  }
389  
390  // Where appends a list predicates to the TokenUpdate builder.
391  func (tuo *TokenUpdateOne) Where(ps ...predicate.Token) *TokenUpdateOne {
392  	tuo.mutation.Where(ps...)
393  	return tuo
394  }
395  
396  // Select allows selecting one or more fields (columns) of the returned entity.
397  // The default is selecting all fields defined in the entity schema.
398  func (tuo *TokenUpdateOne) Select(field string, fields ...string) *TokenUpdateOne {
399  	tuo.fields = append([]string{field}, fields...)
400  	return tuo
401  }
402  
403  // Save executes the query and returns the updated Token entity.
404  func (tuo *TokenUpdateOne) Save(ctx context.Context) (*Token, error) {
405  	tuo.defaults()
406  	return withHooks(ctx, tuo.sqlSave, tuo.mutation, tuo.hooks)
407  }
408  
409  // SaveX is like Save, but panics if an error occurs.
410  func (tuo *TokenUpdateOne) SaveX(ctx context.Context) *Token {
411  	node, err := tuo.Save(ctx)
412  	if err != nil {
413  		panic(err)
414  	}
415  	return node
416  }
417  
418  // Exec executes the query on the entity.
419  func (tuo *TokenUpdateOne) Exec(ctx context.Context) error {
420  	_, err := tuo.Save(ctx)
421  	return err
422  }
423  
424  // ExecX is like Exec, but panics if an error occurs.
425  func (tuo *TokenUpdateOne) ExecX(ctx context.Context) {
426  	if err := tuo.Exec(ctx); err != nil {
427  		panic(err)
428  	}
429  }
430  
431  // defaults sets the default values of the builder before save.
432  func (tuo *TokenUpdateOne) defaults() {
433  	if _, ok := tuo.mutation.UpdatedAt(); !ok {
434  		v := token.UpdateDefaultUpdatedAt()
435  		tuo.mutation.SetUpdatedAt(v)
436  	}
437  }
438  
439  // check runs all checks and user-defined validators on the builder.
440  func (tuo *TokenUpdateOne) check() error {
441  	if v, ok := tuo.mutation.GetType(); ok {
442  		if err := token.TypeValidator(v); err != nil {
443  			return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "Token.type": %w`, err)}
444  		}
445  	}
446  	if v, ok := tuo.mutation.Name(); ok {
447  		if err := token.NameValidator(v); err != nil {
448  			return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Token.name": %w`, err)}
449  		}
450  	}
451  	return nil
452  }
453  
454  func (tuo *TokenUpdateOne) sqlSave(ctx context.Context) (_node *Token, err error) {
455  	if err := tuo.check(); err != nil {
456  		return _node, err
457  	}
458  	_spec := sqlgraph.NewUpdateSpec(token.Table, token.Columns, sqlgraph.NewFieldSpec(token.FieldID, field.TypeUUID))
459  	id, ok := tuo.mutation.ID()
460  	if !ok {
461  		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Token.id" for update`)}
462  	}
463  	_spec.Node.ID.Value = id
464  	if fields := tuo.fields; len(fields) > 0 {
465  		_spec.Node.Columns = make([]string, 0, len(fields))
466  		_spec.Node.Columns = append(_spec.Node.Columns, token.FieldID)
467  		for _, f := range fields {
468  			if !token.ValidColumn(f) {
469  				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
470  			}
471  			if f != token.FieldID {
472  				_spec.Node.Columns = append(_spec.Node.Columns, f)
473  			}
474  		}
475  	}
476  	if ps := tuo.mutation.predicates; len(ps) > 0 {
477  		_spec.Predicate = func(selector *sql.Selector) {
478  			for i := range ps {
479  				ps[i](selector)
480  			}
481  		}
482  	}
483  	if value, ok := tuo.mutation.GetType(); ok {
484  		_spec.SetField(token.FieldType, field.TypeString, value)
485  	}
486  	if value, ok := tuo.mutation.Name(); ok {
487  		_spec.SetField(token.FieldName, field.TypeString, value)
488  	}
489  	if value, ok := tuo.mutation.Token(); ok {
490  		_spec.SetField(token.FieldToken, field.TypeString, value)
491  	}
492  	if value, ok := tuo.mutation.CreatedAt(); ok {
493  		_spec.SetField(token.FieldCreatedAt, field.TypeTime, value)
494  	}
495  	if value, ok := tuo.mutation.UpdatedAt(); ok {
496  		_spec.SetField(token.FieldUpdatedAt, field.TypeTime, value)
497  	}
498  	if value, ok := tuo.mutation.DeletedAt(); ok {
499  		_spec.SetField(token.FieldDeletedAt, field.TypeTime, value)
500  	}
501  	if tuo.mutation.DeletedAtCleared() {
502  		_spec.ClearField(token.FieldDeletedAt, field.TypeTime)
503  	}
504  	if tuo.mutation.OwnerCleared() {
505  		edge := &sqlgraph.EdgeSpec{
506  			Rel:     sqlgraph.M2O,
507  			Inverse: true,
508  			Table:   token.OwnerTable,
509  			Columns: []string{token.OwnerColumn},
510  			Bidi:    false,
511  			Target: &sqlgraph.EdgeTarget{
512  				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID),
513  			},
514  		}
515  		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
516  	}
517  	if nodes := tuo.mutation.OwnerIDs(); len(nodes) > 0 {
518  		edge := &sqlgraph.EdgeSpec{
519  			Rel:     sqlgraph.M2O,
520  			Inverse: true,
521  			Table:   token.OwnerTable,
522  			Columns: []string{token.OwnerColumn},
523  			Bidi:    false,
524  			Target: &sqlgraph.EdgeTarget{
525  				IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID),
526  			},
527  		}
528  		for _, k := range nodes {
529  			edge.Target.Nodes = append(edge.Target.Nodes, k)
530  		}
531  		_spec.Edges.Add = append(_spec.Edges.Add, edge)
532  	}
533  	_node = &Token{config: tuo.config}
534  	_spec.Assign = _node.assignValues
535  	_spec.ScanValues = _node.scanValues
536  	if err = sqlgraph.UpdateNode(ctx, tuo.driver, _spec); err != nil {
537  		if _, ok := err.(*sqlgraph.NotFoundError); ok {
538  			err = &NotFoundError{token.Label}
539  		} else if sqlgraph.IsConstraintError(err) {
540  			err = &ConstraintError{msg: err.Error(), wrap: err}
541  		}
542  		return nil, err
543  	}
544  	tuo.mutation.done = true
545  	return _node, nil
546  }