mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
@@ -36,14 +36,14 @@ func (gc *GroupCreate) Mutation() *GroupMutation {
|
||||
|
||||
// Save creates the Group in the database.
|
||||
func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) {
|
||||
if err := gc.preSave(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var (
|
||||
err error
|
||||
node *Group
|
||||
)
|
||||
if len(gc.hooks) == 0 {
|
||||
if err = gc.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node, err = gc.sqlSave(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
@@ -51,6 +51,9 @@ func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) {
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
if err = gc.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gc.mutation = mutation
|
||||
node, err = gc.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
@@ -75,7 +78,8 @@ func (gc *GroupCreate) SaveX(ctx context.Context) *Group {
|
||||
return v
|
||||
}
|
||||
|
||||
func (gc *GroupCreate) preSave() error {
|
||||
// check runs all checks and user-defined validators on the builder.
|
||||
func (gc *GroupCreate) check() error {
|
||||
if _, ok := gc.mutation.MaxUsers(); !ok {
|
||||
return &ValidationError{Name: "max_users", err: errors.New("ent: missing required field \"max_users\"")}
|
||||
}
|
||||
@@ -132,13 +136,13 @@ func (gcb *GroupCreateBulk) Save(ctx context.Context) ([]*Group, error) {
|
||||
func(i int, root context.Context) {
|
||||
builder := gcb.builders[i]
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
if err := builder.preSave(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mutation, ok := m.(*GroupMutation)
|
||||
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
|
||||
|
||||
@@ -71,14 +71,14 @@ func (pc *PetCreate) Mutation() *PetMutation {
|
||||
|
||||
// Save creates the Pet in the database.
|
||||
func (pc *PetCreate) Save(ctx context.Context) (*Pet, error) {
|
||||
if err := pc.preSave(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var (
|
||||
err error
|
||||
node *Pet
|
||||
)
|
||||
if len(pc.hooks) == 0 {
|
||||
if err = pc.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node, err = pc.sqlSave(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
@@ -86,6 +86,9 @@ func (pc *PetCreate) Save(ctx context.Context) (*Pet, error) {
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
if err = pc.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pc.mutation = mutation
|
||||
node, err = pc.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
@@ -110,7 +113,8 @@ func (pc *PetCreate) SaveX(ctx context.Context) *Pet {
|
||||
return v
|
||||
}
|
||||
|
||||
func (pc *PetCreate) preSave() error {
|
||||
// check runs all checks and user-defined validators on the builder.
|
||||
func (pc *PetCreate) check() error {
|
||||
if _, ok := pc.mutation.Age(); !ok {
|
||||
return &ValidationError{Name: "age", err: errors.New("ent: missing required field \"age\"")}
|
||||
}
|
||||
@@ -194,13 +198,13 @@ func (pcb *PetCreateBulk) Save(ctx context.Context) ([]*Pet, error) {
|
||||
func(i int, root context.Context) {
|
||||
builder := pcb.builders[i]
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
if err := builder.preSave(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mutation, ok := m.(*PetMutation)
|
||||
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
|
||||
|
||||
@@ -98,7 +98,6 @@ func (pu *PetUpdate) ClearOwner() *PetUpdate {
|
||||
|
||||
// Save executes the query and returns the number of rows/vertices matched by this operation.
|
||||
func (pu *PetUpdate) Save(ctx context.Context) (int, error) {
|
||||
|
||||
var (
|
||||
err error
|
||||
affected int
|
||||
@@ -311,7 +310,6 @@ func (puo *PetUpdateOne) ClearOwner() *PetUpdateOne {
|
||||
|
||||
// Save executes the query and returns the updated entity.
|
||||
func (puo *PetUpdateOne) Save(ctx context.Context) (*Pet, error) {
|
||||
|
||||
var (
|
||||
err error
|
||||
node *Pet
|
||||
|
||||
@@ -67,14 +67,14 @@ func (uc *UserCreate) Mutation() *UserMutation {
|
||||
|
||||
// Save creates the User in the database.
|
||||
func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
|
||||
if err := uc.preSave(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
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) {
|
||||
@@ -82,6 +82,9 @@ func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
|
||||
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
|
||||
@@ -106,7 +109,8 @@ func (uc *UserCreate) SaveX(ctx context.Context) *User {
|
||||
return v
|
||||
}
|
||||
|
||||
func (uc *UserCreate) preSave() error {
|
||||
// check runs all checks and user-defined validators on the builder.
|
||||
func (uc *UserCreate) check() error {
|
||||
if _, ok := uc.mutation.Name(); !ok {
|
||||
return &ValidationError{Name: "name", err: errors.New("ent: missing required field \"name\"")}
|
||||
}
|
||||
@@ -201,13 +205,13 @@ func (ucb *UserCreateBulk) Save(ctx context.Context) ([]*User, error) {
|
||||
func(i int, root context.Context) {
|
||||
builder := ucb.builders[i]
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
if err := builder.preSave(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
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
|
||||
|
||||
@@ -117,7 +117,6 @@ func (uu *UserUpdate) RemoveFriends(u ...*User) *UserUpdate {
|
||||
|
||||
// Save executes the query and returns the number of rows/vertices matched by this operation.
|
||||
func (uu *UserUpdate) Save(ctx context.Context) (int, error) {
|
||||
|
||||
var (
|
||||
err error
|
||||
affected int
|
||||
@@ -403,7 +402,6 @@ func (uuo *UserUpdateOne) RemoveFriends(u ...*User) *UserUpdateOne {
|
||||
|
||||
// Save executes the query and returns the updated entity.
|
||||
func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) {
|
||||
|
||||
var (
|
||||
err error
|
||||
node *User
|
||||
|
||||
Reference in New Issue
Block a user