| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081 |
- // 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"
- )
- // PromoCodeCreate is the builder for creating a PromoCode entity.
- type PromoCodeCreate struct {
- config
- mutation *PromoCodeMutation
- hooks []Hook
- conflict []sql.ConflictOption
- }
- // SetCode sets the "code" field.
- func (_c *PromoCodeCreate) SetCode(v string) *PromoCodeCreate {
- _c.mutation.SetCode(v)
- return _c
- }
- // SetBonusAmount sets the "bonus_amount" field.
- func (_c *PromoCodeCreate) SetBonusAmount(v float64) *PromoCodeCreate {
- _c.mutation.SetBonusAmount(v)
- return _c
- }
- // SetNillableBonusAmount sets the "bonus_amount" field if the given value is not nil.
- func (_c *PromoCodeCreate) SetNillableBonusAmount(v *float64) *PromoCodeCreate {
- if v != nil {
- _c.SetBonusAmount(*v)
- }
- return _c
- }
- // SetMaxUses sets the "max_uses" field.
- func (_c *PromoCodeCreate) SetMaxUses(v int) *PromoCodeCreate {
- _c.mutation.SetMaxUses(v)
- return _c
- }
- // SetNillableMaxUses sets the "max_uses" field if the given value is not nil.
- func (_c *PromoCodeCreate) SetNillableMaxUses(v *int) *PromoCodeCreate {
- if v != nil {
- _c.SetMaxUses(*v)
- }
- return _c
- }
- // SetUsedCount sets the "used_count" field.
- func (_c *PromoCodeCreate) SetUsedCount(v int) *PromoCodeCreate {
- _c.mutation.SetUsedCount(v)
- return _c
- }
- // SetNillableUsedCount sets the "used_count" field if the given value is not nil.
- func (_c *PromoCodeCreate) SetNillableUsedCount(v *int) *PromoCodeCreate {
- if v != nil {
- _c.SetUsedCount(*v)
- }
- return _c
- }
- // SetStatus sets the "status" field.
- func (_c *PromoCodeCreate) SetStatus(v string) *PromoCodeCreate {
- _c.mutation.SetStatus(v)
- return _c
- }
- // SetNillableStatus sets the "status" field if the given value is not nil.
- func (_c *PromoCodeCreate) SetNillableStatus(v *string) *PromoCodeCreate {
- if v != nil {
- _c.SetStatus(*v)
- }
- return _c
- }
- // SetExpiresAt sets the "expires_at" field.
- func (_c *PromoCodeCreate) SetExpiresAt(v time.Time) *PromoCodeCreate {
- _c.mutation.SetExpiresAt(v)
- return _c
- }
- // SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
- func (_c *PromoCodeCreate) SetNillableExpiresAt(v *time.Time) *PromoCodeCreate {
- if v != nil {
- _c.SetExpiresAt(*v)
- }
- return _c
- }
- // SetNotes sets the "notes" field.
- func (_c *PromoCodeCreate) SetNotes(v string) *PromoCodeCreate {
- _c.mutation.SetNotes(v)
- return _c
- }
- // SetNillableNotes sets the "notes" field if the given value is not nil.
- func (_c *PromoCodeCreate) SetNillableNotes(v *string) *PromoCodeCreate {
- if v != nil {
- _c.SetNotes(*v)
- }
- return _c
- }
- // SetCreatedAt sets the "created_at" field.
- func (_c *PromoCodeCreate) SetCreatedAt(v time.Time) *PromoCodeCreate {
- _c.mutation.SetCreatedAt(v)
- return _c
- }
- // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
- func (_c *PromoCodeCreate) SetNillableCreatedAt(v *time.Time) *PromoCodeCreate {
- if v != nil {
- _c.SetCreatedAt(*v)
- }
- return _c
- }
- // SetUpdatedAt sets the "updated_at" field.
- func (_c *PromoCodeCreate) SetUpdatedAt(v time.Time) *PromoCodeCreate {
- _c.mutation.SetUpdatedAt(v)
- return _c
- }
- // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
- func (_c *PromoCodeCreate) SetNillableUpdatedAt(v *time.Time) *PromoCodeCreate {
- if v != nil {
- _c.SetUpdatedAt(*v)
- }
- return _c
- }
- // AddUsageRecordIDs adds the "usage_records" edge to the PromoCodeUsage entity by IDs.
- func (_c *PromoCodeCreate) AddUsageRecordIDs(ids ...int64) *PromoCodeCreate {
- _c.mutation.AddUsageRecordIDs(ids...)
- return _c
- }
- // AddUsageRecords adds the "usage_records" edges to the PromoCodeUsage entity.
- func (_c *PromoCodeCreate) AddUsageRecords(v ...*PromoCodeUsage) *PromoCodeCreate {
- ids := make([]int64, len(v))
- for i := range v {
- ids[i] = v[i].ID
- }
- return _c.AddUsageRecordIDs(ids...)
- }
- // Mutation returns the PromoCodeMutation object of the builder.
- func (_c *PromoCodeCreate) Mutation() *PromoCodeMutation {
- return _c.mutation
- }
- // Save creates the PromoCode in the database.
- func (_c *PromoCodeCreate) Save(ctx context.Context) (*PromoCode, error) {
- _c.defaults()
- return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
- }
- // SaveX calls Save and panics if Save returns an error.
- func (_c *PromoCodeCreate) SaveX(ctx context.Context) *PromoCode {
- v, err := _c.Save(ctx)
- if err != nil {
- panic(err)
- }
- return v
- }
- // Exec executes the query.
- func (_c *PromoCodeCreate) Exec(ctx context.Context) error {
- _, err := _c.Save(ctx)
- return err
- }
- // ExecX is like Exec, but panics if an error occurs.
- func (_c *PromoCodeCreate) 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 *PromoCodeCreate) defaults() {
- if _, ok := _c.mutation.BonusAmount(); !ok {
- v := promocode.DefaultBonusAmount
- _c.mutation.SetBonusAmount(v)
- }
- if _, ok := _c.mutation.MaxUses(); !ok {
- v := promocode.DefaultMaxUses
- _c.mutation.SetMaxUses(v)
- }
- if _, ok := _c.mutation.UsedCount(); !ok {
- v := promocode.DefaultUsedCount
- _c.mutation.SetUsedCount(v)
- }
- if _, ok := _c.mutation.Status(); !ok {
- v := promocode.DefaultStatus
- _c.mutation.SetStatus(v)
- }
- if _, ok := _c.mutation.CreatedAt(); !ok {
- v := promocode.DefaultCreatedAt()
- _c.mutation.SetCreatedAt(v)
- }
- if _, ok := _c.mutation.UpdatedAt(); !ok {
- v := promocode.DefaultUpdatedAt()
- _c.mutation.SetUpdatedAt(v)
- }
- }
- // check runs all checks and user-defined validators on the builder.
- func (_c *PromoCodeCreate) check() error {
- if _, ok := _c.mutation.Code(); !ok {
- return &ValidationError{Name: "code", err: errors.New(`ent: missing required field "PromoCode.code"`)}
- }
- if v, ok := _c.mutation.Code(); ok {
- if err := promocode.CodeValidator(v); err != nil {
- return &ValidationError{Name: "code", err: fmt.Errorf(`ent: validator failed for field "PromoCode.code": %w`, err)}
- }
- }
- if _, ok := _c.mutation.BonusAmount(); !ok {
- return &ValidationError{Name: "bonus_amount", err: errors.New(`ent: missing required field "PromoCode.bonus_amount"`)}
- }
- if _, ok := _c.mutation.MaxUses(); !ok {
- return &ValidationError{Name: "max_uses", err: errors.New(`ent: missing required field "PromoCode.max_uses"`)}
- }
- if _, ok := _c.mutation.UsedCount(); !ok {
- return &ValidationError{Name: "used_count", err: errors.New(`ent: missing required field "PromoCode.used_count"`)}
- }
- if _, ok := _c.mutation.Status(); !ok {
- return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "PromoCode.status"`)}
- }
- if v, ok := _c.mutation.Status(); ok {
- if err := promocode.StatusValidator(v); err != nil {
- return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "PromoCode.status": %w`, err)}
- }
- }
- if _, ok := _c.mutation.CreatedAt(); !ok {
- return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "PromoCode.created_at"`)}
- }
- if _, ok := _c.mutation.UpdatedAt(); !ok {
- return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "PromoCode.updated_at"`)}
- }
- return nil
- }
- func (_c *PromoCodeCreate) sqlSave(ctx context.Context) (*PromoCode, 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 *PromoCodeCreate) createSpec() (*PromoCode, *sqlgraph.CreateSpec) {
- var (
- _node = &PromoCode{config: _c.config}
- _spec = sqlgraph.NewCreateSpec(promocode.Table, sqlgraph.NewFieldSpec(promocode.FieldID, field.TypeInt64))
- )
- _spec.OnConflict = _c.conflict
- if value, ok := _c.mutation.Code(); ok {
- _spec.SetField(promocode.FieldCode, field.TypeString, value)
- _node.Code = value
- }
- if value, ok := _c.mutation.BonusAmount(); ok {
- _spec.SetField(promocode.FieldBonusAmount, field.TypeFloat64, value)
- _node.BonusAmount = value
- }
- if value, ok := _c.mutation.MaxUses(); ok {
- _spec.SetField(promocode.FieldMaxUses, field.TypeInt, value)
- _node.MaxUses = value
- }
- if value, ok := _c.mutation.UsedCount(); ok {
- _spec.SetField(promocode.FieldUsedCount, field.TypeInt, value)
- _node.UsedCount = value
- }
- if value, ok := _c.mutation.Status(); ok {
- _spec.SetField(promocode.FieldStatus, field.TypeString, value)
- _node.Status = value
- }
- if value, ok := _c.mutation.ExpiresAt(); ok {
- _spec.SetField(promocode.FieldExpiresAt, field.TypeTime, value)
- _node.ExpiresAt = &value
- }
- if value, ok := _c.mutation.Notes(); ok {
- _spec.SetField(promocode.FieldNotes, field.TypeString, value)
- _node.Notes = &value
- }
- if value, ok := _c.mutation.CreatedAt(); ok {
- _spec.SetField(promocode.FieldCreatedAt, field.TypeTime, value)
- _node.CreatedAt = value
- }
- if value, ok := _c.mutation.UpdatedAt(); ok {
- _spec.SetField(promocode.FieldUpdatedAt, field.TypeTime, value)
- _node.UpdatedAt = value
- }
- if nodes := _c.mutation.UsageRecordsIDs(); len(nodes) > 0 {
- edge := &sqlgraph.EdgeSpec{
- Rel: sqlgraph.O2M,
- Inverse: false,
- Table: promocode.UsageRecordsTable,
- Columns: []string{promocode.UsageRecordsColumn},
- Bidi: false,
- Target: &sqlgraph.EdgeTarget{
- IDSpec: sqlgraph.NewFieldSpec(promocodeusage.FieldID, field.TypeInt64),
- },
- }
- for _, k := range nodes {
- edge.Target.Nodes = append(edge.Target.Nodes, k)
- }
- _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.PromoCode.Create().
- // SetCode(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.PromoCodeUpsert) {
- // SetCode(v+v).
- // }).
- // Exec(ctx)
- func (_c *PromoCodeCreate) OnConflict(opts ...sql.ConflictOption) *PromoCodeUpsertOne {
- _c.conflict = opts
- return &PromoCodeUpsertOne{
- create: _c,
- }
- }
- // OnConflictColumns calls `OnConflict` and configures the columns
- // as conflict target. Using this option is equivalent to using:
- //
- // client.PromoCode.Create().
- // OnConflict(sql.ConflictColumns(columns...)).
- // Exec(ctx)
- func (_c *PromoCodeCreate) OnConflictColumns(columns ...string) *PromoCodeUpsertOne {
- _c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
- return &PromoCodeUpsertOne{
- create: _c,
- }
- }
- type (
- // PromoCodeUpsertOne is the builder for "upsert"-ing
- // one PromoCode node.
- PromoCodeUpsertOne struct {
- create *PromoCodeCreate
- }
- // PromoCodeUpsert is the "OnConflict" setter.
- PromoCodeUpsert struct {
- *sql.UpdateSet
- }
- )
- // SetCode sets the "code" field.
- func (u *PromoCodeUpsert) SetCode(v string) *PromoCodeUpsert {
- u.Set(promocode.FieldCode, v)
- return u
- }
- // UpdateCode sets the "code" field to the value that was provided on create.
- func (u *PromoCodeUpsert) UpdateCode() *PromoCodeUpsert {
- u.SetExcluded(promocode.FieldCode)
- return u
- }
- // SetBonusAmount sets the "bonus_amount" field.
- func (u *PromoCodeUpsert) SetBonusAmount(v float64) *PromoCodeUpsert {
- u.Set(promocode.FieldBonusAmount, v)
- return u
- }
- // UpdateBonusAmount sets the "bonus_amount" field to the value that was provided on create.
- func (u *PromoCodeUpsert) UpdateBonusAmount() *PromoCodeUpsert {
- u.SetExcluded(promocode.FieldBonusAmount)
- return u
- }
- // AddBonusAmount adds v to the "bonus_amount" field.
- func (u *PromoCodeUpsert) AddBonusAmount(v float64) *PromoCodeUpsert {
- u.Add(promocode.FieldBonusAmount, v)
- return u
- }
- // SetMaxUses sets the "max_uses" field.
- func (u *PromoCodeUpsert) SetMaxUses(v int) *PromoCodeUpsert {
- u.Set(promocode.FieldMaxUses, v)
- return u
- }
- // UpdateMaxUses sets the "max_uses" field to the value that was provided on create.
- func (u *PromoCodeUpsert) UpdateMaxUses() *PromoCodeUpsert {
- u.SetExcluded(promocode.FieldMaxUses)
- return u
- }
- // AddMaxUses adds v to the "max_uses" field.
- func (u *PromoCodeUpsert) AddMaxUses(v int) *PromoCodeUpsert {
- u.Add(promocode.FieldMaxUses, v)
- return u
- }
- // SetUsedCount sets the "used_count" field.
- func (u *PromoCodeUpsert) SetUsedCount(v int) *PromoCodeUpsert {
- u.Set(promocode.FieldUsedCount, v)
- return u
- }
- // UpdateUsedCount sets the "used_count" field to the value that was provided on create.
- func (u *PromoCodeUpsert) UpdateUsedCount() *PromoCodeUpsert {
- u.SetExcluded(promocode.FieldUsedCount)
- return u
- }
- // AddUsedCount adds v to the "used_count" field.
- func (u *PromoCodeUpsert) AddUsedCount(v int) *PromoCodeUpsert {
- u.Add(promocode.FieldUsedCount, v)
- return u
- }
- // SetStatus sets the "status" field.
- func (u *PromoCodeUpsert) SetStatus(v string) *PromoCodeUpsert {
- u.Set(promocode.FieldStatus, v)
- return u
- }
- // UpdateStatus sets the "status" field to the value that was provided on create.
- func (u *PromoCodeUpsert) UpdateStatus() *PromoCodeUpsert {
- u.SetExcluded(promocode.FieldStatus)
- return u
- }
- // SetExpiresAt sets the "expires_at" field.
- func (u *PromoCodeUpsert) SetExpiresAt(v time.Time) *PromoCodeUpsert {
- u.Set(promocode.FieldExpiresAt, v)
- return u
- }
- // UpdateExpiresAt sets the "expires_at" field to the value that was provided on create.
- func (u *PromoCodeUpsert) UpdateExpiresAt() *PromoCodeUpsert {
- u.SetExcluded(promocode.FieldExpiresAt)
- return u
- }
- // ClearExpiresAt clears the value of the "expires_at" field.
- func (u *PromoCodeUpsert) ClearExpiresAt() *PromoCodeUpsert {
- u.SetNull(promocode.FieldExpiresAt)
- return u
- }
- // SetNotes sets the "notes" field.
- func (u *PromoCodeUpsert) SetNotes(v string) *PromoCodeUpsert {
- u.Set(promocode.FieldNotes, v)
- return u
- }
- // UpdateNotes sets the "notes" field to the value that was provided on create.
- func (u *PromoCodeUpsert) UpdateNotes() *PromoCodeUpsert {
- u.SetExcluded(promocode.FieldNotes)
- return u
- }
- // ClearNotes clears the value of the "notes" field.
- func (u *PromoCodeUpsert) ClearNotes() *PromoCodeUpsert {
- u.SetNull(promocode.FieldNotes)
- return u
- }
- // SetUpdatedAt sets the "updated_at" field.
- func (u *PromoCodeUpsert) SetUpdatedAt(v time.Time) *PromoCodeUpsert {
- u.Set(promocode.FieldUpdatedAt, v)
- return u
- }
- // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
- func (u *PromoCodeUpsert) UpdateUpdatedAt() *PromoCodeUpsert {
- u.SetExcluded(promocode.FieldUpdatedAt)
- return u
- }
- // UpdateNewValues updates the mutable fields using the new values that were set on create.
- // Using this option is equivalent to using:
- //
- // client.PromoCode.Create().
- // OnConflict(
- // sql.ResolveWithNewValues(),
- // ).
- // Exec(ctx)
- func (u *PromoCodeUpsertOne) UpdateNewValues() *PromoCodeUpsertOne {
- u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
- u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
- if _, exists := u.create.mutation.CreatedAt(); exists {
- s.SetIgnore(promocode.FieldCreatedAt)
- }
- }))
- return u
- }
- // Ignore sets each column to itself in case of conflict.
- // Using this option is equivalent to using:
- //
- // client.PromoCode.Create().
- // OnConflict(sql.ResolveWithIgnore()).
- // Exec(ctx)
- func (u *PromoCodeUpsertOne) Ignore() *PromoCodeUpsertOne {
- 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 *PromoCodeUpsertOne) DoNothing() *PromoCodeUpsertOne {
- u.create.conflict = append(u.create.conflict, sql.DoNothing())
- return u
- }
- // Update allows overriding fields `UPDATE` values. See the PromoCodeCreate.OnConflict
- // documentation for more info.
- func (u *PromoCodeUpsertOne) Update(set func(*PromoCodeUpsert)) *PromoCodeUpsertOne {
- u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
- set(&PromoCodeUpsert{UpdateSet: update})
- }))
- return u
- }
- // SetCode sets the "code" field.
- func (u *PromoCodeUpsertOne) SetCode(v string) *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.SetCode(v)
- })
- }
- // UpdateCode sets the "code" field to the value that was provided on create.
- func (u *PromoCodeUpsertOne) UpdateCode() *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.UpdateCode()
- })
- }
- // SetBonusAmount sets the "bonus_amount" field.
- func (u *PromoCodeUpsertOne) SetBonusAmount(v float64) *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.SetBonusAmount(v)
- })
- }
- // AddBonusAmount adds v to the "bonus_amount" field.
- func (u *PromoCodeUpsertOne) AddBonusAmount(v float64) *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.AddBonusAmount(v)
- })
- }
- // UpdateBonusAmount sets the "bonus_amount" field to the value that was provided on create.
- func (u *PromoCodeUpsertOne) UpdateBonusAmount() *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.UpdateBonusAmount()
- })
- }
- // SetMaxUses sets the "max_uses" field.
- func (u *PromoCodeUpsertOne) SetMaxUses(v int) *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.SetMaxUses(v)
- })
- }
- // AddMaxUses adds v to the "max_uses" field.
- func (u *PromoCodeUpsertOne) AddMaxUses(v int) *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.AddMaxUses(v)
- })
- }
- // UpdateMaxUses sets the "max_uses" field to the value that was provided on create.
- func (u *PromoCodeUpsertOne) UpdateMaxUses() *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.UpdateMaxUses()
- })
- }
- // SetUsedCount sets the "used_count" field.
- func (u *PromoCodeUpsertOne) SetUsedCount(v int) *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.SetUsedCount(v)
- })
- }
- // AddUsedCount adds v to the "used_count" field.
- func (u *PromoCodeUpsertOne) AddUsedCount(v int) *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.AddUsedCount(v)
- })
- }
- // UpdateUsedCount sets the "used_count" field to the value that was provided on create.
- func (u *PromoCodeUpsertOne) UpdateUsedCount() *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.UpdateUsedCount()
- })
- }
- // SetStatus sets the "status" field.
- func (u *PromoCodeUpsertOne) SetStatus(v string) *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.SetStatus(v)
- })
- }
- // UpdateStatus sets the "status" field to the value that was provided on create.
- func (u *PromoCodeUpsertOne) UpdateStatus() *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.UpdateStatus()
- })
- }
- // SetExpiresAt sets the "expires_at" field.
- func (u *PromoCodeUpsertOne) SetExpiresAt(v time.Time) *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.SetExpiresAt(v)
- })
- }
- // UpdateExpiresAt sets the "expires_at" field to the value that was provided on create.
- func (u *PromoCodeUpsertOne) UpdateExpiresAt() *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.UpdateExpiresAt()
- })
- }
- // ClearExpiresAt clears the value of the "expires_at" field.
- func (u *PromoCodeUpsertOne) ClearExpiresAt() *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.ClearExpiresAt()
- })
- }
- // SetNotes sets the "notes" field.
- func (u *PromoCodeUpsertOne) SetNotes(v string) *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.SetNotes(v)
- })
- }
- // UpdateNotes sets the "notes" field to the value that was provided on create.
- func (u *PromoCodeUpsertOne) UpdateNotes() *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.UpdateNotes()
- })
- }
- // ClearNotes clears the value of the "notes" field.
- func (u *PromoCodeUpsertOne) ClearNotes() *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.ClearNotes()
- })
- }
- // SetUpdatedAt sets the "updated_at" field.
- func (u *PromoCodeUpsertOne) SetUpdatedAt(v time.Time) *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.SetUpdatedAt(v)
- })
- }
- // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
- func (u *PromoCodeUpsertOne) UpdateUpdatedAt() *PromoCodeUpsertOne {
- return u.Update(func(s *PromoCodeUpsert) {
- s.UpdateUpdatedAt()
- })
- }
- // Exec executes the query.
- func (u *PromoCodeUpsertOne) Exec(ctx context.Context) error {
- if len(u.create.conflict) == 0 {
- return errors.New("ent: missing options for PromoCodeCreate.OnConflict")
- }
- return u.create.Exec(ctx)
- }
- // ExecX is like Exec, but panics if an error occurs.
- func (u *PromoCodeUpsertOne) 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 *PromoCodeUpsertOne) 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 *PromoCodeUpsertOne) IDX(ctx context.Context) int64 {
- id, err := u.ID(ctx)
- if err != nil {
- panic(err)
- }
- return id
- }
- // PromoCodeCreateBulk is the builder for creating many PromoCode entities in bulk.
- type PromoCodeCreateBulk struct {
- config
- err error
- builders []*PromoCodeCreate
- conflict []sql.ConflictOption
- }
- // Save creates the PromoCode entities in the database.
- func (_c *PromoCodeCreateBulk) Save(ctx context.Context) ([]*PromoCode, error) {
- if _c.err != nil {
- return nil, _c.err
- }
- specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
- nodes := make([]*PromoCode, 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.(*PromoCodeMutation)
- 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 *PromoCodeCreateBulk) SaveX(ctx context.Context) []*PromoCode {
- v, err := _c.Save(ctx)
- if err != nil {
- panic(err)
- }
- return v
- }
- // Exec executes the query.
- func (_c *PromoCodeCreateBulk) Exec(ctx context.Context) error {
- _, err := _c.Save(ctx)
- return err
- }
- // ExecX is like Exec, but panics if an error occurs.
- func (_c *PromoCodeCreateBulk) 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.PromoCode.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.PromoCodeUpsert) {
- // SetCode(v+v).
- // }).
- // Exec(ctx)
- func (_c *PromoCodeCreateBulk) OnConflict(opts ...sql.ConflictOption) *PromoCodeUpsertBulk {
- _c.conflict = opts
- return &PromoCodeUpsertBulk{
- create: _c,
- }
- }
- // OnConflictColumns calls `OnConflict` and configures the columns
- // as conflict target. Using this option is equivalent to using:
- //
- // client.PromoCode.Create().
- // OnConflict(sql.ConflictColumns(columns...)).
- // Exec(ctx)
- func (_c *PromoCodeCreateBulk) OnConflictColumns(columns ...string) *PromoCodeUpsertBulk {
- _c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
- return &PromoCodeUpsertBulk{
- create: _c,
- }
- }
- // PromoCodeUpsertBulk is the builder for "upsert"-ing
- // a bulk of PromoCode nodes.
- type PromoCodeUpsertBulk struct {
- create *PromoCodeCreateBulk
- }
- // UpdateNewValues updates the mutable fields using the new values that
- // were set on create. Using this option is equivalent to using:
- //
- // client.PromoCode.Create().
- // OnConflict(
- // sql.ResolveWithNewValues(),
- // ).
- // Exec(ctx)
- func (u *PromoCodeUpsertBulk) UpdateNewValues() *PromoCodeUpsertBulk {
- u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
- u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
- for _, b := range u.create.builders {
- if _, exists := b.mutation.CreatedAt(); exists {
- s.SetIgnore(promocode.FieldCreatedAt)
- }
- }
- }))
- return u
- }
- // Ignore sets each column to itself in case of conflict.
- // Using this option is equivalent to using:
- //
- // client.PromoCode.Create().
- // OnConflict(sql.ResolveWithIgnore()).
- // Exec(ctx)
- func (u *PromoCodeUpsertBulk) Ignore() *PromoCodeUpsertBulk {
- 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 *PromoCodeUpsertBulk) DoNothing() *PromoCodeUpsertBulk {
- u.create.conflict = append(u.create.conflict, sql.DoNothing())
- return u
- }
- // Update allows overriding fields `UPDATE` values. See the PromoCodeCreateBulk.OnConflict
- // documentation for more info.
- func (u *PromoCodeUpsertBulk) Update(set func(*PromoCodeUpsert)) *PromoCodeUpsertBulk {
- u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
- set(&PromoCodeUpsert{UpdateSet: update})
- }))
- return u
- }
- // SetCode sets the "code" field.
- func (u *PromoCodeUpsertBulk) SetCode(v string) *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.SetCode(v)
- })
- }
- // UpdateCode sets the "code" field to the value that was provided on create.
- func (u *PromoCodeUpsertBulk) UpdateCode() *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.UpdateCode()
- })
- }
- // SetBonusAmount sets the "bonus_amount" field.
- func (u *PromoCodeUpsertBulk) SetBonusAmount(v float64) *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.SetBonusAmount(v)
- })
- }
- // AddBonusAmount adds v to the "bonus_amount" field.
- func (u *PromoCodeUpsertBulk) AddBonusAmount(v float64) *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.AddBonusAmount(v)
- })
- }
- // UpdateBonusAmount sets the "bonus_amount" field to the value that was provided on create.
- func (u *PromoCodeUpsertBulk) UpdateBonusAmount() *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.UpdateBonusAmount()
- })
- }
- // SetMaxUses sets the "max_uses" field.
- func (u *PromoCodeUpsertBulk) SetMaxUses(v int) *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.SetMaxUses(v)
- })
- }
- // AddMaxUses adds v to the "max_uses" field.
- func (u *PromoCodeUpsertBulk) AddMaxUses(v int) *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.AddMaxUses(v)
- })
- }
- // UpdateMaxUses sets the "max_uses" field to the value that was provided on create.
- func (u *PromoCodeUpsertBulk) UpdateMaxUses() *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.UpdateMaxUses()
- })
- }
- // SetUsedCount sets the "used_count" field.
- func (u *PromoCodeUpsertBulk) SetUsedCount(v int) *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.SetUsedCount(v)
- })
- }
- // AddUsedCount adds v to the "used_count" field.
- func (u *PromoCodeUpsertBulk) AddUsedCount(v int) *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.AddUsedCount(v)
- })
- }
- // UpdateUsedCount sets the "used_count" field to the value that was provided on create.
- func (u *PromoCodeUpsertBulk) UpdateUsedCount() *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.UpdateUsedCount()
- })
- }
- // SetStatus sets the "status" field.
- func (u *PromoCodeUpsertBulk) SetStatus(v string) *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.SetStatus(v)
- })
- }
- // UpdateStatus sets the "status" field to the value that was provided on create.
- func (u *PromoCodeUpsertBulk) UpdateStatus() *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.UpdateStatus()
- })
- }
- // SetExpiresAt sets the "expires_at" field.
- func (u *PromoCodeUpsertBulk) SetExpiresAt(v time.Time) *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.SetExpiresAt(v)
- })
- }
- // UpdateExpiresAt sets the "expires_at" field to the value that was provided on create.
- func (u *PromoCodeUpsertBulk) UpdateExpiresAt() *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.UpdateExpiresAt()
- })
- }
- // ClearExpiresAt clears the value of the "expires_at" field.
- func (u *PromoCodeUpsertBulk) ClearExpiresAt() *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.ClearExpiresAt()
- })
- }
- // SetNotes sets the "notes" field.
- func (u *PromoCodeUpsertBulk) SetNotes(v string) *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.SetNotes(v)
- })
- }
- // UpdateNotes sets the "notes" field to the value that was provided on create.
- func (u *PromoCodeUpsertBulk) UpdateNotes() *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.UpdateNotes()
- })
- }
- // ClearNotes clears the value of the "notes" field.
- func (u *PromoCodeUpsertBulk) ClearNotes() *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.ClearNotes()
- })
- }
- // SetUpdatedAt sets the "updated_at" field.
- func (u *PromoCodeUpsertBulk) SetUpdatedAt(v time.Time) *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.SetUpdatedAt(v)
- })
- }
- // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
- func (u *PromoCodeUpsertBulk) UpdateUpdatedAt() *PromoCodeUpsertBulk {
- return u.Update(func(s *PromoCodeUpsert) {
- s.UpdateUpdatedAt()
- })
- }
- // Exec executes the query.
- func (u *PromoCodeUpsertBulk) 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 PromoCodeCreateBulk instead", i)
- }
- }
- if len(u.create.conflict) == 0 {
- return errors.New("ent: missing options for PromoCodeCreateBulk.OnConflict")
- }
- return u.create.Exec(ctx)
- }
- // ExecX is like Exec, but panics if an error occurs.
- func (u *PromoCodeUpsertBulk) ExecX(ctx context.Context) {
- if err := u.create.Exec(ctx); err != nil {
- panic(err)
- }
- }
|