github.com/ngocphuongnb/tetua@v0.0.7-alpha/packages/entrepository/ent/topic_create.go (about)

     1  // Code generated by entc, 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/ngocphuongnb/tetua/packages/entrepository/ent/post"
    15  	"github.com/ngocphuongnb/tetua/packages/entrepository/ent/topic"
    16  )
    17  
    18  // TopicCreate is the builder for creating a Topic entity.
    19  type TopicCreate struct {
    20  	config
    21  	mutation *TopicMutation
    22  	hooks    []Hook
    23  	conflict []sql.ConflictOption
    24  }
    25  
    26  // SetCreatedAt sets the "created_at" field.
    27  func (tc *TopicCreate) SetCreatedAt(t time.Time) *TopicCreate {
    28  	tc.mutation.SetCreatedAt(t)
    29  	return tc
    30  }
    31  
    32  // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
    33  func (tc *TopicCreate) SetNillableCreatedAt(t *time.Time) *TopicCreate {
    34  	if t != nil {
    35  		tc.SetCreatedAt(*t)
    36  	}
    37  	return tc
    38  }
    39  
    40  // SetUpdatedAt sets the "updated_at" field.
    41  func (tc *TopicCreate) SetUpdatedAt(t time.Time) *TopicCreate {
    42  	tc.mutation.SetUpdatedAt(t)
    43  	return tc
    44  }
    45  
    46  // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
    47  func (tc *TopicCreate) SetNillableUpdatedAt(t *time.Time) *TopicCreate {
    48  	if t != nil {
    49  		tc.SetUpdatedAt(*t)
    50  	}
    51  	return tc
    52  }
    53  
    54  // SetDeletedAt sets the "deleted_at" field.
    55  func (tc *TopicCreate) SetDeletedAt(t time.Time) *TopicCreate {
    56  	tc.mutation.SetDeletedAt(t)
    57  	return tc
    58  }
    59  
    60  // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
    61  func (tc *TopicCreate) SetNillableDeletedAt(t *time.Time) *TopicCreate {
    62  	if t != nil {
    63  		tc.SetDeletedAt(*t)
    64  	}
    65  	return tc
    66  }
    67  
    68  // SetName sets the "name" field.
    69  func (tc *TopicCreate) SetName(s string) *TopicCreate {
    70  	tc.mutation.SetName(s)
    71  	return tc
    72  }
    73  
    74  // SetSlug sets the "slug" field.
    75  func (tc *TopicCreate) SetSlug(s string) *TopicCreate {
    76  	tc.mutation.SetSlug(s)
    77  	return tc
    78  }
    79  
    80  // SetDescription sets the "description" field.
    81  func (tc *TopicCreate) SetDescription(s string) *TopicCreate {
    82  	tc.mutation.SetDescription(s)
    83  	return tc
    84  }
    85  
    86  // SetNillableDescription sets the "description" field if the given value is not nil.
    87  func (tc *TopicCreate) SetNillableDescription(s *string) *TopicCreate {
    88  	if s != nil {
    89  		tc.SetDescription(*s)
    90  	}
    91  	return tc
    92  }
    93  
    94  // SetContent sets the "content" field.
    95  func (tc *TopicCreate) SetContent(s string) *TopicCreate {
    96  	tc.mutation.SetContent(s)
    97  	return tc
    98  }
    99  
   100  // SetContentHTML sets the "content_html" field.
   101  func (tc *TopicCreate) SetContentHTML(s string) *TopicCreate {
   102  	tc.mutation.SetContentHTML(s)
   103  	return tc
   104  }
   105  
   106  // SetParentID sets the "parent_id" field.
   107  func (tc *TopicCreate) SetParentID(i int) *TopicCreate {
   108  	tc.mutation.SetParentID(i)
   109  	return tc
   110  }
   111  
   112  // SetNillableParentID sets the "parent_id" field if the given value is not nil.
   113  func (tc *TopicCreate) SetNillableParentID(i *int) *TopicCreate {
   114  	if i != nil {
   115  		tc.SetParentID(*i)
   116  	}
   117  	return tc
   118  }
   119  
   120  // AddPostIDs adds the "posts" edge to the Post entity by IDs.
   121  func (tc *TopicCreate) AddPostIDs(ids ...int) *TopicCreate {
   122  	tc.mutation.AddPostIDs(ids...)
   123  	return tc
   124  }
   125  
   126  // AddPosts adds the "posts" edges to the Post entity.
   127  func (tc *TopicCreate) AddPosts(p ...*Post) *TopicCreate {
   128  	ids := make([]int, len(p))
   129  	for i := range p {
   130  		ids[i] = p[i].ID
   131  	}
   132  	return tc.AddPostIDs(ids...)
   133  }
   134  
   135  // AddChildIDs adds the "children" edge to the Topic entity by IDs.
   136  func (tc *TopicCreate) AddChildIDs(ids ...int) *TopicCreate {
   137  	tc.mutation.AddChildIDs(ids...)
   138  	return tc
   139  }
   140  
   141  // AddChildren adds the "children" edges to the Topic entity.
   142  func (tc *TopicCreate) AddChildren(t ...*Topic) *TopicCreate {
   143  	ids := make([]int, len(t))
   144  	for i := range t {
   145  		ids[i] = t[i].ID
   146  	}
   147  	return tc.AddChildIDs(ids...)
   148  }
   149  
   150  // SetParent sets the "parent" edge to the Topic entity.
   151  func (tc *TopicCreate) SetParent(t *Topic) *TopicCreate {
   152  	return tc.SetParentID(t.ID)
   153  }
   154  
   155  // Mutation returns the TopicMutation object of the builder.
   156  func (tc *TopicCreate) Mutation() *TopicMutation {
   157  	return tc.mutation
   158  }
   159  
   160  // Save creates the Topic in the database.
   161  func (tc *TopicCreate) Save(ctx context.Context) (*Topic, error) {
   162  	var (
   163  		err  error
   164  		node *Topic
   165  	)
   166  	tc.defaults()
   167  	if len(tc.hooks) == 0 {
   168  		if err = tc.check(); err != nil {
   169  			return nil, err
   170  		}
   171  		node, err = tc.sqlSave(ctx)
   172  	} else {
   173  		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
   174  			mutation, ok := m.(*TopicMutation)
   175  			if !ok {
   176  				return nil, fmt.Errorf("unexpected mutation type %T", m)
   177  			}
   178  			if err = tc.check(); err != nil {
   179  				return nil, err
   180  			}
   181  			tc.mutation = mutation
   182  			if node, err = tc.sqlSave(ctx); err != nil {
   183  				return nil, err
   184  			}
   185  			mutation.id = &node.ID
   186  			mutation.done = true
   187  			return node, err
   188  		})
   189  		for i := len(tc.hooks) - 1; i >= 0; i-- {
   190  			if tc.hooks[i] == nil {
   191  				return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
   192  			}
   193  			mut = tc.hooks[i](mut)
   194  		}
   195  		if _, err := mut.Mutate(ctx, tc.mutation); err != nil {
   196  			return nil, err
   197  		}
   198  	}
   199  	return node, err
   200  }
   201  
   202  // SaveX calls Save and panics if Save returns an error.
   203  func (tc *TopicCreate) SaveX(ctx context.Context) *Topic {
   204  	v, err := tc.Save(ctx)
   205  	if err != nil {
   206  		panic(err)
   207  	}
   208  	return v
   209  }
   210  
   211  // Exec executes the query.
   212  func (tc *TopicCreate) Exec(ctx context.Context) error {
   213  	_, err := tc.Save(ctx)
   214  	return err
   215  }
   216  
   217  // ExecX is like Exec, but panics if an error occurs.
   218  func (tc *TopicCreate) ExecX(ctx context.Context) {
   219  	if err := tc.Exec(ctx); err != nil {
   220  		panic(err)
   221  	}
   222  }
   223  
   224  // defaults sets the default values of the builder before save.
   225  func (tc *TopicCreate) defaults() {
   226  	if _, ok := tc.mutation.CreatedAt(); !ok {
   227  		v := topic.DefaultCreatedAt()
   228  		tc.mutation.SetCreatedAt(v)
   229  	}
   230  	if _, ok := tc.mutation.UpdatedAt(); !ok {
   231  		v := topic.DefaultUpdatedAt()
   232  		tc.mutation.SetUpdatedAt(v)
   233  	}
   234  }
   235  
   236  // check runs all checks and user-defined validators on the builder.
   237  func (tc *TopicCreate) check() error {
   238  	if _, ok := tc.mutation.CreatedAt(); !ok {
   239  		return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Topic.created_at"`)}
   240  	}
   241  	if _, ok := tc.mutation.UpdatedAt(); !ok {
   242  		return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Topic.updated_at"`)}
   243  	}
   244  	if _, ok := tc.mutation.Name(); !ok {
   245  		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Topic.name"`)}
   246  	}
   247  	if _, ok := tc.mutation.Slug(); !ok {
   248  		return &ValidationError{Name: "slug", err: errors.New(`ent: missing required field "Topic.slug"`)}
   249  	}
   250  	if _, ok := tc.mutation.Content(); !ok {
   251  		return &ValidationError{Name: "content", err: errors.New(`ent: missing required field "Topic.content"`)}
   252  	}
   253  	if _, ok := tc.mutation.ContentHTML(); !ok {
   254  		return &ValidationError{Name: "content_html", err: errors.New(`ent: missing required field "Topic.content_html"`)}
   255  	}
   256  	return nil
   257  }
   258  
   259  func (tc *TopicCreate) sqlSave(ctx context.Context) (*Topic, error) {
   260  	_node, _spec := tc.createSpec()
   261  	if err := sqlgraph.CreateNode(ctx, tc.driver, _spec); err != nil {
   262  		if sqlgraph.IsConstraintError(err) {
   263  			err = &ConstraintError{err.Error(), err}
   264  		}
   265  		return nil, err
   266  	}
   267  	id := _spec.ID.Value.(int64)
   268  	_node.ID = int(id)
   269  	return _node, nil
   270  }
   271  
   272  func (tc *TopicCreate) createSpec() (*Topic, *sqlgraph.CreateSpec) {
   273  	var (
   274  		_node = &Topic{config: tc.config}
   275  		_spec = &sqlgraph.CreateSpec{
   276  			Table: topic.Table,
   277  			ID: &sqlgraph.FieldSpec{
   278  				Type:   field.TypeInt,
   279  				Column: topic.FieldID,
   280  			},
   281  		}
   282  	)
   283  	_spec.OnConflict = tc.conflict
   284  	if value, ok := tc.mutation.CreatedAt(); ok {
   285  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   286  			Type:   field.TypeTime,
   287  			Value:  value,
   288  			Column: topic.FieldCreatedAt,
   289  		})
   290  		_node.CreatedAt = value
   291  	}
   292  	if value, ok := tc.mutation.UpdatedAt(); ok {
   293  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   294  			Type:   field.TypeTime,
   295  			Value:  value,
   296  			Column: topic.FieldUpdatedAt,
   297  		})
   298  		_node.UpdatedAt = value
   299  	}
   300  	if value, ok := tc.mutation.DeletedAt(); ok {
   301  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   302  			Type:   field.TypeTime,
   303  			Value:  value,
   304  			Column: topic.FieldDeletedAt,
   305  		})
   306  		_node.DeletedAt = value
   307  	}
   308  	if value, ok := tc.mutation.Name(); ok {
   309  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   310  			Type:   field.TypeString,
   311  			Value:  value,
   312  			Column: topic.FieldName,
   313  		})
   314  		_node.Name = value
   315  	}
   316  	if value, ok := tc.mutation.Slug(); ok {
   317  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   318  			Type:   field.TypeString,
   319  			Value:  value,
   320  			Column: topic.FieldSlug,
   321  		})
   322  		_node.Slug = value
   323  	}
   324  	if value, ok := tc.mutation.Description(); ok {
   325  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   326  			Type:   field.TypeString,
   327  			Value:  value,
   328  			Column: topic.FieldDescription,
   329  		})
   330  		_node.Description = value
   331  	}
   332  	if value, ok := tc.mutation.Content(); ok {
   333  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   334  			Type:   field.TypeString,
   335  			Value:  value,
   336  			Column: topic.FieldContent,
   337  		})
   338  		_node.Content = value
   339  	}
   340  	if value, ok := tc.mutation.ContentHTML(); ok {
   341  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   342  			Type:   field.TypeString,
   343  			Value:  value,
   344  			Column: topic.FieldContentHTML,
   345  		})
   346  		_node.ContentHTML = value
   347  	}
   348  	if nodes := tc.mutation.PostsIDs(); len(nodes) > 0 {
   349  		edge := &sqlgraph.EdgeSpec{
   350  			Rel:     sqlgraph.M2M,
   351  			Inverse: false,
   352  			Table:   topic.PostsTable,
   353  			Columns: topic.PostsPrimaryKey,
   354  			Bidi:    false,
   355  			Target: &sqlgraph.EdgeTarget{
   356  				IDSpec: &sqlgraph.FieldSpec{
   357  					Type:   field.TypeInt,
   358  					Column: post.FieldID,
   359  				},
   360  			},
   361  		}
   362  		for _, k := range nodes {
   363  			edge.Target.Nodes = append(edge.Target.Nodes, k)
   364  		}
   365  		_spec.Edges = append(_spec.Edges, edge)
   366  	}
   367  	if nodes := tc.mutation.ChildrenIDs(); len(nodes) > 0 {
   368  		edge := &sqlgraph.EdgeSpec{
   369  			Rel:     sqlgraph.O2M,
   370  			Inverse: false,
   371  			Table:   topic.ChildrenTable,
   372  			Columns: []string{topic.ChildrenColumn},
   373  			Bidi:    true,
   374  			Target: &sqlgraph.EdgeTarget{
   375  				IDSpec: &sqlgraph.FieldSpec{
   376  					Type:   field.TypeInt,
   377  					Column: topic.FieldID,
   378  				},
   379  			},
   380  		}
   381  		for _, k := range nodes {
   382  			edge.Target.Nodes = append(edge.Target.Nodes, k)
   383  		}
   384  		_spec.Edges = append(_spec.Edges, edge)
   385  	}
   386  	if nodes := tc.mutation.ParentIDs(); len(nodes) > 0 {
   387  		edge := &sqlgraph.EdgeSpec{
   388  			Rel:     sqlgraph.M2O,
   389  			Inverse: true,
   390  			Table:   topic.ParentTable,
   391  			Columns: []string{topic.ParentColumn},
   392  			Bidi:    false,
   393  			Target: &sqlgraph.EdgeTarget{
   394  				IDSpec: &sqlgraph.FieldSpec{
   395  					Type:   field.TypeInt,
   396  					Column: topic.FieldID,
   397  				},
   398  			},
   399  		}
   400  		for _, k := range nodes {
   401  			edge.Target.Nodes = append(edge.Target.Nodes, k)
   402  		}
   403  		_node.ParentID = nodes[0]
   404  		_spec.Edges = append(_spec.Edges, edge)
   405  	}
   406  	return _node, _spec
   407  }
   408  
   409  // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
   410  // of the `INSERT` statement. For example:
   411  //
   412  //	client.Topic.Create().
   413  //		SetCreatedAt(v).
   414  //		OnConflict(
   415  //			// Update the row with the new values
   416  //			// the was proposed for insertion.
   417  //			sql.ResolveWithNewValues(),
   418  //		).
   419  //		// Override some of the fields with custom
   420  //		// update values.
   421  //		Update(func(u *ent.TopicUpsert) {
   422  //			SetCreatedAt(v+v).
   423  //		}).
   424  //		Exec(ctx)
   425  //
   426  func (tc *TopicCreate) OnConflict(opts ...sql.ConflictOption) *TopicUpsertOne {
   427  	tc.conflict = opts
   428  	return &TopicUpsertOne{
   429  		create: tc,
   430  	}
   431  }
   432  
   433  // OnConflictColumns calls `OnConflict` and configures the columns
   434  // as conflict target. Using this option is equivalent to using:
   435  //
   436  //	client.Topic.Create().
   437  //		OnConflict(sql.ConflictColumns(columns...)).
   438  //		Exec(ctx)
   439  //
   440  func (tc *TopicCreate) OnConflictColumns(columns ...string) *TopicUpsertOne {
   441  	tc.conflict = append(tc.conflict, sql.ConflictColumns(columns...))
   442  	return &TopicUpsertOne{
   443  		create: tc,
   444  	}
   445  }
   446  
   447  type (
   448  	// TopicUpsertOne is the builder for "upsert"-ing
   449  	//  one Topic node.
   450  	TopicUpsertOne struct {
   451  		create *TopicCreate
   452  	}
   453  
   454  	// TopicUpsert is the "OnConflict" setter.
   455  	TopicUpsert struct {
   456  		*sql.UpdateSet
   457  	}
   458  )
   459  
   460  // SetCreatedAt sets the "created_at" field.
   461  func (u *TopicUpsert) SetCreatedAt(v time.Time) *TopicUpsert {
   462  	u.Set(topic.FieldCreatedAt, v)
   463  	return u
   464  }
   465  
   466  // UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
   467  func (u *TopicUpsert) UpdateCreatedAt() *TopicUpsert {
   468  	u.SetExcluded(topic.FieldCreatedAt)
   469  	return u
   470  }
   471  
   472  // SetUpdatedAt sets the "updated_at" field.
   473  func (u *TopicUpsert) SetUpdatedAt(v time.Time) *TopicUpsert {
   474  	u.Set(topic.FieldUpdatedAt, v)
   475  	return u
   476  }
   477  
   478  // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
   479  func (u *TopicUpsert) UpdateUpdatedAt() *TopicUpsert {
   480  	u.SetExcluded(topic.FieldUpdatedAt)
   481  	return u
   482  }
   483  
   484  // SetDeletedAt sets the "deleted_at" field.
   485  func (u *TopicUpsert) SetDeletedAt(v time.Time) *TopicUpsert {
   486  	u.Set(topic.FieldDeletedAt, v)
   487  	return u
   488  }
   489  
   490  // UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
   491  func (u *TopicUpsert) UpdateDeletedAt() *TopicUpsert {
   492  	u.SetExcluded(topic.FieldDeletedAt)
   493  	return u
   494  }
   495  
   496  // ClearDeletedAt clears the value of the "deleted_at" field.
   497  func (u *TopicUpsert) ClearDeletedAt() *TopicUpsert {
   498  	u.SetNull(topic.FieldDeletedAt)
   499  	return u
   500  }
   501  
   502  // SetName sets the "name" field.
   503  func (u *TopicUpsert) SetName(v string) *TopicUpsert {
   504  	u.Set(topic.FieldName, v)
   505  	return u
   506  }
   507  
   508  // UpdateName sets the "name" field to the value that was provided on create.
   509  func (u *TopicUpsert) UpdateName() *TopicUpsert {
   510  	u.SetExcluded(topic.FieldName)
   511  	return u
   512  }
   513  
   514  // SetSlug sets the "slug" field.
   515  func (u *TopicUpsert) SetSlug(v string) *TopicUpsert {
   516  	u.Set(topic.FieldSlug, v)
   517  	return u
   518  }
   519  
   520  // UpdateSlug sets the "slug" field to the value that was provided on create.
   521  func (u *TopicUpsert) UpdateSlug() *TopicUpsert {
   522  	u.SetExcluded(topic.FieldSlug)
   523  	return u
   524  }
   525  
   526  // SetDescription sets the "description" field.
   527  func (u *TopicUpsert) SetDescription(v string) *TopicUpsert {
   528  	u.Set(topic.FieldDescription, v)
   529  	return u
   530  }
   531  
   532  // UpdateDescription sets the "description" field to the value that was provided on create.
   533  func (u *TopicUpsert) UpdateDescription() *TopicUpsert {
   534  	u.SetExcluded(topic.FieldDescription)
   535  	return u
   536  }
   537  
   538  // ClearDescription clears the value of the "description" field.
   539  func (u *TopicUpsert) ClearDescription() *TopicUpsert {
   540  	u.SetNull(topic.FieldDescription)
   541  	return u
   542  }
   543  
   544  // SetContent sets the "content" field.
   545  func (u *TopicUpsert) SetContent(v string) *TopicUpsert {
   546  	u.Set(topic.FieldContent, v)
   547  	return u
   548  }
   549  
   550  // UpdateContent sets the "content" field to the value that was provided on create.
   551  func (u *TopicUpsert) UpdateContent() *TopicUpsert {
   552  	u.SetExcluded(topic.FieldContent)
   553  	return u
   554  }
   555  
   556  // SetContentHTML sets the "content_html" field.
   557  func (u *TopicUpsert) SetContentHTML(v string) *TopicUpsert {
   558  	u.Set(topic.FieldContentHTML, v)
   559  	return u
   560  }
   561  
   562  // UpdateContentHTML sets the "content_html" field to the value that was provided on create.
   563  func (u *TopicUpsert) UpdateContentHTML() *TopicUpsert {
   564  	u.SetExcluded(topic.FieldContentHTML)
   565  	return u
   566  }
   567  
   568  // SetParentID sets the "parent_id" field.
   569  func (u *TopicUpsert) SetParentID(v int) *TopicUpsert {
   570  	u.Set(topic.FieldParentID, v)
   571  	return u
   572  }
   573  
   574  // UpdateParentID sets the "parent_id" field to the value that was provided on create.
   575  func (u *TopicUpsert) UpdateParentID() *TopicUpsert {
   576  	u.SetExcluded(topic.FieldParentID)
   577  	return u
   578  }
   579  
   580  // ClearParentID clears the value of the "parent_id" field.
   581  func (u *TopicUpsert) ClearParentID() *TopicUpsert {
   582  	u.SetNull(topic.FieldParentID)
   583  	return u
   584  }
   585  
   586  // UpdateNewValues updates the mutable fields using the new values that were set on create.
   587  // Using this option is equivalent to using:
   588  //
   589  //	client.Topic.Create().
   590  //		OnConflict(
   591  //			sql.ResolveWithNewValues(),
   592  //		).
   593  //		Exec(ctx)
   594  //
   595  func (u *TopicUpsertOne) UpdateNewValues() *TopicUpsertOne {
   596  	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
   597  	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
   598  		if _, exists := u.create.mutation.CreatedAt(); exists {
   599  			s.SetIgnore(topic.FieldCreatedAt)
   600  		}
   601  	}))
   602  	return u
   603  }
   604  
   605  // Ignore sets each column to itself in case of conflict.
   606  // Using this option is equivalent to using:
   607  //
   608  //  client.Topic.Create().
   609  //      OnConflict(sql.ResolveWithIgnore()).
   610  //      Exec(ctx)
   611  //
   612  func (u *TopicUpsertOne) Ignore() *TopicUpsertOne {
   613  	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
   614  	return u
   615  }
   616  
   617  // DoNothing configures the conflict_action to `DO NOTHING`.
   618  // Supported only by SQLite and PostgreSQL.
   619  func (u *TopicUpsertOne) DoNothing() *TopicUpsertOne {
   620  	u.create.conflict = append(u.create.conflict, sql.DoNothing())
   621  	return u
   622  }
   623  
   624  // Update allows overriding fields `UPDATE` values. See the TopicCreate.OnConflict
   625  // documentation for more info.
   626  func (u *TopicUpsertOne) Update(set func(*TopicUpsert)) *TopicUpsertOne {
   627  	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
   628  		set(&TopicUpsert{UpdateSet: update})
   629  	}))
   630  	return u
   631  }
   632  
   633  // SetCreatedAt sets the "created_at" field.
   634  func (u *TopicUpsertOne) SetCreatedAt(v time.Time) *TopicUpsertOne {
   635  	return u.Update(func(s *TopicUpsert) {
   636  		s.SetCreatedAt(v)
   637  	})
   638  }
   639  
   640  // UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
   641  func (u *TopicUpsertOne) UpdateCreatedAt() *TopicUpsertOne {
   642  	return u.Update(func(s *TopicUpsert) {
   643  		s.UpdateCreatedAt()
   644  	})
   645  }
   646  
   647  // SetUpdatedAt sets the "updated_at" field.
   648  func (u *TopicUpsertOne) SetUpdatedAt(v time.Time) *TopicUpsertOne {
   649  	return u.Update(func(s *TopicUpsert) {
   650  		s.SetUpdatedAt(v)
   651  	})
   652  }
   653  
   654  // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
   655  func (u *TopicUpsertOne) UpdateUpdatedAt() *TopicUpsertOne {
   656  	return u.Update(func(s *TopicUpsert) {
   657  		s.UpdateUpdatedAt()
   658  	})
   659  }
   660  
   661  // SetDeletedAt sets the "deleted_at" field.
   662  func (u *TopicUpsertOne) SetDeletedAt(v time.Time) *TopicUpsertOne {
   663  	return u.Update(func(s *TopicUpsert) {
   664  		s.SetDeletedAt(v)
   665  	})
   666  }
   667  
   668  // UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
   669  func (u *TopicUpsertOne) UpdateDeletedAt() *TopicUpsertOne {
   670  	return u.Update(func(s *TopicUpsert) {
   671  		s.UpdateDeletedAt()
   672  	})
   673  }
   674  
   675  // ClearDeletedAt clears the value of the "deleted_at" field.
   676  func (u *TopicUpsertOne) ClearDeletedAt() *TopicUpsertOne {
   677  	return u.Update(func(s *TopicUpsert) {
   678  		s.ClearDeletedAt()
   679  	})
   680  }
   681  
   682  // SetName sets the "name" field.
   683  func (u *TopicUpsertOne) SetName(v string) *TopicUpsertOne {
   684  	return u.Update(func(s *TopicUpsert) {
   685  		s.SetName(v)
   686  	})
   687  }
   688  
   689  // UpdateName sets the "name" field to the value that was provided on create.
   690  func (u *TopicUpsertOne) UpdateName() *TopicUpsertOne {
   691  	return u.Update(func(s *TopicUpsert) {
   692  		s.UpdateName()
   693  	})
   694  }
   695  
   696  // SetSlug sets the "slug" field.
   697  func (u *TopicUpsertOne) SetSlug(v string) *TopicUpsertOne {
   698  	return u.Update(func(s *TopicUpsert) {
   699  		s.SetSlug(v)
   700  	})
   701  }
   702  
   703  // UpdateSlug sets the "slug" field to the value that was provided on create.
   704  func (u *TopicUpsertOne) UpdateSlug() *TopicUpsertOne {
   705  	return u.Update(func(s *TopicUpsert) {
   706  		s.UpdateSlug()
   707  	})
   708  }
   709  
   710  // SetDescription sets the "description" field.
   711  func (u *TopicUpsertOne) SetDescription(v string) *TopicUpsertOne {
   712  	return u.Update(func(s *TopicUpsert) {
   713  		s.SetDescription(v)
   714  	})
   715  }
   716  
   717  // UpdateDescription sets the "description" field to the value that was provided on create.
   718  func (u *TopicUpsertOne) UpdateDescription() *TopicUpsertOne {
   719  	return u.Update(func(s *TopicUpsert) {
   720  		s.UpdateDescription()
   721  	})
   722  }
   723  
   724  // ClearDescription clears the value of the "description" field.
   725  func (u *TopicUpsertOne) ClearDescription() *TopicUpsertOne {
   726  	return u.Update(func(s *TopicUpsert) {
   727  		s.ClearDescription()
   728  	})
   729  }
   730  
   731  // SetContent sets the "content" field.
   732  func (u *TopicUpsertOne) SetContent(v string) *TopicUpsertOne {
   733  	return u.Update(func(s *TopicUpsert) {
   734  		s.SetContent(v)
   735  	})
   736  }
   737  
   738  // UpdateContent sets the "content" field to the value that was provided on create.
   739  func (u *TopicUpsertOne) UpdateContent() *TopicUpsertOne {
   740  	return u.Update(func(s *TopicUpsert) {
   741  		s.UpdateContent()
   742  	})
   743  }
   744  
   745  // SetContentHTML sets the "content_html" field.
   746  func (u *TopicUpsertOne) SetContentHTML(v string) *TopicUpsertOne {
   747  	return u.Update(func(s *TopicUpsert) {
   748  		s.SetContentHTML(v)
   749  	})
   750  }
   751  
   752  // UpdateContentHTML sets the "content_html" field to the value that was provided on create.
   753  func (u *TopicUpsertOne) UpdateContentHTML() *TopicUpsertOne {
   754  	return u.Update(func(s *TopicUpsert) {
   755  		s.UpdateContentHTML()
   756  	})
   757  }
   758  
   759  // SetParentID sets the "parent_id" field.
   760  func (u *TopicUpsertOne) SetParentID(v int) *TopicUpsertOne {
   761  	return u.Update(func(s *TopicUpsert) {
   762  		s.SetParentID(v)
   763  	})
   764  }
   765  
   766  // UpdateParentID sets the "parent_id" field to the value that was provided on create.
   767  func (u *TopicUpsertOne) UpdateParentID() *TopicUpsertOne {
   768  	return u.Update(func(s *TopicUpsert) {
   769  		s.UpdateParentID()
   770  	})
   771  }
   772  
   773  // ClearParentID clears the value of the "parent_id" field.
   774  func (u *TopicUpsertOne) ClearParentID() *TopicUpsertOne {
   775  	return u.Update(func(s *TopicUpsert) {
   776  		s.ClearParentID()
   777  	})
   778  }
   779  
   780  // Exec executes the query.
   781  func (u *TopicUpsertOne) Exec(ctx context.Context) error {
   782  	if len(u.create.conflict) == 0 {
   783  		return errors.New("ent: missing options for TopicCreate.OnConflict")
   784  	}
   785  	return u.create.Exec(ctx)
   786  }
   787  
   788  // ExecX is like Exec, but panics if an error occurs.
   789  func (u *TopicUpsertOne) ExecX(ctx context.Context) {
   790  	if err := u.create.Exec(ctx); err != nil {
   791  		panic(err)
   792  	}
   793  }
   794  
   795  // Exec executes the UPSERT query and returns the inserted/updated ID.
   796  func (u *TopicUpsertOne) ID(ctx context.Context) (id int, err error) {
   797  	node, err := u.create.Save(ctx)
   798  	if err != nil {
   799  		return id, err
   800  	}
   801  	return node.ID, nil
   802  }
   803  
   804  // IDX is like ID, but panics if an error occurs.
   805  func (u *TopicUpsertOne) IDX(ctx context.Context) int {
   806  	id, err := u.ID(ctx)
   807  	if err != nil {
   808  		panic(err)
   809  	}
   810  	return id
   811  }
   812  
   813  // TopicCreateBulk is the builder for creating many Topic entities in bulk.
   814  type TopicCreateBulk struct {
   815  	config
   816  	builders []*TopicCreate
   817  	conflict []sql.ConflictOption
   818  }
   819  
   820  // Save creates the Topic entities in the database.
   821  func (tcb *TopicCreateBulk) Save(ctx context.Context) ([]*Topic, error) {
   822  	specs := make([]*sqlgraph.CreateSpec, len(tcb.builders))
   823  	nodes := make([]*Topic, len(tcb.builders))
   824  	mutators := make([]Mutator, len(tcb.builders))
   825  	for i := range tcb.builders {
   826  		func(i int, root context.Context) {
   827  			builder := tcb.builders[i]
   828  			builder.defaults()
   829  			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
   830  				mutation, ok := m.(*TopicMutation)
   831  				if !ok {
   832  					return nil, fmt.Errorf("unexpected mutation type %T", m)
   833  				}
   834  				if err := builder.check(); err != nil {
   835  					return nil, err
   836  				}
   837  				builder.mutation = mutation
   838  				nodes[i], specs[i] = builder.createSpec()
   839  				var err error
   840  				if i < len(mutators)-1 {
   841  					_, err = mutators[i+1].Mutate(root, tcb.builders[i+1].mutation)
   842  				} else {
   843  					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
   844  					spec.OnConflict = tcb.conflict
   845  					// Invoke the actual operation on the latest mutation in the chain.
   846  					if err = sqlgraph.BatchCreate(ctx, tcb.driver, spec); err != nil {
   847  						if sqlgraph.IsConstraintError(err) {
   848  							err = &ConstraintError{err.Error(), err}
   849  						}
   850  					}
   851  				}
   852  				if err != nil {
   853  					return nil, err
   854  				}
   855  				mutation.id = &nodes[i].ID
   856  				mutation.done = true
   857  				if specs[i].ID.Value != nil {
   858  					id := specs[i].ID.Value.(int64)
   859  					nodes[i].ID = int(id)
   860  				}
   861  				return nodes[i], nil
   862  			})
   863  			for i := len(builder.hooks) - 1; i >= 0; i-- {
   864  				mut = builder.hooks[i](mut)
   865  			}
   866  			mutators[i] = mut
   867  		}(i, ctx)
   868  	}
   869  	if len(mutators) > 0 {
   870  		if _, err := mutators[0].Mutate(ctx, tcb.builders[0].mutation); err != nil {
   871  			return nil, err
   872  		}
   873  	}
   874  	return nodes, nil
   875  }
   876  
   877  // SaveX is like Save, but panics if an error occurs.
   878  func (tcb *TopicCreateBulk) SaveX(ctx context.Context) []*Topic {
   879  	v, err := tcb.Save(ctx)
   880  	if err != nil {
   881  		panic(err)
   882  	}
   883  	return v
   884  }
   885  
   886  // Exec executes the query.
   887  func (tcb *TopicCreateBulk) Exec(ctx context.Context) error {
   888  	_, err := tcb.Save(ctx)
   889  	return err
   890  }
   891  
   892  // ExecX is like Exec, but panics if an error occurs.
   893  func (tcb *TopicCreateBulk) ExecX(ctx context.Context) {
   894  	if err := tcb.Exec(ctx); err != nil {
   895  		panic(err)
   896  	}
   897  }
   898  
   899  // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
   900  // of the `INSERT` statement. For example:
   901  //
   902  //	client.Topic.CreateBulk(builders...).
   903  //		OnConflict(
   904  //			// Update the row with the new values
   905  //			// the was proposed for insertion.
   906  //			sql.ResolveWithNewValues(),
   907  //		).
   908  //		// Override some of the fields with custom
   909  //		// update values.
   910  //		Update(func(u *ent.TopicUpsert) {
   911  //			SetCreatedAt(v+v).
   912  //		}).
   913  //		Exec(ctx)
   914  //
   915  func (tcb *TopicCreateBulk) OnConflict(opts ...sql.ConflictOption) *TopicUpsertBulk {
   916  	tcb.conflict = opts
   917  	return &TopicUpsertBulk{
   918  		create: tcb,
   919  	}
   920  }
   921  
   922  // OnConflictColumns calls `OnConflict` and configures the columns
   923  // as conflict target. Using this option is equivalent to using:
   924  //
   925  //	client.Topic.Create().
   926  //		OnConflict(sql.ConflictColumns(columns...)).
   927  //		Exec(ctx)
   928  //
   929  func (tcb *TopicCreateBulk) OnConflictColumns(columns ...string) *TopicUpsertBulk {
   930  	tcb.conflict = append(tcb.conflict, sql.ConflictColumns(columns...))
   931  	return &TopicUpsertBulk{
   932  		create: tcb,
   933  	}
   934  }
   935  
   936  // TopicUpsertBulk is the builder for "upsert"-ing
   937  // a bulk of Topic nodes.
   938  type TopicUpsertBulk struct {
   939  	create *TopicCreateBulk
   940  }
   941  
   942  // UpdateNewValues updates the mutable fields using the new values that
   943  // were set on create. Using this option is equivalent to using:
   944  //
   945  //	client.Topic.Create().
   946  //		OnConflict(
   947  //			sql.ResolveWithNewValues(),
   948  //		).
   949  //		Exec(ctx)
   950  //
   951  func (u *TopicUpsertBulk) UpdateNewValues() *TopicUpsertBulk {
   952  	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
   953  	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
   954  		for _, b := range u.create.builders {
   955  			if _, exists := b.mutation.CreatedAt(); exists {
   956  				s.SetIgnore(topic.FieldCreatedAt)
   957  			}
   958  		}
   959  	}))
   960  	return u
   961  }
   962  
   963  // Ignore sets each column to itself in case of conflict.
   964  // Using this option is equivalent to using:
   965  //
   966  //	client.Topic.Create().
   967  //		OnConflict(sql.ResolveWithIgnore()).
   968  //		Exec(ctx)
   969  //
   970  func (u *TopicUpsertBulk) Ignore() *TopicUpsertBulk {
   971  	u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
   972  	return u
   973  }
   974  
   975  // DoNothing configures the conflict_action to `DO NOTHING`.
   976  // Supported only by SQLite and PostgreSQL.
   977  func (u *TopicUpsertBulk) DoNothing() *TopicUpsertBulk {
   978  	u.create.conflict = append(u.create.conflict, sql.DoNothing())
   979  	return u
   980  }
   981  
   982  // Update allows overriding fields `UPDATE` values. See the TopicCreateBulk.OnConflict
   983  // documentation for more info.
   984  func (u *TopicUpsertBulk) Update(set func(*TopicUpsert)) *TopicUpsertBulk {
   985  	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
   986  		set(&TopicUpsert{UpdateSet: update})
   987  	}))
   988  	return u
   989  }
   990  
   991  // SetCreatedAt sets the "created_at" field.
   992  func (u *TopicUpsertBulk) SetCreatedAt(v time.Time) *TopicUpsertBulk {
   993  	return u.Update(func(s *TopicUpsert) {
   994  		s.SetCreatedAt(v)
   995  	})
   996  }
   997  
   998  // UpdateCreatedAt sets the "created_at" field to the value that was provided on create.
   999  func (u *TopicUpsertBulk) UpdateCreatedAt() *TopicUpsertBulk {
  1000  	return u.Update(func(s *TopicUpsert) {
  1001  		s.UpdateCreatedAt()
  1002  	})
  1003  }
  1004  
  1005  // SetUpdatedAt sets the "updated_at" field.
  1006  func (u *TopicUpsertBulk) SetUpdatedAt(v time.Time) *TopicUpsertBulk {
  1007  	return u.Update(func(s *TopicUpsert) {
  1008  		s.SetUpdatedAt(v)
  1009  	})
  1010  }
  1011  
  1012  // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
  1013  func (u *TopicUpsertBulk) UpdateUpdatedAt() *TopicUpsertBulk {
  1014  	return u.Update(func(s *TopicUpsert) {
  1015  		s.UpdateUpdatedAt()
  1016  	})
  1017  }
  1018  
  1019  // SetDeletedAt sets the "deleted_at" field.
  1020  func (u *TopicUpsertBulk) SetDeletedAt(v time.Time) *TopicUpsertBulk {
  1021  	return u.Update(func(s *TopicUpsert) {
  1022  		s.SetDeletedAt(v)
  1023  	})
  1024  }
  1025  
  1026  // UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
  1027  func (u *TopicUpsertBulk) UpdateDeletedAt() *TopicUpsertBulk {
  1028  	return u.Update(func(s *TopicUpsert) {
  1029  		s.UpdateDeletedAt()
  1030  	})
  1031  }
  1032  
  1033  // ClearDeletedAt clears the value of the "deleted_at" field.
  1034  func (u *TopicUpsertBulk) ClearDeletedAt() *TopicUpsertBulk {
  1035  	return u.Update(func(s *TopicUpsert) {
  1036  		s.ClearDeletedAt()
  1037  	})
  1038  }
  1039  
  1040  // SetName sets the "name" field.
  1041  func (u *TopicUpsertBulk) SetName(v string) *TopicUpsertBulk {
  1042  	return u.Update(func(s *TopicUpsert) {
  1043  		s.SetName(v)
  1044  	})
  1045  }
  1046  
  1047  // UpdateName sets the "name" field to the value that was provided on create.
  1048  func (u *TopicUpsertBulk) UpdateName() *TopicUpsertBulk {
  1049  	return u.Update(func(s *TopicUpsert) {
  1050  		s.UpdateName()
  1051  	})
  1052  }
  1053  
  1054  // SetSlug sets the "slug" field.
  1055  func (u *TopicUpsertBulk) SetSlug(v string) *TopicUpsertBulk {
  1056  	return u.Update(func(s *TopicUpsert) {
  1057  		s.SetSlug(v)
  1058  	})
  1059  }
  1060  
  1061  // UpdateSlug sets the "slug" field to the value that was provided on create.
  1062  func (u *TopicUpsertBulk) UpdateSlug() *TopicUpsertBulk {
  1063  	return u.Update(func(s *TopicUpsert) {
  1064  		s.UpdateSlug()
  1065  	})
  1066  }
  1067  
  1068  // SetDescription sets the "description" field.
  1069  func (u *TopicUpsertBulk) SetDescription(v string) *TopicUpsertBulk {
  1070  	return u.Update(func(s *TopicUpsert) {
  1071  		s.SetDescription(v)
  1072  	})
  1073  }
  1074  
  1075  // UpdateDescription sets the "description" field to the value that was provided on create.
  1076  func (u *TopicUpsertBulk) UpdateDescription() *TopicUpsertBulk {
  1077  	return u.Update(func(s *TopicUpsert) {
  1078  		s.UpdateDescription()
  1079  	})
  1080  }
  1081  
  1082  // ClearDescription clears the value of the "description" field.
  1083  func (u *TopicUpsertBulk) ClearDescription() *TopicUpsertBulk {
  1084  	return u.Update(func(s *TopicUpsert) {
  1085  		s.ClearDescription()
  1086  	})
  1087  }
  1088  
  1089  // SetContent sets the "content" field.
  1090  func (u *TopicUpsertBulk) SetContent(v string) *TopicUpsertBulk {
  1091  	return u.Update(func(s *TopicUpsert) {
  1092  		s.SetContent(v)
  1093  	})
  1094  }
  1095  
  1096  // UpdateContent sets the "content" field to the value that was provided on create.
  1097  func (u *TopicUpsertBulk) UpdateContent() *TopicUpsertBulk {
  1098  	return u.Update(func(s *TopicUpsert) {
  1099  		s.UpdateContent()
  1100  	})
  1101  }
  1102  
  1103  // SetContentHTML sets the "content_html" field.
  1104  func (u *TopicUpsertBulk) SetContentHTML(v string) *TopicUpsertBulk {
  1105  	return u.Update(func(s *TopicUpsert) {
  1106  		s.SetContentHTML(v)
  1107  	})
  1108  }
  1109  
  1110  // UpdateContentHTML sets the "content_html" field to the value that was provided on create.
  1111  func (u *TopicUpsertBulk) UpdateContentHTML() *TopicUpsertBulk {
  1112  	return u.Update(func(s *TopicUpsert) {
  1113  		s.UpdateContentHTML()
  1114  	})
  1115  }
  1116  
  1117  // SetParentID sets the "parent_id" field.
  1118  func (u *TopicUpsertBulk) SetParentID(v int) *TopicUpsertBulk {
  1119  	return u.Update(func(s *TopicUpsert) {
  1120  		s.SetParentID(v)
  1121  	})
  1122  }
  1123  
  1124  // UpdateParentID sets the "parent_id" field to the value that was provided on create.
  1125  func (u *TopicUpsertBulk) UpdateParentID() *TopicUpsertBulk {
  1126  	return u.Update(func(s *TopicUpsert) {
  1127  		s.UpdateParentID()
  1128  	})
  1129  }
  1130  
  1131  // ClearParentID clears the value of the "parent_id" field.
  1132  func (u *TopicUpsertBulk) ClearParentID() *TopicUpsertBulk {
  1133  	return u.Update(func(s *TopicUpsert) {
  1134  		s.ClearParentID()
  1135  	})
  1136  }
  1137  
  1138  // Exec executes the query.
  1139  func (u *TopicUpsertBulk) Exec(ctx context.Context) error {
  1140  	for i, b := range u.create.builders {
  1141  		if len(b.conflict) != 0 {
  1142  			return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the TopicCreateBulk instead", i)
  1143  		}
  1144  	}
  1145  	if len(u.create.conflict) == 0 {
  1146  		return errors.New("ent: missing options for TopicCreateBulk.OnConflict")
  1147  	}
  1148  	return u.create.Exec(ctx)
  1149  }
  1150  
  1151  // ExecX is like Exec, but panics if an error occurs.
  1152  func (u *TopicUpsertBulk) ExecX(ctx context.Context) {
  1153  	if err := u.create.Exec(ctx); err != nil {
  1154  		panic(err)
  1155  	}
  1156  }