// 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 ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "time" "entgo.io/ent/dialect/gremlin" "entgo.io/ent/dialect/gremlin/graph/dsl" "entgo.io/ent/dialect/gremlin/graph/dsl/__" "entgo.io/ent/dialect/gremlin/graph/dsl/g" "entgo.io/ent/dialect/gremlin/graph/dsl/p" "entgo.io/ent/entc/integration/gremlin/ent/card" "entgo.io/ent/entc/integration/gremlin/ent/spec" "entgo.io/ent/entc/integration/gremlin/ent/user" ) // CardCreate is the builder for creating a Card entity. type CardCreate struct { config mutation *CardMutation hooks []Hook } // SetCreateTime sets the "create_time" field. func (_c *CardCreate) SetCreateTime(v time.Time) *CardCreate { _c.mutation.SetCreateTime(v) return _c } // SetNillableCreateTime sets the "create_time" field if the given value is not nil. func (_c *CardCreate) SetNillableCreateTime(v *time.Time) *CardCreate { if v != nil { _c.SetCreateTime(*v) } return _c } // SetUpdateTime sets the "update_time" field. func (_c *CardCreate) SetUpdateTime(v time.Time) *CardCreate { _c.mutation.SetUpdateTime(v) return _c } // SetNillableUpdateTime sets the "update_time" field if the given value is not nil. func (_c *CardCreate) SetNillableUpdateTime(v *time.Time) *CardCreate { if v != nil { _c.SetUpdateTime(*v) } return _c } // SetBalance sets the "balance" field. func (_c *CardCreate) SetBalance(v float64) *CardCreate { _c.mutation.SetBalance(v) return _c } // SetNillableBalance sets the "balance" field if the given value is not nil. func (_c *CardCreate) SetNillableBalance(v *float64) *CardCreate { if v != nil { _c.SetBalance(*v) } return _c } // SetNumber sets the "number" field. func (_c *CardCreate) SetNumber(v string) *CardCreate { _c.mutation.SetNumber(v) return _c } // SetName sets the "name" field. func (_c *CardCreate) SetName(v string) *CardCreate { _c.mutation.SetName(v) return _c } // SetNillableName sets the "name" field if the given value is not nil. func (_c *CardCreate) SetNillableName(v *string) *CardCreate { if v != nil { _c.SetName(*v) } return _c } // SetOwnerID sets the "owner" edge to the User entity by ID. func (_c *CardCreate) SetOwnerID(id string) *CardCreate { _c.mutation.SetOwnerID(id) return _c } // SetNillableOwnerID sets the "owner" edge to the User entity by ID if the given value is not nil. func (_c *CardCreate) SetNillableOwnerID(id *string) *CardCreate { if id != nil { _c = _c.SetOwnerID(*id) } return _c } // SetOwner sets the "owner" edge to the User entity. func (_c *CardCreate) SetOwner(v *User) *CardCreate { return _c.SetOwnerID(v.ID) } // AddSpecIDs adds the "spec" edge to the Spec entity by IDs. func (_c *CardCreate) AddSpecIDs(ids ...string) *CardCreate { _c.mutation.AddSpecIDs(ids...) return _c } // AddSpec adds the "spec" edges to the Spec entity. func (_c *CardCreate) AddSpec(v ...*Spec) *CardCreate { ids := make([]string, len(v)) for i := range v { ids[i] = v[i].ID } return _c.AddSpecIDs(ids...) } // Mutation returns the CardMutation object of the builder. func (_c *CardCreate) Mutation() *CardMutation { return _c.mutation } // Save creates the Card in the database. func (_c *CardCreate) Save(ctx context.Context) (*Card, error) { _c.defaults() return withHooks(ctx, _c.gremlinSave, _c.mutation, _c.hooks) } // SaveX calls Save and panics if Save returns an error. func (_c *CardCreate) SaveX(ctx context.Context) *Card { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *CardCreate) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *CardCreate) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (_c *CardCreate) defaults() { if _, ok := _c.mutation.CreateTime(); !ok { v := card.DefaultCreateTime() _c.mutation.SetCreateTime(v) } if _, ok := _c.mutation.UpdateTime(); !ok { v := card.DefaultUpdateTime() _c.mutation.SetUpdateTime(v) } if _, ok := _c.mutation.Balance(); !ok { v := card.DefaultBalance _c.mutation.SetBalance(v) } } // check runs all checks and user-defined validators on the builder. func (_c *CardCreate) check() error { if _, ok := _c.mutation.CreateTime(); !ok { return &ValidationError{Name: "create_time", err: errors.New(`ent: missing required field "Card.create_time"`)} } if _, ok := _c.mutation.UpdateTime(); !ok { return &ValidationError{Name: "update_time", err: errors.New(`ent: missing required field "Card.update_time"`)} } if _, ok := _c.mutation.Balance(); !ok { return &ValidationError{Name: "balance", err: errors.New(`ent: missing required field "Card.balance"`)} } if _, ok := _c.mutation.Number(); !ok { return &ValidationError{Name: "number", err: errors.New(`ent: missing required field "Card.number"`)} } if v, ok := _c.mutation.Number(); ok { if err := card.NumberValidator(v); err != nil { return &ValidationError{Name: "number", err: fmt.Errorf(`ent: validator failed for field "Card.number": %w`, err)} } } if v, ok := _c.mutation.Name(); ok { if err := card.NameValidator(v); err != nil { return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Card.name": %w`, err)} } } return nil } func (_c *CardCreate) gremlinSave(ctx context.Context) (*Card, error) { if err := _c.check(); err != nil { return nil, err } res := &gremlin.Response{} query, bindings := _c.gremlin().Query() if err := _c.driver.Exec(ctx, query, bindings, res); err != nil { return nil, err } if err, ok := isConstantError(res); ok { return nil, err } rnode := &Card{config: _c.config} if err := rnode.FromResponse(res); err != nil { return nil, err } _c.mutation.id = &rnode.ID _c.mutation.done = true return rnode, nil } func (_c *CardCreate) gremlin() *dsl.Traversal { type constraint struct { pred *dsl.Traversal // constraint predicate. test *dsl.Traversal // test matches and its constant. } constraints := make([]*constraint, 0, 1) v := g.AddV(card.Label) if value, ok := _c.mutation.CreateTime(); ok { v.Property(dsl.Single, card.FieldCreateTime, value) } if value, ok := _c.mutation.UpdateTime(); ok { v.Property(dsl.Single, card.FieldUpdateTime, value) } if value, ok := _c.mutation.Balance(); ok { v.Property(dsl.Single, card.FieldBalance, value) } if value, ok := _c.mutation.Number(); ok { v.Property(dsl.Single, card.FieldNumber, value) } if value, ok := _c.mutation.Name(); ok { v.Property(dsl.Single, card.FieldName, value) } for _, id := range _c.mutation.OwnerIDs() { v.AddE(user.CardLabel).From(g.V(id)).InV() constraints = append(constraints, &constraint{ pred: g.E().HasLabel(user.CardLabel).OutV().HasID(id).Count(), test: __.Is(p.NEQ(0)).Constant(NewErrUniqueEdge(card.Label, user.CardLabel, id)), }) } for _, id := range _c.mutation.SpecIDs() { v.AddE(spec.CardLabel).From(g.V(id)).InV() } if len(constraints) == 0 { return v.ValueMap(true) } tr := constraints[0].pred.Coalesce(constraints[0].test, v.ValueMap(true)) for _, cr := range constraints[1:] { tr = cr.pred.Coalesce(cr.test, tr) } return tr } // CardCreateBulk is the builder for creating many Card entities in bulk. type CardCreateBulk struct { config err error builders []*CardCreate }