homebox/backend/internal/data/ent/user_create.go

496 lines
14 KiB
Go
Raw Normal View History

2022-08-30 18:04:50 +00:00
// Code generated by ent, DO NOT EDIT.
2022-08-30 02:30:36 +00:00
package ent
import (
"context"
"errors"
"fmt"
2022-08-30 18:04:50 +00:00
"time"
2022-08-30 02:30:36 +00:00
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/google/uuid"
"github.com/hay-kot/homebox/backend/internal/data/ent/authtokens"
"github.com/hay-kot/homebox/backend/internal/data/ent/group"
"github.com/hay-kot/homebox/backend/internal/data/ent/notifier"
"github.com/hay-kot/homebox/backend/internal/data/ent/user"
2022-08-30 02:30:36 +00:00
)
// UserCreate is the builder for creating a User entity.
type UserCreate struct {
config
mutation *UserMutation
hooks []Hook
}
2022-08-30 18:04:50 +00:00
// 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
}
2022-08-30 02:30:36 +00:00
// SetName sets the "name" field.
func (uc *UserCreate) SetName(s string) *UserCreate {
uc.mutation.SetName(s)
return uc
}
// SetEmail sets the "email" field.
func (uc *UserCreate) SetEmail(s string) *UserCreate {
uc.mutation.SetEmail(s)
return uc
}
// SetPassword sets the "password" field.
func (uc *UserCreate) SetPassword(s string) *UserCreate {
uc.mutation.SetPassword(s)
return uc
}
// SetIsSuperuser sets the "is_superuser" field.
func (uc *UserCreate) SetIsSuperuser(b bool) *UserCreate {
uc.mutation.SetIsSuperuser(b)
return uc
}
// SetNillableIsSuperuser sets the "is_superuser" field if the given value is not nil.
func (uc *UserCreate) SetNillableIsSuperuser(b *bool) *UserCreate {
if b != nil {
uc.SetIsSuperuser(*b)
}
return uc
}
// SetSuperuser sets the "superuser" field.
func (uc *UserCreate) SetSuperuser(b bool) *UserCreate {
uc.mutation.SetSuperuser(b)
return uc
}
// SetNillableSuperuser sets the "superuser" field if the given value is not nil.
func (uc *UserCreate) SetNillableSuperuser(b *bool) *UserCreate {
if b != nil {
uc.SetSuperuser(*b)
}
return uc
}
// SetRole sets the "role" field.
func (uc *UserCreate) SetRole(u user.Role) *UserCreate {
uc.mutation.SetRole(u)
return uc
}
// SetNillableRole sets the "role" field if the given value is not nil.
func (uc *UserCreate) SetNillableRole(u *user.Role) *UserCreate {
if u != nil {
uc.SetRole(*u)
}
return uc
}
// SetActivatedOn sets the "activated_on" field.
func (uc *UserCreate) SetActivatedOn(t time.Time) *UserCreate {
uc.mutation.SetActivatedOn(t)
return uc
}
// SetNillableActivatedOn sets the "activated_on" field if the given value is not nil.
func (uc *UserCreate) SetNillableActivatedOn(t *time.Time) *UserCreate {
if t != nil {
uc.SetActivatedOn(*t)
}
return uc
}
2022-08-30 02:30:36 +00:00
// SetID sets the "id" field.
func (uc *UserCreate) SetID(u uuid.UUID) *UserCreate {
uc.mutation.SetID(u)
return uc
}
// SetNillableID sets the "id" field if the given value is not nil.
func (uc *UserCreate) SetNillableID(u *uuid.UUID) *UserCreate {
if u != nil {
uc.SetID(*u)
}
return uc
}
2022-08-30 18:04:50 +00:00
// SetGroupID sets the "group" edge to the Group entity by ID.
func (uc *UserCreate) SetGroupID(id uuid.UUID) *UserCreate {
uc.mutation.SetGroupID(id)
return uc
}
// SetGroup sets the "group" edge to the Group entity.
func (uc *UserCreate) SetGroup(g *Group) *UserCreate {
return uc.SetGroupID(g.ID)
}
2022-08-30 02:30:36 +00:00
// AddAuthTokenIDs adds the "auth_tokens" edge to the AuthTokens entity by IDs.
2022-08-30 18:04:50 +00:00
func (uc *UserCreate) AddAuthTokenIDs(ids ...uuid.UUID) *UserCreate {
2022-08-30 02:30:36 +00:00
uc.mutation.AddAuthTokenIDs(ids...)
return uc
}
// AddAuthTokens adds the "auth_tokens" edges to the AuthTokens entity.
func (uc *UserCreate) AddAuthTokens(a ...*AuthTokens) *UserCreate {
2022-08-30 18:04:50 +00:00
ids := make([]uuid.UUID, len(a))
2022-08-30 02:30:36 +00:00
for i := range a {
ids[i] = a[i].ID
}
return uc.AddAuthTokenIDs(ids...)
}
// AddNotifierIDs adds the "notifiers" edge to the Notifier entity by IDs.
func (uc *UserCreate) AddNotifierIDs(ids ...uuid.UUID) *UserCreate {
uc.mutation.AddNotifierIDs(ids...)
return uc
}
// AddNotifiers adds the "notifiers" edges to the Notifier entity.
func (uc *UserCreate) AddNotifiers(n ...*Notifier) *UserCreate {
ids := make([]uuid.UUID, len(n))
for i := range n {
ids[i] = n[i].ID
}
return uc.AddNotifierIDs(ids...)
}
2022-08-30 02:30:36 +00:00
// 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[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks)
2022-08-30 02:30:36 +00:00
}
// 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() {
2022-08-30 18:04:50 +00:00
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)
}
2022-08-30 02:30:36 +00:00
if _, ok := uc.mutation.IsSuperuser(); !ok {
v := user.DefaultIsSuperuser
uc.mutation.SetIsSuperuser(v)
}
if _, ok := uc.mutation.Superuser(); !ok {
v := user.DefaultSuperuser
uc.mutation.SetSuperuser(v)
}
if _, ok := uc.mutation.Role(); !ok {
v := user.DefaultRole
uc.mutation.SetRole(v)
}
2022-08-30 02:30:36 +00:00
if _, ok := uc.mutation.ID(); !ok {
v := user.DefaultID()
uc.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (uc *UserCreate) check() error {
2022-08-30 18:04:50 +00:00
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"`)}
}
2022-08-30 02:30:36 +00:00
if _, ok := uc.mutation.Name(); !ok {
return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "User.name"`)}
}
if v, ok := uc.mutation.Name(); ok {
if err := user.NameValidator(v); err != nil {
return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "User.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.Password(); !ok {
return &ValidationError{Name: "password", err: errors.New(`ent: missing required field "User.password"`)}
}
if v, ok := uc.mutation.Password(); ok {
if err := user.PasswordValidator(v); err != nil {
return &ValidationError{Name: "password", err: fmt.Errorf(`ent: validator failed for field "User.password": %w`, err)}
}
}
if _, ok := uc.mutation.IsSuperuser(); !ok {
return &ValidationError{Name: "is_superuser", err: errors.New(`ent: missing required field "User.is_superuser"`)}
}
if _, ok := uc.mutation.Superuser(); !ok {
return &ValidationError{Name: "superuser", err: errors.New(`ent: missing required field "User.superuser"`)}
}
if _, ok := uc.mutation.Role(); !ok {
return &ValidationError{Name: "role", err: errors.New(`ent: missing required field "User.role"`)}
}
if v, ok := uc.mutation.Role(); ok {
if err := user.RoleValidator(v); err != nil {
return &ValidationError{Name: "role", err: fmt.Errorf(`ent: validator failed for field "User.role": %w`, err)}
}
}
2022-08-30 18:04:50 +00:00
if _, ok := uc.mutation.GroupID(); !ok {
return &ValidationError{Name: "group", err: errors.New(`ent: missing required edge "User.group"`)}
}
2022-08-30 02:30:36 +00:00
return nil
}
func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
if err := uc.check(); err != nil {
return nil, err
}
2022-08-30 02:30:36 +00:00
_node, _spec := uc.createSpec()
if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil {
if sqlgraph.IsConstraintError(err) {
2022-08-30 18:04:50 +00:00
err = &ConstraintError{msg: err.Error(), wrap: err}
2022-08-30 02:30:36 +00:00
}
return nil, err
}
if _spec.ID.Value != nil {
if id, ok := _spec.ID.Value.(*uuid.UUID); ok {
_node.ID = *id
} else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
return nil, err
}
}
uc.mutation.id = &_node.ID
uc.mutation.done = true
2022-08-30 02:30:36 +00:00
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.TypeUUID))
2022-08-30 02:30:36 +00:00
)
if id, ok := uc.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = &id
}
2022-08-30 18:04:50 +00:00
if value, ok := uc.mutation.CreatedAt(); ok {
_spec.SetField(user.FieldCreatedAt, field.TypeTime, value)
2022-08-30 18:04:50 +00:00
_node.CreatedAt = value
}
if value, ok := uc.mutation.UpdatedAt(); ok {
_spec.SetField(user.FieldUpdatedAt, field.TypeTime, value)
2022-08-30 18:04:50 +00:00
_node.UpdatedAt = value
}
2022-08-30 02:30:36 +00:00
if value, ok := uc.mutation.Name(); ok {
_spec.SetField(user.FieldName, field.TypeString, value)
2022-08-30 02:30:36 +00:00
_node.Name = value
}
if value, ok := uc.mutation.Email(); ok {
_spec.SetField(user.FieldEmail, field.TypeString, value)
2022-08-30 02:30:36 +00:00
_node.Email = value
}
if value, ok := uc.mutation.Password(); ok {
_spec.SetField(user.FieldPassword, field.TypeString, value)
2022-08-30 02:30:36 +00:00
_node.Password = value
}
if value, ok := uc.mutation.IsSuperuser(); ok {
_spec.SetField(user.FieldIsSuperuser, field.TypeBool, value)
2022-08-30 02:30:36 +00:00
_node.IsSuperuser = value
}
if value, ok := uc.mutation.Superuser(); ok {
_spec.SetField(user.FieldSuperuser, field.TypeBool, value)
_node.Superuser = value
}
if value, ok := uc.mutation.Role(); ok {
_spec.SetField(user.FieldRole, field.TypeEnum, value)
_node.Role = value
}
if value, ok := uc.mutation.ActivatedOn(); ok {
_spec.SetField(user.FieldActivatedOn, field.TypeTime, value)
_node.ActivatedOn = value
}
2022-08-30 18:04:50 +00:00
if nodes := uc.mutation.GroupIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: user.GroupTable,
Columns: []string{user.GroupColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
2023-03-23 04:16:04 +00:00
IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeUUID),
2022-08-30 18:04:50 +00:00
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.group_users = &nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
2022-08-30 02:30:36 +00:00
if nodes := uc.mutation.AuthTokensIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: user.AuthTokensTable,
Columns: []string{user.AuthTokensColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
2023-03-23 04:16:04 +00:00
IDSpec: sqlgraph.NewFieldSpec(authtokens.FieldID, field.TypeUUID),
2022-08-30 02:30:36 +00:00
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := uc.mutation.NotifiersIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: user.NotifiersTable,
Columns: []string{user.NotifiersColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
2023-03-23 04:16:04 +00:00
IDSpec: sqlgraph.NewFieldSpec(notifier.FieldID, field.TypeUUID),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
2022-08-30 02:30:36 +00:00
return _node, _spec
}
// UserCreateBulk is the builder for creating many User entities in bulk.
type UserCreateBulk struct {
config
builders []*UserCreate
}
// Save creates the User entities in the database.
func (ucb *UserCreateBulk) Save(ctx context.Context) ([]*User, error) {
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
nodes[i], specs[i] = builder.createSpec()
var err error
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) {
2022-08-30 18:04:50 +00:00
err = &ConstraintError{msg: err.Error(), wrap: err}
2022-08-30 02:30:36 +00:00
}
}
}
if err != nil {
return nil, err
}
mutation.id = &nodes[i].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)
}
}