rano/db/ent/verifytoken_create.go

306 lines
8.6 KiB
Go

// 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/user"
"gitserver.in/patialtech/rano/db/ent/verifytoken"
)
// VerifyTokenCreate is the builder for creating a VerifyToken entity.
type VerifyTokenCreate struct {
config
mutation *VerifyTokenMutation
hooks []Hook
}
// SetCreatedAt sets the "created_at" field.
func (vtc *VerifyTokenCreate) SetCreatedAt(t time.Time) *VerifyTokenCreate {
vtc.mutation.SetCreatedAt(t)
return vtc
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (vtc *VerifyTokenCreate) SetNillableCreatedAt(t *time.Time) *VerifyTokenCreate {
if t != nil {
vtc.SetCreatedAt(*t)
}
return vtc
}
// SetExpiresAt sets the "expires_at" field.
func (vtc *VerifyTokenCreate) SetExpiresAt(t time.Time) *VerifyTokenCreate {
vtc.mutation.SetExpiresAt(t)
return vtc
}
// SetToken sets the "token" field.
func (vtc *VerifyTokenCreate) SetToken(s string) *VerifyTokenCreate {
vtc.mutation.SetToken(s)
return vtc
}
// SetPurpose sets the "purpose" field.
func (vtc *VerifyTokenCreate) SetPurpose(s string) *VerifyTokenCreate {
vtc.mutation.SetPurpose(s)
return vtc
}
// SetNillablePurpose sets the "purpose" field if the given value is not nil.
func (vtc *VerifyTokenCreate) SetNillablePurpose(s *string) *VerifyTokenCreate {
if s != nil {
vtc.SetPurpose(*s)
}
return vtc
}
// SetID sets the "id" field.
func (vtc *VerifyTokenCreate) SetID(i int64) *VerifyTokenCreate {
vtc.mutation.SetID(i)
return vtc
}
// SetUserID sets the "user" edge to the User entity by ID.
func (vtc *VerifyTokenCreate) SetUserID(id int64) *VerifyTokenCreate {
vtc.mutation.SetUserID(id)
return vtc
}
// SetNillableUserID sets the "user" edge to the User entity by ID if the given value is not nil.
func (vtc *VerifyTokenCreate) SetNillableUserID(id *int64) *VerifyTokenCreate {
if id != nil {
vtc = vtc.SetUserID(*id)
}
return vtc
}
// SetUser sets the "user" edge to the User entity.
func (vtc *VerifyTokenCreate) SetUser(u *User) *VerifyTokenCreate {
return vtc.SetUserID(u.ID)
}
// Mutation returns the VerifyTokenMutation object of the builder.
func (vtc *VerifyTokenCreate) Mutation() *VerifyTokenMutation {
return vtc.mutation
}
// Save creates the VerifyToken in the database.
func (vtc *VerifyTokenCreate) Save(ctx context.Context) (*VerifyToken, error) {
vtc.defaults()
return withHooks(ctx, vtc.sqlSave, vtc.mutation, vtc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (vtc *VerifyTokenCreate) SaveX(ctx context.Context) *VerifyToken {
v, err := vtc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (vtc *VerifyTokenCreate) Exec(ctx context.Context) error {
_, err := vtc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (vtc *VerifyTokenCreate) ExecX(ctx context.Context) {
if err := vtc.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (vtc *VerifyTokenCreate) defaults() {
if _, ok := vtc.mutation.CreatedAt(); !ok {
v := verifytoken.DefaultCreatedAt()
vtc.mutation.SetCreatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (vtc *VerifyTokenCreate) check() error {
if _, ok := vtc.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "VerifyToken.created_at"`)}
}
if _, ok := vtc.mutation.ExpiresAt(); !ok {
return &ValidationError{Name: "expires_at", err: errors.New(`ent: missing required field "VerifyToken.expires_at"`)}
}
if _, ok := vtc.mutation.Token(); !ok {
return &ValidationError{Name: "token", err: errors.New(`ent: missing required field "VerifyToken.token"`)}
}
if v, ok := vtc.mutation.Token(); ok {
if err := verifytoken.TokenValidator(v); err != nil {
return &ValidationError{Name: "token", err: fmt.Errorf(`ent: validator failed for field "VerifyToken.token": %w`, err)}
}
}
if v, ok := vtc.mutation.Purpose(); ok {
if err := verifytoken.PurposeValidator(v); err != nil {
return &ValidationError{Name: "purpose", err: fmt.Errorf(`ent: validator failed for field "VerifyToken.purpose": %w`, err)}
}
}
return nil
}
func (vtc *VerifyTokenCreate) sqlSave(ctx context.Context) (*VerifyToken, error) {
if err := vtc.check(); err != nil {
return nil, err
}
_node, _spec := vtc.createSpec()
if err := sqlgraph.CreateNode(ctx, vtc.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)
}
vtc.mutation.id = &_node.ID
vtc.mutation.done = true
return _node, nil
}
func (vtc *VerifyTokenCreate) createSpec() (*VerifyToken, *sqlgraph.CreateSpec) {
var (
_node = &VerifyToken{config: vtc.config}
_spec = sqlgraph.NewCreateSpec(verifytoken.Table, sqlgraph.NewFieldSpec(verifytoken.FieldID, field.TypeInt64))
)
if id, ok := vtc.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = id
}
if value, ok := vtc.mutation.CreatedAt(); ok {
_spec.SetField(verifytoken.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := vtc.mutation.ExpiresAt(); ok {
_spec.SetField(verifytoken.FieldExpiresAt, field.TypeTime, value)
_node.ExpiresAt = value
}
if value, ok := vtc.mutation.Token(); ok {
_spec.SetField(verifytoken.FieldToken, field.TypeString, value)
_node.Token = value
}
if value, ok := vtc.mutation.Purpose(); ok {
_spec.SetField(verifytoken.FieldPurpose, field.TypeString, value)
_node.Purpose = value
}
if nodes := vtc.mutation.UserIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: verifytoken.UserTable,
Columns: []string{verifytoken.UserColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.user_id = &nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// VerifyTokenCreateBulk is the builder for creating many VerifyToken entities in bulk.
type VerifyTokenCreateBulk struct {
config
err error
builders []*VerifyTokenCreate
}
// Save creates the VerifyToken entities in the database.
func (vtcb *VerifyTokenCreateBulk) Save(ctx context.Context) ([]*VerifyToken, error) {
if vtcb.err != nil {
return nil, vtcb.err
}
specs := make([]*sqlgraph.CreateSpec, len(vtcb.builders))
nodes := make([]*VerifyToken, len(vtcb.builders))
mutators := make([]Mutator, len(vtcb.builders))
for i := range vtcb.builders {
func(i int, root context.Context) {
builder := vtcb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*VerifyTokenMutation)
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, vtcb.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, vtcb.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, vtcb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (vtcb *VerifyTokenCreateBulk) SaveX(ctx context.Context) []*VerifyToken {
v, err := vtcb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (vtcb *VerifyTokenCreateBulk) Exec(ctx context.Context) error {
_, err := vtcb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (vtcb *VerifyTokenCreateBulk) ExecX(ctx context.Context) {
if err := vtcb.Exec(ctx); err != nil {
panic(err)
}
}