group_create.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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/group"
  8. "code.osinet.fr/fgm/go__ent_demo/ent/user"
  9. "entgo.io/ent/dialect/sql/sqlgraph"
  10. "entgo.io/ent/schema/field"
  11. )
  12. // GroupCreate is the builder for creating a Group entity.
  13. type GroupCreate struct {
  14. config
  15. mutation *GroupMutation
  16. hooks []Hook
  17. }
  18. // SetName sets the "name" field.
  19. func (gc *GroupCreate) SetName(s string) *GroupCreate {
  20. gc.mutation.SetName(s)
  21. return gc
  22. }
  23. // AddUserIDs adds the "users" edge to the User entity by IDs.
  24. func (gc *GroupCreate) AddUserIDs(ids ...int) *GroupCreate {
  25. gc.mutation.AddUserIDs(ids...)
  26. return gc
  27. }
  28. // AddUsers adds the "users" edges to the User entity.
  29. func (gc *GroupCreate) AddUsers(u ...*User) *GroupCreate {
  30. ids := make([]int, len(u))
  31. for i := range u {
  32. ids[i] = u[i].ID
  33. }
  34. return gc.AddUserIDs(ids...)
  35. }
  36. // Mutation returns the GroupMutation object of the builder.
  37. func (gc *GroupCreate) Mutation() *GroupMutation {
  38. return gc.mutation
  39. }
  40. // Save creates the Group in the database.
  41. func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) {
  42. var (
  43. err error
  44. node *Group
  45. )
  46. if len(gc.hooks) == 0 {
  47. if err = gc.check(); err != nil {
  48. return nil, err
  49. }
  50. node, err = gc.sqlSave(ctx)
  51. } else {
  52. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  53. mutation, ok := m.(*GroupMutation)
  54. if !ok {
  55. return nil, fmt.Errorf("unexpected mutation type %T", m)
  56. }
  57. if err = gc.check(); err != nil {
  58. return nil, err
  59. }
  60. gc.mutation = mutation
  61. if node, err = gc.sqlSave(ctx); err != nil {
  62. return nil, err
  63. }
  64. mutation.id = &node.ID
  65. mutation.done = true
  66. return node, err
  67. })
  68. for i := len(gc.hooks) - 1; i >= 0; i-- {
  69. if gc.hooks[i] == nil {
  70. return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
  71. }
  72. mut = gc.hooks[i](mut)
  73. }
  74. if _, err := mut.Mutate(ctx, gc.mutation); err != nil {
  75. return nil, err
  76. }
  77. }
  78. return node, err
  79. }
  80. // SaveX calls Save and panics if Save returns an error.
  81. func (gc *GroupCreate) SaveX(ctx context.Context) *Group {
  82. v, err := gc.Save(ctx)
  83. if err != nil {
  84. panic(err)
  85. }
  86. return v
  87. }
  88. // Exec executes the query.
  89. func (gc *GroupCreate) Exec(ctx context.Context) error {
  90. _, err := gc.Save(ctx)
  91. return err
  92. }
  93. // ExecX is like Exec, but panics if an error occurs.
  94. func (gc *GroupCreate) ExecX(ctx context.Context) {
  95. if err := gc.Exec(ctx); err != nil {
  96. panic(err)
  97. }
  98. }
  99. // check runs all checks and user-defined validators on the builder.
  100. func (gc *GroupCreate) check() error {
  101. if _, ok := gc.mutation.Name(); !ok {
  102. return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Group.name"`)}
  103. }
  104. if v, ok := gc.mutation.Name(); ok {
  105. if err := group.NameValidator(v); err != nil {
  106. return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Group.name": %w`, err)}
  107. }
  108. }
  109. return nil
  110. }
  111. func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) {
  112. _node, _spec := gc.createSpec()
  113. if err := sqlgraph.CreateNode(ctx, gc.driver, _spec); err != nil {
  114. if sqlgraph.IsConstraintError(err) {
  115. err = &ConstraintError{err.Error(), err}
  116. }
  117. return nil, err
  118. }
  119. id := _spec.ID.Value.(int64)
  120. _node.ID = int(id)
  121. return _node, nil
  122. }
  123. func (gc *GroupCreate) createSpec() (*Group, *sqlgraph.CreateSpec) {
  124. var (
  125. _node = &Group{config: gc.config}
  126. _spec = &sqlgraph.CreateSpec{
  127. Table: group.Table,
  128. ID: &sqlgraph.FieldSpec{
  129. Type: field.TypeInt,
  130. Column: group.FieldID,
  131. },
  132. }
  133. )
  134. if value, ok := gc.mutation.Name(); ok {
  135. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  136. Type: field.TypeString,
  137. Value: value,
  138. Column: group.FieldName,
  139. })
  140. _node.Name = value
  141. }
  142. if nodes := gc.mutation.UsersIDs(); len(nodes) > 0 {
  143. edge := &sqlgraph.EdgeSpec{
  144. Rel: sqlgraph.M2M,
  145. Inverse: false,
  146. Table: group.UsersTable,
  147. Columns: group.UsersPrimaryKey,
  148. Bidi: false,
  149. Target: &sqlgraph.EdgeTarget{
  150. IDSpec: &sqlgraph.FieldSpec{
  151. Type: field.TypeInt,
  152. Column: user.FieldID,
  153. },
  154. },
  155. }
  156. for _, k := range nodes {
  157. edge.Target.Nodes = append(edge.Target.Nodes, k)
  158. }
  159. _spec.Edges = append(_spec.Edges, edge)
  160. }
  161. return _node, _spec
  162. }
  163. // GroupCreateBulk is the builder for creating many Group entities in bulk.
  164. type GroupCreateBulk struct {
  165. config
  166. builders []*GroupCreate
  167. }
  168. // Save creates the Group entities in the database.
  169. func (gcb *GroupCreateBulk) Save(ctx context.Context) ([]*Group, error) {
  170. specs := make([]*sqlgraph.CreateSpec, len(gcb.builders))
  171. nodes := make([]*Group, len(gcb.builders))
  172. mutators := make([]Mutator, len(gcb.builders))
  173. for i := range gcb.builders {
  174. func(i int, root context.Context) {
  175. builder := gcb.builders[i]
  176. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  177. mutation, ok := m.(*GroupMutation)
  178. if !ok {
  179. return nil, fmt.Errorf("unexpected mutation type %T", m)
  180. }
  181. if err := builder.check(); err != nil {
  182. return nil, err
  183. }
  184. builder.mutation = mutation
  185. nodes[i], specs[i] = builder.createSpec()
  186. var err error
  187. if i < len(mutators)-1 {
  188. _, err = mutators[i+1].Mutate(root, gcb.builders[i+1].mutation)
  189. } else {
  190. spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
  191. // Invoke the actual operation on the latest mutation in the chain.
  192. if err = sqlgraph.BatchCreate(ctx, gcb.driver, spec); err != nil {
  193. if sqlgraph.IsConstraintError(err) {
  194. err = &ConstraintError{err.Error(), err}
  195. }
  196. }
  197. }
  198. if err != nil {
  199. return nil, err
  200. }
  201. mutation.id = &nodes[i].ID
  202. mutation.done = true
  203. if specs[i].ID.Value != nil {
  204. id := specs[i].ID.Value.(int64)
  205. nodes[i].ID = int(id)
  206. }
  207. return nodes[i], nil
  208. })
  209. for i := len(builder.hooks) - 1; i >= 0; i-- {
  210. mut = builder.hooks[i](mut)
  211. }
  212. mutators[i] = mut
  213. }(i, ctx)
  214. }
  215. if len(mutators) > 0 {
  216. if _, err := mutators[0].Mutate(ctx, gcb.builders[0].mutation); err != nil {
  217. return nil, err
  218. }
  219. }
  220. return nodes, nil
  221. }
  222. // SaveX is like Save, but panics if an error occurs.
  223. func (gcb *GroupCreateBulk) SaveX(ctx context.Context) []*Group {
  224. v, err := gcb.Save(ctx)
  225. if err != nil {
  226. panic(err)
  227. }
  228. return v
  229. }
  230. // Exec executes the query.
  231. func (gcb *GroupCreateBulk) Exec(ctx context.Context) error {
  232. _, err := gcb.Save(ctx)
  233. return err
  234. }
  235. // ExecX is like Exec, but panics if an error occurs.
  236. func (gcb *GroupCreateBulk) ExecX(ctx context.Context) {
  237. if err := gcb.Exec(ctx); err != nil {
  238. panic(err)
  239. }
  240. }