// Code generated by ent, DO NOT EDIT.

package serversitemicros

import (
	"context"
	"convert-server/domain/entity/serversitemicros/docserverapp"
	"errors"
	"fmt"
	"time"

	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// DocServerAppCreate is the builder for creating a DocServerApp entity.
type DocServerAppCreate struct {
	config
	mutation *DocServerAppMutation
	hooks    []Hook
}

// SetApplication sets the "application" field.
func (dsac *DocServerAppCreate) SetApplication(s string) *DocServerAppCreate {
	dsac.mutation.SetApplication(s)
	return dsac
}

// SetName sets the "name" field.
func (dsac *DocServerAppCreate) SetName(s string) *DocServerAppCreate {
	dsac.mutation.SetName(s)
	return dsac
}

// SetNillableName sets the "name" field if the given value is not nil.
func (dsac *DocServerAppCreate) SetNillableName(s *string) *DocServerAppCreate {
	if s != nil {
		dsac.SetName(*s)
	}
	return dsac
}

// SetCreatedAt sets the "created_at" field.
func (dsac *DocServerAppCreate) SetCreatedAt(t time.Time) *DocServerAppCreate {
	dsac.mutation.SetCreatedAt(t)
	return dsac
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (dsac *DocServerAppCreate) SetNillableCreatedAt(t *time.Time) *DocServerAppCreate {
	if t != nil {
		dsac.SetCreatedAt(*t)
	}
	return dsac
}

// SetID sets the "id" field.
func (dsac *DocServerAppCreate) SetID(i int32) *DocServerAppCreate {
	dsac.mutation.SetID(i)
	return dsac
}

// Mutation returns the DocServerAppMutation object of the builder.
func (dsac *DocServerAppCreate) Mutation() *DocServerAppMutation {
	return dsac.mutation
}

// Save creates the DocServerApp in the database.
func (dsac *DocServerAppCreate) Save(ctx context.Context) (*DocServerApp, error) {
	if err := dsac.defaults(); err != nil {
		return nil, err
	}
	return withHooks(ctx, dsac.sqlSave, dsac.mutation, dsac.hooks)
}

// SaveX calls Save and panics if Save returns an error.
func (dsac *DocServerAppCreate) SaveX(ctx context.Context) *DocServerApp {
	v, err := dsac.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func (dsac *DocServerAppCreate) Exec(ctx context.Context) error {
	_, err := dsac.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (dsac *DocServerAppCreate) ExecX(ctx context.Context) {
	if err := dsac.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (dsac *DocServerAppCreate) defaults() error {
	if _, ok := dsac.mutation.Name(); !ok {
		v := docserverapp.DefaultName
		dsac.mutation.SetName(v)
	}
	return nil
}

// check runs all checks and user-defined validators on the builder.
func (dsac *DocServerAppCreate) check() error {
	if _, ok := dsac.mutation.Application(); !ok {
		return &ValidationError{Name: "application", err: errors.New(`serversitemicros: missing required field "DocServerApp.application"`)}
	}
	if _, ok := dsac.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`serversitemicros: missing required field "DocServerApp.name"`)}
	}
	return nil
}

func (dsac *DocServerAppCreate) sqlSave(ctx context.Context) (*DocServerApp, error) {
	if err := dsac.check(); err != nil {
		return nil, err
	}
	_node, _spec := dsac.createSpec()
	if err := sqlgraph.CreateNode(ctx, dsac.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 = int32(id)
	}
	dsac.mutation.id = &_node.ID
	dsac.mutation.done = true
	return _node, nil
}

func (dsac *DocServerAppCreate) createSpec() (*DocServerApp, *sqlgraph.CreateSpec) {
	var (
		_node = &DocServerApp{config: dsac.config}
		_spec = sqlgraph.NewCreateSpec(docserverapp.Table, sqlgraph.NewFieldSpec(docserverapp.FieldID, field.TypeInt32))
	)
	if id, ok := dsac.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := dsac.mutation.Application(); ok {
		_spec.SetField(docserverapp.FieldApplication, field.TypeString, value)
		_node.Application = value
	}
	if value, ok := dsac.mutation.Name(); ok {
		_spec.SetField(docserverapp.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := dsac.mutation.CreatedAt(); ok {
		_spec.SetField(docserverapp.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = &value
	}
	return _node, _spec
}

// DocServerAppCreateBulk is the builder for creating many DocServerApp entities in bulk.
type DocServerAppCreateBulk struct {
	config
	err      error
	builders []*DocServerAppCreate
}

// Save creates the DocServerApp entities in the database.
func (dsacb *DocServerAppCreateBulk) Save(ctx context.Context) ([]*DocServerApp, error) {
	if dsacb.err != nil {
		return nil, dsacb.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(dsacb.builders))
	nodes := make([]*DocServerApp, len(dsacb.builders))
	mutators := make([]Mutator, len(dsacb.builders))
	for i := range dsacb.builders {
		func(i int, root context.Context) {
			builder := dsacb.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*DocServerAppMutation)
				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, dsacb.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, dsacb.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 = int32(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, dsacb.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func (dsacb *DocServerAppCreateBulk) SaveX(ctx context.Context) []*DocServerApp {
	v, err := dsacb.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func (dsacb *DocServerAppCreateBulk) Exec(ctx context.Context) error {
	_, err := dsacb.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (dsacb *DocServerAppCreateBulk) ExecX(ctx context.Context) {
	if err := dsacb.Exec(ctx); err != nil {
		panic(err)
	}
}
