user_create.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "code.osinet.fr/fgm/entdemo/ent/car"
  8. "code.osinet.fr/fgm/entdemo/ent/group"
  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. // 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. // AddCarIDs adds the cars edge to Car by ids.
  37. func (uc *UserCreate) AddCarIDs(ids ...int) *UserCreate {
  38. uc.mutation.AddCarIDs(ids...)
  39. return uc
  40. }
  41. // AddCars adds the cars edges to Car.
  42. func (uc *UserCreate) AddCars(c ...*Car) *UserCreate {
  43. ids := make([]int, len(c))
  44. for i := range c {
  45. ids[i] = c[i].ID
  46. }
  47. return uc.AddCarIDs(ids...)
  48. }
  49. // AddGroupIDs adds the groups edge to Group by ids.
  50. func (uc *UserCreate) AddGroupIDs(ids ...int) *UserCreate {
  51. uc.mutation.AddGroupIDs(ids...)
  52. return uc
  53. }
  54. // AddGroups adds the groups edges to Group.
  55. func (uc *UserCreate) AddGroups(g ...*Group) *UserCreate {
  56. ids := make([]int, len(g))
  57. for i := range g {
  58. ids[i] = g[i].ID
  59. }
  60. return uc.AddGroupIDs(ids...)
  61. }
  62. // Save creates the User in the database.
  63. func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
  64. if _, ok := uc.mutation.Age(); !ok {
  65. return nil, errors.New("ent: missing required field \"age\"")
  66. }
  67. if v, ok := uc.mutation.Age(); ok {
  68. if err := user.AgeValidator(v); err != nil {
  69. return nil, fmt.Errorf("ent: validator failed for field \"age\": %v", err)
  70. }
  71. }
  72. if _, ok := uc.mutation.Name(); !ok {
  73. v := user.DefaultName
  74. uc.mutation.SetName(v)
  75. }
  76. var (
  77. err error
  78. node *User
  79. )
  80. if len(uc.hooks) == 0 {
  81. node, err = uc.sqlSave(ctx)
  82. } else {
  83. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  84. mutation, ok := m.(*UserMutation)
  85. if !ok {
  86. return nil, fmt.Errorf("unexpected mutation type %T", m)
  87. }
  88. uc.mutation = mutation
  89. node, err = uc.sqlSave(ctx)
  90. return node, err
  91. })
  92. for i := len(uc.hooks) - 1; i >= 0; i-- {
  93. mut = uc.hooks[i](mut)
  94. }
  95. if _, err := mut.Mutate(ctx, uc.mutation); err != nil {
  96. return nil, err
  97. }
  98. }
  99. return node, err
  100. }
  101. // SaveX calls Save and panics if Save returns an error.
  102. func (uc *UserCreate) SaveX(ctx context.Context) *User {
  103. v, err := uc.Save(ctx)
  104. if err != nil {
  105. panic(err)
  106. }
  107. return v
  108. }
  109. func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
  110. var (
  111. u = &User{config: uc.config}
  112. _spec = &sqlgraph.CreateSpec{
  113. Table: user.Table,
  114. ID: &sqlgraph.FieldSpec{
  115. Type: field.TypeInt,
  116. Column: user.FieldID,
  117. },
  118. }
  119. )
  120. if value, ok := uc.mutation.Age(); ok {
  121. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  122. Type: field.TypeInt,
  123. Value: value,
  124. Column: user.FieldAge,
  125. })
  126. u.Age = value
  127. }
  128. if value, ok := uc.mutation.Name(); ok {
  129. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  130. Type: field.TypeString,
  131. Value: value,
  132. Column: user.FieldName,
  133. })
  134. u.Name = value
  135. }
  136. if nodes := uc.mutation.CarsIDs(); len(nodes) > 0 {
  137. edge := &sqlgraph.EdgeSpec{
  138. Rel: sqlgraph.O2M,
  139. Inverse: false,
  140. Table: user.CarsTable,
  141. Columns: []string{user.CarsColumn},
  142. Bidi: false,
  143. Target: &sqlgraph.EdgeTarget{
  144. IDSpec: &sqlgraph.FieldSpec{
  145. Type: field.TypeInt,
  146. Column: car.FieldID,
  147. },
  148. },
  149. }
  150. for _, k := range nodes {
  151. edge.Target.Nodes = append(edge.Target.Nodes, k)
  152. }
  153. _spec.Edges = append(_spec.Edges, edge)
  154. }
  155. if nodes := uc.mutation.GroupsIDs(); len(nodes) > 0 {
  156. edge := &sqlgraph.EdgeSpec{
  157. Rel: sqlgraph.M2M,
  158. Inverse: true,
  159. Table: user.GroupsTable,
  160. Columns: user.GroupsPrimaryKey,
  161. Bidi: false,
  162. Target: &sqlgraph.EdgeTarget{
  163. IDSpec: &sqlgraph.FieldSpec{
  164. Type: field.TypeInt,
  165. Column: group.FieldID,
  166. },
  167. },
  168. }
  169. for _, k := range nodes {
  170. edge.Target.Nodes = append(edge.Target.Nodes, k)
  171. }
  172. _spec.Edges = append(_spec.Edges, edge)
  173. }
  174. if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil {
  175. if cerr, ok := isSQLConstraintError(err); ok {
  176. err = cerr
  177. }
  178. return nil, err
  179. }
  180. id := _spec.ID.Value.(int64)
  181. u.ID = int(id)
  182. return u, nil
  183. }