user_create.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "code.osinet.fr/fgm/go__ent_demo/ent/car"
  8. "code.osinet.fr/fgm/go__ent_demo/ent/group"
  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. // UserCreate is the builder for creating a User entity.
  14. type UserCreate struct {
  15. config
  16. mutation *UserMutation
  17. hooks []Hook
  18. }
  19. // SetAge sets the "age" field.
  20. func (uc *UserCreate) SetAge(i int) *UserCreate {
  21. uc.mutation.SetAge(i)
  22. return uc
  23. }
  24. // SetName sets the "name" field.
  25. func (uc *UserCreate) SetName(s string) *UserCreate {
  26. uc.mutation.SetName(s)
  27. return uc
  28. }
  29. // SetNillableName sets the "name" field if the given value is not nil.
  30. func (uc *UserCreate) SetNillableName(s *string) *UserCreate {
  31. if s != nil {
  32. uc.SetName(*s)
  33. }
  34. return uc
  35. }
  36. // SetPassword sets the "password" field.
  37. func (uc *UserCreate) SetPassword(s string) *UserCreate {
  38. uc.mutation.SetPassword(s)
  39. return uc
  40. }
  41. // SetNillablePassword sets the "password" field if the given value is not nil.
  42. func (uc *UserCreate) SetNillablePassword(s *string) *UserCreate {
  43. if s != nil {
  44. uc.SetPassword(*s)
  45. }
  46. return uc
  47. }
  48. // SetSize sets the "size" field.
  49. func (uc *UserCreate) SetSize(u user.Size) *UserCreate {
  50. uc.mutation.SetSize(u)
  51. return uc
  52. }
  53. // SetNillableSize sets the "size" field if the given value is not nil.
  54. func (uc *UserCreate) SetNillableSize(u *user.Size) *UserCreate {
  55. if u != nil {
  56. uc.SetSize(*u)
  57. }
  58. return uc
  59. }
  60. // SetSpouseID sets the "spouse_id" field.
  61. func (uc *UserCreate) SetSpouseID(i int) *UserCreate {
  62. uc.mutation.SetSpouseID(i)
  63. return uc
  64. }
  65. // SetNillableSpouseID sets the "spouse_id" field if the given value is not nil.
  66. func (uc *UserCreate) SetNillableSpouseID(i *int) *UserCreate {
  67. if i != nil {
  68. uc.SetSpouseID(*i)
  69. }
  70. return uc
  71. }
  72. // AddCarIDs adds the "cars" edge to the Car entity by IDs.
  73. func (uc *UserCreate) AddCarIDs(ids ...int) *UserCreate {
  74. uc.mutation.AddCarIDs(ids...)
  75. return uc
  76. }
  77. // AddCars adds the "cars" edges to the Car entity.
  78. func (uc *UserCreate) AddCars(c ...*Car) *UserCreate {
  79. ids := make([]int, len(c))
  80. for i := range c {
  81. ids[i] = c[i].ID
  82. }
  83. return uc.AddCarIDs(ids...)
  84. }
  85. // AddGroupIDs adds the "groups" edge to the Group entity by IDs.
  86. func (uc *UserCreate) AddGroupIDs(ids ...int) *UserCreate {
  87. uc.mutation.AddGroupIDs(ids...)
  88. return uc
  89. }
  90. // AddGroups adds the "groups" edges to the Group entity.
  91. func (uc *UserCreate) AddGroups(g ...*Group) *UserCreate {
  92. ids := make([]int, len(g))
  93. for i := range g {
  94. ids[i] = g[i].ID
  95. }
  96. return uc.AddGroupIDs(ids...)
  97. }
  98. // SetSpouse sets the "spouse" edge to the User entity.
  99. func (uc *UserCreate) SetSpouse(u *User) *UserCreate {
  100. return uc.SetSpouseID(u.ID)
  101. }
  102. // AddFollowerIDs adds the "followers" edge to the User entity by IDs.
  103. func (uc *UserCreate) AddFollowerIDs(ids ...int) *UserCreate {
  104. uc.mutation.AddFollowerIDs(ids...)
  105. return uc
  106. }
  107. // AddFollowers adds the "followers" edges to the User entity.
  108. func (uc *UserCreate) AddFollowers(u ...*User) *UserCreate {
  109. ids := make([]int, len(u))
  110. for i := range u {
  111. ids[i] = u[i].ID
  112. }
  113. return uc.AddFollowerIDs(ids...)
  114. }
  115. // AddFollowingIDs adds the "following" edge to the User entity by IDs.
  116. func (uc *UserCreate) AddFollowingIDs(ids ...int) *UserCreate {
  117. uc.mutation.AddFollowingIDs(ids...)
  118. return uc
  119. }
  120. // AddFollowing adds the "following" edges to the User entity.
  121. func (uc *UserCreate) AddFollowing(u ...*User) *UserCreate {
  122. ids := make([]int, len(u))
  123. for i := range u {
  124. ids[i] = u[i].ID
  125. }
  126. return uc.AddFollowingIDs(ids...)
  127. }
  128. // AddFriendIDs adds the "friends" edge to the User entity by IDs.
  129. func (uc *UserCreate) AddFriendIDs(ids ...int) *UserCreate {
  130. uc.mutation.AddFriendIDs(ids...)
  131. return uc
  132. }
  133. // AddFriends adds the "friends" edges to the User entity.
  134. func (uc *UserCreate) AddFriends(u ...*User) *UserCreate {
  135. ids := make([]int, len(u))
  136. for i := range u {
  137. ids[i] = u[i].ID
  138. }
  139. return uc.AddFriendIDs(ids...)
  140. }
  141. // Mutation returns the UserMutation object of the builder.
  142. func (uc *UserCreate) Mutation() *UserMutation {
  143. return uc.mutation
  144. }
  145. // Save creates the User in the database.
  146. func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
  147. var (
  148. err error
  149. node *User
  150. )
  151. uc.defaults()
  152. if len(uc.hooks) == 0 {
  153. if err = uc.check(); err != nil {
  154. return nil, err
  155. }
  156. node, err = uc.sqlSave(ctx)
  157. } else {
  158. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  159. mutation, ok := m.(*UserMutation)
  160. if !ok {
  161. return nil, fmt.Errorf("unexpected mutation type %T", m)
  162. }
  163. if err = uc.check(); err != nil {
  164. return nil, err
  165. }
  166. uc.mutation = mutation
  167. if node, err = uc.sqlSave(ctx); err != nil {
  168. return nil, err
  169. }
  170. mutation.id = &node.ID
  171. mutation.done = true
  172. return node, err
  173. })
  174. for i := len(uc.hooks) - 1; i >= 0; i-- {
  175. if uc.hooks[i] == nil {
  176. return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
  177. }
  178. mut = uc.hooks[i](mut)
  179. }
  180. if _, err := mut.Mutate(ctx, uc.mutation); err != nil {
  181. return nil, err
  182. }
  183. }
  184. return node, err
  185. }
  186. // SaveX calls Save and panics if Save returns an error.
  187. func (uc *UserCreate) SaveX(ctx context.Context) *User {
  188. v, err := uc.Save(ctx)
  189. if err != nil {
  190. panic(err)
  191. }
  192. return v
  193. }
  194. // Exec executes the query.
  195. func (uc *UserCreate) Exec(ctx context.Context) error {
  196. _, err := uc.Save(ctx)
  197. return err
  198. }
  199. // ExecX is like Exec, but panics if an error occurs.
  200. func (uc *UserCreate) ExecX(ctx context.Context) {
  201. if err := uc.Exec(ctx); err != nil {
  202. panic(err)
  203. }
  204. }
  205. // defaults sets the default values of the builder before save.
  206. func (uc *UserCreate) defaults() {
  207. if _, ok := uc.mutation.Name(); !ok {
  208. v := user.DefaultName
  209. uc.mutation.SetName(v)
  210. }
  211. if _, ok := uc.mutation.Size(); !ok {
  212. v := user.DefaultSize
  213. uc.mutation.SetSize(v)
  214. }
  215. }
  216. // check runs all checks and user-defined validators on the builder.
  217. func (uc *UserCreate) check() error {
  218. if _, ok := uc.mutation.Age(); !ok {
  219. return &ValidationError{Name: "age", err: errors.New(`ent: missing required field "User.age"`)}
  220. }
  221. if v, ok := uc.mutation.Age(); ok {
  222. if err := user.AgeValidator(v); err != nil {
  223. return &ValidationError{Name: "age", err: fmt.Errorf(`ent: validator failed for field "User.age": %w`, err)}
  224. }
  225. }
  226. if _, ok := uc.mutation.Name(); !ok {
  227. return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "User.name"`)}
  228. }
  229. if _, ok := uc.mutation.Size(); !ok {
  230. return &ValidationError{Name: "size", err: errors.New(`ent: missing required field "User.size"`)}
  231. }
  232. if v, ok := uc.mutation.Size(); ok {
  233. if err := user.SizeValidator(v); err != nil {
  234. return &ValidationError{Name: "size", err: fmt.Errorf(`ent: validator failed for field "User.size": %w`, err)}
  235. }
  236. }
  237. return nil
  238. }
  239. func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
  240. _node, _spec := uc.createSpec()
  241. if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil {
  242. if sqlgraph.IsConstraintError(err) {
  243. err = &ConstraintError{err.Error(), err}
  244. }
  245. return nil, err
  246. }
  247. id := _spec.ID.Value.(int64)
  248. _node.ID = int(id)
  249. return _node, nil
  250. }
  251. func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) {
  252. var (
  253. _node = &User{config: uc.config}
  254. _spec = &sqlgraph.CreateSpec{
  255. Table: user.Table,
  256. ID: &sqlgraph.FieldSpec{
  257. Type: field.TypeInt,
  258. Column: user.FieldID,
  259. },
  260. }
  261. )
  262. if value, ok := uc.mutation.Age(); ok {
  263. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  264. Type: field.TypeInt,
  265. Value: value,
  266. Column: user.FieldAge,
  267. })
  268. _node.Age = value
  269. }
  270. if value, ok := uc.mutation.Name(); ok {
  271. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  272. Type: field.TypeString,
  273. Value: value,
  274. Column: user.FieldName,
  275. })
  276. _node.Name = value
  277. }
  278. if value, ok := uc.mutation.Password(); ok {
  279. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  280. Type: field.TypeString,
  281. Value: value,
  282. Column: user.FieldPassword,
  283. })
  284. _node.Password = value
  285. }
  286. if value, ok := uc.mutation.Size(); ok {
  287. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  288. Type: field.TypeEnum,
  289. Value: value,
  290. Column: user.FieldSize,
  291. })
  292. _node.Size = value
  293. }
  294. if nodes := uc.mutation.CarsIDs(); len(nodes) > 0 {
  295. edge := &sqlgraph.EdgeSpec{
  296. Rel: sqlgraph.O2M,
  297. Inverse: false,
  298. Table: user.CarsTable,
  299. Columns: []string{user.CarsColumn},
  300. Bidi: false,
  301. Target: &sqlgraph.EdgeTarget{
  302. IDSpec: &sqlgraph.FieldSpec{
  303. Type: field.TypeInt,
  304. Column: car.FieldID,
  305. },
  306. },
  307. }
  308. for _, k := range nodes {
  309. edge.Target.Nodes = append(edge.Target.Nodes, k)
  310. }
  311. _spec.Edges = append(_spec.Edges, edge)
  312. }
  313. if nodes := uc.mutation.GroupsIDs(); len(nodes) > 0 {
  314. edge := &sqlgraph.EdgeSpec{
  315. Rel: sqlgraph.M2M,
  316. Inverse: true,
  317. Table: user.GroupsTable,
  318. Columns: user.GroupsPrimaryKey,
  319. Bidi: false,
  320. Target: &sqlgraph.EdgeTarget{
  321. IDSpec: &sqlgraph.FieldSpec{
  322. Type: field.TypeInt,
  323. Column: group.FieldID,
  324. },
  325. },
  326. }
  327. for _, k := range nodes {
  328. edge.Target.Nodes = append(edge.Target.Nodes, k)
  329. }
  330. _spec.Edges = append(_spec.Edges, edge)
  331. }
  332. if nodes := uc.mutation.SpouseIDs(); len(nodes) > 0 {
  333. edge := &sqlgraph.EdgeSpec{
  334. Rel: sqlgraph.O2O,
  335. Inverse: false,
  336. Table: user.SpouseTable,
  337. Columns: []string{user.SpouseColumn},
  338. Bidi: true,
  339. Target: &sqlgraph.EdgeTarget{
  340. IDSpec: &sqlgraph.FieldSpec{
  341. Type: field.TypeInt,
  342. Column: user.FieldID,
  343. },
  344. },
  345. }
  346. for _, k := range nodes {
  347. edge.Target.Nodes = append(edge.Target.Nodes, k)
  348. }
  349. _node.SpouseID = nodes[0]
  350. _spec.Edges = append(_spec.Edges, edge)
  351. }
  352. if nodes := uc.mutation.FollowersIDs(); len(nodes) > 0 {
  353. edge := &sqlgraph.EdgeSpec{
  354. Rel: sqlgraph.M2M,
  355. Inverse: true,
  356. Table: user.FollowersTable,
  357. Columns: user.FollowersPrimaryKey,
  358. Bidi: false,
  359. Target: &sqlgraph.EdgeTarget{
  360. IDSpec: &sqlgraph.FieldSpec{
  361. Type: field.TypeInt,
  362. Column: user.FieldID,
  363. },
  364. },
  365. }
  366. for _, k := range nodes {
  367. edge.Target.Nodes = append(edge.Target.Nodes, k)
  368. }
  369. _spec.Edges = append(_spec.Edges, edge)
  370. }
  371. if nodes := uc.mutation.FollowingIDs(); len(nodes) > 0 {
  372. edge := &sqlgraph.EdgeSpec{
  373. Rel: sqlgraph.M2M,
  374. Inverse: false,
  375. Table: user.FollowingTable,
  376. Columns: user.FollowingPrimaryKey,
  377. Bidi: false,
  378. Target: &sqlgraph.EdgeTarget{
  379. IDSpec: &sqlgraph.FieldSpec{
  380. Type: field.TypeInt,
  381. Column: user.FieldID,
  382. },
  383. },
  384. }
  385. for _, k := range nodes {
  386. edge.Target.Nodes = append(edge.Target.Nodes, k)
  387. }
  388. _spec.Edges = append(_spec.Edges, edge)
  389. }
  390. if nodes := uc.mutation.FriendsIDs(); len(nodes) > 0 {
  391. edge := &sqlgraph.EdgeSpec{
  392. Rel: sqlgraph.M2M,
  393. Inverse: false,
  394. Table: user.FriendsTable,
  395. Columns: user.FriendsPrimaryKey,
  396. Bidi: true,
  397. Target: &sqlgraph.EdgeTarget{
  398. IDSpec: &sqlgraph.FieldSpec{
  399. Type: field.TypeInt,
  400. Column: user.FieldID,
  401. },
  402. },
  403. }
  404. for _, k := range nodes {
  405. edge.Target.Nodes = append(edge.Target.Nodes, k)
  406. }
  407. _spec.Edges = append(_spec.Edges, edge)
  408. }
  409. return _node, _spec
  410. }
  411. // UserCreateBulk is the builder for creating many User entities in bulk.
  412. type UserCreateBulk struct {
  413. config
  414. builders []*UserCreate
  415. }
  416. // Save creates the User entities in the database.
  417. func (ucb *UserCreateBulk) Save(ctx context.Context) ([]*User, error) {
  418. specs := make([]*sqlgraph.CreateSpec, len(ucb.builders))
  419. nodes := make([]*User, len(ucb.builders))
  420. mutators := make([]Mutator, len(ucb.builders))
  421. for i := range ucb.builders {
  422. func(i int, root context.Context) {
  423. builder := ucb.builders[i]
  424. builder.defaults()
  425. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  426. mutation, ok := m.(*UserMutation)
  427. if !ok {
  428. return nil, fmt.Errorf("unexpected mutation type %T", m)
  429. }
  430. if err := builder.check(); err != nil {
  431. return nil, err
  432. }
  433. builder.mutation = mutation
  434. nodes[i], specs[i] = builder.createSpec()
  435. var err error
  436. if i < len(mutators)-1 {
  437. _, err = mutators[i+1].Mutate(root, ucb.builders[i+1].mutation)
  438. } else {
  439. spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
  440. // Invoke the actual operation on the latest mutation in the chain.
  441. if err = sqlgraph.BatchCreate(ctx, ucb.driver, spec); err != nil {
  442. if sqlgraph.IsConstraintError(err) {
  443. err = &ConstraintError{err.Error(), err}
  444. }
  445. }
  446. }
  447. if err != nil {
  448. return nil, err
  449. }
  450. mutation.id = &nodes[i].ID
  451. mutation.done = true
  452. if specs[i].ID.Value != nil {
  453. id := specs[i].ID.Value.(int64)
  454. nodes[i].ID = int(id)
  455. }
  456. return nodes[i], nil
  457. })
  458. for i := len(builder.hooks) - 1; i >= 0; i-- {
  459. mut = builder.hooks[i](mut)
  460. }
  461. mutators[i] = mut
  462. }(i, ctx)
  463. }
  464. if len(mutators) > 0 {
  465. if _, err := mutators[0].Mutate(ctx, ucb.builders[0].mutation); err != nil {
  466. return nil, err
  467. }
  468. }
  469. return nodes, nil
  470. }
  471. // SaveX is like Save, but panics if an error occurs.
  472. func (ucb *UserCreateBulk) SaveX(ctx context.Context) []*User {
  473. v, err := ucb.Save(ctx)
  474. if err != nil {
  475. panic(err)
  476. }
  477. return v
  478. }
  479. // Exec executes the query.
  480. func (ucb *UserCreateBulk) Exec(ctx context.Context) error {
  481. _, err := ucb.Save(ctx)
  482. return err
  483. }
  484. // ExecX is like Exec, but panics if an error occurs.
  485. func (ucb *UserCreateBulk) ExecX(ctx context.Context) {
  486. if err := ucb.Exec(ctx); err != nil {
  487. panic(err)
  488. }
  489. }