car_create.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "time"
  8. "code.osinet.fr/fgm/entdemo/ent/car"
  9. "code.osinet.fr/fgm/entdemo/ent/user"
  10. "github.com/facebookincubator/ent/dialect/sql/sqlgraph"
  11. "github.com/facebookincubator/ent/schema/field"
  12. )
  13. // CarCreate is the builder for creating a Car entity.
  14. type CarCreate struct {
  15. config
  16. mutation *CarMutation
  17. hooks []Hook
  18. }
  19. // SetModel sets the model field.
  20. func (cc *CarCreate) SetModel(s string) *CarCreate {
  21. cc.mutation.SetModel(s)
  22. return cc
  23. }
  24. // SetRegisteredAt sets the registered_at field.
  25. func (cc *CarCreate) SetRegisteredAt(t time.Time) *CarCreate {
  26. cc.mutation.SetRegisteredAt(t)
  27. return cc
  28. }
  29. // SetOwnerID sets the owner edge to User by id.
  30. func (cc *CarCreate) SetOwnerID(id int) *CarCreate {
  31. cc.mutation.SetOwnerID(id)
  32. return cc
  33. }
  34. // SetNillableOwnerID sets the owner edge to User by id if the given value is not nil.
  35. func (cc *CarCreate) SetNillableOwnerID(id *int) *CarCreate {
  36. if id != nil {
  37. cc = cc.SetOwnerID(*id)
  38. }
  39. return cc
  40. }
  41. // SetOwner sets the owner edge to User.
  42. func (cc *CarCreate) SetOwner(u *User) *CarCreate {
  43. return cc.SetOwnerID(u.ID)
  44. }
  45. // Save creates the Car in the database.
  46. func (cc *CarCreate) Save(ctx context.Context) (*Car, error) {
  47. if _, ok := cc.mutation.Model(); !ok {
  48. return nil, errors.New("ent: missing required field \"model\"")
  49. }
  50. if _, ok := cc.mutation.RegisteredAt(); !ok {
  51. return nil, errors.New("ent: missing required field \"registered_at\"")
  52. }
  53. var (
  54. err error
  55. node *Car
  56. )
  57. if len(cc.hooks) == 0 {
  58. node, err = cc.sqlSave(ctx)
  59. } else {
  60. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  61. mutation, ok := m.(*CarMutation)
  62. if !ok {
  63. return nil, fmt.Errorf("unexpected mutation type %T", m)
  64. }
  65. cc.mutation = mutation
  66. node, err = cc.sqlSave(ctx)
  67. return node, err
  68. })
  69. for i := len(cc.hooks) - 1; i >= 0; i-- {
  70. mut = cc.hooks[i](mut)
  71. }
  72. if _, err := mut.Mutate(ctx, cc.mutation); err != nil {
  73. return nil, err
  74. }
  75. }
  76. return node, err
  77. }
  78. // SaveX calls Save and panics if Save returns an error.
  79. func (cc *CarCreate) SaveX(ctx context.Context) *Car {
  80. v, err := cc.Save(ctx)
  81. if err != nil {
  82. panic(err)
  83. }
  84. return v
  85. }
  86. func (cc *CarCreate) sqlSave(ctx context.Context) (*Car, error) {
  87. var (
  88. c = &Car{config: cc.config}
  89. _spec = &sqlgraph.CreateSpec{
  90. Table: car.Table,
  91. ID: &sqlgraph.FieldSpec{
  92. Type: field.TypeInt,
  93. Column: car.FieldID,
  94. },
  95. }
  96. )
  97. if value, ok := cc.mutation.Model(); ok {
  98. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  99. Type: field.TypeString,
  100. Value: value,
  101. Column: car.FieldModel,
  102. })
  103. c.Model = value
  104. }
  105. if value, ok := cc.mutation.RegisteredAt(); ok {
  106. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  107. Type: field.TypeTime,
  108. Value: value,
  109. Column: car.FieldRegisteredAt,
  110. })
  111. c.RegisteredAt = value
  112. }
  113. if nodes := cc.mutation.OwnerIDs(); len(nodes) > 0 {
  114. edge := &sqlgraph.EdgeSpec{
  115. Rel: sqlgraph.M2O,
  116. Inverse: true,
  117. Table: car.OwnerTable,
  118. Columns: []string{car.OwnerColumn},
  119. Bidi: false,
  120. Target: &sqlgraph.EdgeTarget{
  121. IDSpec: &sqlgraph.FieldSpec{
  122. Type: field.TypeInt,
  123. Column: user.FieldID,
  124. },
  125. },
  126. }
  127. for _, k := range nodes {
  128. edge.Target.Nodes = append(edge.Target.Nodes, k)
  129. }
  130. _spec.Edges = append(_spec.Edges, edge)
  131. }
  132. if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil {
  133. if cerr, ok := isSQLConstraintError(err); ok {
  134. err = cerr
  135. }
  136. return nil, err
  137. }
  138. id := _spec.ID.Value.(int64)
  139. c.ID = int(id)
  140. return c, nil
  141. }