// Copyright 2019-present Facebook Inc. All rights reserved. // This source code is licensed under the Apache 2.0 license found // in the LICENSE file in the root directory of this source tree. // Code generated by entc, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "github.com/facebook/ent/dialect/sql/sqlgraph" "github.com/facebook/ent/examples/traversal/ent/group" "github.com/facebook/ent/examples/traversal/ent/pet" "github.com/facebook/ent/examples/traversal/ent/user" "github.com/facebook/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 } // AddPetIDs adds the pets edge to Pet by ids. func (uc *UserCreate) AddPetIDs(ids ...int) *UserCreate { uc.mutation.AddPetIDs(ids...) return uc } // AddPets adds the pets edges to Pet. func (uc *UserCreate) AddPets(p ...*Pet) *UserCreate { ids := make([]int, len(p)) for i := range p { ids[i] = p[i].ID } return uc.AddPetIDs(ids...) } // AddFriendIDs adds the friends edge to User by ids. func (uc *UserCreate) AddFriendIDs(ids ...int) *UserCreate { uc.mutation.AddFriendIDs(ids...) return uc } // AddFriends adds the friends edges to User. func (uc *UserCreate) AddFriends(u ...*User) *UserCreate { ids := make([]int, len(u)) for i := range u { ids[i] = u[i].ID } return uc.AddFriendIDs(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...) } // AddManageIDs adds the manage edge to Group by ids. func (uc *UserCreate) AddManageIDs(ids ...int) *UserCreate { uc.mutation.AddManageIDs(ids...) return uc } // AddManage adds the manage edges to Group. func (uc *UserCreate) AddManage(g ...*Group) *UserCreate { ids := make([]int, len(g)) for i := range g { ids[i] = g[i].ID } return uc.AddManageIDs(ids...) } // Mutation returns the UserMutation object of the builder. func (uc *UserCreate) Mutation() *UserMutation { return uc.mutation } // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { var ( err error node *User ) if len(uc.hooks) == 0 { if err = uc.check(); err != nil { return nil, err } 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) } if err = uc.check(); err != nil { return nil, err } uc.mutation = mutation node, err = uc.sqlSave(ctx) mutation.done = true 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 } // check runs all checks and user-defined validators on the builder. func (uc *UserCreate) check() error { if _, ok := uc.mutation.Age(); !ok { return &ValidationError{Name: "age", err: errors.New("ent: missing required field \"age\"")} } if _, ok := uc.mutation.Name(); !ok { return &ValidationError{Name: "name", err: errors.New("ent: missing required field \"name\"")} } return nil } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { _node, _spec := uc.createSpec() 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) _node.ID = int(id) return _node, nil } func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) { var ( _node = &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, }) _node.Age = value } if value, ok := uc.mutation.Name(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeString, Value: value, Column: user.FieldName, }) _node.Name = value } if nodes := uc.mutation.PetsIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: false, Table: user.PetsTable, Columns: []string{user.PetsColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ Type: field.TypeInt, Column: pet.FieldID, }, }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } if nodes := uc.mutation.FriendsIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, Inverse: false, Table: user.FriendsTable, Columns: user.FriendsPrimaryKey, Bidi: true, 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 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 nodes := uc.mutation.ManageIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: true, Table: user.ManageTable, Columns: []string{user.ManageColumn}, 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) } return _node, _spec } // UserCreateBulk is the builder for creating a bulk of User entities. type UserCreateBulk struct { config builders []*UserCreate } // Save creates the User entities in the database. func (ucb *UserCreateBulk) Save(ctx context.Context) ([]*User, error) { specs := make([]*sqlgraph.CreateSpec, len(ucb.builders)) nodes := make([]*User, len(ucb.builders)) mutators := make([]Mutator, len(ucb.builders)) for i := range ucb.builders { func(i int, root context.Context) { builder := ucb.builders[i] 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) } if err := builder.check(); err != nil { return nil, err } builder.mutation = mutation nodes[i], specs[i] = builder.createSpec() var err error if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, ucb.builders[i+1].mutation) } else { // Invoke the actual operation on the latest mutation in the chain. if err = sqlgraph.BatchCreate(ctx, ucb.driver, &sqlgraph.BatchCreateSpec{Nodes: specs}); err != nil { if cerr, ok := isSQLConstraintError(err); ok { err = cerr } } } mutation.done = true if err != nil { return nil, err } id := specs[i].ID.Value.(int64) nodes[i].ID = int(id) return nodes[i], nil }) for i := len(builder.hooks) - 1; i >= 0; i-- { mut = builder.hooks[i](mut) } mutators[i] = mut }(i, ctx) } if len(mutators) > 0 { if _, err := mutators[0].Mutate(ctx, ucb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX calls Save and panics if Save returns an error. func (ucb *UserCreateBulk) SaveX(ctx context.Context) []*User { v, err := ucb.Save(ctx) if err != nil { panic(err) } return v }