mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
@@ -61,14 +61,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) {
|
||||
@@ -76,6 +76,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
|
||||
@@ -100,7 +103,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\"")}
|
||||
}
|
||||
@@ -187,13 +191,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
|
||||
|
||||
@@ -82,7 +82,6 @@ func (uu *UserUpdate) ClearSpouse() *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
|
||||
@@ -275,7 +274,6 @@ func (uuo *UserUpdateOne) ClearSpouse() *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