// Code generated by entc, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "code.osinet.fr/fgm/entdemo/ent/car" "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" ) // UserCreate is the builder for creating a User entity. type UserCreate struct { config mutation *UserMutation hooks []Hook } // SetAge sets the age field. func (uc *UserCreate) SetAge(i int) *UserCreate { uc.mutation.SetAge(i) return uc } // SetName sets the name field. func (uc *UserCreate) SetName(s string) *UserCreate { uc.mutation.SetName(s) return uc } // SetNillableName sets the name field if the given value is not nil. func (uc *UserCreate) SetNillableName(s *string) *UserCreate { if s != nil { uc.SetName(*s) } return uc } // AddCarIDs adds the cars edge to Car by ids. func (uc *UserCreate) AddCarIDs(ids ...int) *UserCreate { uc.mutation.AddCarIDs(ids...) return uc } // AddCars adds the cars edges to Car. func (uc *UserCreate) AddCars(c ...*Car) *UserCreate { ids := make([]int, len(c)) for i := range c { ids[i] = c[i].ID } return uc.AddCarIDs(ids...) } // AddGroupIDs adds the groups edge to Group by ids. func (uc *UserCreate) AddGroupIDs(ids ...int) *UserCreate { uc.mutation.AddGroupIDs(ids...) return uc } // AddGroups adds the groups edges to Group. func (uc *UserCreate) AddGroups(g ...*Group) *UserCreate { ids := make([]int, len(g)) for i := range g { ids[i] = g[i].ID } return uc.AddGroupIDs(ids...) } // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { if _, ok := uc.mutation.Age(); !ok { return nil, errors.New("ent: missing required field \"age\"") } if v, ok := uc.mutation.Age(); ok { if err := user.AgeValidator(v); err != nil { return nil, fmt.Errorf("ent: validator failed for field \"age\": %v", err) } } if _, ok := uc.mutation.Name(); !ok { v := user.DefaultName uc.mutation.SetName(v) } var ( err error node *User ) if len(uc.hooks) == 0 { node, err = uc.sqlSave(ctx) } else { var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*UserMutation) if !ok { return nil, fmt.Errorf("unexpected mutation type %T", m) } uc.mutation = mutation node, err = uc.sqlSave(ctx) return node, err }) for i := len(uc.hooks) - 1; i >= 0; i-- { mut = uc.hooks[i](mut) } if _, err := mut.Mutate(ctx, uc.mutation); err != nil { return nil, err } } return node, err } // SaveX calls Save and panics if Save returns an error. func (uc *UserCreate) SaveX(ctx context.Context) *User { v, err := uc.Save(ctx) if err != nil { panic(err) } return v } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { var ( u = &User{config: uc.config} _spec = &sqlgraph.CreateSpec{ Table: user.Table, ID: &sqlgraph.FieldSpec{ Type: field.TypeInt, Column: user.FieldID, }, } ) if value, ok := uc.mutation.Age(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeInt, Value: value, Column: user.FieldAge, }) u.Age = value } if value, ok := uc.mutation.Name(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeString, Value: value, Column: user.FieldName, }) u.Name = value } if nodes := uc.mutation.CarsIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: false, Table: user.CarsTable, Columns: []string{user.CarsColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ Type: field.TypeInt, Column: car.FieldID, }, }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } if nodes := uc.mutation.GroupsIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, Inverse: true, Table: user.GroupsTable, Columns: user.GroupsPrimaryKey, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ Type: field.TypeInt, Column: group.FieldID, }, }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if cerr, ok := isSQLConstraintError(err); ok { err = cerr } return nil, err } id := _spec.ID.Value.(int64) u.ID = int(id) return u, nil }