// 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/group" "entgo.io/ent/entc/integration/gremlin/ent/user" ) // GroupCreate is the builder for creating a Group entity. type GroupCreate struct { config mutation *GroupMutation hooks []Hook } // SetActive sets the "active" field. func (_c *GroupCreate) SetActive(v bool) *GroupCreate { _c.mutation.SetActive(v) return _c } // SetNillableActive sets the "active" field if the given value is not nil. func (_c *GroupCreate) SetNillableActive(v *bool) *GroupCreate { if v != nil { _c.SetActive(*v) } return _c } // SetExpire sets the "expire" field. func (_c *GroupCreate) SetExpire(v time.Time) *GroupCreate { _c.mutation.SetExpire(v) return _c } // SetType sets the "type" field. func (_c *GroupCreate) SetType(v string) *GroupCreate { _c.mutation.SetType(v) return _c } // SetNillableType sets the "type" field if the given value is not nil. func (_c *GroupCreate) SetNillableType(v *string) *GroupCreate { if v != nil { _c.SetType(*v) } return _c } // SetMaxUsers sets the "max_users" field. func (_c *GroupCreate) SetMaxUsers(v int) *GroupCreate { _c.mutation.SetMaxUsers(v) return _c } // SetNillableMaxUsers sets the "max_users" field if the given value is not nil. func (_c *GroupCreate) SetNillableMaxUsers(v *int) *GroupCreate { if v != nil { _c.SetMaxUsers(*v) } return _c } // SetName sets the "name" field. func (_c *GroupCreate) SetName(v string) *GroupCreate { _c.mutation.SetName(v) return _c } // AddFileIDs adds the "files" edge to the File entity by IDs. func (_c *GroupCreate) AddFileIDs(ids ...string) *GroupCreate { _c.mutation.AddFileIDs(ids...) return _c } // AddFiles adds the "files" edges to the File entity. func (_c *GroupCreate) AddFiles(v ...*File) *GroupCreate { ids := make([]string, len(v)) for i := range v { ids[i] = v[i].ID } return _c.AddFileIDs(ids...) } // AddBlockedIDs adds the "blocked" edge to the User entity by IDs. func (_c *GroupCreate) AddBlockedIDs(ids ...string) *GroupCreate { _c.mutation.AddBlockedIDs(ids...) return _c } // AddBlocked adds the "blocked" edges to the User entity. func (_c *GroupCreate) AddBlocked(v ...*User) *GroupCreate { ids := make([]string, len(v)) for i := range v { ids[i] = v[i].ID } return _c.AddBlockedIDs(ids...) } // AddUserIDs adds the "users" edge to the User entity by IDs. func (_c *GroupCreate) AddUserIDs(ids ...string) *GroupCreate { _c.mutation.AddUserIDs(ids...) return _c } // AddUsers adds the "users" edges to the User entity. func (_c *GroupCreate) AddUsers(v ...*User) *GroupCreate { ids := make([]string, len(v)) for i := range v { ids[i] = v[i].ID } return _c.AddUserIDs(ids...) } // SetInfoID sets the "info" edge to the GroupInfo entity by ID. func (_c *GroupCreate) SetInfoID(id string) *GroupCreate { _c.mutation.SetInfoID(id) return _c } // SetInfo sets the "info" edge to the GroupInfo entity. func (_c *GroupCreate) SetInfo(v *GroupInfo) *GroupCreate { return _c.SetInfoID(v.ID) } // Mutation returns the GroupMutation object of the builder. func (_c *GroupCreate) Mutation() *GroupMutation { return _c.mutation } // Save creates the Group in the database. func (_c *GroupCreate) Save(ctx context.Context) (*Group, error) { _c.defaults() return withHooks(ctx, _c.gremlinSave, _c.mutation, _c.hooks) } // SaveX calls Save and panics if Save returns an error. func (_c *GroupCreate) SaveX(ctx context.Context) *Group { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *GroupCreate) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *GroupCreate) 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 *GroupCreate) defaults() { if _, ok := _c.mutation.Active(); !ok { v := group.DefaultActive _c.mutation.SetActive(v) } if _, ok := _c.mutation.MaxUsers(); !ok { v := group.DefaultMaxUsers _c.mutation.SetMaxUsers(v) } } // check runs all checks and user-defined validators on the builder. func (_c *GroupCreate) check() error { if _, ok := _c.mutation.Active(); !ok { return &ValidationError{Name: "active", err: errors.New(`ent: missing required field "Group.active"`)} } if _, ok := _c.mutation.Expire(); !ok { return &ValidationError{Name: "expire", err: errors.New(`ent: missing required field "Group.expire"`)} } if v, ok := _c.mutation.GetType(); ok { if err := group.TypeValidator(v); err != nil { return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "Group.type": %w`, err)} } } if v, ok := _c.mutation.MaxUsers(); ok { if err := group.MaxUsersValidator(v); err != nil { return &ValidationError{Name: "max_users", err: fmt.Errorf(`ent: validator failed for field "Group.max_users": %w`, err)} } } if _, ok := _c.mutation.Name(); !ok { return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Group.name"`)} } if v, ok := _c.mutation.Name(); ok { if err := group.NameValidator(v); err != nil { return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Group.name": %w`, err)} } } if len(_c.mutation.InfoIDs()) == 0 { return &ValidationError{Name: "info", err: errors.New(`ent: missing required edge "Group.info"`)} } return nil } func (_c *GroupCreate) gremlinSave(ctx context.Context) (*Group, 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 := &Group{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 *GroupCreate) gremlin() *dsl.Traversal { type constraint struct { pred *dsl.Traversal // constraint predicate. test *dsl.Traversal // test matches and its constant. } constraints := make([]*constraint, 0, 2) v := g.AddV(group.Label) if value, ok := _c.mutation.Active(); ok { v.Property(dsl.Single, group.FieldActive, value) } if value, ok := _c.mutation.Expire(); ok { v.Property(dsl.Single, group.FieldExpire, value) } if value, ok := _c.mutation.GetType(); ok { v.Property(dsl.Single, group.FieldType, value) } if value, ok := _c.mutation.MaxUsers(); ok { v.Property(dsl.Single, group.FieldMaxUsers, value) } if value, ok := _c.mutation.Name(); ok { v.Property(dsl.Single, group.FieldName, value) } for _, id := range _c.mutation.FilesIDs() { v.AddE(group.FilesLabel).To(g.V(id)).OutV() constraints = append(constraints, &constraint{ pred: g.E().HasLabel(group.FilesLabel).InV().HasID(id).Count(), test: __.Is(p.NEQ(0)).Constant(NewErrUniqueEdge(group.Label, group.FilesLabel, id)), }) } for _, id := range _c.mutation.BlockedIDs() { v.AddE(group.BlockedLabel).To(g.V(id)).OutV() constraints = append(constraints, &constraint{ pred: g.E().HasLabel(group.BlockedLabel).InV().HasID(id).Count(), test: __.Is(p.NEQ(0)).Constant(NewErrUniqueEdge(group.Label, group.BlockedLabel, id)), }) } for _, id := range _c.mutation.UsersIDs() { v.AddE(user.GroupsLabel).From(g.V(id)).InV() } for _, id := range _c.mutation.InfoIDs() { v.AddE(group.InfoLabel).To(g.V(id)).OutV() } 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 } // GroupCreateBulk is the builder for creating many Group entities in bulk. type GroupCreateBulk struct { config err error builders []*GroupCreate }