// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "time" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" "github.com/Wei-Shaw/sub2api/ent/promocode" "github.com/Wei-Shaw/sub2api/ent/promocodeusage" "github.com/Wei-Shaw/sub2api/ent/user" ) // PromoCodeUsageCreate is the builder for creating a PromoCodeUsage entity. type PromoCodeUsageCreate struct { config mutation *PromoCodeUsageMutation hooks []Hook conflict []sql.ConflictOption } // SetPromoCodeID sets the "promo_code_id" field. func (_c *PromoCodeUsageCreate) SetPromoCodeID(v int64) *PromoCodeUsageCreate { _c.mutation.SetPromoCodeID(v) return _c } // SetUserID sets the "user_id" field. func (_c *PromoCodeUsageCreate) SetUserID(v int64) *PromoCodeUsageCreate { _c.mutation.SetUserID(v) return _c } // SetBonusAmount sets the "bonus_amount" field. func (_c *PromoCodeUsageCreate) SetBonusAmount(v float64) *PromoCodeUsageCreate { _c.mutation.SetBonusAmount(v) return _c } // SetUsedAt sets the "used_at" field. func (_c *PromoCodeUsageCreate) SetUsedAt(v time.Time) *PromoCodeUsageCreate { _c.mutation.SetUsedAt(v) return _c } // SetNillableUsedAt sets the "used_at" field if the given value is not nil. func (_c *PromoCodeUsageCreate) SetNillableUsedAt(v *time.Time) *PromoCodeUsageCreate { if v != nil { _c.SetUsedAt(*v) } return _c } // SetPromoCode sets the "promo_code" edge to the PromoCode entity. func (_c *PromoCodeUsageCreate) SetPromoCode(v *PromoCode) *PromoCodeUsageCreate { return _c.SetPromoCodeID(v.ID) } // SetUser sets the "user" edge to the User entity. func (_c *PromoCodeUsageCreate) SetUser(v *User) *PromoCodeUsageCreate { return _c.SetUserID(v.ID) } // Mutation returns the PromoCodeUsageMutation object of the builder. func (_c *PromoCodeUsageCreate) Mutation() *PromoCodeUsageMutation { return _c.mutation } // Save creates the PromoCodeUsage in the database. func (_c *PromoCodeUsageCreate) Save(ctx context.Context) (*PromoCodeUsage, error) { _c.defaults() return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) } // SaveX calls Save and panics if Save returns an error. func (_c *PromoCodeUsageCreate) SaveX(ctx context.Context) *PromoCodeUsage { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *PromoCodeUsageCreate) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *PromoCodeUsageCreate) 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 *PromoCodeUsageCreate) defaults() { if _, ok := _c.mutation.UsedAt(); !ok { v := promocodeusage.DefaultUsedAt() _c.mutation.SetUsedAt(v) } } // check runs all checks and user-defined validators on the builder. func (_c *PromoCodeUsageCreate) check() error { if _, ok := _c.mutation.PromoCodeID(); !ok { return &ValidationError{Name: "promo_code_id", err: errors.New(`ent: missing required field "PromoCodeUsage.promo_code_id"`)} } if _, ok := _c.mutation.UserID(); !ok { return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "PromoCodeUsage.user_id"`)} } if _, ok := _c.mutation.BonusAmount(); !ok { return &ValidationError{Name: "bonus_amount", err: errors.New(`ent: missing required field "PromoCodeUsage.bonus_amount"`)} } if _, ok := _c.mutation.UsedAt(); !ok { return &ValidationError{Name: "used_at", err: errors.New(`ent: missing required field "PromoCodeUsage.used_at"`)} } if len(_c.mutation.PromoCodeIDs()) == 0 { return &ValidationError{Name: "promo_code", err: errors.New(`ent: missing required edge "PromoCodeUsage.promo_code"`)} } if len(_c.mutation.UserIDs()) == 0 { return &ValidationError{Name: "user", err: errors.New(`ent: missing required edge "PromoCodeUsage.user"`)} } return nil } func (_c *PromoCodeUsageCreate) sqlSave(ctx context.Context) (*PromoCodeUsage, 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 = int64(id) _c.mutation.id = &_node.ID _c.mutation.done = true return _node, nil } func (_c *PromoCodeUsageCreate) createSpec() (*PromoCodeUsage, *sqlgraph.CreateSpec) { var ( _node = &PromoCodeUsage{config: _c.config} _spec = sqlgraph.NewCreateSpec(promocodeusage.Table, sqlgraph.NewFieldSpec(promocodeusage.FieldID, field.TypeInt64)) ) _spec.OnConflict = _c.conflict if value, ok := _c.mutation.BonusAmount(); ok { _spec.SetField(promocodeusage.FieldBonusAmount, field.TypeFloat64, value) _node.BonusAmount = value } if value, ok := _c.mutation.UsedAt(); ok { _spec.SetField(promocodeusage.FieldUsedAt, field.TypeTime, value) _node.UsedAt = value } if nodes := _c.mutation.PromoCodeIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: promocodeusage.PromoCodeTable, Columns: []string{promocodeusage.PromoCodeColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(promocode.FieldID, field.TypeInt64), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.PromoCodeID = nodes[0] _spec.Edges = append(_spec.Edges, edge) } if nodes := _c.mutation.UserIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: promocodeusage.UserTable, Columns: []string{promocodeusage.UserColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.UserID = nodes[0] _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause // of the `INSERT` statement. For example: // // client.PromoCodeUsage.Create(). // SetPromoCodeID(v). // OnConflict( // // Update the row with the new values // // the was proposed for insertion. // sql.ResolveWithNewValues(), // ). // // Override some of the fields with custom // // update values. // Update(func(u *ent.PromoCodeUsageUpsert) { // SetPromoCodeID(v+v). // }). // Exec(ctx) func (_c *PromoCodeUsageCreate) OnConflict(opts ...sql.ConflictOption) *PromoCodeUsageUpsertOne { _c.conflict = opts return &PromoCodeUsageUpsertOne{ create: _c, } } // OnConflictColumns calls `OnConflict` and configures the columns // as conflict target. Using this option is equivalent to using: // // client.PromoCodeUsage.Create(). // OnConflict(sql.ConflictColumns(columns...)). // Exec(ctx) func (_c *PromoCodeUsageCreate) OnConflictColumns(columns ...string) *PromoCodeUsageUpsertOne { _c.conflict = append(_c.conflict, sql.ConflictColumns(columns...)) return &PromoCodeUsageUpsertOne{ create: _c, } } type ( // PromoCodeUsageUpsertOne is the builder for "upsert"-ing // one PromoCodeUsage node. PromoCodeUsageUpsertOne struct { create *PromoCodeUsageCreate } // PromoCodeUsageUpsert is the "OnConflict" setter. PromoCodeUsageUpsert struct { *sql.UpdateSet } ) // SetPromoCodeID sets the "promo_code_id" field. func (u *PromoCodeUsageUpsert) SetPromoCodeID(v int64) *PromoCodeUsageUpsert { u.Set(promocodeusage.FieldPromoCodeID, v) return u } // UpdatePromoCodeID sets the "promo_code_id" field to the value that was provided on create. func (u *PromoCodeUsageUpsert) UpdatePromoCodeID() *PromoCodeUsageUpsert { u.SetExcluded(promocodeusage.FieldPromoCodeID) return u } // SetUserID sets the "user_id" field. func (u *PromoCodeUsageUpsert) SetUserID(v int64) *PromoCodeUsageUpsert { u.Set(promocodeusage.FieldUserID, v) return u } // UpdateUserID sets the "user_id" field to the value that was provided on create. func (u *PromoCodeUsageUpsert) UpdateUserID() *PromoCodeUsageUpsert { u.SetExcluded(promocodeusage.FieldUserID) return u } // SetBonusAmount sets the "bonus_amount" field. func (u *PromoCodeUsageUpsert) SetBonusAmount(v float64) *PromoCodeUsageUpsert { u.Set(promocodeusage.FieldBonusAmount, v) return u } // UpdateBonusAmount sets the "bonus_amount" field to the value that was provided on create. func (u *PromoCodeUsageUpsert) UpdateBonusAmount() *PromoCodeUsageUpsert { u.SetExcluded(promocodeusage.FieldBonusAmount) return u } // AddBonusAmount adds v to the "bonus_amount" field. func (u *PromoCodeUsageUpsert) AddBonusAmount(v float64) *PromoCodeUsageUpsert { u.Add(promocodeusage.FieldBonusAmount, v) return u } // SetUsedAt sets the "used_at" field. func (u *PromoCodeUsageUpsert) SetUsedAt(v time.Time) *PromoCodeUsageUpsert { u.Set(promocodeusage.FieldUsedAt, v) return u } // UpdateUsedAt sets the "used_at" field to the value that was provided on create. func (u *PromoCodeUsageUpsert) UpdateUsedAt() *PromoCodeUsageUpsert { u.SetExcluded(promocodeusage.FieldUsedAt) return u } // UpdateNewValues updates the mutable fields using the new values that were set on create. // Using this option is equivalent to using: // // client.PromoCodeUsage.Create(). // OnConflict( // sql.ResolveWithNewValues(), // ). // Exec(ctx) func (u *PromoCodeUsageUpsertOne) UpdateNewValues() *PromoCodeUsageUpsertOne { u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues()) return u } // Ignore sets each column to itself in case of conflict. // Using this option is equivalent to using: // // client.PromoCodeUsage.Create(). // OnConflict(sql.ResolveWithIgnore()). // Exec(ctx) func (u *PromoCodeUsageUpsertOne) Ignore() *PromoCodeUsageUpsertOne { u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore()) return u } // DoNothing configures the conflict_action to `DO NOTHING`. // Supported only by SQLite and PostgreSQL. func (u *PromoCodeUsageUpsertOne) DoNothing() *PromoCodeUsageUpsertOne { u.create.conflict = append(u.create.conflict, sql.DoNothing()) return u } // Update allows overriding fields `UPDATE` values. See the PromoCodeUsageCreate.OnConflict // documentation for more info. func (u *PromoCodeUsageUpsertOne) Update(set func(*PromoCodeUsageUpsert)) *PromoCodeUsageUpsertOne { u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) { set(&PromoCodeUsageUpsert{UpdateSet: update}) })) return u } // SetPromoCodeID sets the "promo_code_id" field. func (u *PromoCodeUsageUpsertOne) SetPromoCodeID(v int64) *PromoCodeUsageUpsertOne { return u.Update(func(s *PromoCodeUsageUpsert) { s.SetPromoCodeID(v) }) } // UpdatePromoCodeID sets the "promo_code_id" field to the value that was provided on create. func (u *PromoCodeUsageUpsertOne) UpdatePromoCodeID() *PromoCodeUsageUpsertOne { return u.Update(func(s *PromoCodeUsageUpsert) { s.UpdatePromoCodeID() }) } // SetUserID sets the "user_id" field. func (u *PromoCodeUsageUpsertOne) SetUserID(v int64) *PromoCodeUsageUpsertOne { return u.Update(func(s *PromoCodeUsageUpsert) { s.SetUserID(v) }) } // UpdateUserID sets the "user_id" field to the value that was provided on create. func (u *PromoCodeUsageUpsertOne) UpdateUserID() *PromoCodeUsageUpsertOne { return u.Update(func(s *PromoCodeUsageUpsert) { s.UpdateUserID() }) } // SetBonusAmount sets the "bonus_amount" field. func (u *PromoCodeUsageUpsertOne) SetBonusAmount(v float64) *PromoCodeUsageUpsertOne { return u.Update(func(s *PromoCodeUsageUpsert) { s.SetBonusAmount(v) }) } // AddBonusAmount adds v to the "bonus_amount" field. func (u *PromoCodeUsageUpsertOne) AddBonusAmount(v float64) *PromoCodeUsageUpsertOne { return u.Update(func(s *PromoCodeUsageUpsert) { s.AddBonusAmount(v) }) } // UpdateBonusAmount sets the "bonus_amount" field to the value that was provided on create. func (u *PromoCodeUsageUpsertOne) UpdateBonusAmount() *PromoCodeUsageUpsertOne { return u.Update(func(s *PromoCodeUsageUpsert) { s.UpdateBonusAmount() }) } // SetUsedAt sets the "used_at" field. func (u *PromoCodeUsageUpsertOne) SetUsedAt(v time.Time) *PromoCodeUsageUpsertOne { return u.Update(func(s *PromoCodeUsageUpsert) { s.SetUsedAt(v) }) } // UpdateUsedAt sets the "used_at" field to the value that was provided on create. func (u *PromoCodeUsageUpsertOne) UpdateUsedAt() *PromoCodeUsageUpsertOne { return u.Update(func(s *PromoCodeUsageUpsert) { s.UpdateUsedAt() }) } // Exec executes the query. func (u *PromoCodeUsageUpsertOne) Exec(ctx context.Context) error { if len(u.create.conflict) == 0 { return errors.New("ent: missing options for PromoCodeUsageCreate.OnConflict") } return u.create.Exec(ctx) } // ExecX is like Exec, but panics if an error occurs. func (u *PromoCodeUsageUpsertOne) ExecX(ctx context.Context) { if err := u.create.Exec(ctx); err != nil { panic(err) } } // Exec executes the UPSERT query and returns the inserted/updated ID. func (u *PromoCodeUsageUpsertOne) ID(ctx context.Context) (id int64, err error) { node, err := u.create.Save(ctx) if err != nil { return id, err } return node.ID, nil } // IDX is like ID, but panics if an error occurs. func (u *PromoCodeUsageUpsertOne) IDX(ctx context.Context) int64 { id, err := u.ID(ctx) if err != nil { panic(err) } return id } // PromoCodeUsageCreateBulk is the builder for creating many PromoCodeUsage entities in bulk. type PromoCodeUsageCreateBulk struct { config err error builders []*PromoCodeUsageCreate conflict []sql.ConflictOption } // Save creates the PromoCodeUsage entities in the database. func (_c *PromoCodeUsageCreateBulk) Save(ctx context.Context) ([]*PromoCodeUsage, error) { if _c.err != nil { return nil, _c.err } specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) nodes := make([]*PromoCodeUsage, 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.(*PromoCodeUsageMutation) 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} spec.OnConflict = _c.conflict // 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 = int64(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 *PromoCodeUsageCreateBulk) SaveX(ctx context.Context) []*PromoCodeUsage { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *PromoCodeUsageCreateBulk) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *PromoCodeUsageCreateBulk) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } } // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause // of the `INSERT` statement. For example: // // client.PromoCodeUsage.CreateBulk(builders...). // OnConflict( // // Update the row with the new values // // the was proposed for insertion. // sql.ResolveWithNewValues(), // ). // // Override some of the fields with custom // // update values. // Update(func(u *ent.PromoCodeUsageUpsert) { // SetPromoCodeID(v+v). // }). // Exec(ctx) func (_c *PromoCodeUsageCreateBulk) OnConflict(opts ...sql.ConflictOption) *PromoCodeUsageUpsertBulk { _c.conflict = opts return &PromoCodeUsageUpsertBulk{ create: _c, } } // OnConflictColumns calls `OnConflict` and configures the columns // as conflict target. Using this option is equivalent to using: // // client.PromoCodeUsage.Create(). // OnConflict(sql.ConflictColumns(columns...)). // Exec(ctx) func (_c *PromoCodeUsageCreateBulk) OnConflictColumns(columns ...string) *PromoCodeUsageUpsertBulk { _c.conflict = append(_c.conflict, sql.ConflictColumns(columns...)) return &PromoCodeUsageUpsertBulk{ create: _c, } } // PromoCodeUsageUpsertBulk is the builder for "upsert"-ing // a bulk of PromoCodeUsage nodes. type PromoCodeUsageUpsertBulk struct { create *PromoCodeUsageCreateBulk } // UpdateNewValues updates the mutable fields using the new values that // were set on create. Using this option is equivalent to using: // // client.PromoCodeUsage.Create(). // OnConflict( // sql.ResolveWithNewValues(), // ). // Exec(ctx) func (u *PromoCodeUsageUpsertBulk) UpdateNewValues() *PromoCodeUsageUpsertBulk { u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues()) return u } // Ignore sets each column to itself in case of conflict. // Using this option is equivalent to using: // // client.PromoCodeUsage.Create(). // OnConflict(sql.ResolveWithIgnore()). // Exec(ctx) func (u *PromoCodeUsageUpsertBulk) Ignore() *PromoCodeUsageUpsertBulk { u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore()) return u } // DoNothing configures the conflict_action to `DO NOTHING`. // Supported only by SQLite and PostgreSQL. func (u *PromoCodeUsageUpsertBulk) DoNothing() *PromoCodeUsageUpsertBulk { u.create.conflict = append(u.create.conflict, sql.DoNothing()) return u } // Update allows overriding fields `UPDATE` values. See the PromoCodeUsageCreateBulk.OnConflict // documentation for more info. func (u *PromoCodeUsageUpsertBulk) Update(set func(*PromoCodeUsageUpsert)) *PromoCodeUsageUpsertBulk { u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) { set(&PromoCodeUsageUpsert{UpdateSet: update}) })) return u } // SetPromoCodeID sets the "promo_code_id" field. func (u *PromoCodeUsageUpsertBulk) SetPromoCodeID(v int64) *PromoCodeUsageUpsertBulk { return u.Update(func(s *PromoCodeUsageUpsert) { s.SetPromoCodeID(v) }) } // UpdatePromoCodeID sets the "promo_code_id" field to the value that was provided on create. func (u *PromoCodeUsageUpsertBulk) UpdatePromoCodeID() *PromoCodeUsageUpsertBulk { return u.Update(func(s *PromoCodeUsageUpsert) { s.UpdatePromoCodeID() }) } // SetUserID sets the "user_id" field. func (u *PromoCodeUsageUpsertBulk) SetUserID(v int64) *PromoCodeUsageUpsertBulk { return u.Update(func(s *PromoCodeUsageUpsert) { s.SetUserID(v) }) } // UpdateUserID sets the "user_id" field to the value that was provided on create. func (u *PromoCodeUsageUpsertBulk) UpdateUserID() *PromoCodeUsageUpsertBulk { return u.Update(func(s *PromoCodeUsageUpsert) { s.UpdateUserID() }) } // SetBonusAmount sets the "bonus_amount" field. func (u *PromoCodeUsageUpsertBulk) SetBonusAmount(v float64) *PromoCodeUsageUpsertBulk { return u.Update(func(s *PromoCodeUsageUpsert) { s.SetBonusAmount(v) }) } // AddBonusAmount adds v to the "bonus_amount" field. func (u *PromoCodeUsageUpsertBulk) AddBonusAmount(v float64) *PromoCodeUsageUpsertBulk { return u.Update(func(s *PromoCodeUsageUpsert) { s.AddBonusAmount(v) }) } // UpdateBonusAmount sets the "bonus_amount" field to the value that was provided on create. func (u *PromoCodeUsageUpsertBulk) UpdateBonusAmount() *PromoCodeUsageUpsertBulk { return u.Update(func(s *PromoCodeUsageUpsert) { s.UpdateBonusAmount() }) } // SetUsedAt sets the "used_at" field. func (u *PromoCodeUsageUpsertBulk) SetUsedAt(v time.Time) *PromoCodeUsageUpsertBulk { return u.Update(func(s *PromoCodeUsageUpsert) { s.SetUsedAt(v) }) } // UpdateUsedAt sets the "used_at" field to the value that was provided on create. func (u *PromoCodeUsageUpsertBulk) UpdateUsedAt() *PromoCodeUsageUpsertBulk { return u.Update(func(s *PromoCodeUsageUpsert) { s.UpdateUsedAt() }) } // Exec executes the query. func (u *PromoCodeUsageUpsertBulk) Exec(ctx context.Context) error { if u.create.err != nil { return u.create.err } for i, b := range u.create.builders { if len(b.conflict) != 0 { return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the PromoCodeUsageCreateBulk instead", i) } } if len(u.create.conflict) == 0 { return errors.New("ent: missing options for PromoCodeUsageCreateBulk.OnConflict") } return u.create.Exec(ctx) } // ExecX is like Exec, but panics if an error occurs. func (u *PromoCodeUsageUpsertBulk) ExecX(ctx context.Context) { if err := u.create.Exec(ctx); err != nil { panic(err) } }