mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
@@ -52,14 +52,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) {
|
||||
@@ -67,6 +67,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
|
||||
@@ -91,7 +94,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.Name(); !ok {
|
||||
return &ValidationError{Name: "name", err: errors.New("ent: missing required field \"name\"")}
|
||||
}
|
||||
@@ -167,13 +171,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
|
||||
|
||||
@@ -81,7 +81,6 @@ func (gu *GroupUpdate) RemoveUsers(u ...*User) *GroupUpdate {
|
||||
|
||||
// Save executes the query and returns the number of rows/vertices matched by this operation.
|
||||
func (gu *GroupUpdate) Save(ctx context.Context) (int, error) {
|
||||
|
||||
var (
|
||||
err error
|
||||
affected int
|
||||
@@ -277,7 +276,6 @@ func (guo *GroupUpdateOne) RemoveUsers(u ...*User) *GroupUpdateOne {
|
||||
|
||||
// Save executes the query and returns the updated entity.
|
||||
func (guo *GroupUpdateOne) Save(ctx context.Context) (*Group, error) {
|
||||
|
||||
var (
|
||||
err error
|
||||
node *Group
|
||||
|
||||
@@ -58,14 +58,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) {
|
||||
@@ -73,6 +73,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
|
||||
@@ -97,7 +100,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.Age(); !ok {
|
||||
return &ValidationError{Name: "age", err: errors.New("ent: missing required field \"age\"")}
|
||||
}
|
||||
@@ -184,13 +188,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
|
||||
|
||||
@@ -94,7 +94,6 @@ func (uu *UserUpdate) RemoveGroups(g ...*Group) *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
|
||||
@@ -317,7 +316,6 @@ func (uuo *UserUpdateOne) RemoveGroups(g ...*Group) *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