pcmt/ent/setup_create.go

223 lines
5.6 KiB
Go
Raw Normal View History

2023-05-05 22:52:59 +02: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"
"git.dotya.ml/mirre-mt/pcmt/ent/setup"
"github.com/google/uuid"
)
// SetupCreate is the builder for creating a Setup entity.
type SetupCreate struct {
config
mutation *SetupMutation
hooks []Hook
}
// SetSetUpAt sets the "set_up_at" field.
func (sc *SetupCreate) SetSetUpAt(t time.Time) *SetupCreate {
sc.mutation.SetSetUpAt(t)
return sc
}
// SetNillableSetUpAt sets the "set_up_at" field if the given value is not nil.
func (sc *SetupCreate) SetNillableSetUpAt(t *time.Time) *SetupCreate {
if t != nil {
sc.SetSetUpAt(*t)
}
return sc
}
// SetID sets the "id" field.
func (sc *SetupCreate) SetID(u uuid.UUID) *SetupCreate {
sc.mutation.SetID(u)
return sc
}
// SetNillableID sets the "id" field if the given value is not nil.
func (sc *SetupCreate) SetNillableID(u *uuid.UUID) *SetupCreate {
if u != nil {
sc.SetID(*u)
}
return sc
}
// Mutation returns the SetupMutation object of the builder.
func (sc *SetupCreate) Mutation() *SetupMutation {
return sc.mutation
}
// Save creates the Setup in the database.
func (sc *SetupCreate) Save(ctx context.Context) (*Setup, error) {
sc.defaults()
return withHooks[*Setup, SetupMutation](ctx, sc.sqlSave, sc.mutation, sc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (sc *SetupCreate) SaveX(ctx context.Context) *Setup {
v, err := sc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (sc *SetupCreate) Exec(ctx context.Context) error {
_, err := sc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (sc *SetupCreate) ExecX(ctx context.Context) {
if err := sc.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (sc *SetupCreate) defaults() {
if _, ok := sc.mutation.SetUpAt(); !ok {
v := setup.DefaultSetUpAt()
sc.mutation.SetSetUpAt(v)
}
if _, ok := sc.mutation.ID(); !ok {
v := setup.DefaultID()
sc.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (sc *SetupCreate) check() error {
if _, ok := sc.mutation.SetUpAt(); !ok {
return &ValidationError{Name: "set_up_at", err: errors.New(`ent: missing required field "Setup.set_up_at"`)}
}
return nil
}
func (sc *SetupCreate) sqlSave(ctx context.Context) (*Setup, error) {
if err := sc.check(); err != nil {
return nil, err
}
_node, _spec := sc.createSpec()
if err := sqlgraph.CreateNode(ctx, sc.driver, _spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
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
}
}
sc.mutation.id = &_node.ID
sc.mutation.done = true
return _node, nil
}
func (sc *SetupCreate) createSpec() (*Setup, *sqlgraph.CreateSpec) {
var (
_node = &Setup{config: sc.config}
_spec = sqlgraph.NewCreateSpec(setup.Table, sqlgraph.NewFieldSpec(setup.FieldID, field.TypeUUID))
)
if id, ok := sc.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = &id
}
if value, ok := sc.mutation.SetUpAt(); ok {
_spec.SetField(setup.FieldSetUpAt, field.TypeTime, value)
_node.SetUpAt = value
}
return _node, _spec
}
// SetupCreateBulk is the builder for creating many Setup entities in bulk.
type SetupCreateBulk struct {
config
builders []*SetupCreate
}
// Save creates the Setup entities in the database.
func (scb *SetupCreateBulk) Save(ctx context.Context) ([]*Setup, error) {
specs := make([]*sqlgraph.CreateSpec, len(scb.builders))
nodes := make([]*Setup, len(scb.builders))
mutators := make([]Mutator, len(scb.builders))
for i := range scb.builders {
func(i int, root context.Context) {
builder := scb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*SetupMutation)
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, scb.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, scb.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
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, scb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (scb *SetupCreateBulk) SaveX(ctx context.Context) []*Setup {
v, err := scb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (scb *SetupCreateBulk) Exec(ctx context.Context) error {
_, err := scb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (scb *SetupCreateBulk) ExecX(ctx context.Context) {
if err := scb.Exec(ctx); err != nil {
panic(err)
}
}