Files
csgowtfd/ent/match_create.go

529 lines
14 KiB
Go

// 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.harting.dev/csgowtf/csgowtfd/ent/match"
"git.harting.dev/csgowtf/csgowtfd/ent/matchplayer"
"git.harting.dev/csgowtf/csgowtfd/ent/player"
)
// MatchCreate is the builder for creating a Match entity.
type MatchCreate struct {
config
mutation *MatchMutation
hooks []Hook
}
// SetShareCode sets the "share_code" field.
func (mc *MatchCreate) SetShareCode(s string) *MatchCreate {
mc.mutation.SetShareCode(s)
return mc
}
// SetMap sets the "map" field.
func (mc *MatchCreate) SetMap(s string) *MatchCreate {
mc.mutation.SetMap(s)
return mc
}
// SetNillableMap sets the "map" field if the given value is not nil.
func (mc *MatchCreate) SetNillableMap(s *string) *MatchCreate {
if s != nil {
mc.SetMap(*s)
}
return mc
}
// SetDate sets the "date" field.
func (mc *MatchCreate) SetDate(t time.Time) *MatchCreate {
mc.mutation.SetDate(t)
return mc
}
// SetScoreTeamA sets the "score_team_a" field.
func (mc *MatchCreate) SetScoreTeamA(i int) *MatchCreate {
mc.mutation.SetScoreTeamA(i)
return mc
}
// SetScoreTeamB sets the "score_team_b" field.
func (mc *MatchCreate) SetScoreTeamB(i int) *MatchCreate {
mc.mutation.SetScoreTeamB(i)
return mc
}
// SetReplayURL sets the "replay_url" field.
func (mc *MatchCreate) SetReplayURL(s string) *MatchCreate {
mc.mutation.SetReplayURL(s)
return mc
}
// SetNillableReplayURL sets the "replay_url" field if the given value is not nil.
func (mc *MatchCreate) SetNillableReplayURL(s *string) *MatchCreate {
if s != nil {
mc.SetReplayURL(*s)
}
return mc
}
// SetDuration sets the "duration" field.
func (mc *MatchCreate) SetDuration(i int) *MatchCreate {
mc.mutation.SetDuration(i)
return mc
}
// SetMatchResult sets the "match_result" field.
func (mc *MatchCreate) SetMatchResult(i int) *MatchCreate {
mc.mutation.SetMatchResult(i)
return mc
}
// SetMaxRounds sets the "max_rounds" field.
func (mc *MatchCreate) SetMaxRounds(i int) *MatchCreate {
mc.mutation.SetMaxRounds(i)
return mc
}
// SetDemoParsed sets the "demo_parsed" field.
func (mc *MatchCreate) SetDemoParsed(b bool) *MatchCreate {
mc.mutation.SetDemoParsed(b)
return mc
}
// SetNillableDemoParsed sets the "demo_parsed" field if the given value is not nil.
func (mc *MatchCreate) SetNillableDemoParsed(b *bool) *MatchCreate {
if b != nil {
mc.SetDemoParsed(*b)
}
return mc
}
// SetVacPresent sets the "vac_present" field.
func (mc *MatchCreate) SetVacPresent(b bool) *MatchCreate {
mc.mutation.SetVacPresent(b)
return mc
}
// SetNillableVacPresent sets the "vac_present" field if the given value is not nil.
func (mc *MatchCreate) SetNillableVacPresent(b *bool) *MatchCreate {
if b != nil {
mc.SetVacPresent(*b)
}
return mc
}
// SetGamebanPresent sets the "gameban_present" field.
func (mc *MatchCreate) SetGamebanPresent(b bool) *MatchCreate {
mc.mutation.SetGamebanPresent(b)
return mc
}
// SetNillableGamebanPresent sets the "gameban_present" field if the given value is not nil.
func (mc *MatchCreate) SetNillableGamebanPresent(b *bool) *MatchCreate {
if b != nil {
mc.SetGamebanPresent(*b)
}
return mc
}
// SetDecryptionKey sets the "decryption_key" field.
func (mc *MatchCreate) SetDecryptionKey(b []byte) *MatchCreate {
mc.mutation.SetDecryptionKey(b)
return mc
}
// SetTickRate sets the "tick_rate" field.
func (mc *MatchCreate) SetTickRate(f float64) *MatchCreate {
mc.mutation.SetTickRate(f)
return mc
}
// SetNillableTickRate sets the "tick_rate" field if the given value is not nil.
func (mc *MatchCreate) SetNillableTickRate(f *float64) *MatchCreate {
if f != nil {
mc.SetTickRate(*f)
}
return mc
}
// SetID sets the "id" field.
func (mc *MatchCreate) SetID(u uint64) *MatchCreate {
mc.mutation.SetID(u)
return mc
}
// AddStatIDs adds the "stats" edge to the MatchPlayer entity by IDs.
func (mc *MatchCreate) AddStatIDs(ids ...int) *MatchCreate {
mc.mutation.AddStatIDs(ids...)
return mc
}
// AddStats adds the "stats" edges to the MatchPlayer entity.
func (mc *MatchCreate) AddStats(m ...*MatchPlayer) *MatchCreate {
ids := make([]int, len(m))
for i := range m {
ids[i] = m[i].ID
}
return mc.AddStatIDs(ids...)
}
// AddPlayerIDs adds the "players" edge to the Player entity by IDs.
func (mc *MatchCreate) AddPlayerIDs(ids ...uint64) *MatchCreate {
mc.mutation.AddPlayerIDs(ids...)
return mc
}
// AddPlayers adds the "players" edges to the Player entity.
func (mc *MatchCreate) AddPlayers(p ...*Player) *MatchCreate {
ids := make([]uint64, len(p))
for i := range p {
ids[i] = p[i].ID
}
return mc.AddPlayerIDs(ids...)
}
// Mutation returns the MatchMutation object of the builder.
func (mc *MatchCreate) Mutation() *MatchMutation {
return mc.mutation
}
// Save creates the Match in the database.
func (mc *MatchCreate) Save(ctx context.Context) (*Match, error) {
var (
err error
node *Match
)
mc.defaults()
if len(mc.hooks) == 0 {
if err = mc.check(); err != nil {
return nil, err
}
node, err = mc.sqlSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*MatchMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err = mc.check(); err != nil {
return nil, err
}
mc.mutation = mutation
if node, err = mc.sqlSave(ctx); err != nil {
return nil, err
}
mutation.id = &node.ID
mutation.done = true
return node, err
})
for i := len(mc.hooks) - 1; i >= 0; i-- {
if mc.hooks[i] == nil {
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
}
mut = mc.hooks[i](mut)
}
v, err := mut.Mutate(ctx, mc.mutation)
if err != nil {
return nil, err
}
nv, ok := v.(*Match)
if !ok {
return nil, fmt.Errorf("unexpected node type %T returned from MatchMutation", v)
}
node = nv
}
return node, err
}
// SaveX calls Save and panics if Save returns an error.
func (mc *MatchCreate) SaveX(ctx context.Context) *Match {
v, err := mc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (mc *MatchCreate) Exec(ctx context.Context) error {
_, err := mc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (mc *MatchCreate) ExecX(ctx context.Context) {
if err := mc.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (mc *MatchCreate) defaults() {
if _, ok := mc.mutation.DemoParsed(); !ok {
v := match.DefaultDemoParsed
mc.mutation.SetDemoParsed(v)
}
if _, ok := mc.mutation.VacPresent(); !ok {
v := match.DefaultVacPresent
mc.mutation.SetVacPresent(v)
}
if _, ok := mc.mutation.GamebanPresent(); !ok {
v := match.DefaultGamebanPresent
mc.mutation.SetGamebanPresent(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (mc *MatchCreate) check() error {
if _, ok := mc.mutation.ShareCode(); !ok {
return &ValidationError{Name: "share_code", err: errors.New(`ent: missing required field "Match.share_code"`)}
}
if _, ok := mc.mutation.Date(); !ok {
return &ValidationError{Name: "date", err: errors.New(`ent: missing required field "Match.date"`)}
}
if _, ok := mc.mutation.ScoreTeamA(); !ok {
return &ValidationError{Name: "score_team_a", err: errors.New(`ent: missing required field "Match.score_team_a"`)}
}
if _, ok := mc.mutation.ScoreTeamB(); !ok {
return &ValidationError{Name: "score_team_b", err: errors.New(`ent: missing required field "Match.score_team_b"`)}
}
if _, ok := mc.mutation.Duration(); !ok {
return &ValidationError{Name: "duration", err: errors.New(`ent: missing required field "Match.duration"`)}
}
if _, ok := mc.mutation.MatchResult(); !ok {
return &ValidationError{Name: "match_result", err: errors.New(`ent: missing required field "Match.match_result"`)}
}
if _, ok := mc.mutation.MaxRounds(); !ok {
return &ValidationError{Name: "max_rounds", err: errors.New(`ent: missing required field "Match.max_rounds"`)}
}
if _, ok := mc.mutation.DemoParsed(); !ok {
return &ValidationError{Name: "demo_parsed", err: errors.New(`ent: missing required field "Match.demo_parsed"`)}
}
if _, ok := mc.mutation.VacPresent(); !ok {
return &ValidationError{Name: "vac_present", err: errors.New(`ent: missing required field "Match.vac_present"`)}
}
if _, ok := mc.mutation.GamebanPresent(); !ok {
return &ValidationError{Name: "gameban_present", err: errors.New(`ent: missing required field "Match.gameban_present"`)}
}
return nil
}
func (mc *MatchCreate) sqlSave(ctx context.Context) (*Match, error) {
_node, _spec := mc.createSpec()
if err := sqlgraph.CreateNode(ctx, mc.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 = uint64(id)
}
return _node, nil
}
func (mc *MatchCreate) createSpec() (*Match, *sqlgraph.CreateSpec) {
var (
_node = &Match{config: mc.config}
_spec = &sqlgraph.CreateSpec{
Table: match.Table,
ID: &sqlgraph.FieldSpec{
Type: field.TypeUint64,
Column: match.FieldID,
},
}
)
if id, ok := mc.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = id
}
if value, ok := mc.mutation.ShareCode(); ok {
_spec.SetField(match.FieldShareCode, field.TypeString, value)
_node.ShareCode = value
}
if value, ok := mc.mutation.Map(); ok {
_spec.SetField(match.FieldMap, field.TypeString, value)
_node.Map = value
}
if value, ok := mc.mutation.Date(); ok {
_spec.SetField(match.FieldDate, field.TypeTime, value)
_node.Date = value
}
if value, ok := mc.mutation.ScoreTeamA(); ok {
_spec.SetField(match.FieldScoreTeamA, field.TypeInt, value)
_node.ScoreTeamA = value
}
if value, ok := mc.mutation.ScoreTeamB(); ok {
_spec.SetField(match.FieldScoreTeamB, field.TypeInt, value)
_node.ScoreTeamB = value
}
if value, ok := mc.mutation.ReplayURL(); ok {
_spec.SetField(match.FieldReplayURL, field.TypeString, value)
_node.ReplayURL = value
}
if value, ok := mc.mutation.Duration(); ok {
_spec.SetField(match.FieldDuration, field.TypeInt, value)
_node.Duration = value
}
if value, ok := mc.mutation.MatchResult(); ok {
_spec.SetField(match.FieldMatchResult, field.TypeInt, value)
_node.MatchResult = value
}
if value, ok := mc.mutation.MaxRounds(); ok {
_spec.SetField(match.FieldMaxRounds, field.TypeInt, value)
_node.MaxRounds = value
}
if value, ok := mc.mutation.DemoParsed(); ok {
_spec.SetField(match.FieldDemoParsed, field.TypeBool, value)
_node.DemoParsed = value
}
if value, ok := mc.mutation.VacPresent(); ok {
_spec.SetField(match.FieldVacPresent, field.TypeBool, value)
_node.VacPresent = value
}
if value, ok := mc.mutation.GamebanPresent(); ok {
_spec.SetField(match.FieldGamebanPresent, field.TypeBool, value)
_node.GamebanPresent = value
}
if value, ok := mc.mutation.DecryptionKey(); ok {
_spec.SetField(match.FieldDecryptionKey, field.TypeBytes, value)
_node.DecryptionKey = value
}
if value, ok := mc.mutation.TickRate(); ok {
_spec.SetField(match.FieldTickRate, field.TypeFloat64, value)
_node.TickRate = value
}
if nodes := mc.mutation.StatsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: match.StatsTable,
Columns: []string{match.StatsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: matchplayer.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := mc.mutation.PlayersIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: true,
Table: match.PlayersTable,
Columns: match.PlayersPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeUint64,
Column: player.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// MatchCreateBulk is the builder for creating many Match entities in bulk.
type MatchCreateBulk struct {
config
builders []*MatchCreate
}
// Save creates the Match entities in the database.
func (mcb *MatchCreateBulk) Save(ctx context.Context) ([]*Match, error) {
specs := make([]*sqlgraph.CreateSpec, len(mcb.builders))
nodes := make([]*Match, len(mcb.builders))
mutators := make([]Mutator, len(mcb.builders))
for i := range mcb.builders {
func(i int, root context.Context) {
builder := mcb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*MatchMutation)
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, mcb.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, mcb.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 = uint64(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, mcb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (mcb *MatchCreateBulk) SaveX(ctx context.Context) []*Match {
v, err := mcb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (mcb *MatchCreateBulk) Exec(ctx context.Context) error {
_, err := mcb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (mcb *MatchCreateBulk) ExecX(ctx context.Context) {
if err := mcb.Exec(ctx); err != nil {
panic(err)
}
}