car_create.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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/go__ent_demo/ent/car"
  9. "code.osinet.fr/fgm/go__ent_demo/ent/user"
  10. "entgo.io/ent/dialect/sql/sqlgraph"
  11. "entgo.io/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 the User entity 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 the User entity 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 the User entity.
  42. func (cc *CarCreate) SetOwner(u *User) *CarCreate {
  43. return cc.SetOwnerID(u.ID)
  44. }
  45. // Mutation returns the CarMutation object of the builder.
  46. func (cc *CarCreate) Mutation() *CarMutation {
  47. return cc.mutation
  48. }
  49. // Save creates the Car in the database.
  50. func (cc *CarCreate) Save(ctx context.Context) (*Car, error) {
  51. var (
  52. err error
  53. node *Car
  54. )
  55. if len(cc.hooks) == 0 {
  56. if err = cc.check(); err != nil {
  57. return nil, err
  58. }
  59. node, err = cc.sqlSave(ctx)
  60. } else {
  61. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  62. mutation, ok := m.(*CarMutation)
  63. if !ok {
  64. return nil, fmt.Errorf("unexpected mutation type %T", m)
  65. }
  66. if err = cc.check(); err != nil {
  67. return nil, err
  68. }
  69. cc.mutation = mutation
  70. if node, err = cc.sqlSave(ctx); err != nil {
  71. return nil, err
  72. }
  73. mutation.id = &node.ID
  74. mutation.done = true
  75. return node, err
  76. })
  77. for i := len(cc.hooks) - 1; i >= 0; i-- {
  78. if cc.hooks[i] == nil {
  79. return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
  80. }
  81. mut = cc.hooks[i](mut)
  82. }
  83. if _, err := mut.Mutate(ctx, cc.mutation); err != nil {
  84. return nil, err
  85. }
  86. }
  87. return node, err
  88. }
  89. // SaveX calls Save and panics if Save returns an error.
  90. func (cc *CarCreate) SaveX(ctx context.Context) *Car {
  91. v, err := cc.Save(ctx)
  92. if err != nil {
  93. panic(err)
  94. }
  95. return v
  96. }
  97. // Exec executes the query.
  98. func (cc *CarCreate) Exec(ctx context.Context) error {
  99. _, err := cc.Save(ctx)
  100. return err
  101. }
  102. // ExecX is like Exec, but panics if an error occurs.
  103. func (cc *CarCreate) ExecX(ctx context.Context) {
  104. if err := cc.Exec(ctx); err != nil {
  105. panic(err)
  106. }
  107. }
  108. // check runs all checks and user-defined validators on the builder.
  109. func (cc *CarCreate) check() error {
  110. if _, ok := cc.mutation.Model(); !ok {
  111. return &ValidationError{Name: "model", err: errors.New(`ent: missing required field "Car.model"`)}
  112. }
  113. if _, ok := cc.mutation.RegisteredAt(); !ok {
  114. return &ValidationError{Name: "registered_at", err: errors.New(`ent: missing required field "Car.registered_at"`)}
  115. }
  116. return nil
  117. }
  118. func (cc *CarCreate) sqlSave(ctx context.Context) (*Car, error) {
  119. _node, _spec := cc.createSpec()
  120. if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil {
  121. if sqlgraph.IsConstraintError(err) {
  122. err = &ConstraintError{err.Error(), err}
  123. }
  124. return nil, err
  125. }
  126. id := _spec.ID.Value.(int64)
  127. _node.ID = int(id)
  128. return _node, nil
  129. }
  130. func (cc *CarCreate) createSpec() (*Car, *sqlgraph.CreateSpec) {
  131. var (
  132. _node = &Car{config: cc.config}
  133. _spec = &sqlgraph.CreateSpec{
  134. Table: car.Table,
  135. ID: &sqlgraph.FieldSpec{
  136. Type: field.TypeInt,
  137. Column: car.FieldID,
  138. },
  139. }
  140. )
  141. if value, ok := cc.mutation.Model(); ok {
  142. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  143. Type: field.TypeString,
  144. Value: value,
  145. Column: car.FieldModel,
  146. })
  147. _node.Model = value
  148. }
  149. if value, ok := cc.mutation.RegisteredAt(); ok {
  150. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  151. Type: field.TypeTime,
  152. Value: value,
  153. Column: car.FieldRegisteredAt,
  154. })
  155. _node.RegisteredAt = value
  156. }
  157. if nodes := cc.mutation.OwnerIDs(); len(nodes) > 0 {
  158. edge := &sqlgraph.EdgeSpec{
  159. Rel: sqlgraph.M2O,
  160. Inverse: true,
  161. Table: car.OwnerTable,
  162. Columns: []string{car.OwnerColumn},
  163. Bidi: false,
  164. Target: &sqlgraph.EdgeTarget{
  165. IDSpec: &sqlgraph.FieldSpec{
  166. Type: field.TypeInt,
  167. Column: user.FieldID,
  168. },
  169. },
  170. }
  171. for _, k := range nodes {
  172. edge.Target.Nodes = append(edge.Target.Nodes, k)
  173. }
  174. _node.user_cars = &nodes[0]
  175. _spec.Edges = append(_spec.Edges, edge)
  176. }
  177. return _node, _spec
  178. }
  179. // CarCreateBulk is the builder for creating many Car entities in bulk.
  180. type CarCreateBulk struct {
  181. config
  182. builders []*CarCreate
  183. }
  184. // Save creates the Car entities in the database.
  185. func (ccb *CarCreateBulk) Save(ctx context.Context) ([]*Car, error) {
  186. specs := make([]*sqlgraph.CreateSpec, len(ccb.builders))
  187. nodes := make([]*Car, len(ccb.builders))
  188. mutators := make([]Mutator, len(ccb.builders))
  189. for i := range ccb.builders {
  190. func(i int, root context.Context) {
  191. builder := ccb.builders[i]
  192. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  193. mutation, ok := m.(*CarMutation)
  194. if !ok {
  195. return nil, fmt.Errorf("unexpected mutation type %T", m)
  196. }
  197. if err := builder.check(); err != nil {
  198. return nil, err
  199. }
  200. builder.mutation = mutation
  201. nodes[i], specs[i] = builder.createSpec()
  202. var err error
  203. if i < len(mutators)-1 {
  204. _, err = mutators[i+1].Mutate(root, ccb.builders[i+1].mutation)
  205. } else {
  206. spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
  207. // Invoke the actual operation on the latest mutation in the chain.
  208. if err = sqlgraph.BatchCreate(ctx, ccb.driver, spec); err != nil {
  209. if sqlgraph.IsConstraintError(err) {
  210. err = &ConstraintError{err.Error(), err}
  211. }
  212. }
  213. }
  214. if err != nil {
  215. return nil, err
  216. }
  217. mutation.id = &nodes[i].ID
  218. mutation.done = true
  219. if specs[i].ID.Value != nil {
  220. id := specs[i].ID.Value.(int64)
  221. nodes[i].ID = int(id)
  222. }
  223. return nodes[i], nil
  224. })
  225. for i := len(builder.hooks) - 1; i >= 0; i-- {
  226. mut = builder.hooks[i](mut)
  227. }
  228. mutators[i] = mut
  229. }(i, ctx)
  230. }
  231. if len(mutators) > 0 {
  232. if _, err := mutators[0].Mutate(ctx, ccb.builders[0].mutation); err != nil {
  233. return nil, err
  234. }
  235. }
  236. return nodes, nil
  237. }
  238. // SaveX is like Save, but panics if an error occurs.
  239. func (ccb *CarCreateBulk) SaveX(ctx context.Context) []*Car {
  240. v, err := ccb.Save(ctx)
  241. if err != nil {
  242. panic(err)
  243. }
  244. return v
  245. }
  246. // Exec executes the query.
  247. func (ccb *CarCreateBulk) Exec(ctx context.Context) error {
  248. _, err := ccb.Save(ctx)
  249. return err
  250. }
  251. // ExecX is like Exec, but panics if an error occurs.
  252. func (ccb *CarCreateBulk) ExecX(ctx context.Context) {
  253. if err := ccb.Exec(ctx); err != nil {
  254. panic(err)
  255. }
  256. }