Files
csgowtfd/ent/weapon_create.go
2025-10-26 05:09:47 +01:00

260 lines
7.0 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"
"somegit.dev/csgowtf/csgowtfd/ent/matchplayer"
"somegit.dev/csgowtf/csgowtfd/ent/weapon"
)
// WeaponCreate is the builder for creating a Weapon entity.
type WeaponCreate struct {
config
mutation *WeaponMutation
hooks []Hook
}
// SetVictim sets the "victim" field.
func (_c *WeaponCreate) SetVictim(v uint64) *WeaponCreate {
_c.mutation.SetVictim(v)
return _c
}
// SetDmg sets the "dmg" field.
func (_c *WeaponCreate) SetDmg(v uint) *WeaponCreate {
_c.mutation.SetDmg(v)
return _c
}
// SetEqType sets the "eq_type" field.
func (_c *WeaponCreate) SetEqType(v int) *WeaponCreate {
_c.mutation.SetEqType(v)
return _c
}
// SetHitGroup sets the "hit_group" field.
func (_c *WeaponCreate) SetHitGroup(v int) *WeaponCreate {
_c.mutation.SetHitGroup(v)
return _c
}
// SetStatID sets the "stat" edge to the MatchPlayer entity by ID.
func (_c *WeaponCreate) SetStatID(id int) *WeaponCreate {
_c.mutation.SetStatID(id)
return _c
}
// SetNillableStatID sets the "stat" edge to the MatchPlayer entity by ID if the given value is not nil.
func (_c *WeaponCreate) SetNillableStatID(id *int) *WeaponCreate {
if id != nil {
_c = _c.SetStatID(*id)
}
return _c
}
// SetStat sets the "stat" edge to the MatchPlayer entity.
func (_c *WeaponCreate) SetStat(v *MatchPlayer) *WeaponCreate {
return _c.SetStatID(v.ID)
}
// Mutation returns the WeaponMutation object of the builder.
func (_c *WeaponCreate) Mutation() *WeaponMutation {
return _c.mutation
}
// Save creates the Weapon in the database.
func (_c *WeaponCreate) Save(ctx context.Context) (*Weapon, error) {
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *WeaponCreate) SaveX(ctx context.Context) *Weapon {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *WeaponCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *WeaponCreate) ExecX(ctx context.Context) {
if err := _c.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (_c *WeaponCreate) check() error {
if _, ok := _c.mutation.Victim(); !ok {
return &ValidationError{Name: "victim", err: errors.New(`ent: missing required field "Weapon.victim"`)}
}
if _, ok := _c.mutation.Dmg(); !ok {
return &ValidationError{Name: "dmg", err: errors.New(`ent: missing required field "Weapon.dmg"`)}
}
if _, ok := _c.mutation.EqType(); !ok {
return &ValidationError{Name: "eq_type", err: errors.New(`ent: missing required field "Weapon.eq_type"`)}
}
if _, ok := _c.mutation.HitGroup(); !ok {
return &ValidationError{Name: "hit_group", err: errors.New(`ent: missing required field "Weapon.hit_group"`)}
}
return nil
}
func (_c *WeaponCreate) sqlSave(ctx context.Context) (*Weapon, error) {
if err := _c.check(); err != nil {
return nil, err
}
_node, _spec := _c.createSpec()
if err := sqlgraph.CreateNode(ctx, _c.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)
_c.mutation.id = &_node.ID
_c.mutation.done = true
return _node, nil
}
func (_c *WeaponCreate) createSpec() (*Weapon, *sqlgraph.CreateSpec) {
var (
_node = &Weapon{config: _c.config}
_spec = sqlgraph.NewCreateSpec(weapon.Table, sqlgraph.NewFieldSpec(weapon.FieldID, field.TypeInt))
)
if value, ok := _c.mutation.Victim(); ok {
_spec.SetField(weapon.FieldVictim, field.TypeUint64, value)
_node.Victim = value
}
if value, ok := _c.mutation.Dmg(); ok {
_spec.SetField(weapon.FieldDmg, field.TypeUint, value)
_node.Dmg = value
}
if value, ok := _c.mutation.EqType(); ok {
_spec.SetField(weapon.FieldEqType, field.TypeInt, value)
_node.EqType = value
}
if value, ok := _c.mutation.HitGroup(); ok {
_spec.SetField(weapon.FieldHitGroup, field.TypeInt, value)
_node.HitGroup = value
}
if nodes := _c.mutation.StatIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: weapon.StatTable,
Columns: []string{weapon.StatColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(matchplayer.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.match_player_weapon_stats = &nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// WeaponCreateBulk is the builder for creating many Weapon entities in bulk.
type WeaponCreateBulk struct {
config
err error
builders []*WeaponCreate
}
// Save creates the Weapon entities in the database.
func (_c *WeaponCreateBulk) Save(ctx context.Context) ([]*Weapon, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*Weapon, len(_c.builders))
mutators := make([]Mutator, len(_c.builders))
for i := range _c.builders {
func(i int, root context.Context) {
builder := _c.builders[i]
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*WeaponMutation)
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, _c.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, _c.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, _c.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (_c *WeaponCreateBulk) SaveX(ctx context.Context) []*Weapon {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *WeaponCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *WeaponCreateBulk) ExecX(ctx context.Context) {
if err := _c.Exec(ctx); err != nil {
panic(err)
}
}