rano/db/ent/user_create.go

605 lines
18 KiB
Go
Raw Normal View History

2024-11-10 09:22:33 +00:00
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"gitserver.in/patialtech/rano/db/ent/audit"
"gitserver.in/patialtech/rano/db/ent/user"
"gitserver.in/patialtech/rano/db/ent/usersession"
2024-11-17 16:58:29 +00:00
"gitserver.in/patialtech/rano/db/ent/verifytoken"
2024-11-10 09:22:33 +00:00
)
// UserCreate is the builder for creating a User entity.
type UserCreate struct {
config
mutation *UserMutation
hooks []Hook
}
// 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
}
// SetEmail sets the "email" field.
func (uc *UserCreate) SetEmail(s string) *UserCreate {
uc.mutation.SetEmail(s)
return uc
}
// SetEmailVerified sets the "email_verified" field.
func (uc *UserCreate) SetEmailVerified(b bool) *UserCreate {
uc.mutation.SetEmailVerified(b)
return uc
}
// SetNillableEmailVerified sets the "email_verified" field if the given value is not nil.
func (uc *UserCreate) SetNillableEmailVerified(b *bool) *UserCreate {
if b != nil {
uc.SetEmailVerified(*b)
}
return uc
}
// SetPhone sets the "phone" field.
func (uc *UserCreate) SetPhone(s string) *UserCreate {
uc.mutation.SetPhone(s)
return uc
}
// SetNillablePhone sets the "phone" field if the given value is not nil.
func (uc *UserCreate) SetNillablePhone(s *string) *UserCreate {
if s != nil {
uc.SetPhone(*s)
}
return uc
}
2024-11-10 09:22:33 +00:00
// SetPhoneVerified sets the "phone_verified" field.
func (uc *UserCreate) SetPhoneVerified(b bool) *UserCreate {
uc.mutation.SetPhoneVerified(b)
return uc
}
// SetNillablePhoneVerified sets the "phone_verified" field if the given value is not nil.
func (uc *UserCreate) SetNillablePhoneVerified(b *bool) *UserCreate {
if b != nil {
uc.SetPhoneVerified(*b)
}
return uc
}
// SetPwdSalt sets the "pwd_salt" field.
func (uc *UserCreate) SetPwdSalt(s string) *UserCreate {
uc.mutation.SetPwdSalt(s)
return uc
}
// SetPwdHash sets the "pwd_hash" field.
func (uc *UserCreate) SetPwdHash(s string) *UserCreate {
uc.mutation.SetPwdHash(s)
return uc
}
// SetLoginFailedCount sets the "login_failed_count" field.
func (uc *UserCreate) SetLoginFailedCount(u uint8) *UserCreate {
uc.mutation.SetLoginFailedCount(u)
return uc
}
// SetNillableLoginFailedCount sets the "login_failed_count" field if the given value is not nil.
func (uc *UserCreate) SetNillableLoginFailedCount(u *uint8) *UserCreate {
if u != nil {
uc.SetLoginFailedCount(*u)
}
return uc
}
// SetLoginAttemptOn sets the "login_attempt_on" field.
func (uc *UserCreate) SetLoginAttemptOn(t time.Time) *UserCreate {
uc.mutation.SetLoginAttemptOn(t)
return uc
}
// SetNillableLoginAttemptOn sets the "login_attempt_on" field if the given value is not nil.
func (uc *UserCreate) SetNillableLoginAttemptOn(t *time.Time) *UserCreate {
if t != nil {
uc.SetLoginAttemptOn(*t)
}
return uc
}
// SetLoginLockedUntil sets the "login_locked_until" field.
func (uc *UserCreate) SetLoginLockedUntil(t time.Time) *UserCreate {
uc.mutation.SetLoginLockedUntil(t)
return uc
}
// SetNillableLoginLockedUntil sets the "login_locked_until" field if the given value is not nil.
func (uc *UserCreate) SetNillableLoginLockedUntil(t *time.Time) *UserCreate {
if t != nil {
uc.SetLoginLockedUntil(*t)
}
return uc
}
// SetFirstName sets the "first_name" field.
func (uc *UserCreate) SetFirstName(s string) *UserCreate {
uc.mutation.SetFirstName(s)
return uc
}
// SetMiddleName sets the "middle_name" field.
func (uc *UserCreate) SetMiddleName(s string) *UserCreate {
uc.mutation.SetMiddleName(s)
return uc
}
// SetLastName sets the "last_name" field.
func (uc *UserCreate) SetLastName(s string) *UserCreate {
uc.mutation.SetLastName(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
}
// SetID sets the "id" field.
func (uc *UserCreate) SetID(i int64) *UserCreate {
uc.mutation.SetID(i)
return uc
}
// AddSessionIDs adds the "sessions" edge to the UserSession entity by IDs.
func (uc *UserCreate) AddSessionIDs(ids ...int64) *UserCreate {
uc.mutation.AddSessionIDs(ids...)
return uc
}
// AddSessions adds the "sessions" edges to the UserSession entity.
func (uc *UserCreate) AddSessions(u ...*UserSession) *UserCreate {
ids := make([]int64, len(u))
for i := range u {
ids[i] = u[i].ID
}
return uc.AddSessionIDs(ids...)
}
// AddAuditLogIDs adds the "audit_logs" edge to the Audit entity by IDs.
func (uc *UserCreate) AddAuditLogIDs(ids ...int64) *UserCreate {
uc.mutation.AddAuditLogIDs(ids...)
return uc
}
// AddAuditLogs adds the "audit_logs" edges to the Audit entity.
func (uc *UserCreate) AddAuditLogs(a ...*Audit) *UserCreate {
ids := make([]int64, len(a))
for i := range a {
ids[i] = a[i].ID
}
return uc.AddAuditLogIDs(ids...)
}
2024-11-17 16:58:29 +00:00
// AddVerifyTokenIDs adds the "verify_tokens" edge to the VerifyToken entity by IDs.
func (uc *UserCreate) AddVerifyTokenIDs(ids ...int64) *UserCreate {
uc.mutation.AddVerifyTokenIDs(ids...)
return uc
}
// AddVerifyTokens adds the "verify_tokens" edges to the VerifyToken entity.
func (uc *UserCreate) AddVerifyTokens(v ...*VerifyToken) *UserCreate {
ids := make([]int64, len(v))
for i := range v {
ids[i] = v[i].ID
}
return uc.AddVerifyTokenIDs(ids...)
}
2024-11-10 09:22:33 +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(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.CreatedAt(); !ok {
v := user.DefaultCreatedAt()
uc.mutation.SetCreatedAt(v)
}
if _, ok := uc.mutation.UpdatedAt(); !ok {
v := user.DefaultUpdatedAt()
uc.mutation.SetUpdatedAt(v)
}
if _, ok := uc.mutation.EmailVerified(); !ok {
v := user.DefaultEmailVerified
uc.mutation.SetEmailVerified(v)
}
if _, ok := uc.mutation.PhoneVerified(); !ok {
v := user.DefaultPhoneVerified
uc.mutation.SetPhoneVerified(v)
}
if _, ok := uc.mutation.LoginFailedCount(); !ok {
v := user.DefaultLoginFailedCount
uc.mutation.SetLoginFailedCount(v)
}
if _, ok := uc.mutation.Status(); !ok {
v := user.DefaultStatus
uc.mutation.SetStatus(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (uc *UserCreate) check() error {
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"`)}
}
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.EmailVerified(); !ok {
return &ValidationError{Name: "email_verified", err: errors.New(`ent: missing required field "User.email_verified"`)}
}
if v, ok := uc.mutation.Phone(); ok {
if err := user.PhoneValidator(v); err != nil {
return &ValidationError{Name: "phone", err: fmt.Errorf(`ent: validator failed for field "User.phone": %w`, err)}
}
}
if _, ok := uc.mutation.PhoneVerified(); !ok {
return &ValidationError{Name: "phone_verified", err: errors.New(`ent: missing required field "User.phone_verified"`)}
}
if _, ok := uc.mutation.PwdSalt(); !ok {
return &ValidationError{Name: "pwd_salt", err: errors.New(`ent: missing required field "User.pwd_salt"`)}
}
if v, ok := uc.mutation.PwdSalt(); ok {
if err := user.PwdSaltValidator(v); err != nil {
return &ValidationError{Name: "pwd_salt", err: fmt.Errorf(`ent: validator failed for field "User.pwd_salt": %w`, err)}
}
}
if _, ok := uc.mutation.PwdHash(); !ok {
return &ValidationError{Name: "pwd_hash", err: errors.New(`ent: missing required field "User.pwd_hash"`)}
}
if v, ok := uc.mutation.PwdHash(); ok {
if err := user.PwdHashValidator(v); err != nil {
return &ValidationError{Name: "pwd_hash", err: fmt.Errorf(`ent: validator failed for field "User.pwd_hash": %w`, err)}
}
}
if _, ok := uc.mutation.FirstName(); !ok {
return &ValidationError{Name: "first_name", err: errors.New(`ent: missing required field "User.first_name"`)}
}
if v, ok := uc.mutation.FirstName(); ok {
if err := user.FirstNameValidator(v); err != nil {
return &ValidationError{Name: "first_name", err: fmt.Errorf(`ent: validator failed for field "User.first_name": %w`, err)}
}
}
if _, ok := uc.mutation.MiddleName(); !ok {
return &ValidationError{Name: "middle_name", err: errors.New(`ent: missing required field "User.middle_name"`)}
}
if v, ok := uc.mutation.MiddleName(); ok {
if err := user.MiddleNameValidator(v); err != nil {
return &ValidationError{Name: "middle_name", err: fmt.Errorf(`ent: validator failed for field "User.middle_name": %w`, err)}
}
}
if _, ok := uc.mutation.LastName(); !ok {
return &ValidationError{Name: "last_name", err: errors.New(`ent: missing required field "User.last_name"`)}
}
if v, ok := uc.mutation.LastName(); ok {
if err := user.LastNameValidator(v); err != nil {
return &ValidationError{Name: "last_name", err: fmt.Errorf(`ent: validator failed for field "User.last_name": %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)}
}
}
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
}
if _spec.ID.Value != _node.ID {
id := _spec.ID.Value.(int64)
_node.ID = int64(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.TypeInt64))
)
if id, ok := uc.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = id
}
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 value, ok := uc.mutation.Email(); ok {
_spec.SetField(user.FieldEmail, field.TypeString, value)
_node.Email = value
}
if value, ok := uc.mutation.EmailVerified(); ok {
_spec.SetField(user.FieldEmailVerified, field.TypeBool, value)
_node.EmailVerified = value
}
if value, ok := uc.mutation.Phone(); ok {
_spec.SetField(user.FieldPhone, field.TypeString, value)
_node.Phone = value
}
if value, ok := uc.mutation.PhoneVerified(); ok {
_spec.SetField(user.FieldPhoneVerified, field.TypeBool, value)
_node.PhoneVerified = value
}
if value, ok := uc.mutation.PwdSalt(); ok {
_spec.SetField(user.FieldPwdSalt, field.TypeString, value)
_node.PwdSalt = value
}
if value, ok := uc.mutation.PwdHash(); ok {
_spec.SetField(user.FieldPwdHash, field.TypeString, value)
_node.PwdHash = value
}
if value, ok := uc.mutation.LoginFailedCount(); ok {
_spec.SetField(user.FieldLoginFailedCount, field.TypeUint8, value)
_node.LoginFailedCount = value
}
if value, ok := uc.mutation.LoginAttemptOn(); ok {
_spec.SetField(user.FieldLoginAttemptOn, field.TypeTime, value)
_node.LoginAttemptOn = &value
}
if value, ok := uc.mutation.LoginLockedUntil(); ok {
_spec.SetField(user.FieldLoginLockedUntil, field.TypeTime, value)
_node.LoginLockedUntil = &value
}
if value, ok := uc.mutation.FirstName(); ok {
_spec.SetField(user.FieldFirstName, field.TypeString, value)
_node.FirstName = value
}
if value, ok := uc.mutation.MiddleName(); ok {
_spec.SetField(user.FieldMiddleName, field.TypeString, value)
_node.MiddleName = &value
}
if value, ok := uc.mutation.LastName(); ok {
_spec.SetField(user.FieldLastName, field.TypeString, value)
_node.LastName = value
}
if value, ok := uc.mutation.Status(); ok {
_spec.SetField(user.FieldStatus, field.TypeEnum, value)
_node.Status = value
}
if nodes := uc.mutation.SessionsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: user.SessionsTable,
Columns: []string{user.SessionsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(usersession.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := uc.mutation.AuditLogsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: user.AuditLogsTable,
Columns: []string{user.AuditLogsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(audit.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
2024-11-17 16:58:29 +00:00
if nodes := uc.mutation.VerifyTokensIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: user.VerifyTokensTable,
Columns: []string{user.VerifyTokensColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(verifytoken.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
2024-11-10 09:22:33 +00:00
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 && nodes[i].ID == 0 {
id := specs[i].ID.Value.(int64)
nodes[i].ID = int64(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)
}
}