github.com/ngocphuongnb/tetua@v0.0.7-alpha/packages/entrepository/ent/topic_update.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/predicate"
    16  	"github.com/ngocphuongnb/tetua/packages/entrepository/ent/topic"
    17  )
    18  
    19  // TopicUpdate is the builder for updating Topic entities.
    20  type TopicUpdate struct {
    21  	config
    22  	hooks    []Hook
    23  	mutation *TopicMutation
    24  }
    25  
    26  // Where appends a list predicates to the TopicUpdate builder.
    27  func (tu *TopicUpdate) Where(ps ...predicate.Topic) *TopicUpdate {
    28  	tu.mutation.Where(ps...)
    29  	return tu
    30  }
    31  
    32  // SetUpdatedAt sets the "updated_at" field.
    33  func (tu *TopicUpdate) SetUpdatedAt(t time.Time) *TopicUpdate {
    34  	tu.mutation.SetUpdatedAt(t)
    35  	return tu
    36  }
    37  
    38  // SetDeletedAt sets the "deleted_at" field.
    39  func (tu *TopicUpdate) SetDeletedAt(t time.Time) *TopicUpdate {
    40  	tu.mutation.SetDeletedAt(t)
    41  	return tu
    42  }
    43  
    44  // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
    45  func (tu *TopicUpdate) SetNillableDeletedAt(t *time.Time) *TopicUpdate {
    46  	if t != nil {
    47  		tu.SetDeletedAt(*t)
    48  	}
    49  	return tu
    50  }
    51  
    52  // ClearDeletedAt clears the value of the "deleted_at" field.
    53  func (tu *TopicUpdate) ClearDeletedAt() *TopicUpdate {
    54  	tu.mutation.ClearDeletedAt()
    55  	return tu
    56  }
    57  
    58  // SetName sets the "name" field.
    59  func (tu *TopicUpdate) SetName(s string) *TopicUpdate {
    60  	tu.mutation.SetName(s)
    61  	return tu
    62  }
    63  
    64  // SetSlug sets the "slug" field.
    65  func (tu *TopicUpdate) SetSlug(s string) *TopicUpdate {
    66  	tu.mutation.SetSlug(s)
    67  	return tu
    68  }
    69  
    70  // SetDescription sets the "description" field.
    71  func (tu *TopicUpdate) SetDescription(s string) *TopicUpdate {
    72  	tu.mutation.SetDescription(s)
    73  	return tu
    74  }
    75  
    76  // SetNillableDescription sets the "description" field if the given value is not nil.
    77  func (tu *TopicUpdate) SetNillableDescription(s *string) *TopicUpdate {
    78  	if s != nil {
    79  		tu.SetDescription(*s)
    80  	}
    81  	return tu
    82  }
    83  
    84  // ClearDescription clears the value of the "description" field.
    85  func (tu *TopicUpdate) ClearDescription() *TopicUpdate {
    86  	tu.mutation.ClearDescription()
    87  	return tu
    88  }
    89  
    90  // SetContent sets the "content" field.
    91  func (tu *TopicUpdate) SetContent(s string) *TopicUpdate {
    92  	tu.mutation.SetContent(s)
    93  	return tu
    94  }
    95  
    96  // SetContentHTML sets the "content_html" field.
    97  func (tu *TopicUpdate) SetContentHTML(s string) *TopicUpdate {
    98  	tu.mutation.SetContentHTML(s)
    99  	return tu
   100  }
   101  
   102  // SetParentID sets the "parent_id" field.
   103  func (tu *TopicUpdate) SetParentID(i int) *TopicUpdate {
   104  	tu.mutation.SetParentID(i)
   105  	return tu
   106  }
   107  
   108  // SetNillableParentID sets the "parent_id" field if the given value is not nil.
   109  func (tu *TopicUpdate) SetNillableParentID(i *int) *TopicUpdate {
   110  	if i != nil {
   111  		tu.SetParentID(*i)
   112  	}
   113  	return tu
   114  }
   115  
   116  // ClearParentID clears the value of the "parent_id" field.
   117  func (tu *TopicUpdate) ClearParentID() *TopicUpdate {
   118  	tu.mutation.ClearParentID()
   119  	return tu
   120  }
   121  
   122  // AddPostIDs adds the "posts" edge to the Post entity by IDs.
   123  func (tu *TopicUpdate) AddPostIDs(ids ...int) *TopicUpdate {
   124  	tu.mutation.AddPostIDs(ids...)
   125  	return tu
   126  }
   127  
   128  // AddPosts adds the "posts" edges to the Post entity.
   129  func (tu *TopicUpdate) AddPosts(p ...*Post) *TopicUpdate {
   130  	ids := make([]int, len(p))
   131  	for i := range p {
   132  		ids[i] = p[i].ID
   133  	}
   134  	return tu.AddPostIDs(ids...)
   135  }
   136  
   137  // AddChildIDs adds the "children" edge to the Topic entity by IDs.
   138  func (tu *TopicUpdate) AddChildIDs(ids ...int) *TopicUpdate {
   139  	tu.mutation.AddChildIDs(ids...)
   140  	return tu
   141  }
   142  
   143  // AddChildren adds the "children" edges to the Topic entity.
   144  func (tu *TopicUpdate) AddChildren(t ...*Topic) *TopicUpdate {
   145  	ids := make([]int, len(t))
   146  	for i := range t {
   147  		ids[i] = t[i].ID
   148  	}
   149  	return tu.AddChildIDs(ids...)
   150  }
   151  
   152  // SetParent sets the "parent" edge to the Topic entity.
   153  func (tu *TopicUpdate) SetParent(t *Topic) *TopicUpdate {
   154  	return tu.SetParentID(t.ID)
   155  }
   156  
   157  // Mutation returns the TopicMutation object of the builder.
   158  func (tu *TopicUpdate) Mutation() *TopicMutation {
   159  	return tu.mutation
   160  }
   161  
   162  // ClearPosts clears all "posts" edges to the Post entity.
   163  func (tu *TopicUpdate) ClearPosts() *TopicUpdate {
   164  	tu.mutation.ClearPosts()
   165  	return tu
   166  }
   167  
   168  // RemovePostIDs removes the "posts" edge to Post entities by IDs.
   169  func (tu *TopicUpdate) RemovePostIDs(ids ...int) *TopicUpdate {
   170  	tu.mutation.RemovePostIDs(ids...)
   171  	return tu
   172  }
   173  
   174  // RemovePosts removes "posts" edges to Post entities.
   175  func (tu *TopicUpdate) RemovePosts(p ...*Post) *TopicUpdate {
   176  	ids := make([]int, len(p))
   177  	for i := range p {
   178  		ids[i] = p[i].ID
   179  	}
   180  	return tu.RemovePostIDs(ids...)
   181  }
   182  
   183  // ClearChildren clears all "children" edges to the Topic entity.
   184  func (tu *TopicUpdate) ClearChildren() *TopicUpdate {
   185  	tu.mutation.ClearChildren()
   186  	return tu
   187  }
   188  
   189  // RemoveChildIDs removes the "children" edge to Topic entities by IDs.
   190  func (tu *TopicUpdate) RemoveChildIDs(ids ...int) *TopicUpdate {
   191  	tu.mutation.RemoveChildIDs(ids...)
   192  	return tu
   193  }
   194  
   195  // RemoveChildren removes "children" edges to Topic entities.
   196  func (tu *TopicUpdate) RemoveChildren(t ...*Topic) *TopicUpdate {
   197  	ids := make([]int, len(t))
   198  	for i := range t {
   199  		ids[i] = t[i].ID
   200  	}
   201  	return tu.RemoveChildIDs(ids...)
   202  }
   203  
   204  // ClearParent clears the "parent" edge to the Topic entity.
   205  func (tu *TopicUpdate) ClearParent() *TopicUpdate {
   206  	tu.mutation.ClearParent()
   207  	return tu
   208  }
   209  
   210  // Save executes the query and returns the number of nodes affected by the update operation.
   211  func (tu *TopicUpdate) Save(ctx context.Context) (int, error) {
   212  	var (
   213  		err      error
   214  		affected int
   215  	)
   216  	tu.defaults()
   217  	if len(tu.hooks) == 0 {
   218  		affected, err = tu.sqlSave(ctx)
   219  	} else {
   220  		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
   221  			mutation, ok := m.(*TopicMutation)
   222  			if !ok {
   223  				return nil, fmt.Errorf("unexpected mutation type %T", m)
   224  			}
   225  			tu.mutation = mutation
   226  			affected, err = tu.sqlSave(ctx)
   227  			mutation.done = true
   228  			return affected, err
   229  		})
   230  		for i := len(tu.hooks) - 1; i >= 0; i-- {
   231  			if tu.hooks[i] == nil {
   232  				return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
   233  			}
   234  			mut = tu.hooks[i](mut)
   235  		}
   236  		if _, err := mut.Mutate(ctx, tu.mutation); err != nil {
   237  			return 0, err
   238  		}
   239  	}
   240  	return affected, err
   241  }
   242  
   243  // SaveX is like Save, but panics if an error occurs.
   244  func (tu *TopicUpdate) SaveX(ctx context.Context) int {
   245  	affected, err := tu.Save(ctx)
   246  	if err != nil {
   247  		panic(err)
   248  	}
   249  	return affected
   250  }
   251  
   252  // Exec executes the query.
   253  func (tu *TopicUpdate) Exec(ctx context.Context) error {
   254  	_, err := tu.Save(ctx)
   255  	return err
   256  }
   257  
   258  // ExecX is like Exec, but panics if an error occurs.
   259  func (tu *TopicUpdate) ExecX(ctx context.Context) {
   260  	if err := tu.Exec(ctx); err != nil {
   261  		panic(err)
   262  	}
   263  }
   264  
   265  // defaults sets the default values of the builder before save.
   266  func (tu *TopicUpdate) defaults() {
   267  	if _, ok := tu.mutation.UpdatedAt(); !ok {
   268  		v := topic.UpdateDefaultUpdatedAt()
   269  		tu.mutation.SetUpdatedAt(v)
   270  	}
   271  }
   272  
   273  func (tu *TopicUpdate) sqlSave(ctx context.Context) (n int, err error) {
   274  	_spec := &sqlgraph.UpdateSpec{
   275  		Node: &sqlgraph.NodeSpec{
   276  			Table:   topic.Table,
   277  			Columns: topic.Columns,
   278  			ID: &sqlgraph.FieldSpec{
   279  				Type:   field.TypeInt,
   280  				Column: topic.FieldID,
   281  			},
   282  		},
   283  	}
   284  	if ps := tu.mutation.predicates; len(ps) > 0 {
   285  		_spec.Predicate = func(selector *sql.Selector) {
   286  			for i := range ps {
   287  				ps[i](selector)
   288  			}
   289  		}
   290  	}
   291  	if value, ok := tu.mutation.UpdatedAt(); ok {
   292  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   293  			Type:   field.TypeTime,
   294  			Value:  value,
   295  			Column: topic.FieldUpdatedAt,
   296  		})
   297  	}
   298  	if value, ok := tu.mutation.DeletedAt(); ok {
   299  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   300  			Type:   field.TypeTime,
   301  			Value:  value,
   302  			Column: topic.FieldDeletedAt,
   303  		})
   304  	}
   305  	if tu.mutation.DeletedAtCleared() {
   306  		_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
   307  			Type:   field.TypeTime,
   308  			Column: topic.FieldDeletedAt,
   309  		})
   310  	}
   311  	if value, ok := tu.mutation.Name(); ok {
   312  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   313  			Type:   field.TypeString,
   314  			Value:  value,
   315  			Column: topic.FieldName,
   316  		})
   317  	}
   318  	if value, ok := tu.mutation.Slug(); ok {
   319  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   320  			Type:   field.TypeString,
   321  			Value:  value,
   322  			Column: topic.FieldSlug,
   323  		})
   324  	}
   325  	if value, ok := tu.mutation.Description(); ok {
   326  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   327  			Type:   field.TypeString,
   328  			Value:  value,
   329  			Column: topic.FieldDescription,
   330  		})
   331  	}
   332  	if tu.mutation.DescriptionCleared() {
   333  		_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
   334  			Type:   field.TypeString,
   335  			Column: topic.FieldDescription,
   336  		})
   337  	}
   338  	if value, ok := tu.mutation.Content(); ok {
   339  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   340  			Type:   field.TypeString,
   341  			Value:  value,
   342  			Column: topic.FieldContent,
   343  		})
   344  	}
   345  	if value, ok := tu.mutation.ContentHTML(); ok {
   346  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   347  			Type:   field.TypeString,
   348  			Value:  value,
   349  			Column: topic.FieldContentHTML,
   350  		})
   351  	}
   352  	if tu.mutation.PostsCleared() {
   353  		edge := &sqlgraph.EdgeSpec{
   354  			Rel:     sqlgraph.M2M,
   355  			Inverse: false,
   356  			Table:   topic.PostsTable,
   357  			Columns: topic.PostsPrimaryKey,
   358  			Bidi:    false,
   359  			Target: &sqlgraph.EdgeTarget{
   360  				IDSpec: &sqlgraph.FieldSpec{
   361  					Type:   field.TypeInt,
   362  					Column: post.FieldID,
   363  				},
   364  			},
   365  		}
   366  		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
   367  	}
   368  	if nodes := tu.mutation.RemovedPostsIDs(); len(nodes) > 0 && !tu.mutation.PostsCleared() {
   369  		edge := &sqlgraph.EdgeSpec{
   370  			Rel:     sqlgraph.M2M,
   371  			Inverse: false,
   372  			Table:   topic.PostsTable,
   373  			Columns: topic.PostsPrimaryKey,
   374  			Bidi:    false,
   375  			Target: &sqlgraph.EdgeTarget{
   376  				IDSpec: &sqlgraph.FieldSpec{
   377  					Type:   field.TypeInt,
   378  					Column: post.FieldID,
   379  				},
   380  			},
   381  		}
   382  		for _, k := range nodes {
   383  			edge.Target.Nodes = append(edge.Target.Nodes, k)
   384  		}
   385  		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
   386  	}
   387  	if nodes := tu.mutation.PostsIDs(); len(nodes) > 0 {
   388  		edge := &sqlgraph.EdgeSpec{
   389  			Rel:     sqlgraph.M2M,
   390  			Inverse: false,
   391  			Table:   topic.PostsTable,
   392  			Columns: topic.PostsPrimaryKey,
   393  			Bidi:    false,
   394  			Target: &sqlgraph.EdgeTarget{
   395  				IDSpec: &sqlgraph.FieldSpec{
   396  					Type:   field.TypeInt,
   397  					Column: post.FieldID,
   398  				},
   399  			},
   400  		}
   401  		for _, k := range nodes {
   402  			edge.Target.Nodes = append(edge.Target.Nodes, k)
   403  		}
   404  		_spec.Edges.Add = append(_spec.Edges.Add, edge)
   405  	}
   406  	if tu.mutation.ChildrenCleared() {
   407  		edge := &sqlgraph.EdgeSpec{
   408  			Rel:     sqlgraph.O2M,
   409  			Inverse: false,
   410  			Table:   topic.ChildrenTable,
   411  			Columns: []string{topic.ChildrenColumn},
   412  			Bidi:    true,
   413  			Target: &sqlgraph.EdgeTarget{
   414  				IDSpec: &sqlgraph.FieldSpec{
   415  					Type:   field.TypeInt,
   416  					Column: topic.FieldID,
   417  				},
   418  			},
   419  		}
   420  		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
   421  	}
   422  	if nodes := tu.mutation.RemovedChildrenIDs(); len(nodes) > 0 && !tu.mutation.ChildrenCleared() {
   423  		edge := &sqlgraph.EdgeSpec{
   424  			Rel:     sqlgraph.O2M,
   425  			Inverse: false,
   426  			Table:   topic.ChildrenTable,
   427  			Columns: []string{topic.ChildrenColumn},
   428  			Bidi:    true,
   429  			Target: &sqlgraph.EdgeTarget{
   430  				IDSpec: &sqlgraph.FieldSpec{
   431  					Type:   field.TypeInt,
   432  					Column: topic.FieldID,
   433  				},
   434  			},
   435  		}
   436  		for _, k := range nodes {
   437  			edge.Target.Nodes = append(edge.Target.Nodes, k)
   438  		}
   439  		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
   440  	}
   441  	if nodes := tu.mutation.ChildrenIDs(); len(nodes) > 0 {
   442  		edge := &sqlgraph.EdgeSpec{
   443  			Rel:     sqlgraph.O2M,
   444  			Inverse: false,
   445  			Table:   topic.ChildrenTable,
   446  			Columns: []string{topic.ChildrenColumn},
   447  			Bidi:    true,
   448  			Target: &sqlgraph.EdgeTarget{
   449  				IDSpec: &sqlgraph.FieldSpec{
   450  					Type:   field.TypeInt,
   451  					Column: topic.FieldID,
   452  				},
   453  			},
   454  		}
   455  		for _, k := range nodes {
   456  			edge.Target.Nodes = append(edge.Target.Nodes, k)
   457  		}
   458  		_spec.Edges.Add = append(_spec.Edges.Add, edge)
   459  	}
   460  	if tu.mutation.ParentCleared() {
   461  		edge := &sqlgraph.EdgeSpec{
   462  			Rel:     sqlgraph.M2O,
   463  			Inverse: true,
   464  			Table:   topic.ParentTable,
   465  			Columns: []string{topic.ParentColumn},
   466  			Bidi:    false,
   467  			Target: &sqlgraph.EdgeTarget{
   468  				IDSpec: &sqlgraph.FieldSpec{
   469  					Type:   field.TypeInt,
   470  					Column: topic.FieldID,
   471  				},
   472  			},
   473  		}
   474  		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
   475  	}
   476  	if nodes := tu.mutation.ParentIDs(); len(nodes) > 0 {
   477  		edge := &sqlgraph.EdgeSpec{
   478  			Rel:     sqlgraph.M2O,
   479  			Inverse: true,
   480  			Table:   topic.ParentTable,
   481  			Columns: []string{topic.ParentColumn},
   482  			Bidi:    false,
   483  			Target: &sqlgraph.EdgeTarget{
   484  				IDSpec: &sqlgraph.FieldSpec{
   485  					Type:   field.TypeInt,
   486  					Column: topic.FieldID,
   487  				},
   488  			},
   489  		}
   490  		for _, k := range nodes {
   491  			edge.Target.Nodes = append(edge.Target.Nodes, k)
   492  		}
   493  		_spec.Edges.Add = append(_spec.Edges.Add, edge)
   494  	}
   495  	if n, err = sqlgraph.UpdateNodes(ctx, tu.driver, _spec); err != nil {
   496  		if _, ok := err.(*sqlgraph.NotFoundError); ok {
   497  			err = &NotFoundError{topic.Label}
   498  		} else if sqlgraph.IsConstraintError(err) {
   499  			err = &ConstraintError{err.Error(), err}
   500  		}
   501  		return 0, err
   502  	}
   503  	return n, nil
   504  }
   505  
   506  // TopicUpdateOne is the builder for updating a single Topic entity.
   507  type TopicUpdateOne struct {
   508  	config
   509  	fields   []string
   510  	hooks    []Hook
   511  	mutation *TopicMutation
   512  }
   513  
   514  // SetUpdatedAt sets the "updated_at" field.
   515  func (tuo *TopicUpdateOne) SetUpdatedAt(t time.Time) *TopicUpdateOne {
   516  	tuo.mutation.SetUpdatedAt(t)
   517  	return tuo
   518  }
   519  
   520  // SetDeletedAt sets the "deleted_at" field.
   521  func (tuo *TopicUpdateOne) SetDeletedAt(t time.Time) *TopicUpdateOne {
   522  	tuo.mutation.SetDeletedAt(t)
   523  	return tuo
   524  }
   525  
   526  // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
   527  func (tuo *TopicUpdateOne) SetNillableDeletedAt(t *time.Time) *TopicUpdateOne {
   528  	if t != nil {
   529  		tuo.SetDeletedAt(*t)
   530  	}
   531  	return tuo
   532  }
   533  
   534  // ClearDeletedAt clears the value of the "deleted_at" field.
   535  func (tuo *TopicUpdateOne) ClearDeletedAt() *TopicUpdateOne {
   536  	tuo.mutation.ClearDeletedAt()
   537  	return tuo
   538  }
   539  
   540  // SetName sets the "name" field.
   541  func (tuo *TopicUpdateOne) SetName(s string) *TopicUpdateOne {
   542  	tuo.mutation.SetName(s)
   543  	return tuo
   544  }
   545  
   546  // SetSlug sets the "slug" field.
   547  func (tuo *TopicUpdateOne) SetSlug(s string) *TopicUpdateOne {
   548  	tuo.mutation.SetSlug(s)
   549  	return tuo
   550  }
   551  
   552  // SetDescription sets the "description" field.
   553  func (tuo *TopicUpdateOne) SetDescription(s string) *TopicUpdateOne {
   554  	tuo.mutation.SetDescription(s)
   555  	return tuo
   556  }
   557  
   558  // SetNillableDescription sets the "description" field if the given value is not nil.
   559  func (tuo *TopicUpdateOne) SetNillableDescription(s *string) *TopicUpdateOne {
   560  	if s != nil {
   561  		tuo.SetDescription(*s)
   562  	}
   563  	return tuo
   564  }
   565  
   566  // ClearDescription clears the value of the "description" field.
   567  func (tuo *TopicUpdateOne) ClearDescription() *TopicUpdateOne {
   568  	tuo.mutation.ClearDescription()
   569  	return tuo
   570  }
   571  
   572  // SetContent sets the "content" field.
   573  func (tuo *TopicUpdateOne) SetContent(s string) *TopicUpdateOne {
   574  	tuo.mutation.SetContent(s)
   575  	return tuo
   576  }
   577  
   578  // SetContentHTML sets the "content_html" field.
   579  func (tuo *TopicUpdateOne) SetContentHTML(s string) *TopicUpdateOne {
   580  	tuo.mutation.SetContentHTML(s)
   581  	return tuo
   582  }
   583  
   584  // SetParentID sets the "parent_id" field.
   585  func (tuo *TopicUpdateOne) SetParentID(i int) *TopicUpdateOne {
   586  	tuo.mutation.SetParentID(i)
   587  	return tuo
   588  }
   589  
   590  // SetNillableParentID sets the "parent_id" field if the given value is not nil.
   591  func (tuo *TopicUpdateOne) SetNillableParentID(i *int) *TopicUpdateOne {
   592  	if i != nil {
   593  		tuo.SetParentID(*i)
   594  	}
   595  	return tuo
   596  }
   597  
   598  // ClearParentID clears the value of the "parent_id" field.
   599  func (tuo *TopicUpdateOne) ClearParentID() *TopicUpdateOne {
   600  	tuo.mutation.ClearParentID()
   601  	return tuo
   602  }
   603  
   604  // AddPostIDs adds the "posts" edge to the Post entity by IDs.
   605  func (tuo *TopicUpdateOne) AddPostIDs(ids ...int) *TopicUpdateOne {
   606  	tuo.mutation.AddPostIDs(ids...)
   607  	return tuo
   608  }
   609  
   610  // AddPosts adds the "posts" edges to the Post entity.
   611  func (tuo *TopicUpdateOne) AddPosts(p ...*Post) *TopicUpdateOne {
   612  	ids := make([]int, len(p))
   613  	for i := range p {
   614  		ids[i] = p[i].ID
   615  	}
   616  	return tuo.AddPostIDs(ids...)
   617  }
   618  
   619  // AddChildIDs adds the "children" edge to the Topic entity by IDs.
   620  func (tuo *TopicUpdateOne) AddChildIDs(ids ...int) *TopicUpdateOne {
   621  	tuo.mutation.AddChildIDs(ids...)
   622  	return tuo
   623  }
   624  
   625  // AddChildren adds the "children" edges to the Topic entity.
   626  func (tuo *TopicUpdateOne) AddChildren(t ...*Topic) *TopicUpdateOne {
   627  	ids := make([]int, len(t))
   628  	for i := range t {
   629  		ids[i] = t[i].ID
   630  	}
   631  	return tuo.AddChildIDs(ids...)
   632  }
   633  
   634  // SetParent sets the "parent" edge to the Topic entity.
   635  func (tuo *TopicUpdateOne) SetParent(t *Topic) *TopicUpdateOne {
   636  	return tuo.SetParentID(t.ID)
   637  }
   638  
   639  // Mutation returns the TopicMutation object of the builder.
   640  func (tuo *TopicUpdateOne) Mutation() *TopicMutation {
   641  	return tuo.mutation
   642  }
   643  
   644  // ClearPosts clears all "posts" edges to the Post entity.
   645  func (tuo *TopicUpdateOne) ClearPosts() *TopicUpdateOne {
   646  	tuo.mutation.ClearPosts()
   647  	return tuo
   648  }
   649  
   650  // RemovePostIDs removes the "posts" edge to Post entities by IDs.
   651  func (tuo *TopicUpdateOne) RemovePostIDs(ids ...int) *TopicUpdateOne {
   652  	tuo.mutation.RemovePostIDs(ids...)
   653  	return tuo
   654  }
   655  
   656  // RemovePosts removes "posts" edges to Post entities.
   657  func (tuo *TopicUpdateOne) RemovePosts(p ...*Post) *TopicUpdateOne {
   658  	ids := make([]int, len(p))
   659  	for i := range p {
   660  		ids[i] = p[i].ID
   661  	}
   662  	return tuo.RemovePostIDs(ids...)
   663  }
   664  
   665  // ClearChildren clears all "children" edges to the Topic entity.
   666  func (tuo *TopicUpdateOne) ClearChildren() *TopicUpdateOne {
   667  	tuo.mutation.ClearChildren()
   668  	return tuo
   669  }
   670  
   671  // RemoveChildIDs removes the "children" edge to Topic entities by IDs.
   672  func (tuo *TopicUpdateOne) RemoveChildIDs(ids ...int) *TopicUpdateOne {
   673  	tuo.mutation.RemoveChildIDs(ids...)
   674  	return tuo
   675  }
   676  
   677  // RemoveChildren removes "children" edges to Topic entities.
   678  func (tuo *TopicUpdateOne) RemoveChildren(t ...*Topic) *TopicUpdateOne {
   679  	ids := make([]int, len(t))
   680  	for i := range t {
   681  		ids[i] = t[i].ID
   682  	}
   683  	return tuo.RemoveChildIDs(ids...)
   684  }
   685  
   686  // ClearParent clears the "parent" edge to the Topic entity.
   687  func (tuo *TopicUpdateOne) ClearParent() *TopicUpdateOne {
   688  	tuo.mutation.ClearParent()
   689  	return tuo
   690  }
   691  
   692  // Select allows selecting one or more fields (columns) of the returned entity.
   693  // The default is selecting all fields defined in the entity schema.
   694  func (tuo *TopicUpdateOne) Select(field string, fields ...string) *TopicUpdateOne {
   695  	tuo.fields = append([]string{field}, fields...)
   696  	return tuo
   697  }
   698  
   699  // Save executes the query and returns the updated Topic entity.
   700  func (tuo *TopicUpdateOne) Save(ctx context.Context) (*Topic, error) {
   701  	var (
   702  		err  error
   703  		node *Topic
   704  	)
   705  	tuo.defaults()
   706  	if len(tuo.hooks) == 0 {
   707  		node, err = tuo.sqlSave(ctx)
   708  	} else {
   709  		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
   710  			mutation, ok := m.(*TopicMutation)
   711  			if !ok {
   712  				return nil, fmt.Errorf("unexpected mutation type %T", m)
   713  			}
   714  			tuo.mutation = mutation
   715  			node, err = tuo.sqlSave(ctx)
   716  			mutation.done = true
   717  			return node, err
   718  		})
   719  		for i := len(tuo.hooks) - 1; i >= 0; i-- {
   720  			if tuo.hooks[i] == nil {
   721  				return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
   722  			}
   723  			mut = tuo.hooks[i](mut)
   724  		}
   725  		if _, err := mut.Mutate(ctx, tuo.mutation); err != nil {
   726  			return nil, err
   727  		}
   728  	}
   729  	return node, err
   730  }
   731  
   732  // SaveX is like Save, but panics if an error occurs.
   733  func (tuo *TopicUpdateOne) SaveX(ctx context.Context) *Topic {
   734  	node, err := tuo.Save(ctx)
   735  	if err != nil {
   736  		panic(err)
   737  	}
   738  	return node
   739  }
   740  
   741  // Exec executes the query on the entity.
   742  func (tuo *TopicUpdateOne) Exec(ctx context.Context) error {
   743  	_, err := tuo.Save(ctx)
   744  	return err
   745  }
   746  
   747  // ExecX is like Exec, but panics if an error occurs.
   748  func (tuo *TopicUpdateOne) ExecX(ctx context.Context) {
   749  	if err := tuo.Exec(ctx); err != nil {
   750  		panic(err)
   751  	}
   752  }
   753  
   754  // defaults sets the default values of the builder before save.
   755  func (tuo *TopicUpdateOne) defaults() {
   756  	if _, ok := tuo.mutation.UpdatedAt(); !ok {
   757  		v := topic.UpdateDefaultUpdatedAt()
   758  		tuo.mutation.SetUpdatedAt(v)
   759  	}
   760  }
   761  
   762  func (tuo *TopicUpdateOne) sqlSave(ctx context.Context) (_node *Topic, err error) {
   763  	_spec := &sqlgraph.UpdateSpec{
   764  		Node: &sqlgraph.NodeSpec{
   765  			Table:   topic.Table,
   766  			Columns: topic.Columns,
   767  			ID: &sqlgraph.FieldSpec{
   768  				Type:   field.TypeInt,
   769  				Column: topic.FieldID,
   770  			},
   771  		},
   772  	}
   773  	id, ok := tuo.mutation.ID()
   774  	if !ok {
   775  		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Topic.id" for update`)}
   776  	}
   777  	_spec.Node.ID.Value = id
   778  	if fields := tuo.fields; len(fields) > 0 {
   779  		_spec.Node.Columns = make([]string, 0, len(fields))
   780  		_spec.Node.Columns = append(_spec.Node.Columns, topic.FieldID)
   781  		for _, f := range fields {
   782  			if !topic.ValidColumn(f) {
   783  				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
   784  			}
   785  			if f != topic.FieldID {
   786  				_spec.Node.Columns = append(_spec.Node.Columns, f)
   787  			}
   788  		}
   789  	}
   790  	if ps := tuo.mutation.predicates; len(ps) > 0 {
   791  		_spec.Predicate = func(selector *sql.Selector) {
   792  			for i := range ps {
   793  				ps[i](selector)
   794  			}
   795  		}
   796  	}
   797  	if value, ok := tuo.mutation.UpdatedAt(); ok {
   798  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   799  			Type:   field.TypeTime,
   800  			Value:  value,
   801  			Column: topic.FieldUpdatedAt,
   802  		})
   803  	}
   804  	if value, ok := tuo.mutation.DeletedAt(); ok {
   805  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   806  			Type:   field.TypeTime,
   807  			Value:  value,
   808  			Column: topic.FieldDeletedAt,
   809  		})
   810  	}
   811  	if tuo.mutation.DeletedAtCleared() {
   812  		_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
   813  			Type:   field.TypeTime,
   814  			Column: topic.FieldDeletedAt,
   815  		})
   816  	}
   817  	if value, ok := tuo.mutation.Name(); ok {
   818  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   819  			Type:   field.TypeString,
   820  			Value:  value,
   821  			Column: topic.FieldName,
   822  		})
   823  	}
   824  	if value, ok := tuo.mutation.Slug(); ok {
   825  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   826  			Type:   field.TypeString,
   827  			Value:  value,
   828  			Column: topic.FieldSlug,
   829  		})
   830  	}
   831  	if value, ok := tuo.mutation.Description(); ok {
   832  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   833  			Type:   field.TypeString,
   834  			Value:  value,
   835  			Column: topic.FieldDescription,
   836  		})
   837  	}
   838  	if tuo.mutation.DescriptionCleared() {
   839  		_spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{
   840  			Type:   field.TypeString,
   841  			Column: topic.FieldDescription,
   842  		})
   843  	}
   844  	if value, ok := tuo.mutation.Content(); ok {
   845  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   846  			Type:   field.TypeString,
   847  			Value:  value,
   848  			Column: topic.FieldContent,
   849  		})
   850  	}
   851  	if value, ok := tuo.mutation.ContentHTML(); ok {
   852  		_spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
   853  			Type:   field.TypeString,
   854  			Value:  value,
   855  			Column: topic.FieldContentHTML,
   856  		})
   857  	}
   858  	if tuo.mutation.PostsCleared() {
   859  		edge := &sqlgraph.EdgeSpec{
   860  			Rel:     sqlgraph.M2M,
   861  			Inverse: false,
   862  			Table:   topic.PostsTable,
   863  			Columns: topic.PostsPrimaryKey,
   864  			Bidi:    false,
   865  			Target: &sqlgraph.EdgeTarget{
   866  				IDSpec: &sqlgraph.FieldSpec{
   867  					Type:   field.TypeInt,
   868  					Column: post.FieldID,
   869  				},
   870  			},
   871  		}
   872  		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
   873  	}
   874  	if nodes := tuo.mutation.RemovedPostsIDs(); len(nodes) > 0 && !tuo.mutation.PostsCleared() {
   875  		edge := &sqlgraph.EdgeSpec{
   876  			Rel:     sqlgraph.M2M,
   877  			Inverse: false,
   878  			Table:   topic.PostsTable,
   879  			Columns: topic.PostsPrimaryKey,
   880  			Bidi:    false,
   881  			Target: &sqlgraph.EdgeTarget{
   882  				IDSpec: &sqlgraph.FieldSpec{
   883  					Type:   field.TypeInt,
   884  					Column: post.FieldID,
   885  				},
   886  			},
   887  		}
   888  		for _, k := range nodes {
   889  			edge.Target.Nodes = append(edge.Target.Nodes, k)
   890  		}
   891  		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
   892  	}
   893  	if nodes := tuo.mutation.PostsIDs(); len(nodes) > 0 {
   894  		edge := &sqlgraph.EdgeSpec{
   895  			Rel:     sqlgraph.M2M,
   896  			Inverse: false,
   897  			Table:   topic.PostsTable,
   898  			Columns: topic.PostsPrimaryKey,
   899  			Bidi:    false,
   900  			Target: &sqlgraph.EdgeTarget{
   901  				IDSpec: &sqlgraph.FieldSpec{
   902  					Type:   field.TypeInt,
   903  					Column: post.FieldID,
   904  				},
   905  			},
   906  		}
   907  		for _, k := range nodes {
   908  			edge.Target.Nodes = append(edge.Target.Nodes, k)
   909  		}
   910  		_spec.Edges.Add = append(_spec.Edges.Add, edge)
   911  	}
   912  	if tuo.mutation.ChildrenCleared() {
   913  		edge := &sqlgraph.EdgeSpec{
   914  			Rel:     sqlgraph.O2M,
   915  			Inverse: false,
   916  			Table:   topic.ChildrenTable,
   917  			Columns: []string{topic.ChildrenColumn},
   918  			Bidi:    true,
   919  			Target: &sqlgraph.EdgeTarget{
   920  				IDSpec: &sqlgraph.FieldSpec{
   921  					Type:   field.TypeInt,
   922  					Column: topic.FieldID,
   923  				},
   924  			},
   925  		}
   926  		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
   927  	}
   928  	if nodes := tuo.mutation.RemovedChildrenIDs(); len(nodes) > 0 && !tuo.mutation.ChildrenCleared() {
   929  		edge := &sqlgraph.EdgeSpec{
   930  			Rel:     sqlgraph.O2M,
   931  			Inverse: false,
   932  			Table:   topic.ChildrenTable,
   933  			Columns: []string{topic.ChildrenColumn},
   934  			Bidi:    true,
   935  			Target: &sqlgraph.EdgeTarget{
   936  				IDSpec: &sqlgraph.FieldSpec{
   937  					Type:   field.TypeInt,
   938  					Column: topic.FieldID,
   939  				},
   940  			},
   941  		}
   942  		for _, k := range nodes {
   943  			edge.Target.Nodes = append(edge.Target.Nodes, k)
   944  		}
   945  		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
   946  	}
   947  	if nodes := tuo.mutation.ChildrenIDs(); len(nodes) > 0 {
   948  		edge := &sqlgraph.EdgeSpec{
   949  			Rel:     sqlgraph.O2M,
   950  			Inverse: false,
   951  			Table:   topic.ChildrenTable,
   952  			Columns: []string{topic.ChildrenColumn},
   953  			Bidi:    true,
   954  			Target: &sqlgraph.EdgeTarget{
   955  				IDSpec: &sqlgraph.FieldSpec{
   956  					Type:   field.TypeInt,
   957  					Column: topic.FieldID,
   958  				},
   959  			},
   960  		}
   961  		for _, k := range nodes {
   962  			edge.Target.Nodes = append(edge.Target.Nodes, k)
   963  		}
   964  		_spec.Edges.Add = append(_spec.Edges.Add, edge)
   965  	}
   966  	if tuo.mutation.ParentCleared() {
   967  		edge := &sqlgraph.EdgeSpec{
   968  			Rel:     sqlgraph.M2O,
   969  			Inverse: true,
   970  			Table:   topic.ParentTable,
   971  			Columns: []string{topic.ParentColumn},
   972  			Bidi:    false,
   973  			Target: &sqlgraph.EdgeTarget{
   974  				IDSpec: &sqlgraph.FieldSpec{
   975  					Type:   field.TypeInt,
   976  					Column: topic.FieldID,
   977  				},
   978  			},
   979  		}
   980  		_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
   981  	}
   982  	if nodes := tuo.mutation.ParentIDs(); len(nodes) > 0 {
   983  		edge := &sqlgraph.EdgeSpec{
   984  			Rel:     sqlgraph.M2O,
   985  			Inverse: true,
   986  			Table:   topic.ParentTable,
   987  			Columns: []string{topic.ParentColumn},
   988  			Bidi:    false,
   989  			Target: &sqlgraph.EdgeTarget{
   990  				IDSpec: &sqlgraph.FieldSpec{
   991  					Type:   field.TypeInt,
   992  					Column: topic.FieldID,
   993  				},
   994  			},
   995  		}
   996  		for _, k := range nodes {
   997  			edge.Target.Nodes = append(edge.Target.Nodes, k)
   998  		}
   999  		_spec.Edges.Add = append(_spec.Edges.Add, edge)
  1000  	}
  1001  	_node = &Topic{config: tuo.config}
  1002  	_spec.Assign = _node.assignValues
  1003  	_spec.ScanValues = _node.scanValues
  1004  	if err = sqlgraph.UpdateNode(ctx, tuo.driver, _spec); err != nil {
  1005  		if _, ok := err.(*sqlgraph.NotFoundError); ok {
  1006  			err = &NotFoundError{topic.Label}
  1007  		} else if sqlgraph.IsConstraintError(err) {
  1008  			err = &ConstraintError{err.Error(), err}
  1009  		}
  1010  		return nil, err
  1011  	}
  1012  	return _node, nil
  1013  }