// 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" "somegit.dev/csgowtf/csgowtfd/ent/match" "somegit.dev/csgowtf/csgowtfd/ent/matchplayer" "somegit.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 (_c *MatchCreate) SetShareCode(v string) *MatchCreate { _c.mutation.SetShareCode(v) return _c } // SetMap sets the "map" field. func (_c *MatchCreate) SetMap(v string) *MatchCreate { _c.mutation.SetMap(v) return _c } // SetNillableMap sets the "map" field if the given value is not nil. func (_c *MatchCreate) SetNillableMap(v *string) *MatchCreate { if v != nil { _c.SetMap(*v) } return _c } // SetDate sets the "date" field. func (_c *MatchCreate) SetDate(v time.Time) *MatchCreate { _c.mutation.SetDate(v) return _c } // SetScoreTeamA sets the "score_team_a" field. func (_c *MatchCreate) SetScoreTeamA(v int) *MatchCreate { _c.mutation.SetScoreTeamA(v) return _c } // SetScoreTeamB sets the "score_team_b" field. func (_c *MatchCreate) SetScoreTeamB(v int) *MatchCreate { _c.mutation.SetScoreTeamB(v) return _c } // SetReplayURL sets the "replay_url" field. func (_c *MatchCreate) SetReplayURL(v string) *MatchCreate { _c.mutation.SetReplayURL(v) return _c } // SetNillableReplayURL sets the "replay_url" field if the given value is not nil. func (_c *MatchCreate) SetNillableReplayURL(v *string) *MatchCreate { if v != nil { _c.SetReplayURL(*v) } return _c } // SetDuration sets the "duration" field. func (_c *MatchCreate) SetDuration(v int) *MatchCreate { _c.mutation.SetDuration(v) return _c } // SetMatchResult sets the "match_result" field. func (_c *MatchCreate) SetMatchResult(v int) *MatchCreate { _c.mutation.SetMatchResult(v) return _c } // SetMaxRounds sets the "max_rounds" field. func (_c *MatchCreate) SetMaxRounds(v int) *MatchCreate { _c.mutation.SetMaxRounds(v) return _c } // SetDemoParsed sets the "demo_parsed" field. func (_c *MatchCreate) SetDemoParsed(v bool) *MatchCreate { _c.mutation.SetDemoParsed(v) return _c } // SetNillableDemoParsed sets the "demo_parsed" field if the given value is not nil. func (_c *MatchCreate) SetNillableDemoParsed(v *bool) *MatchCreate { if v != nil { _c.SetDemoParsed(*v) } return _c } // SetVacPresent sets the "vac_present" field. func (_c *MatchCreate) SetVacPresent(v bool) *MatchCreate { _c.mutation.SetVacPresent(v) return _c } // SetNillableVacPresent sets the "vac_present" field if the given value is not nil. func (_c *MatchCreate) SetNillableVacPresent(v *bool) *MatchCreate { if v != nil { _c.SetVacPresent(*v) } return _c } // SetGamebanPresent sets the "gameban_present" field. func (_c *MatchCreate) SetGamebanPresent(v bool) *MatchCreate { _c.mutation.SetGamebanPresent(v) return _c } // SetNillableGamebanPresent sets the "gameban_present" field if the given value is not nil. func (_c *MatchCreate) SetNillableGamebanPresent(v *bool) *MatchCreate { if v != nil { _c.SetGamebanPresent(*v) } return _c } // SetDecryptionKey sets the "decryption_key" field. func (_c *MatchCreate) SetDecryptionKey(v []byte) *MatchCreate { _c.mutation.SetDecryptionKey(v) return _c } // SetTickRate sets the "tick_rate" field. func (_c *MatchCreate) SetTickRate(v float64) *MatchCreate { _c.mutation.SetTickRate(v) return _c } // SetNillableTickRate sets the "tick_rate" field if the given value is not nil. func (_c *MatchCreate) SetNillableTickRate(v *float64) *MatchCreate { if v != nil { _c.SetTickRate(*v) } return _c } // SetID sets the "id" field. func (_c *MatchCreate) SetID(v uint64) *MatchCreate { _c.mutation.SetID(v) return _c } // AddStatIDs adds the "stats" edge to the MatchPlayer entity by IDs. func (_c *MatchCreate) AddStatIDs(ids ...int) *MatchCreate { _c.mutation.AddStatIDs(ids...) return _c } // AddStats adds the "stats" edges to the MatchPlayer entity. func (_c *MatchCreate) AddStats(v ...*MatchPlayer) *MatchCreate { ids := make([]int, len(v)) for i := range v { ids[i] = v[i].ID } return _c.AddStatIDs(ids...) } // AddPlayerIDs adds the "players" edge to the Player entity by IDs. func (_c *MatchCreate) AddPlayerIDs(ids ...uint64) *MatchCreate { _c.mutation.AddPlayerIDs(ids...) return _c } // AddPlayers adds the "players" edges to the Player entity. func (_c *MatchCreate) AddPlayers(v ...*Player) *MatchCreate { ids := make([]uint64, len(v)) for i := range v { ids[i] = v[i].ID } return _c.AddPlayerIDs(ids...) } // Mutation returns the MatchMutation object of the builder. func (_c *MatchCreate) Mutation() *MatchMutation { return _c.mutation } // Save creates the Match in the database. func (_c *MatchCreate) Save(ctx context.Context) (*Match, error) { _c.defaults() return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) } // SaveX calls Save and panics if Save returns an error. func (_c *MatchCreate) SaveX(ctx context.Context) *Match { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *MatchCreate) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *MatchCreate) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (_c *MatchCreate) defaults() { if _, ok := _c.mutation.DemoParsed(); !ok { v := match.DefaultDemoParsed _c.mutation.SetDemoParsed(v) } if _, ok := _c.mutation.VacPresent(); !ok { v := match.DefaultVacPresent _c.mutation.SetVacPresent(v) } if _, ok := _c.mutation.GamebanPresent(); !ok { v := match.DefaultGamebanPresent _c.mutation.SetGamebanPresent(v) } } // check runs all checks and user-defined validators on the builder. func (_c *MatchCreate) check() error { if _, ok := _c.mutation.ShareCode(); !ok { return &ValidationError{Name: "share_code", err: errors.New(`ent: missing required field "Match.share_code"`)} } if _, ok := _c.mutation.Date(); !ok { return &ValidationError{Name: "date", err: errors.New(`ent: missing required field "Match.date"`)} } if _, ok := _c.mutation.ScoreTeamA(); !ok { return &ValidationError{Name: "score_team_a", err: errors.New(`ent: missing required field "Match.score_team_a"`)} } if _, ok := _c.mutation.ScoreTeamB(); !ok { return &ValidationError{Name: "score_team_b", err: errors.New(`ent: missing required field "Match.score_team_b"`)} } if _, ok := _c.mutation.Duration(); !ok { return &ValidationError{Name: "duration", err: errors.New(`ent: missing required field "Match.duration"`)} } if _, ok := _c.mutation.MatchResult(); !ok { return &ValidationError{Name: "match_result", err: errors.New(`ent: missing required field "Match.match_result"`)} } if _, ok := _c.mutation.MaxRounds(); !ok { return &ValidationError{Name: "max_rounds", err: errors.New(`ent: missing required field "Match.max_rounds"`)} } if _, ok := _c.mutation.DemoParsed(); !ok { return &ValidationError{Name: "demo_parsed", err: errors.New(`ent: missing required field "Match.demo_parsed"`)} } if _, ok := _c.mutation.VacPresent(); !ok { return &ValidationError{Name: "vac_present", err: errors.New(`ent: missing required field "Match.vac_present"`)} } if _, ok := _c.mutation.GamebanPresent(); !ok { return &ValidationError{Name: "gameban_present", err: errors.New(`ent: missing required field "Match.gameban_present"`)} } return nil } func (_c *MatchCreate) sqlSave(ctx context.Context) (*Match, 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 } if _spec.ID.Value != _node.ID { id := _spec.ID.Value.(int64) _node.ID = uint64(id) } _c.mutation.id = &_node.ID _c.mutation.done = true return _node, nil } func (_c *MatchCreate) createSpec() (*Match, *sqlgraph.CreateSpec) { var ( _node = &Match{config: _c.config} _spec = sqlgraph.NewCreateSpec(match.Table, sqlgraph.NewFieldSpec(match.FieldID, field.TypeUint64)) ) if id, ok := _c.mutation.ID(); ok { _node.ID = id _spec.ID.Value = id } if value, ok := _c.mutation.ShareCode(); ok { _spec.SetField(match.FieldShareCode, field.TypeString, value) _node.ShareCode = value } if value, ok := _c.mutation.Map(); ok { _spec.SetField(match.FieldMap, field.TypeString, value) _node.Map = value } if value, ok := _c.mutation.Date(); ok { _spec.SetField(match.FieldDate, field.TypeTime, value) _node.Date = value } if value, ok := _c.mutation.ScoreTeamA(); ok { _spec.SetField(match.FieldScoreTeamA, field.TypeInt, value) _node.ScoreTeamA = value } if value, ok := _c.mutation.ScoreTeamB(); ok { _spec.SetField(match.FieldScoreTeamB, field.TypeInt, value) _node.ScoreTeamB = value } if value, ok := _c.mutation.ReplayURL(); ok { _spec.SetField(match.FieldReplayURL, field.TypeString, value) _node.ReplayURL = value } if value, ok := _c.mutation.Duration(); ok { _spec.SetField(match.FieldDuration, field.TypeInt, value) _node.Duration = value } if value, ok := _c.mutation.MatchResult(); ok { _spec.SetField(match.FieldMatchResult, field.TypeInt, value) _node.MatchResult = value } if value, ok := _c.mutation.MaxRounds(); ok { _spec.SetField(match.FieldMaxRounds, field.TypeInt, value) _node.MaxRounds = value } if value, ok := _c.mutation.DemoParsed(); ok { _spec.SetField(match.FieldDemoParsed, field.TypeBool, value) _node.DemoParsed = value } if value, ok := _c.mutation.VacPresent(); ok { _spec.SetField(match.FieldVacPresent, field.TypeBool, value) _node.VacPresent = value } if value, ok := _c.mutation.GamebanPresent(); ok { _spec.SetField(match.FieldGamebanPresent, field.TypeBool, value) _node.GamebanPresent = value } if value, ok := _c.mutation.DecryptionKey(); ok { _spec.SetField(match.FieldDecryptionKey, field.TypeBytes, value) _node.DecryptionKey = value } if value, ok := _c.mutation.TickRate(); ok { _spec.SetField(match.FieldTickRate, field.TypeFloat64, value) _node.TickRate = value } if nodes := _c.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.NewFieldSpec(matchplayer.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } if nodes := _c.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.NewFieldSpec(player.FieldID, field.TypeUint64), }, } 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 err error builders []*MatchCreate } // Save creates the Match entities in the database. func (_c *MatchCreateBulk) Save(ctx context.Context) ([]*Match, error) { if _c.err != nil { return nil, _c.err } specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) nodes := make([]*Match, len(_c.builders)) mutators := make([]Mutator, len(_c.builders)) for i := range _c.builders { func(i int, root context.Context) { builder := _c.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 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 && 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, _c.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (_c *MatchCreateBulk) SaveX(ctx context.Context) []*Match { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *MatchCreateBulk) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *MatchCreateBulk) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } }