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