| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745 |
- // 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/predicate"
- "github.com/Wei-Shaw/sub2api/ent/promocode"
- "github.com/Wei-Shaw/sub2api/ent/promocodeusage"
- )
- // PromoCodeUpdate is the builder for updating PromoCode entities.
- type PromoCodeUpdate struct {
- config
- hooks []Hook
- mutation *PromoCodeMutation
- }
- // Where appends a list predicates to the PromoCodeUpdate builder.
- func (_u *PromoCodeUpdate) Where(ps ...predicate.PromoCode) *PromoCodeUpdate {
- _u.mutation.Where(ps...)
- return _u
- }
- // SetCode sets the "code" field.
- func (_u *PromoCodeUpdate) SetCode(v string) *PromoCodeUpdate {
- _u.mutation.SetCode(v)
- return _u
- }
- // SetNillableCode sets the "code" field if the given value is not nil.
- func (_u *PromoCodeUpdate) SetNillableCode(v *string) *PromoCodeUpdate {
- if v != nil {
- _u.SetCode(*v)
- }
- return _u
- }
- // SetBonusAmount sets the "bonus_amount" field.
- func (_u *PromoCodeUpdate) SetBonusAmount(v float64) *PromoCodeUpdate {
- _u.mutation.ResetBonusAmount()
- _u.mutation.SetBonusAmount(v)
- return _u
- }
- // SetNillableBonusAmount sets the "bonus_amount" field if the given value is not nil.
- func (_u *PromoCodeUpdate) SetNillableBonusAmount(v *float64) *PromoCodeUpdate {
- if v != nil {
- _u.SetBonusAmount(*v)
- }
- return _u
- }
- // AddBonusAmount adds value to the "bonus_amount" field.
- func (_u *PromoCodeUpdate) AddBonusAmount(v float64) *PromoCodeUpdate {
- _u.mutation.AddBonusAmount(v)
- return _u
- }
- // SetMaxUses sets the "max_uses" field.
- func (_u *PromoCodeUpdate) SetMaxUses(v int) *PromoCodeUpdate {
- _u.mutation.ResetMaxUses()
- _u.mutation.SetMaxUses(v)
- return _u
- }
- // SetNillableMaxUses sets the "max_uses" field if the given value is not nil.
- func (_u *PromoCodeUpdate) SetNillableMaxUses(v *int) *PromoCodeUpdate {
- if v != nil {
- _u.SetMaxUses(*v)
- }
- return _u
- }
- // AddMaxUses adds value to the "max_uses" field.
- func (_u *PromoCodeUpdate) AddMaxUses(v int) *PromoCodeUpdate {
- _u.mutation.AddMaxUses(v)
- return _u
- }
- // SetUsedCount sets the "used_count" field.
- func (_u *PromoCodeUpdate) SetUsedCount(v int) *PromoCodeUpdate {
- _u.mutation.ResetUsedCount()
- _u.mutation.SetUsedCount(v)
- return _u
- }
- // SetNillableUsedCount sets the "used_count" field if the given value is not nil.
- func (_u *PromoCodeUpdate) SetNillableUsedCount(v *int) *PromoCodeUpdate {
- if v != nil {
- _u.SetUsedCount(*v)
- }
- return _u
- }
- // AddUsedCount adds value to the "used_count" field.
- func (_u *PromoCodeUpdate) AddUsedCount(v int) *PromoCodeUpdate {
- _u.mutation.AddUsedCount(v)
- return _u
- }
- // SetStatus sets the "status" field.
- func (_u *PromoCodeUpdate) SetStatus(v string) *PromoCodeUpdate {
- _u.mutation.SetStatus(v)
- return _u
- }
- // SetNillableStatus sets the "status" field if the given value is not nil.
- func (_u *PromoCodeUpdate) SetNillableStatus(v *string) *PromoCodeUpdate {
- if v != nil {
- _u.SetStatus(*v)
- }
- return _u
- }
- // SetExpiresAt sets the "expires_at" field.
- func (_u *PromoCodeUpdate) SetExpiresAt(v time.Time) *PromoCodeUpdate {
- _u.mutation.SetExpiresAt(v)
- return _u
- }
- // SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
- func (_u *PromoCodeUpdate) SetNillableExpiresAt(v *time.Time) *PromoCodeUpdate {
- if v != nil {
- _u.SetExpiresAt(*v)
- }
- return _u
- }
- // ClearExpiresAt clears the value of the "expires_at" field.
- func (_u *PromoCodeUpdate) ClearExpiresAt() *PromoCodeUpdate {
- _u.mutation.ClearExpiresAt()
- return _u
- }
- // SetNotes sets the "notes" field.
- func (_u *PromoCodeUpdate) SetNotes(v string) *PromoCodeUpdate {
- _u.mutation.SetNotes(v)
- return _u
- }
- // SetNillableNotes sets the "notes" field if the given value is not nil.
- func (_u *PromoCodeUpdate) SetNillableNotes(v *string) *PromoCodeUpdate {
- if v != nil {
- _u.SetNotes(*v)
- }
- return _u
- }
- // ClearNotes clears the value of the "notes" field.
- func (_u *PromoCodeUpdate) ClearNotes() *PromoCodeUpdate {
- _u.mutation.ClearNotes()
- return _u
- }
- // SetUpdatedAt sets the "updated_at" field.
- func (_u *PromoCodeUpdate) SetUpdatedAt(v time.Time) *PromoCodeUpdate {
- _u.mutation.SetUpdatedAt(v)
- return _u
- }
- // AddUsageRecordIDs adds the "usage_records" edge to the PromoCodeUsage entity by IDs.
- func (_u *PromoCodeUpdate) AddUsageRecordIDs(ids ...int64) *PromoCodeUpdate {
- _u.mutation.AddUsageRecordIDs(ids...)
- return _u
- }
- // AddUsageRecords adds the "usage_records" edges to the PromoCodeUsage entity.
- func (_u *PromoCodeUpdate) AddUsageRecords(v ...*PromoCodeUsage) *PromoCodeUpdate {
- ids := make([]int64, len(v))
- for i := range v {
- ids[i] = v[i].ID
- }
- return _u.AddUsageRecordIDs(ids...)
- }
- // Mutation returns the PromoCodeMutation object of the builder.
- func (_u *PromoCodeUpdate) Mutation() *PromoCodeMutation {
- return _u.mutation
- }
- // ClearUsageRecords clears all "usage_records" edges to the PromoCodeUsage entity.
- func (_u *PromoCodeUpdate) ClearUsageRecords() *PromoCodeUpdate {
- _u.mutation.ClearUsageRecords()
- return _u
- }
- // RemoveUsageRecordIDs removes the "usage_records" edge to PromoCodeUsage entities by IDs.
- func (_u *PromoCodeUpdate) RemoveUsageRecordIDs(ids ...int64) *PromoCodeUpdate {
- _u.mutation.RemoveUsageRecordIDs(ids...)
- return _u
- }
- // RemoveUsageRecords removes "usage_records" edges to PromoCodeUsage entities.
- func (_u *PromoCodeUpdate) RemoveUsageRecords(v ...*PromoCodeUsage) *PromoCodeUpdate {
- ids := make([]int64, len(v))
- for i := range v {
- ids[i] = v[i].ID
- }
- return _u.RemoveUsageRecordIDs(ids...)
- }
- // Save executes the query and returns the number of nodes affected by the update operation.
- func (_u *PromoCodeUpdate) Save(ctx context.Context) (int, error) {
- _u.defaults()
- return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
- }
- // SaveX is like Save, but panics if an error occurs.
- func (_u *PromoCodeUpdate) SaveX(ctx context.Context) int {
- affected, err := _u.Save(ctx)
- if err != nil {
- panic(err)
- }
- return affected
- }
- // Exec executes the query.
- func (_u *PromoCodeUpdate) Exec(ctx context.Context) error {
- _, err := _u.Save(ctx)
- return err
- }
- // ExecX is like Exec, but panics if an error occurs.
- func (_u *PromoCodeUpdate) ExecX(ctx context.Context) {
- if err := _u.Exec(ctx); err != nil {
- panic(err)
- }
- }
- // defaults sets the default values of the builder before save.
- func (_u *PromoCodeUpdate) defaults() {
- if _, ok := _u.mutation.UpdatedAt(); !ok {
- v := promocode.UpdateDefaultUpdatedAt()
- _u.mutation.SetUpdatedAt(v)
- }
- }
- // check runs all checks and user-defined validators on the builder.
- func (_u *PromoCodeUpdate) check() error {
- if v, ok := _u.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 v, ok := _u.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)}
- }
- }
- return nil
- }
- func (_u *PromoCodeUpdate) sqlSave(ctx context.Context) (_node int, err error) {
- if err := _u.check(); err != nil {
- return _node, err
- }
- _spec := sqlgraph.NewUpdateSpec(promocode.Table, promocode.Columns, sqlgraph.NewFieldSpec(promocode.FieldID, field.TypeInt64))
- if ps := _u.mutation.predicates; len(ps) > 0 {
- _spec.Predicate = func(selector *sql.Selector) {
- for i := range ps {
- ps[i](selector)
- }
- }
- }
- if value, ok := _u.mutation.Code(); ok {
- _spec.SetField(promocode.FieldCode, field.TypeString, value)
- }
- if value, ok := _u.mutation.BonusAmount(); ok {
- _spec.SetField(promocode.FieldBonusAmount, field.TypeFloat64, value)
- }
- if value, ok := _u.mutation.AddedBonusAmount(); ok {
- _spec.AddField(promocode.FieldBonusAmount, field.TypeFloat64, value)
- }
- if value, ok := _u.mutation.MaxUses(); ok {
- _spec.SetField(promocode.FieldMaxUses, field.TypeInt, value)
- }
- if value, ok := _u.mutation.AddedMaxUses(); ok {
- _spec.AddField(promocode.FieldMaxUses, field.TypeInt, value)
- }
- if value, ok := _u.mutation.UsedCount(); ok {
- _spec.SetField(promocode.FieldUsedCount, field.TypeInt, value)
- }
- if value, ok := _u.mutation.AddedUsedCount(); ok {
- _spec.AddField(promocode.FieldUsedCount, field.TypeInt, value)
- }
- if value, ok := _u.mutation.Status(); ok {
- _spec.SetField(promocode.FieldStatus, field.TypeString, value)
- }
- if value, ok := _u.mutation.ExpiresAt(); ok {
- _spec.SetField(promocode.FieldExpiresAt, field.TypeTime, value)
- }
- if _u.mutation.ExpiresAtCleared() {
- _spec.ClearField(promocode.FieldExpiresAt, field.TypeTime)
- }
- if value, ok := _u.mutation.Notes(); ok {
- _spec.SetField(promocode.FieldNotes, field.TypeString, value)
- }
- if _u.mutation.NotesCleared() {
- _spec.ClearField(promocode.FieldNotes, field.TypeString)
- }
- if value, ok := _u.mutation.UpdatedAt(); ok {
- _spec.SetField(promocode.FieldUpdatedAt, field.TypeTime, value)
- }
- if _u.mutation.UsageRecordsCleared() {
- 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),
- },
- }
- _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
- }
- if nodes := _u.mutation.RemovedUsageRecordsIDs(); len(nodes) > 0 && !_u.mutation.UsageRecordsCleared() {
- 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.Clear = append(_spec.Edges.Clear, edge)
- }
- if nodes := _u.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.Add = append(_spec.Edges.Add, edge)
- }
- if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
- if _, ok := err.(*sqlgraph.NotFoundError); ok {
- err = &NotFoundError{promocode.Label}
- } else if sqlgraph.IsConstraintError(err) {
- err = &ConstraintError{msg: err.Error(), wrap: err}
- }
- return 0, err
- }
- _u.mutation.done = true
- return _node, nil
- }
- // PromoCodeUpdateOne is the builder for updating a single PromoCode entity.
- type PromoCodeUpdateOne struct {
- config
- fields []string
- hooks []Hook
- mutation *PromoCodeMutation
- }
- // SetCode sets the "code" field.
- func (_u *PromoCodeUpdateOne) SetCode(v string) *PromoCodeUpdateOne {
- _u.mutation.SetCode(v)
- return _u
- }
- // SetNillableCode sets the "code" field if the given value is not nil.
- func (_u *PromoCodeUpdateOne) SetNillableCode(v *string) *PromoCodeUpdateOne {
- if v != nil {
- _u.SetCode(*v)
- }
- return _u
- }
- // SetBonusAmount sets the "bonus_amount" field.
- func (_u *PromoCodeUpdateOne) SetBonusAmount(v float64) *PromoCodeUpdateOne {
- _u.mutation.ResetBonusAmount()
- _u.mutation.SetBonusAmount(v)
- return _u
- }
- // SetNillableBonusAmount sets the "bonus_amount" field if the given value is not nil.
- func (_u *PromoCodeUpdateOne) SetNillableBonusAmount(v *float64) *PromoCodeUpdateOne {
- if v != nil {
- _u.SetBonusAmount(*v)
- }
- return _u
- }
- // AddBonusAmount adds value to the "bonus_amount" field.
- func (_u *PromoCodeUpdateOne) AddBonusAmount(v float64) *PromoCodeUpdateOne {
- _u.mutation.AddBonusAmount(v)
- return _u
- }
- // SetMaxUses sets the "max_uses" field.
- func (_u *PromoCodeUpdateOne) SetMaxUses(v int) *PromoCodeUpdateOne {
- _u.mutation.ResetMaxUses()
- _u.mutation.SetMaxUses(v)
- return _u
- }
- // SetNillableMaxUses sets the "max_uses" field if the given value is not nil.
- func (_u *PromoCodeUpdateOne) SetNillableMaxUses(v *int) *PromoCodeUpdateOne {
- if v != nil {
- _u.SetMaxUses(*v)
- }
- return _u
- }
- // AddMaxUses adds value to the "max_uses" field.
- func (_u *PromoCodeUpdateOne) AddMaxUses(v int) *PromoCodeUpdateOne {
- _u.mutation.AddMaxUses(v)
- return _u
- }
- // SetUsedCount sets the "used_count" field.
- func (_u *PromoCodeUpdateOne) SetUsedCount(v int) *PromoCodeUpdateOne {
- _u.mutation.ResetUsedCount()
- _u.mutation.SetUsedCount(v)
- return _u
- }
- // SetNillableUsedCount sets the "used_count" field if the given value is not nil.
- func (_u *PromoCodeUpdateOne) SetNillableUsedCount(v *int) *PromoCodeUpdateOne {
- if v != nil {
- _u.SetUsedCount(*v)
- }
- return _u
- }
- // AddUsedCount adds value to the "used_count" field.
- func (_u *PromoCodeUpdateOne) AddUsedCount(v int) *PromoCodeUpdateOne {
- _u.mutation.AddUsedCount(v)
- return _u
- }
- // SetStatus sets the "status" field.
- func (_u *PromoCodeUpdateOne) SetStatus(v string) *PromoCodeUpdateOne {
- _u.mutation.SetStatus(v)
- return _u
- }
- // SetNillableStatus sets the "status" field if the given value is not nil.
- func (_u *PromoCodeUpdateOne) SetNillableStatus(v *string) *PromoCodeUpdateOne {
- if v != nil {
- _u.SetStatus(*v)
- }
- return _u
- }
- // SetExpiresAt sets the "expires_at" field.
- func (_u *PromoCodeUpdateOne) SetExpiresAt(v time.Time) *PromoCodeUpdateOne {
- _u.mutation.SetExpiresAt(v)
- return _u
- }
- // SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
- func (_u *PromoCodeUpdateOne) SetNillableExpiresAt(v *time.Time) *PromoCodeUpdateOne {
- if v != nil {
- _u.SetExpiresAt(*v)
- }
- return _u
- }
- // ClearExpiresAt clears the value of the "expires_at" field.
- func (_u *PromoCodeUpdateOne) ClearExpiresAt() *PromoCodeUpdateOne {
- _u.mutation.ClearExpiresAt()
- return _u
- }
- // SetNotes sets the "notes" field.
- func (_u *PromoCodeUpdateOne) SetNotes(v string) *PromoCodeUpdateOne {
- _u.mutation.SetNotes(v)
- return _u
- }
- // SetNillableNotes sets the "notes" field if the given value is not nil.
- func (_u *PromoCodeUpdateOne) SetNillableNotes(v *string) *PromoCodeUpdateOne {
- if v != nil {
- _u.SetNotes(*v)
- }
- return _u
- }
- // ClearNotes clears the value of the "notes" field.
- func (_u *PromoCodeUpdateOne) ClearNotes() *PromoCodeUpdateOne {
- _u.mutation.ClearNotes()
- return _u
- }
- // SetUpdatedAt sets the "updated_at" field.
- func (_u *PromoCodeUpdateOne) SetUpdatedAt(v time.Time) *PromoCodeUpdateOne {
- _u.mutation.SetUpdatedAt(v)
- return _u
- }
- // AddUsageRecordIDs adds the "usage_records" edge to the PromoCodeUsage entity by IDs.
- func (_u *PromoCodeUpdateOne) AddUsageRecordIDs(ids ...int64) *PromoCodeUpdateOne {
- _u.mutation.AddUsageRecordIDs(ids...)
- return _u
- }
- // AddUsageRecords adds the "usage_records" edges to the PromoCodeUsage entity.
- func (_u *PromoCodeUpdateOne) AddUsageRecords(v ...*PromoCodeUsage) *PromoCodeUpdateOne {
- ids := make([]int64, len(v))
- for i := range v {
- ids[i] = v[i].ID
- }
- return _u.AddUsageRecordIDs(ids...)
- }
- // Mutation returns the PromoCodeMutation object of the builder.
- func (_u *PromoCodeUpdateOne) Mutation() *PromoCodeMutation {
- return _u.mutation
- }
- // ClearUsageRecords clears all "usage_records" edges to the PromoCodeUsage entity.
- func (_u *PromoCodeUpdateOne) ClearUsageRecords() *PromoCodeUpdateOne {
- _u.mutation.ClearUsageRecords()
- return _u
- }
- // RemoveUsageRecordIDs removes the "usage_records" edge to PromoCodeUsage entities by IDs.
- func (_u *PromoCodeUpdateOne) RemoveUsageRecordIDs(ids ...int64) *PromoCodeUpdateOne {
- _u.mutation.RemoveUsageRecordIDs(ids...)
- return _u
- }
- // RemoveUsageRecords removes "usage_records" edges to PromoCodeUsage entities.
- func (_u *PromoCodeUpdateOne) RemoveUsageRecords(v ...*PromoCodeUsage) *PromoCodeUpdateOne {
- ids := make([]int64, len(v))
- for i := range v {
- ids[i] = v[i].ID
- }
- return _u.RemoveUsageRecordIDs(ids...)
- }
- // Where appends a list predicates to the PromoCodeUpdate builder.
- func (_u *PromoCodeUpdateOne) Where(ps ...predicate.PromoCode) *PromoCodeUpdateOne {
- _u.mutation.Where(ps...)
- return _u
- }
- // Select allows selecting one or more fields (columns) of the returned entity.
- // The default is selecting all fields defined in the entity schema.
- func (_u *PromoCodeUpdateOne) Select(field string, fields ...string) *PromoCodeUpdateOne {
- _u.fields = append([]string{field}, fields...)
- return _u
- }
- // Save executes the query and returns the updated PromoCode entity.
- func (_u *PromoCodeUpdateOne) Save(ctx context.Context) (*PromoCode, error) {
- _u.defaults()
- return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
- }
- // SaveX is like Save, but panics if an error occurs.
- func (_u *PromoCodeUpdateOne) SaveX(ctx context.Context) *PromoCode {
- node, err := _u.Save(ctx)
- if err != nil {
- panic(err)
- }
- return node
- }
- // Exec executes the query on the entity.
- func (_u *PromoCodeUpdateOne) Exec(ctx context.Context) error {
- _, err := _u.Save(ctx)
- return err
- }
- // ExecX is like Exec, but panics if an error occurs.
- func (_u *PromoCodeUpdateOne) ExecX(ctx context.Context) {
- if err := _u.Exec(ctx); err != nil {
- panic(err)
- }
- }
- // defaults sets the default values of the builder before save.
- func (_u *PromoCodeUpdateOne) defaults() {
- if _, ok := _u.mutation.UpdatedAt(); !ok {
- v := promocode.UpdateDefaultUpdatedAt()
- _u.mutation.SetUpdatedAt(v)
- }
- }
- // check runs all checks and user-defined validators on the builder.
- func (_u *PromoCodeUpdateOne) check() error {
- if v, ok := _u.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 v, ok := _u.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)}
- }
- }
- return nil
- }
- func (_u *PromoCodeUpdateOne) sqlSave(ctx context.Context) (_node *PromoCode, err error) {
- if err := _u.check(); err != nil {
- return _node, err
- }
- _spec := sqlgraph.NewUpdateSpec(promocode.Table, promocode.Columns, sqlgraph.NewFieldSpec(promocode.FieldID, field.TypeInt64))
- id, ok := _u.mutation.ID()
- if !ok {
- return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "PromoCode.id" for update`)}
- }
- _spec.Node.ID.Value = id
- if fields := _u.fields; len(fields) > 0 {
- _spec.Node.Columns = make([]string, 0, len(fields))
- _spec.Node.Columns = append(_spec.Node.Columns, promocode.FieldID)
- for _, f := range fields {
- if !promocode.ValidColumn(f) {
- return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
- }
- if f != promocode.FieldID {
- _spec.Node.Columns = append(_spec.Node.Columns, f)
- }
- }
- }
- if ps := _u.mutation.predicates; len(ps) > 0 {
- _spec.Predicate = func(selector *sql.Selector) {
- for i := range ps {
- ps[i](selector)
- }
- }
- }
- if value, ok := _u.mutation.Code(); ok {
- _spec.SetField(promocode.FieldCode, field.TypeString, value)
- }
- if value, ok := _u.mutation.BonusAmount(); ok {
- _spec.SetField(promocode.FieldBonusAmount, field.TypeFloat64, value)
- }
- if value, ok := _u.mutation.AddedBonusAmount(); ok {
- _spec.AddField(promocode.FieldBonusAmount, field.TypeFloat64, value)
- }
- if value, ok := _u.mutation.MaxUses(); ok {
- _spec.SetField(promocode.FieldMaxUses, field.TypeInt, value)
- }
- if value, ok := _u.mutation.AddedMaxUses(); ok {
- _spec.AddField(promocode.FieldMaxUses, field.TypeInt, value)
- }
- if value, ok := _u.mutation.UsedCount(); ok {
- _spec.SetField(promocode.FieldUsedCount, field.TypeInt, value)
- }
- if value, ok := _u.mutation.AddedUsedCount(); ok {
- _spec.AddField(promocode.FieldUsedCount, field.TypeInt, value)
- }
- if value, ok := _u.mutation.Status(); ok {
- _spec.SetField(promocode.FieldStatus, field.TypeString, value)
- }
- if value, ok := _u.mutation.ExpiresAt(); ok {
- _spec.SetField(promocode.FieldExpiresAt, field.TypeTime, value)
- }
- if _u.mutation.ExpiresAtCleared() {
- _spec.ClearField(promocode.FieldExpiresAt, field.TypeTime)
- }
- if value, ok := _u.mutation.Notes(); ok {
- _spec.SetField(promocode.FieldNotes, field.TypeString, value)
- }
- if _u.mutation.NotesCleared() {
- _spec.ClearField(promocode.FieldNotes, field.TypeString)
- }
- if value, ok := _u.mutation.UpdatedAt(); ok {
- _spec.SetField(promocode.FieldUpdatedAt, field.TypeTime, value)
- }
- if _u.mutation.UsageRecordsCleared() {
- 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),
- },
- }
- _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
- }
- if nodes := _u.mutation.RemovedUsageRecordsIDs(); len(nodes) > 0 && !_u.mutation.UsageRecordsCleared() {
- 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.Clear = append(_spec.Edges.Clear, edge)
- }
- if nodes := _u.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.Add = append(_spec.Edges.Add, edge)
- }
- _node = &PromoCode{config: _u.config}
- _spec.Assign = _node.assignValues
- _spec.ScanValues = _node.scanValues
- if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
- if _, ok := err.(*sqlgraph.NotFoundError); ok {
- err = &NotFoundError{promocode.Label}
- } else if sqlgraph.IsConstraintError(err) {
- err = &ConstraintError{msg: err.Error(), wrap: err}
- }
- return nil, err
- }
- _u.mutation.done = true
- return _node, nil
- }
|