Files
ent/examples/traversal/ent/pet_create.go
2019-12-17 22:45:04 +02:00

164 lines
3.8 KiB
Go

// Copyright (c) Facebook, Inc. and its affiliates. 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 (@generated) by entc, DO NOT EDIT.
package ent
import (
"context"
"errors"
"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
"github.com/facebookincubator/ent/examples/traversal/ent/pet"
"github.com/facebookincubator/ent/examples/traversal/ent/user"
"github.com/facebookincubator/ent/schema/field"
)
// PetCreate is the builder for creating a Pet entity.
type PetCreate struct {
config
name *string
friends map[int]struct{}
owner map[int]struct{}
}
// SetName sets the name field.
func (pc *PetCreate) SetName(s string) *PetCreate {
pc.name = &s
return pc
}
// AddFriendIDs adds the friends edge to Pet by ids.
func (pc *PetCreate) AddFriendIDs(ids ...int) *PetCreate {
if pc.friends == nil {
pc.friends = make(map[int]struct{})
}
for i := range ids {
pc.friends[ids[i]] = struct{}{}
}
return pc
}
// AddFriends adds the friends edges to Pet.
func (pc *PetCreate) AddFriends(p ...*Pet) *PetCreate {
ids := make([]int, len(p))
for i := range p {
ids[i] = p[i].ID
}
return pc.AddFriendIDs(ids...)
}
// SetOwnerID sets the owner edge to User by id.
func (pc *PetCreate) SetOwnerID(id int) *PetCreate {
if pc.owner == nil {
pc.owner = make(map[int]struct{})
}
pc.owner[id] = struct{}{}
return pc
}
// SetNillableOwnerID sets the owner edge to User by id if the given value is not nil.
func (pc *PetCreate) SetNillableOwnerID(id *int) *PetCreate {
if id != nil {
pc = pc.SetOwnerID(*id)
}
return pc
}
// SetOwner sets the owner edge to User.
func (pc *PetCreate) SetOwner(u *User) *PetCreate {
return pc.SetOwnerID(u.ID)
}
// Save creates the Pet in the database.
func (pc *PetCreate) Save(ctx context.Context) (*Pet, error) {
if pc.name == nil {
return nil, errors.New("ent: missing required field \"name\"")
}
if len(pc.owner) > 1 {
return nil, errors.New("ent: multiple assignments on a unique edge \"owner\"")
}
return pc.sqlSave(ctx)
}
// SaveX calls Save and panics if Save returns an error.
func (pc *PetCreate) SaveX(ctx context.Context) *Pet {
v, err := pc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) {
var (
pe = &Pet{config: pc.config}
spec = &sqlgraph.CreateSpec{
Table: pet.Table,
ID: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: pet.FieldID,
},
}
)
if value := pc.name; value != nil {
spec.Fields = append(spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: *value,
Column: pet.FieldName,
})
pe.Name = *value
}
if nodes := pc.friends; len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: false,
Table: pet.FriendsTable,
Columns: pet.FriendsPrimaryKey,
Bidi: true,
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 := pc.owner; len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: pet.OwnerTable,
Columns: []string{pet.OwnerColumn},
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, pc.driver, spec); err != nil {
if cerr, ok := isSQLConstraintError(err); ok {
err = cerr
}
return nil, err
}
id := spec.ID.Value.(int64)
pe.ID = int(id)
return pe, nil
}