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