123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- // Code generated by entc, DO NOT EDIT.
- package ent
- import (
- "context"
- "errors"
- "fmt"
- "code.osinet.fr/fgm/entdemo/ent/group"
- "code.osinet.fr/fgm/entdemo/ent/user"
- "github.com/facebookincubator/ent/dialect/sql/sqlgraph"
- "github.com/facebookincubator/ent/schema/field"
- )
- // GroupCreate is the builder for creating a Group entity.
- type GroupCreate struct {
- config
- mutation *GroupMutation
- hooks []Hook
- }
- // SetName sets the name field.
- func (gc *GroupCreate) SetName(s string) *GroupCreate {
- gc.mutation.SetName(s)
- return gc
- }
- // AddUserIDs adds the users edge to User by ids.
- func (gc *GroupCreate) AddUserIDs(ids ...int) *GroupCreate {
- gc.mutation.AddUserIDs(ids...)
- return gc
- }
- // AddUsers adds the users edges to User.
- func (gc *GroupCreate) AddUsers(u ...*User) *GroupCreate {
- ids := make([]int, len(u))
- for i := range u {
- ids[i] = u[i].ID
- }
- return gc.AddUserIDs(ids...)
- }
- // Save creates the Group in the database.
- func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) {
- if _, ok := gc.mutation.Name(); !ok {
- return nil, errors.New("ent: missing required field \"name\"")
- }
- if v, ok := gc.mutation.Name(); ok {
- if err := group.NameValidator(v); err != nil {
- return nil, fmt.Errorf("ent: validator failed for field \"name\": %v", err)
- }
- }
- var (
- err error
- node *Group
- )
- if len(gc.hooks) == 0 {
- node, err = gc.sqlSave(ctx)
- } else {
- var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
- mutation, ok := m.(*GroupMutation)
- if !ok {
- return nil, fmt.Errorf("unexpected mutation type %T", m)
- }
- gc.mutation = mutation
- node, err = gc.sqlSave(ctx)
- return node, err
- })
- for i := len(gc.hooks) - 1; i >= 0; i-- {
- mut = gc.hooks[i](mut)
- }
- if _, err := mut.Mutate(ctx, gc.mutation); err != nil {
- return nil, err
- }
- }
- return node, err
- }
- // SaveX calls Save and panics if Save returns an error.
- func (gc *GroupCreate) SaveX(ctx context.Context) *Group {
- v, err := gc.Save(ctx)
- if err != nil {
- panic(err)
- }
- return v
- }
- func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) {
- var (
- gr = &Group{config: gc.config}
- _spec = &sqlgraph.CreateSpec{
- Table: group.Table,
- ID: &sqlgraph.FieldSpec{
- Type: field.TypeInt,
- Column: group.FieldID,
- },
- }
- )
- if value, ok := gc.mutation.Name(); ok {
- _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
- Type: field.TypeString,
- Value: value,
- Column: group.FieldName,
- })
- gr.Name = value
- }
- if nodes := gc.mutation.UsersIDs(); len(nodes) > 0 {
- edge := &sqlgraph.EdgeSpec{
- Rel: sqlgraph.M2M,
- Inverse: false,
- Table: group.UsersTable,
- Columns: group.UsersPrimaryKey,
- Bidi: false,
- Target: &sqlgraph.EdgeTarget{
- IDSpec: &sqlgraph.FieldSpec{
- Type: field.TypeInt,
- Column: user.FieldID,
- },
- },
- }
- for _, k := range nodes {
- edge.Target.Nodes = append(edge.Target.Nodes, k)
- }
- _spec.Edges = append(_spec.Edges, edge)
- }
- if err := sqlgraph.CreateNode(ctx, gc.driver, _spec); err != nil {
- if cerr, ok := isSQLConstraintError(err); ok {
- err = cerr
- }
- return nil, err
- }
- id := _spec.ID.Value.(int64)
- gr.ID = int(id)
- return gr, nil
- }
|