// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "time" "tss-rocks-be/ent/contributor" "tss-rocks-be/ent/media" "tss-rocks-be/ent/role" "tss-rocks-be/ent/user" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" ) // UserCreate is the builder for creating a User entity. type UserCreate struct { config mutation *UserMutation hooks []Hook } // SetUsername sets the "username" field. func (uc *UserCreate) SetUsername(s string) *UserCreate { uc.mutation.SetUsername(s) return uc } // SetDisplayName sets the "display_name" field. func (uc *UserCreate) SetDisplayName(s string) *UserCreate { uc.mutation.SetDisplayName(s) return uc } // SetNillableDisplayName sets the "display_name" field if the given value is not nil. func (uc *UserCreate) SetNillableDisplayName(s *string) *UserCreate { if s != nil { uc.SetDisplayName(*s) } return uc } // SetEmail sets the "email" field. func (uc *UserCreate) SetEmail(s string) *UserCreate { uc.mutation.SetEmail(s) return uc } // SetPasswordHash sets the "password_hash" field. func (uc *UserCreate) SetPasswordHash(s string) *UserCreate { uc.mutation.SetPasswordHash(s) return uc } // SetStatus sets the "status" field. func (uc *UserCreate) SetStatus(u user.Status) *UserCreate { uc.mutation.SetStatus(u) return uc } // SetNillableStatus sets the "status" field if the given value is not nil. func (uc *UserCreate) SetNillableStatus(u *user.Status) *UserCreate { if u != nil { uc.SetStatus(*u) } return uc } // SetCreatedAt sets the "created_at" field. func (uc *UserCreate) SetCreatedAt(t time.Time) *UserCreate { uc.mutation.SetCreatedAt(t) return uc } // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. func (uc *UserCreate) SetNillableCreatedAt(t *time.Time) *UserCreate { if t != nil { uc.SetCreatedAt(*t) } return uc } // SetUpdatedAt sets the "updated_at" field. func (uc *UserCreate) SetUpdatedAt(t time.Time) *UserCreate { uc.mutation.SetUpdatedAt(t) return uc } // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. func (uc *UserCreate) SetNillableUpdatedAt(t *time.Time) *UserCreate { if t != nil { uc.SetUpdatedAt(*t) } return uc } // AddRoleIDs adds the "roles" edge to the Role entity by IDs. func (uc *UserCreate) AddRoleIDs(ids ...int) *UserCreate { uc.mutation.AddRoleIDs(ids...) return uc } // AddRoles adds the "roles" edges to the Role entity. func (uc *UserCreate) AddRoles(r ...*Role) *UserCreate { ids := make([]int, len(r)) for i := range r { ids[i] = r[i].ID } return uc.AddRoleIDs(ids...) } // AddContributorIDs adds the "contributors" edge to the Contributor entity by IDs. func (uc *UserCreate) AddContributorIDs(ids ...int) *UserCreate { uc.mutation.AddContributorIDs(ids...) return uc } // AddContributors adds the "contributors" edges to the Contributor entity. func (uc *UserCreate) AddContributors(c ...*Contributor) *UserCreate { ids := make([]int, len(c)) for i := range c { ids[i] = c[i].ID } return uc.AddContributorIDs(ids...) } // AddMediumIDs adds the "media" edge to the Media entity by IDs. func (uc *UserCreate) AddMediumIDs(ids ...int) *UserCreate { uc.mutation.AddMediumIDs(ids...) return uc } // AddMedia adds the "media" edges to the Media entity. func (uc *UserCreate) AddMedia(m ...*Media) *UserCreate { ids := make([]int, len(m)) for i := range m { ids[i] = m[i].ID } return uc.AddMediumIDs(ids...) } // Mutation returns the UserMutation object of the builder. func (uc *UserCreate) Mutation() *UserMutation { return uc.mutation } // Save creates the User in the database. func (uc *UserCreate) Save(ctx context.Context) (*User, error) { uc.defaults() return withHooks(ctx, uc.sqlSave, uc.mutation, uc.hooks) } // SaveX calls Save and panics if Save returns an error. func (uc *UserCreate) SaveX(ctx context.Context) *User { v, err := uc.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (uc *UserCreate) Exec(ctx context.Context) error { _, err := uc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (uc *UserCreate) ExecX(ctx context.Context) { if err := uc.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (uc *UserCreate) defaults() { if _, ok := uc.mutation.Status(); !ok { v := user.DefaultStatus uc.mutation.SetStatus(v) } if _, ok := uc.mutation.CreatedAt(); !ok { v := user.DefaultCreatedAt() uc.mutation.SetCreatedAt(v) } if _, ok := uc.mutation.UpdatedAt(); !ok { v := user.DefaultUpdatedAt() uc.mutation.SetUpdatedAt(v) } } // check runs all checks and user-defined validators on the builder. func (uc *UserCreate) check() error { if _, ok := uc.mutation.Username(); !ok { return &ValidationError{Name: "username", err: errors.New(`ent: missing required field "User.username"`)} } if v, ok := uc.mutation.Username(); ok { if err := user.UsernameValidator(v); err != nil { return &ValidationError{Name: "username", err: fmt.Errorf(`ent: validator failed for field "User.username": %w`, err)} } } if v, ok := uc.mutation.DisplayName(); ok { if err := user.DisplayNameValidator(v); err != nil { return &ValidationError{Name: "display_name", err: fmt.Errorf(`ent: validator failed for field "User.display_name": %w`, err)} } } if _, ok := uc.mutation.Email(); !ok { return &ValidationError{Name: "email", err: errors.New(`ent: missing required field "User.email"`)} } if v, ok := uc.mutation.Email(); ok { if err := user.EmailValidator(v); err != nil { return &ValidationError{Name: "email", err: fmt.Errorf(`ent: validator failed for field "User.email": %w`, err)} } } if _, ok := uc.mutation.PasswordHash(); !ok { return &ValidationError{Name: "password_hash", err: errors.New(`ent: missing required field "User.password_hash"`)} } if v, ok := uc.mutation.PasswordHash(); ok { if err := user.PasswordHashValidator(v); err != nil { return &ValidationError{Name: "password_hash", err: fmt.Errorf(`ent: validator failed for field "User.password_hash": %w`, err)} } } if _, ok := uc.mutation.Status(); !ok { return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "User.status"`)} } if v, ok := uc.mutation.Status(); ok { if err := user.StatusValidator(v); err != nil { return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "User.status": %w`, err)} } } if _, ok := uc.mutation.CreatedAt(); !ok { return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "User.created_at"`)} } if _, ok := uc.mutation.UpdatedAt(); !ok { return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "User.updated_at"`)} } return nil } func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) { if err := uc.check(); err != nil { return nil, err } _node, _spec := uc.createSpec() if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return nil, err } id := _spec.ID.Value.(int64) _node.ID = int(id) uc.mutation.id = &_node.ID uc.mutation.done = true return _node, nil } func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) { var ( _node = &User{config: uc.config} _spec = sqlgraph.NewCreateSpec(user.Table, sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt)) ) if value, ok := uc.mutation.Username(); ok { _spec.SetField(user.FieldUsername, field.TypeString, value) _node.Username = value } if value, ok := uc.mutation.DisplayName(); ok { _spec.SetField(user.FieldDisplayName, field.TypeString, value) _node.DisplayName = value } if value, ok := uc.mutation.Email(); ok { _spec.SetField(user.FieldEmail, field.TypeString, value) _node.Email = value } if value, ok := uc.mutation.PasswordHash(); ok { _spec.SetField(user.FieldPasswordHash, field.TypeString, value) _node.PasswordHash = value } if value, ok := uc.mutation.Status(); ok { _spec.SetField(user.FieldStatus, field.TypeEnum, value) _node.Status = value } if value, ok := uc.mutation.CreatedAt(); ok { _spec.SetField(user.FieldCreatedAt, field.TypeTime, value) _node.CreatedAt = value } if value, ok := uc.mutation.UpdatedAt(); ok { _spec.SetField(user.FieldUpdatedAt, field.TypeTime, value) _node.UpdatedAt = value } if nodes := uc.mutation.RolesIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, Inverse: false, Table: user.RolesTable, Columns: user.RolesPrimaryKey, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(role.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } if nodes := uc.mutation.ContributorsIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: false, Table: user.ContributorsTable, Columns: []string{user.ContributorsColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(contributor.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } if nodes := uc.mutation.MediaIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: false, Table: user.MediaTable, Columns: []string{user.MediaColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(media.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // UserCreateBulk is the builder for creating many User entities in bulk. type UserCreateBulk struct { config err error builders []*UserCreate } // Save creates the User entities in the database. func (ucb *UserCreateBulk) Save(ctx context.Context) ([]*User, error) { if ucb.err != nil { return nil, ucb.err } specs := make([]*sqlgraph.CreateSpec, len(ucb.builders)) nodes := make([]*User, len(ucb.builders)) mutators := make([]Mutator, len(ucb.builders)) for i := range ucb.builders { func(i int, root context.Context) { builder := ucb.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { 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 var err error nodes[i], specs[i] = builder.createSpec() if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, ucb.builders[i+1].mutation) } else { spec := &sqlgraph.BatchCreateSpec{Nodes: specs} // Invoke the actual operation on the latest mutation in the chain. if err = sqlgraph.BatchCreate(ctx, ucb.driver, spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } } } if err != nil { return nil, err } mutation.id = &nodes[i].ID if specs[i].ID.Value != nil { id := specs[i].ID.Value.(int64) nodes[i].ID = int(id) } mutation.done = true return nodes[i], nil }) for i := len(builder.hooks) - 1; i >= 0; i-- { mut = builder.hooks[i](mut) } mutators[i] = mut }(i, ctx) } if len(mutators) > 0 { if _, err := mutators[0].Mutate(ctx, ucb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (ucb *UserCreateBulk) SaveX(ctx context.Context) []*User { v, err := ucb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (ucb *UserCreateBulk) Exec(ctx context.Context) error { _, err := ucb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (ucb *UserCreateBulk) ExecX(ctx context.Context) { if err := ucb.Exec(ctx); err != nil { panic(err) } }