github.com/ngocphuongnb/tetua@v0.0.7-alpha/packages/entrepository/ent/page_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/predicate" 17 ) 18 19 // PageUpdate is the builder for updating Page entities. 20 type PageUpdate struct { 21 config 22 hooks []Hook 23 mutation *PageMutation 24 } 25 26 // Where appends a list predicates to the PageUpdate builder. 27 func (pu *PageUpdate) Where(ps ...predicate.Page) *PageUpdate { 28 pu.mutation.Where(ps...) 29 return pu 30 } 31 32 // SetUpdatedAt sets the "updated_at" field. 33 func (pu *PageUpdate) SetUpdatedAt(t time.Time) *PageUpdate { 34 pu.mutation.SetUpdatedAt(t) 35 return pu 36 } 37 38 // SetDeletedAt sets the "deleted_at" field. 39 func (pu *PageUpdate) SetDeletedAt(t time.Time) *PageUpdate { 40 pu.mutation.SetDeletedAt(t) 41 return pu 42 } 43 44 // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil. 45 func (pu *PageUpdate) SetNillableDeletedAt(t *time.Time) *PageUpdate { 46 if t != nil { 47 pu.SetDeletedAt(*t) 48 } 49 return pu 50 } 51 52 // ClearDeletedAt clears the value of the "deleted_at" field. 53 func (pu *PageUpdate) ClearDeletedAt() *PageUpdate { 54 pu.mutation.ClearDeletedAt() 55 return pu 56 } 57 58 // SetName sets the "name" field. 59 func (pu *PageUpdate) SetName(s string) *PageUpdate { 60 pu.mutation.SetName(s) 61 return pu 62 } 63 64 // SetSlug sets the "slug" field. 65 func (pu *PageUpdate) SetSlug(s string) *PageUpdate { 66 pu.mutation.SetSlug(s) 67 return pu 68 } 69 70 // SetContent sets the "content" field. 71 func (pu *PageUpdate) SetContent(s string) *PageUpdate { 72 pu.mutation.SetContent(s) 73 return pu 74 } 75 76 // SetContentHTML sets the "content_html" field. 77 func (pu *PageUpdate) SetContentHTML(s string) *PageUpdate { 78 pu.mutation.SetContentHTML(s) 79 return pu 80 } 81 82 // SetDraft sets the "draft" field. 83 func (pu *PageUpdate) SetDraft(b bool) *PageUpdate { 84 pu.mutation.SetDraft(b) 85 return pu 86 } 87 88 // SetNillableDraft sets the "draft" field if the given value is not nil. 89 func (pu *PageUpdate) SetNillableDraft(b *bool) *PageUpdate { 90 if b != nil { 91 pu.SetDraft(*b) 92 } 93 return pu 94 } 95 96 // ClearDraft clears the value of the "draft" field. 97 func (pu *PageUpdate) ClearDraft() *PageUpdate { 98 pu.mutation.ClearDraft() 99 return pu 100 } 101 102 // SetFeaturedImageID sets the "featured_image_id" field. 103 func (pu *PageUpdate) SetFeaturedImageID(i int) *PageUpdate { 104 pu.mutation.SetFeaturedImageID(i) 105 return pu 106 } 107 108 // SetNillableFeaturedImageID sets the "featured_image_id" field if the given value is not nil. 109 func (pu *PageUpdate) SetNillableFeaturedImageID(i *int) *PageUpdate { 110 if i != nil { 111 pu.SetFeaturedImageID(*i) 112 } 113 return pu 114 } 115 116 // ClearFeaturedImageID clears the value of the "featured_image_id" field. 117 func (pu *PageUpdate) ClearFeaturedImageID() *PageUpdate { 118 pu.mutation.ClearFeaturedImageID() 119 return pu 120 } 121 122 // SetFeaturedImage sets the "featured_image" edge to the File entity. 123 func (pu *PageUpdate) SetFeaturedImage(f *File) *PageUpdate { 124 return pu.SetFeaturedImageID(f.ID) 125 } 126 127 // Mutation returns the PageMutation object of the builder. 128 func (pu *PageUpdate) Mutation() *PageMutation { 129 return pu.mutation 130 } 131 132 // ClearFeaturedImage clears the "featured_image" edge to the File entity. 133 func (pu *PageUpdate) ClearFeaturedImage() *PageUpdate { 134 pu.mutation.ClearFeaturedImage() 135 return pu 136 } 137 138 // Save executes the query and returns the number of nodes affected by the update operation. 139 func (pu *PageUpdate) Save(ctx context.Context) (int, error) { 140 var ( 141 err error 142 affected int 143 ) 144 pu.defaults() 145 if len(pu.hooks) == 0 { 146 affected, err = pu.sqlSave(ctx) 147 } else { 148 var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { 149 mutation, ok := m.(*PageMutation) 150 if !ok { 151 return nil, fmt.Errorf("unexpected mutation type %T", m) 152 } 153 pu.mutation = mutation 154 affected, err = pu.sqlSave(ctx) 155 mutation.done = true 156 return affected, err 157 }) 158 for i := len(pu.hooks) - 1; i >= 0; i-- { 159 if pu.hooks[i] == nil { 160 return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") 161 } 162 mut = pu.hooks[i](mut) 163 } 164 if _, err := mut.Mutate(ctx, pu.mutation); err != nil { 165 return 0, err 166 } 167 } 168 return affected, err 169 } 170 171 // SaveX is like Save, but panics if an error occurs. 172 func (pu *PageUpdate) SaveX(ctx context.Context) int { 173 affected, err := pu.Save(ctx) 174 if err != nil { 175 panic(err) 176 } 177 return affected 178 } 179 180 // Exec executes the query. 181 func (pu *PageUpdate) Exec(ctx context.Context) error { 182 _, err := pu.Save(ctx) 183 return err 184 } 185 186 // ExecX is like Exec, but panics if an error occurs. 187 func (pu *PageUpdate) ExecX(ctx context.Context) { 188 if err := pu.Exec(ctx); err != nil { 189 panic(err) 190 } 191 } 192 193 // defaults sets the default values of the builder before save. 194 func (pu *PageUpdate) defaults() { 195 if _, ok := pu.mutation.UpdatedAt(); !ok { 196 v := page.UpdateDefaultUpdatedAt() 197 pu.mutation.SetUpdatedAt(v) 198 } 199 } 200 201 func (pu *PageUpdate) sqlSave(ctx context.Context) (n int, err error) { 202 _spec := &sqlgraph.UpdateSpec{ 203 Node: &sqlgraph.NodeSpec{ 204 Table: page.Table, 205 Columns: page.Columns, 206 ID: &sqlgraph.FieldSpec{ 207 Type: field.TypeInt, 208 Column: page.FieldID, 209 }, 210 }, 211 } 212 if ps := pu.mutation.predicates; len(ps) > 0 { 213 _spec.Predicate = func(selector *sql.Selector) { 214 for i := range ps { 215 ps[i](selector) 216 } 217 } 218 } 219 if value, ok := pu.mutation.UpdatedAt(); ok { 220 _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ 221 Type: field.TypeTime, 222 Value: value, 223 Column: page.FieldUpdatedAt, 224 }) 225 } 226 if value, ok := pu.mutation.DeletedAt(); ok { 227 _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ 228 Type: field.TypeTime, 229 Value: value, 230 Column: page.FieldDeletedAt, 231 }) 232 } 233 if pu.mutation.DeletedAtCleared() { 234 _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ 235 Type: field.TypeTime, 236 Column: page.FieldDeletedAt, 237 }) 238 } 239 if value, ok := pu.mutation.Name(); ok { 240 _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ 241 Type: field.TypeString, 242 Value: value, 243 Column: page.FieldName, 244 }) 245 } 246 if value, ok := pu.mutation.Slug(); ok { 247 _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ 248 Type: field.TypeString, 249 Value: value, 250 Column: page.FieldSlug, 251 }) 252 } 253 if value, ok := pu.mutation.Content(); ok { 254 _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ 255 Type: field.TypeString, 256 Value: value, 257 Column: page.FieldContent, 258 }) 259 } 260 if value, ok := pu.mutation.ContentHTML(); ok { 261 _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ 262 Type: field.TypeString, 263 Value: value, 264 Column: page.FieldContentHTML, 265 }) 266 } 267 if value, ok := pu.mutation.Draft(); ok { 268 _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ 269 Type: field.TypeBool, 270 Value: value, 271 Column: page.FieldDraft, 272 }) 273 } 274 if pu.mutation.DraftCleared() { 275 _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ 276 Type: field.TypeBool, 277 Column: page.FieldDraft, 278 }) 279 } 280 if pu.mutation.FeaturedImageCleared() { 281 edge := &sqlgraph.EdgeSpec{ 282 Rel: sqlgraph.M2O, 283 Inverse: true, 284 Table: page.FeaturedImageTable, 285 Columns: []string{page.FeaturedImageColumn}, 286 Bidi: false, 287 Target: &sqlgraph.EdgeTarget{ 288 IDSpec: &sqlgraph.FieldSpec{ 289 Type: field.TypeInt, 290 Column: file.FieldID, 291 }, 292 }, 293 } 294 _spec.Edges.Clear = append(_spec.Edges.Clear, edge) 295 } 296 if nodes := pu.mutation.FeaturedImageIDs(); len(nodes) > 0 { 297 edge := &sqlgraph.EdgeSpec{ 298 Rel: sqlgraph.M2O, 299 Inverse: true, 300 Table: page.FeaturedImageTable, 301 Columns: []string{page.FeaturedImageColumn}, 302 Bidi: false, 303 Target: &sqlgraph.EdgeTarget{ 304 IDSpec: &sqlgraph.FieldSpec{ 305 Type: field.TypeInt, 306 Column: file.FieldID, 307 }, 308 }, 309 } 310 for _, k := range nodes { 311 edge.Target.Nodes = append(edge.Target.Nodes, k) 312 } 313 _spec.Edges.Add = append(_spec.Edges.Add, edge) 314 } 315 if n, err = sqlgraph.UpdateNodes(ctx, pu.driver, _spec); err != nil { 316 if _, ok := err.(*sqlgraph.NotFoundError); ok { 317 err = &NotFoundError{page.Label} 318 } else if sqlgraph.IsConstraintError(err) { 319 err = &ConstraintError{err.Error(), err} 320 } 321 return 0, err 322 } 323 return n, nil 324 } 325 326 // PageUpdateOne is the builder for updating a single Page entity. 327 type PageUpdateOne struct { 328 config 329 fields []string 330 hooks []Hook 331 mutation *PageMutation 332 } 333 334 // SetUpdatedAt sets the "updated_at" field. 335 func (puo *PageUpdateOne) SetUpdatedAt(t time.Time) *PageUpdateOne { 336 puo.mutation.SetUpdatedAt(t) 337 return puo 338 } 339 340 // SetDeletedAt sets the "deleted_at" field. 341 func (puo *PageUpdateOne) SetDeletedAt(t time.Time) *PageUpdateOne { 342 puo.mutation.SetDeletedAt(t) 343 return puo 344 } 345 346 // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil. 347 func (puo *PageUpdateOne) SetNillableDeletedAt(t *time.Time) *PageUpdateOne { 348 if t != nil { 349 puo.SetDeletedAt(*t) 350 } 351 return puo 352 } 353 354 // ClearDeletedAt clears the value of the "deleted_at" field. 355 func (puo *PageUpdateOne) ClearDeletedAt() *PageUpdateOne { 356 puo.mutation.ClearDeletedAt() 357 return puo 358 } 359 360 // SetName sets the "name" field. 361 func (puo *PageUpdateOne) SetName(s string) *PageUpdateOne { 362 puo.mutation.SetName(s) 363 return puo 364 } 365 366 // SetSlug sets the "slug" field. 367 func (puo *PageUpdateOne) SetSlug(s string) *PageUpdateOne { 368 puo.mutation.SetSlug(s) 369 return puo 370 } 371 372 // SetContent sets the "content" field. 373 func (puo *PageUpdateOne) SetContent(s string) *PageUpdateOne { 374 puo.mutation.SetContent(s) 375 return puo 376 } 377 378 // SetContentHTML sets the "content_html" field. 379 func (puo *PageUpdateOne) SetContentHTML(s string) *PageUpdateOne { 380 puo.mutation.SetContentHTML(s) 381 return puo 382 } 383 384 // SetDraft sets the "draft" field. 385 func (puo *PageUpdateOne) SetDraft(b bool) *PageUpdateOne { 386 puo.mutation.SetDraft(b) 387 return puo 388 } 389 390 // SetNillableDraft sets the "draft" field if the given value is not nil. 391 func (puo *PageUpdateOne) SetNillableDraft(b *bool) *PageUpdateOne { 392 if b != nil { 393 puo.SetDraft(*b) 394 } 395 return puo 396 } 397 398 // ClearDraft clears the value of the "draft" field. 399 func (puo *PageUpdateOne) ClearDraft() *PageUpdateOne { 400 puo.mutation.ClearDraft() 401 return puo 402 } 403 404 // SetFeaturedImageID sets the "featured_image_id" field. 405 func (puo *PageUpdateOne) SetFeaturedImageID(i int) *PageUpdateOne { 406 puo.mutation.SetFeaturedImageID(i) 407 return puo 408 } 409 410 // SetNillableFeaturedImageID sets the "featured_image_id" field if the given value is not nil. 411 func (puo *PageUpdateOne) SetNillableFeaturedImageID(i *int) *PageUpdateOne { 412 if i != nil { 413 puo.SetFeaturedImageID(*i) 414 } 415 return puo 416 } 417 418 // ClearFeaturedImageID clears the value of the "featured_image_id" field. 419 func (puo *PageUpdateOne) ClearFeaturedImageID() *PageUpdateOne { 420 puo.mutation.ClearFeaturedImageID() 421 return puo 422 } 423 424 // SetFeaturedImage sets the "featured_image" edge to the File entity. 425 func (puo *PageUpdateOne) SetFeaturedImage(f *File) *PageUpdateOne { 426 return puo.SetFeaturedImageID(f.ID) 427 } 428 429 // Mutation returns the PageMutation object of the builder. 430 func (puo *PageUpdateOne) Mutation() *PageMutation { 431 return puo.mutation 432 } 433 434 // ClearFeaturedImage clears the "featured_image" edge to the File entity. 435 func (puo *PageUpdateOne) ClearFeaturedImage() *PageUpdateOne { 436 puo.mutation.ClearFeaturedImage() 437 return puo 438 } 439 440 // Select allows selecting one or more fields (columns) of the returned entity. 441 // The default is selecting all fields defined in the entity schema. 442 func (puo *PageUpdateOne) Select(field string, fields ...string) *PageUpdateOne { 443 puo.fields = append([]string{field}, fields...) 444 return puo 445 } 446 447 // Save executes the query and returns the updated Page entity. 448 func (puo *PageUpdateOne) Save(ctx context.Context) (*Page, error) { 449 var ( 450 err error 451 node *Page 452 ) 453 puo.defaults() 454 if len(puo.hooks) == 0 { 455 node, err = puo.sqlSave(ctx) 456 } else { 457 var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { 458 mutation, ok := m.(*PageMutation) 459 if !ok { 460 return nil, fmt.Errorf("unexpected mutation type %T", m) 461 } 462 puo.mutation = mutation 463 node, err = puo.sqlSave(ctx) 464 mutation.done = true 465 return node, err 466 }) 467 for i := len(puo.hooks) - 1; i >= 0; i-- { 468 if puo.hooks[i] == nil { 469 return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") 470 } 471 mut = puo.hooks[i](mut) 472 } 473 if _, err := mut.Mutate(ctx, puo.mutation); err != nil { 474 return nil, err 475 } 476 } 477 return node, err 478 } 479 480 // SaveX is like Save, but panics if an error occurs. 481 func (puo *PageUpdateOne) SaveX(ctx context.Context) *Page { 482 node, err := puo.Save(ctx) 483 if err != nil { 484 panic(err) 485 } 486 return node 487 } 488 489 // Exec executes the query on the entity. 490 func (puo *PageUpdateOne) Exec(ctx context.Context) error { 491 _, err := puo.Save(ctx) 492 return err 493 } 494 495 // ExecX is like Exec, but panics if an error occurs. 496 func (puo *PageUpdateOne) ExecX(ctx context.Context) { 497 if err := puo.Exec(ctx); err != nil { 498 panic(err) 499 } 500 } 501 502 // defaults sets the default values of the builder before save. 503 func (puo *PageUpdateOne) defaults() { 504 if _, ok := puo.mutation.UpdatedAt(); !ok { 505 v := page.UpdateDefaultUpdatedAt() 506 puo.mutation.SetUpdatedAt(v) 507 } 508 } 509 510 func (puo *PageUpdateOne) sqlSave(ctx context.Context) (_node *Page, err error) { 511 _spec := &sqlgraph.UpdateSpec{ 512 Node: &sqlgraph.NodeSpec{ 513 Table: page.Table, 514 Columns: page.Columns, 515 ID: &sqlgraph.FieldSpec{ 516 Type: field.TypeInt, 517 Column: page.FieldID, 518 }, 519 }, 520 } 521 id, ok := puo.mutation.ID() 522 if !ok { 523 return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Page.id" for update`)} 524 } 525 _spec.Node.ID.Value = id 526 if fields := puo.fields; len(fields) > 0 { 527 _spec.Node.Columns = make([]string, 0, len(fields)) 528 _spec.Node.Columns = append(_spec.Node.Columns, page.FieldID) 529 for _, f := range fields { 530 if !page.ValidColumn(f) { 531 return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} 532 } 533 if f != page.FieldID { 534 _spec.Node.Columns = append(_spec.Node.Columns, f) 535 } 536 } 537 } 538 if ps := puo.mutation.predicates; len(ps) > 0 { 539 _spec.Predicate = func(selector *sql.Selector) { 540 for i := range ps { 541 ps[i](selector) 542 } 543 } 544 } 545 if value, ok := puo.mutation.UpdatedAt(); ok { 546 _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ 547 Type: field.TypeTime, 548 Value: value, 549 Column: page.FieldUpdatedAt, 550 }) 551 } 552 if value, ok := puo.mutation.DeletedAt(); ok { 553 _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ 554 Type: field.TypeTime, 555 Value: value, 556 Column: page.FieldDeletedAt, 557 }) 558 } 559 if puo.mutation.DeletedAtCleared() { 560 _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ 561 Type: field.TypeTime, 562 Column: page.FieldDeletedAt, 563 }) 564 } 565 if value, ok := puo.mutation.Name(); ok { 566 _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ 567 Type: field.TypeString, 568 Value: value, 569 Column: page.FieldName, 570 }) 571 } 572 if value, ok := puo.mutation.Slug(); ok { 573 _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ 574 Type: field.TypeString, 575 Value: value, 576 Column: page.FieldSlug, 577 }) 578 } 579 if value, ok := puo.mutation.Content(); ok { 580 _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ 581 Type: field.TypeString, 582 Value: value, 583 Column: page.FieldContent, 584 }) 585 } 586 if value, ok := puo.mutation.ContentHTML(); ok { 587 _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ 588 Type: field.TypeString, 589 Value: value, 590 Column: page.FieldContentHTML, 591 }) 592 } 593 if value, ok := puo.mutation.Draft(); ok { 594 _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ 595 Type: field.TypeBool, 596 Value: value, 597 Column: page.FieldDraft, 598 }) 599 } 600 if puo.mutation.DraftCleared() { 601 _spec.Fields.Clear = append(_spec.Fields.Clear, &sqlgraph.FieldSpec{ 602 Type: field.TypeBool, 603 Column: page.FieldDraft, 604 }) 605 } 606 if puo.mutation.FeaturedImageCleared() { 607 edge := &sqlgraph.EdgeSpec{ 608 Rel: sqlgraph.M2O, 609 Inverse: true, 610 Table: page.FeaturedImageTable, 611 Columns: []string{page.FeaturedImageColumn}, 612 Bidi: false, 613 Target: &sqlgraph.EdgeTarget{ 614 IDSpec: &sqlgraph.FieldSpec{ 615 Type: field.TypeInt, 616 Column: file.FieldID, 617 }, 618 }, 619 } 620 _spec.Edges.Clear = append(_spec.Edges.Clear, edge) 621 } 622 if nodes := puo.mutation.FeaturedImageIDs(); len(nodes) > 0 { 623 edge := &sqlgraph.EdgeSpec{ 624 Rel: sqlgraph.M2O, 625 Inverse: true, 626 Table: page.FeaturedImageTable, 627 Columns: []string{page.FeaturedImageColumn}, 628 Bidi: false, 629 Target: &sqlgraph.EdgeTarget{ 630 IDSpec: &sqlgraph.FieldSpec{ 631 Type: field.TypeInt, 632 Column: file.FieldID, 633 }, 634 }, 635 } 636 for _, k := range nodes { 637 edge.Target.Nodes = append(edge.Target.Nodes, k) 638 } 639 _spec.Edges.Add = append(_spec.Edges.Add, edge) 640 } 641 _node = &Page{config: puo.config} 642 _spec.Assign = _node.assignValues 643 _spec.ScanValues = _node.scanValues 644 if err = sqlgraph.UpdateNode(ctx, puo.driver, _spec); err != nil { 645 if _, ok := err.(*sqlgraph.NotFoundError); ok { 646 err = &NotFoundError{page.Label} 647 } else if sqlgraph.IsConstraintError(err) { 648 err = &ConstraintError{err.Error(), err} 649 } 650 return nil, err 651 } 652 return _node, nil 653 }