zkt24/z2/backend/ent/validators_create.go
2024-04-25 13:51:47 +02:00

221 lines
6.0 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"thesis/ent/key"
"thesis/ent/validators"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
)
// ValidatorsCreate is the builder for creating a Validators entity.
type ValidatorsCreate struct {
config
mutation *ValidatorsMutation
hooks []Hook
}
// SetFacilitator sets the "facilitator" field.
func (vc *ValidatorsCreate) SetFacilitator(s string) *ValidatorsCreate {
vc.mutation.SetFacilitator(s)
return vc
}
// AddKeyIDs adds the "key" edge to the Key entity by IDs.
func (vc *ValidatorsCreate) AddKeyIDs(ids ...int) *ValidatorsCreate {
vc.mutation.AddKeyIDs(ids...)
return vc
}
// AddKey adds the "key" edges to the Key entity.
func (vc *ValidatorsCreate) AddKey(k ...*Key) *ValidatorsCreate {
ids := make([]int, len(k))
for i := range k {
ids[i] = k[i].ID
}
return vc.AddKeyIDs(ids...)
}
// Mutation returns the ValidatorsMutation object of the builder.
func (vc *ValidatorsCreate) Mutation() *ValidatorsMutation {
return vc.mutation
}
// Save creates the Validators in the database.
func (vc *ValidatorsCreate) Save(ctx context.Context) (*Validators, error) {
return withHooks(ctx, vc.sqlSave, vc.mutation, vc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (vc *ValidatorsCreate) SaveX(ctx context.Context) *Validators {
v, err := vc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (vc *ValidatorsCreate) Exec(ctx context.Context) error {
_, err := vc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (vc *ValidatorsCreate) ExecX(ctx context.Context) {
if err := vc.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (vc *ValidatorsCreate) check() error {
if _, ok := vc.mutation.Facilitator(); !ok {
return &ValidationError{Name: "facilitator", err: errors.New(`ent: missing required field "Validators.facilitator"`)}
}
if v, ok := vc.mutation.Facilitator(); ok {
if err := validators.FacilitatorValidator(v); err != nil {
return &ValidationError{Name: "facilitator", err: fmt.Errorf(`ent: validator failed for field "Validators.facilitator": %w`, err)}
}
}
return nil
}
func (vc *ValidatorsCreate) sqlSave(ctx context.Context) (*Validators, error) {
if err := vc.check(); err != nil {
return nil, err
}
_node, _spec := vc.createSpec()
if err := sqlgraph.CreateNode(ctx, vc.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)
vc.mutation.id = &_node.ID
vc.mutation.done = true
return _node, nil
}
func (vc *ValidatorsCreate) createSpec() (*Validators, *sqlgraph.CreateSpec) {
var (
_node = &Validators{config: vc.config}
_spec = sqlgraph.NewCreateSpec(validators.Table, sqlgraph.NewFieldSpec(validators.FieldID, field.TypeInt))
)
if value, ok := vc.mutation.Facilitator(); ok {
_spec.SetField(validators.FieldFacilitator, field.TypeString, value)
_node.Facilitator = value
}
if nodes := vc.mutation.KeyIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: validators.KeyTable,
Columns: []string{validators.KeyColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(key.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// ValidatorsCreateBulk is the builder for creating many Validators entities in bulk.
type ValidatorsCreateBulk struct {
config
err error
builders []*ValidatorsCreate
}
// Save creates the Validators entities in the database.
func (vcb *ValidatorsCreateBulk) Save(ctx context.Context) ([]*Validators, error) {
if vcb.err != nil {
return nil, vcb.err
}
specs := make([]*sqlgraph.CreateSpec, len(vcb.builders))
nodes := make([]*Validators, len(vcb.builders))
mutators := make([]Mutator, len(vcb.builders))
for i := range vcb.builders {
func(i int, root context.Context) {
builder := vcb.builders[i]
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*ValidatorsMutation)
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, vcb.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, vcb.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, vcb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (vcb *ValidatorsCreateBulk) SaveX(ctx context.Context) []*Validators {
v, err := vcb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (vcb *ValidatorsCreateBulk) Exec(ctx context.Context) error {
_, err := vcb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (vcb *ValidatorsCreateBulk) ExecX(ctx context.Context) {
if err := vcb.Exec(ctx); err != nil {
panic(err)
}
}