frankenapi/ent/punishments_create.go
2022-09-08 09:18:04 +02:00

307 lines
8.2 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/FrankenBotDev/FrankenAPI/ent/punishments"
)
// PunishmentsCreate is the builder for creating a Punishments entity.
type PunishmentsCreate struct {
config
mutation *PunishmentsMutation
hooks []Hook
}
// SetServerid sets the "serverid" field.
func (pc *PunishmentsCreate) SetServerid(s string) *PunishmentsCreate {
pc.mutation.SetServerid(s)
return pc
}
// SetWarnamount sets the "warnamount" field.
func (pc *PunishmentsCreate) SetWarnamount(s string) *PunishmentsCreate {
pc.mutation.SetWarnamount(s)
return pc
}
// SetActiontype sets the "actiontype" field.
func (pc *PunishmentsCreate) SetActiontype(s string) *PunishmentsCreate {
pc.mutation.SetActiontype(s)
return pc
}
// SetDuration sets the "duration" field.
func (pc *PunishmentsCreate) SetDuration(s string) *PunishmentsCreate {
pc.mutation.SetDuration(s)
return pc
}
// SetReason sets the "reason" field.
func (pc *PunishmentsCreate) SetReason(s string) *PunishmentsCreate {
pc.mutation.SetReason(s)
return pc
}
// SetID sets the "id" field.
func (pc *PunishmentsCreate) SetID(i int) *PunishmentsCreate {
pc.mutation.SetID(i)
return pc
}
// Mutation returns the PunishmentsMutation object of the builder.
func (pc *PunishmentsCreate) Mutation() *PunishmentsMutation {
return pc.mutation
}
// Save creates the Punishments in the database.
func (pc *PunishmentsCreate) Save(ctx context.Context) (*Punishments, error) {
var (
err error
node *Punishments
)
if len(pc.hooks) == 0 {
if err = pc.check(); err != nil {
return nil, err
}
node, err = pc.sqlSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*PunishmentsMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err = pc.check(); err != nil {
return nil, err
}
pc.mutation = mutation
if node, err = pc.sqlSave(ctx); err != nil {
return nil, err
}
mutation.id = &node.ID
mutation.done = true
return node, err
})
for i := len(pc.hooks) - 1; i >= 0; i-- {
if pc.hooks[i] == nil {
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
}
mut = pc.hooks[i](mut)
}
v, err := mut.Mutate(ctx, pc.mutation)
if err != nil {
return nil, err
}
nv, ok := v.(*Punishments)
if !ok {
return nil, fmt.Errorf("unexpected node type %T returned from PunishmentsMutation", v)
}
node = nv
}
return node, err
}
// SaveX calls Save and panics if Save returns an error.
func (pc *PunishmentsCreate) SaveX(ctx context.Context) *Punishments {
v, err := pc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (pc *PunishmentsCreate) Exec(ctx context.Context) error {
_, err := pc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (pc *PunishmentsCreate) ExecX(ctx context.Context) {
if err := pc.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (pc *PunishmentsCreate) check() error {
if _, ok := pc.mutation.Serverid(); !ok {
return &ValidationError{Name: "serverid", err: errors.New(`ent: missing required field "Punishments.serverid"`)}
}
if _, ok := pc.mutation.Warnamount(); !ok {
return &ValidationError{Name: "warnamount", err: errors.New(`ent: missing required field "Punishments.warnamount"`)}
}
if _, ok := pc.mutation.Actiontype(); !ok {
return &ValidationError{Name: "actiontype", err: errors.New(`ent: missing required field "Punishments.actiontype"`)}
}
if _, ok := pc.mutation.Duration(); !ok {
return &ValidationError{Name: "duration", err: errors.New(`ent: missing required field "Punishments.duration"`)}
}
if _, ok := pc.mutation.Reason(); !ok {
return &ValidationError{Name: "reason", err: errors.New(`ent: missing required field "Punishments.reason"`)}
}
return nil
}
func (pc *PunishmentsCreate) sqlSave(ctx context.Context) (*Punishments, error) {
_node, _spec := pc.createSpec()
if err := sqlgraph.CreateNode(ctx, pc.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 = int(id)
}
return _node, nil
}
func (pc *PunishmentsCreate) createSpec() (*Punishments, *sqlgraph.CreateSpec) {
var (
_node = &Punishments{config: pc.config}
_spec = &sqlgraph.CreateSpec{
Table: punishments.Table,
ID: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: punishments.FieldID,
},
}
)
if id, ok := pc.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = id
}
if value, ok := pc.mutation.Serverid(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: punishments.FieldServerid,
})
_node.Serverid = value
}
if value, ok := pc.mutation.Warnamount(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: punishments.FieldWarnamount,
})
_node.Warnamount = value
}
if value, ok := pc.mutation.Actiontype(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: punishments.FieldActiontype,
})
_node.Actiontype = value
}
if value, ok := pc.mutation.Duration(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: punishments.FieldDuration,
})
_node.Duration = value
}
if value, ok := pc.mutation.Reason(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: punishments.FieldReason,
})
_node.Reason = value
}
return _node, _spec
}
// PunishmentsCreateBulk is the builder for creating many Punishments entities in bulk.
type PunishmentsCreateBulk struct {
config
builders []*PunishmentsCreate
}
// Save creates the Punishments entities in the database.
func (pcb *PunishmentsCreateBulk) Save(ctx context.Context) ([]*Punishments, error) {
specs := make([]*sqlgraph.CreateSpec, len(pcb.builders))
nodes := make([]*Punishments, len(pcb.builders))
mutators := make([]Mutator, len(pcb.builders))
for i := range pcb.builders {
func(i int, root context.Context) {
builder := pcb.builders[i]
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*PunishmentsMutation)
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, pcb.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, pcb.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 = 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, pcb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (pcb *PunishmentsCreateBulk) SaveX(ctx context.Context) []*Punishments {
v, err := pcb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (pcb *PunishmentsCreateBulk) Exec(ctx context.Context) error {
_, err := pcb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (pcb *PunishmentsCreateBulk) ExecX(ctx context.Context) {
if err := pcb.Exec(ctx); err != nil {
panic(err)
}
}