- 支持创建/编辑/删除优惠码,设置赠送金额和使用限制 - 注册页面实时验证优惠码并显示赠送金额 - 支持 URL 参数自动填充 (?promo=CODE) - 添加优惠码验证接口速率限制 - 使用数据库行锁防止并发超限 - 新增后台优惠码管理页面,支持复制注册链接
746 lines
22 KiB
Go
746 lines
22 KiB
Go
// 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
|
|
}
|