group_create.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/group"
  8. "code.osinet.fr/fgm/entdemo/ent/user"
  9. "github.com/facebookincubator/ent/dialect/sql/sqlgraph"
  10. "github.com/facebookincubator/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 User 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 User.
  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. // Save creates the Group in the database.
  37. func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) {
  38. if _, ok := gc.mutation.Name(); !ok {
  39. return nil, errors.New("ent: missing required field \"name\"")
  40. }
  41. if v, ok := gc.mutation.Name(); ok {
  42. if err := group.NameValidator(v); err != nil {
  43. return nil, fmt.Errorf("ent: validator failed for field \"name\": %v", err)
  44. }
  45. }
  46. var (
  47. err error
  48. node *Group
  49. )
  50. if len(gc.hooks) == 0 {
  51. node, err = gc.sqlSave(ctx)
  52. } else {
  53. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  54. mutation, ok := m.(*GroupMutation)
  55. if !ok {
  56. return nil, fmt.Errorf("unexpected mutation type %T", m)
  57. }
  58. gc.mutation = mutation
  59. node, err = gc.sqlSave(ctx)
  60. return node, err
  61. })
  62. for i := len(gc.hooks) - 1; i >= 0; i-- {
  63. mut = gc.hooks[i](mut)
  64. }
  65. if _, err := mut.Mutate(ctx, gc.mutation); err != nil {
  66. return nil, err
  67. }
  68. }
  69. return node, err
  70. }
  71. // SaveX calls Save and panics if Save returns an error.
  72. func (gc *GroupCreate) SaveX(ctx context.Context) *Group {
  73. v, err := gc.Save(ctx)
  74. if err != nil {
  75. panic(err)
  76. }
  77. return v
  78. }
  79. func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) {
  80. var (
  81. gr = &Group{config: gc.config}
  82. _spec = &sqlgraph.CreateSpec{
  83. Table: group.Table,
  84. ID: &sqlgraph.FieldSpec{
  85. Type: field.TypeInt,
  86. Column: group.FieldID,
  87. },
  88. }
  89. )
  90. if value, ok := gc.mutation.Name(); ok {
  91. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  92. Type: field.TypeString,
  93. Value: value,
  94. Column: group.FieldName,
  95. })
  96. gr.Name = value
  97. }
  98. if nodes := gc.mutation.UsersIDs(); len(nodes) > 0 {
  99. edge := &sqlgraph.EdgeSpec{
  100. Rel: sqlgraph.M2M,
  101. Inverse: false,
  102. Table: group.UsersTable,
  103. Columns: group.UsersPrimaryKey,
  104. Bidi: false,
  105. Target: &sqlgraph.EdgeTarget{
  106. IDSpec: &sqlgraph.FieldSpec{
  107. Type: field.TypeInt,
  108. Column: user.FieldID,
  109. },
  110. },
  111. }
  112. for _, k := range nodes {
  113. edge.Target.Nodes = append(edge.Target.Nodes, k)
  114. }
  115. _spec.Edges = append(_spec.Edges, edge)
  116. }
  117. if err := sqlgraph.CreateNode(ctx, gc.driver, _spec); err != nil {
  118. if cerr, ok := isSQLConstraintError(err); ok {
  119. err = cerr
  120. }
  121. return nil, err
  122. }
  123. id := _spec.ID.Value.(int64)
  124. gr.ID = int(id)
  125. return gr, nil
  126. }