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