promocode_update.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "time"
  8. "entgo.io/ent/dialect/sql"
  9. "entgo.io/ent/dialect/sql/sqlgraph"
  10. "entgo.io/ent/schema/field"
  11. "github.com/Wei-Shaw/sub2api/ent/predicate"
  12. "github.com/Wei-Shaw/sub2api/ent/promocode"
  13. "github.com/Wei-Shaw/sub2api/ent/promocodeusage"
  14. )
  15. // PromoCodeUpdate is the builder for updating PromoCode entities.
  16. type PromoCodeUpdate struct {
  17. config
  18. hooks []Hook
  19. mutation *PromoCodeMutation
  20. }
  21. // Where appends a list predicates to the PromoCodeUpdate builder.
  22. func (_u *PromoCodeUpdate) Where(ps ...predicate.PromoCode) *PromoCodeUpdate {
  23. _u.mutation.Where(ps...)
  24. return _u
  25. }
  26. // SetCode sets the "code" field.
  27. func (_u *PromoCodeUpdate) SetCode(v string) *PromoCodeUpdate {
  28. _u.mutation.SetCode(v)
  29. return _u
  30. }
  31. // SetNillableCode sets the "code" field if the given value is not nil.
  32. func (_u *PromoCodeUpdate) SetNillableCode(v *string) *PromoCodeUpdate {
  33. if v != nil {
  34. _u.SetCode(*v)
  35. }
  36. return _u
  37. }
  38. // SetBonusAmount sets the "bonus_amount" field.
  39. func (_u *PromoCodeUpdate) SetBonusAmount(v float64) *PromoCodeUpdate {
  40. _u.mutation.ResetBonusAmount()
  41. _u.mutation.SetBonusAmount(v)
  42. return _u
  43. }
  44. // SetNillableBonusAmount sets the "bonus_amount" field if the given value is not nil.
  45. func (_u *PromoCodeUpdate) SetNillableBonusAmount(v *float64) *PromoCodeUpdate {
  46. if v != nil {
  47. _u.SetBonusAmount(*v)
  48. }
  49. return _u
  50. }
  51. // AddBonusAmount adds value to the "bonus_amount" field.
  52. func (_u *PromoCodeUpdate) AddBonusAmount(v float64) *PromoCodeUpdate {
  53. _u.mutation.AddBonusAmount(v)
  54. return _u
  55. }
  56. // SetMaxUses sets the "max_uses" field.
  57. func (_u *PromoCodeUpdate) SetMaxUses(v int) *PromoCodeUpdate {
  58. _u.mutation.ResetMaxUses()
  59. _u.mutation.SetMaxUses(v)
  60. return _u
  61. }
  62. // SetNillableMaxUses sets the "max_uses" field if the given value is not nil.
  63. func (_u *PromoCodeUpdate) SetNillableMaxUses(v *int) *PromoCodeUpdate {
  64. if v != nil {
  65. _u.SetMaxUses(*v)
  66. }
  67. return _u
  68. }
  69. // AddMaxUses adds value to the "max_uses" field.
  70. func (_u *PromoCodeUpdate) AddMaxUses(v int) *PromoCodeUpdate {
  71. _u.mutation.AddMaxUses(v)
  72. return _u
  73. }
  74. // SetUsedCount sets the "used_count" field.
  75. func (_u *PromoCodeUpdate) SetUsedCount(v int) *PromoCodeUpdate {
  76. _u.mutation.ResetUsedCount()
  77. _u.mutation.SetUsedCount(v)
  78. return _u
  79. }
  80. // SetNillableUsedCount sets the "used_count" field if the given value is not nil.
  81. func (_u *PromoCodeUpdate) SetNillableUsedCount(v *int) *PromoCodeUpdate {
  82. if v != nil {
  83. _u.SetUsedCount(*v)
  84. }
  85. return _u
  86. }
  87. // AddUsedCount adds value to the "used_count" field.
  88. func (_u *PromoCodeUpdate) AddUsedCount(v int) *PromoCodeUpdate {
  89. _u.mutation.AddUsedCount(v)
  90. return _u
  91. }
  92. // SetStatus sets the "status" field.
  93. func (_u *PromoCodeUpdate) SetStatus(v string) *PromoCodeUpdate {
  94. _u.mutation.SetStatus(v)
  95. return _u
  96. }
  97. // SetNillableStatus sets the "status" field if the given value is not nil.
  98. func (_u *PromoCodeUpdate) SetNillableStatus(v *string) *PromoCodeUpdate {
  99. if v != nil {
  100. _u.SetStatus(*v)
  101. }
  102. return _u
  103. }
  104. // SetExpiresAt sets the "expires_at" field.
  105. func (_u *PromoCodeUpdate) SetExpiresAt(v time.Time) *PromoCodeUpdate {
  106. _u.mutation.SetExpiresAt(v)
  107. return _u
  108. }
  109. // SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
  110. func (_u *PromoCodeUpdate) SetNillableExpiresAt(v *time.Time) *PromoCodeUpdate {
  111. if v != nil {
  112. _u.SetExpiresAt(*v)
  113. }
  114. return _u
  115. }
  116. // ClearExpiresAt clears the value of the "expires_at" field.
  117. func (_u *PromoCodeUpdate) ClearExpiresAt() *PromoCodeUpdate {
  118. _u.mutation.ClearExpiresAt()
  119. return _u
  120. }
  121. // SetNotes sets the "notes" field.
  122. func (_u *PromoCodeUpdate) SetNotes(v string) *PromoCodeUpdate {
  123. _u.mutation.SetNotes(v)
  124. return _u
  125. }
  126. // SetNillableNotes sets the "notes" field if the given value is not nil.
  127. func (_u *PromoCodeUpdate) SetNillableNotes(v *string) *PromoCodeUpdate {
  128. if v != nil {
  129. _u.SetNotes(*v)
  130. }
  131. return _u
  132. }
  133. // ClearNotes clears the value of the "notes" field.
  134. func (_u *PromoCodeUpdate) ClearNotes() *PromoCodeUpdate {
  135. _u.mutation.ClearNotes()
  136. return _u
  137. }
  138. // SetUpdatedAt sets the "updated_at" field.
  139. func (_u *PromoCodeUpdate) SetUpdatedAt(v time.Time) *PromoCodeUpdate {
  140. _u.mutation.SetUpdatedAt(v)
  141. return _u
  142. }
  143. // AddUsageRecordIDs adds the "usage_records" edge to the PromoCodeUsage entity by IDs.
  144. func (_u *PromoCodeUpdate) AddUsageRecordIDs(ids ...int64) *PromoCodeUpdate {
  145. _u.mutation.AddUsageRecordIDs(ids...)
  146. return _u
  147. }
  148. // AddUsageRecords adds the "usage_records" edges to the PromoCodeUsage entity.
  149. func (_u *PromoCodeUpdate) AddUsageRecords(v ...*PromoCodeUsage) *PromoCodeUpdate {
  150. ids := make([]int64, len(v))
  151. for i := range v {
  152. ids[i] = v[i].ID
  153. }
  154. return _u.AddUsageRecordIDs(ids...)
  155. }
  156. // Mutation returns the PromoCodeMutation object of the builder.
  157. func (_u *PromoCodeUpdate) Mutation() *PromoCodeMutation {
  158. return _u.mutation
  159. }
  160. // ClearUsageRecords clears all "usage_records" edges to the PromoCodeUsage entity.
  161. func (_u *PromoCodeUpdate) ClearUsageRecords() *PromoCodeUpdate {
  162. _u.mutation.ClearUsageRecords()
  163. return _u
  164. }
  165. // RemoveUsageRecordIDs removes the "usage_records" edge to PromoCodeUsage entities by IDs.
  166. func (_u *PromoCodeUpdate) RemoveUsageRecordIDs(ids ...int64) *PromoCodeUpdate {
  167. _u.mutation.RemoveUsageRecordIDs(ids...)
  168. return _u
  169. }
  170. // RemoveUsageRecords removes "usage_records" edges to PromoCodeUsage entities.
  171. func (_u *PromoCodeUpdate) RemoveUsageRecords(v ...*PromoCodeUsage) *PromoCodeUpdate {
  172. ids := make([]int64, len(v))
  173. for i := range v {
  174. ids[i] = v[i].ID
  175. }
  176. return _u.RemoveUsageRecordIDs(ids...)
  177. }
  178. // Save executes the query and returns the number of nodes affected by the update operation.
  179. func (_u *PromoCodeUpdate) Save(ctx context.Context) (int, error) {
  180. _u.defaults()
  181. return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
  182. }
  183. // SaveX is like Save, but panics if an error occurs.
  184. func (_u *PromoCodeUpdate) SaveX(ctx context.Context) int {
  185. affected, err := _u.Save(ctx)
  186. if err != nil {
  187. panic(err)
  188. }
  189. return affected
  190. }
  191. // Exec executes the query.
  192. func (_u *PromoCodeUpdate) Exec(ctx context.Context) error {
  193. _, err := _u.Save(ctx)
  194. return err
  195. }
  196. // ExecX is like Exec, but panics if an error occurs.
  197. func (_u *PromoCodeUpdate) ExecX(ctx context.Context) {
  198. if err := _u.Exec(ctx); err != nil {
  199. panic(err)
  200. }
  201. }
  202. // defaults sets the default values of the builder before save.
  203. func (_u *PromoCodeUpdate) defaults() {
  204. if _, ok := _u.mutation.UpdatedAt(); !ok {
  205. v := promocode.UpdateDefaultUpdatedAt()
  206. _u.mutation.SetUpdatedAt(v)
  207. }
  208. }
  209. // check runs all checks and user-defined validators on the builder.
  210. func (_u *PromoCodeUpdate) check() error {
  211. if v, ok := _u.mutation.Code(); ok {
  212. if err := promocode.CodeValidator(v); err != nil {
  213. return &ValidationError{Name: "code", err: fmt.Errorf(`ent: validator failed for field "PromoCode.code": %w`, err)}
  214. }
  215. }
  216. if v, ok := _u.mutation.Status(); ok {
  217. if err := promocode.StatusValidator(v); err != nil {
  218. return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "PromoCode.status": %w`, err)}
  219. }
  220. }
  221. return nil
  222. }
  223. func (_u *PromoCodeUpdate) sqlSave(ctx context.Context) (_node int, err error) {
  224. if err := _u.check(); err != nil {
  225. return _node, err
  226. }
  227. _spec := sqlgraph.NewUpdateSpec(promocode.Table, promocode.Columns, sqlgraph.NewFieldSpec(promocode.FieldID, field.TypeInt64))
  228. if ps := _u.mutation.predicates; len(ps) > 0 {
  229. _spec.Predicate = func(selector *sql.Selector) {
  230. for i := range ps {
  231. ps[i](selector)
  232. }
  233. }
  234. }
  235. if value, ok := _u.mutation.Code(); ok {
  236. _spec.SetField(promocode.FieldCode, field.TypeString, value)
  237. }
  238. if value, ok := _u.mutation.BonusAmount(); ok {
  239. _spec.SetField(promocode.FieldBonusAmount, field.TypeFloat64, value)
  240. }
  241. if value, ok := _u.mutation.AddedBonusAmount(); ok {
  242. _spec.AddField(promocode.FieldBonusAmount, field.TypeFloat64, value)
  243. }
  244. if value, ok := _u.mutation.MaxUses(); ok {
  245. _spec.SetField(promocode.FieldMaxUses, field.TypeInt, value)
  246. }
  247. if value, ok := _u.mutation.AddedMaxUses(); ok {
  248. _spec.AddField(promocode.FieldMaxUses, field.TypeInt, value)
  249. }
  250. if value, ok := _u.mutation.UsedCount(); ok {
  251. _spec.SetField(promocode.FieldUsedCount, field.TypeInt, value)
  252. }
  253. if value, ok := _u.mutation.AddedUsedCount(); ok {
  254. _spec.AddField(promocode.FieldUsedCount, field.TypeInt, value)
  255. }
  256. if value, ok := _u.mutation.Status(); ok {
  257. _spec.SetField(promocode.FieldStatus, field.TypeString, value)
  258. }
  259. if value, ok := _u.mutation.ExpiresAt(); ok {
  260. _spec.SetField(promocode.FieldExpiresAt, field.TypeTime, value)
  261. }
  262. if _u.mutation.ExpiresAtCleared() {
  263. _spec.ClearField(promocode.FieldExpiresAt, field.TypeTime)
  264. }
  265. if value, ok := _u.mutation.Notes(); ok {
  266. _spec.SetField(promocode.FieldNotes, field.TypeString, value)
  267. }
  268. if _u.mutation.NotesCleared() {
  269. _spec.ClearField(promocode.FieldNotes, field.TypeString)
  270. }
  271. if value, ok := _u.mutation.UpdatedAt(); ok {
  272. _spec.SetField(promocode.FieldUpdatedAt, field.TypeTime, value)
  273. }
  274. if _u.mutation.UsageRecordsCleared() {
  275. edge := &sqlgraph.EdgeSpec{
  276. Rel: sqlgraph.O2M,
  277. Inverse: false,
  278. Table: promocode.UsageRecordsTable,
  279. Columns: []string{promocode.UsageRecordsColumn},
  280. Bidi: false,
  281. Target: &sqlgraph.EdgeTarget{
  282. IDSpec: sqlgraph.NewFieldSpec(promocodeusage.FieldID, field.TypeInt64),
  283. },
  284. }
  285. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  286. }
  287. if nodes := _u.mutation.RemovedUsageRecordsIDs(); len(nodes) > 0 && !_u.mutation.UsageRecordsCleared() {
  288. edge := &sqlgraph.EdgeSpec{
  289. Rel: sqlgraph.O2M,
  290. Inverse: false,
  291. Table: promocode.UsageRecordsTable,
  292. Columns: []string{promocode.UsageRecordsColumn},
  293. Bidi: false,
  294. Target: &sqlgraph.EdgeTarget{
  295. IDSpec: sqlgraph.NewFieldSpec(promocodeusage.FieldID, field.TypeInt64),
  296. },
  297. }
  298. for _, k := range nodes {
  299. edge.Target.Nodes = append(edge.Target.Nodes, k)
  300. }
  301. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  302. }
  303. if nodes := _u.mutation.UsageRecordsIDs(); len(nodes) > 0 {
  304. edge := &sqlgraph.EdgeSpec{
  305. Rel: sqlgraph.O2M,
  306. Inverse: false,
  307. Table: promocode.UsageRecordsTable,
  308. Columns: []string{promocode.UsageRecordsColumn},
  309. Bidi: false,
  310. Target: &sqlgraph.EdgeTarget{
  311. IDSpec: sqlgraph.NewFieldSpec(promocodeusage.FieldID, field.TypeInt64),
  312. },
  313. }
  314. for _, k := range nodes {
  315. edge.Target.Nodes = append(edge.Target.Nodes, k)
  316. }
  317. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  318. }
  319. if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
  320. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  321. err = &NotFoundError{promocode.Label}
  322. } else if sqlgraph.IsConstraintError(err) {
  323. err = &ConstraintError{msg: err.Error(), wrap: err}
  324. }
  325. return 0, err
  326. }
  327. _u.mutation.done = true
  328. return _node, nil
  329. }
  330. // PromoCodeUpdateOne is the builder for updating a single PromoCode entity.
  331. type PromoCodeUpdateOne struct {
  332. config
  333. fields []string
  334. hooks []Hook
  335. mutation *PromoCodeMutation
  336. }
  337. // SetCode sets the "code" field.
  338. func (_u *PromoCodeUpdateOne) SetCode(v string) *PromoCodeUpdateOne {
  339. _u.mutation.SetCode(v)
  340. return _u
  341. }
  342. // SetNillableCode sets the "code" field if the given value is not nil.
  343. func (_u *PromoCodeUpdateOne) SetNillableCode(v *string) *PromoCodeUpdateOne {
  344. if v != nil {
  345. _u.SetCode(*v)
  346. }
  347. return _u
  348. }
  349. // SetBonusAmount sets the "bonus_amount" field.
  350. func (_u *PromoCodeUpdateOne) SetBonusAmount(v float64) *PromoCodeUpdateOne {
  351. _u.mutation.ResetBonusAmount()
  352. _u.mutation.SetBonusAmount(v)
  353. return _u
  354. }
  355. // SetNillableBonusAmount sets the "bonus_amount" field if the given value is not nil.
  356. func (_u *PromoCodeUpdateOne) SetNillableBonusAmount(v *float64) *PromoCodeUpdateOne {
  357. if v != nil {
  358. _u.SetBonusAmount(*v)
  359. }
  360. return _u
  361. }
  362. // AddBonusAmount adds value to the "bonus_amount" field.
  363. func (_u *PromoCodeUpdateOne) AddBonusAmount(v float64) *PromoCodeUpdateOne {
  364. _u.mutation.AddBonusAmount(v)
  365. return _u
  366. }
  367. // SetMaxUses sets the "max_uses" field.
  368. func (_u *PromoCodeUpdateOne) SetMaxUses(v int) *PromoCodeUpdateOne {
  369. _u.mutation.ResetMaxUses()
  370. _u.mutation.SetMaxUses(v)
  371. return _u
  372. }
  373. // SetNillableMaxUses sets the "max_uses" field if the given value is not nil.
  374. func (_u *PromoCodeUpdateOne) SetNillableMaxUses(v *int) *PromoCodeUpdateOne {
  375. if v != nil {
  376. _u.SetMaxUses(*v)
  377. }
  378. return _u
  379. }
  380. // AddMaxUses adds value to the "max_uses" field.
  381. func (_u *PromoCodeUpdateOne) AddMaxUses(v int) *PromoCodeUpdateOne {
  382. _u.mutation.AddMaxUses(v)
  383. return _u
  384. }
  385. // SetUsedCount sets the "used_count" field.
  386. func (_u *PromoCodeUpdateOne) SetUsedCount(v int) *PromoCodeUpdateOne {
  387. _u.mutation.ResetUsedCount()
  388. _u.mutation.SetUsedCount(v)
  389. return _u
  390. }
  391. // SetNillableUsedCount sets the "used_count" field if the given value is not nil.
  392. func (_u *PromoCodeUpdateOne) SetNillableUsedCount(v *int) *PromoCodeUpdateOne {
  393. if v != nil {
  394. _u.SetUsedCount(*v)
  395. }
  396. return _u
  397. }
  398. // AddUsedCount adds value to the "used_count" field.
  399. func (_u *PromoCodeUpdateOne) AddUsedCount(v int) *PromoCodeUpdateOne {
  400. _u.mutation.AddUsedCount(v)
  401. return _u
  402. }
  403. // SetStatus sets the "status" field.
  404. func (_u *PromoCodeUpdateOne) SetStatus(v string) *PromoCodeUpdateOne {
  405. _u.mutation.SetStatus(v)
  406. return _u
  407. }
  408. // SetNillableStatus sets the "status" field if the given value is not nil.
  409. func (_u *PromoCodeUpdateOne) SetNillableStatus(v *string) *PromoCodeUpdateOne {
  410. if v != nil {
  411. _u.SetStatus(*v)
  412. }
  413. return _u
  414. }
  415. // SetExpiresAt sets the "expires_at" field.
  416. func (_u *PromoCodeUpdateOne) SetExpiresAt(v time.Time) *PromoCodeUpdateOne {
  417. _u.mutation.SetExpiresAt(v)
  418. return _u
  419. }
  420. // SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
  421. func (_u *PromoCodeUpdateOne) SetNillableExpiresAt(v *time.Time) *PromoCodeUpdateOne {
  422. if v != nil {
  423. _u.SetExpiresAt(*v)
  424. }
  425. return _u
  426. }
  427. // ClearExpiresAt clears the value of the "expires_at" field.
  428. func (_u *PromoCodeUpdateOne) ClearExpiresAt() *PromoCodeUpdateOne {
  429. _u.mutation.ClearExpiresAt()
  430. return _u
  431. }
  432. // SetNotes sets the "notes" field.
  433. func (_u *PromoCodeUpdateOne) SetNotes(v string) *PromoCodeUpdateOne {
  434. _u.mutation.SetNotes(v)
  435. return _u
  436. }
  437. // SetNillableNotes sets the "notes" field if the given value is not nil.
  438. func (_u *PromoCodeUpdateOne) SetNillableNotes(v *string) *PromoCodeUpdateOne {
  439. if v != nil {
  440. _u.SetNotes(*v)
  441. }
  442. return _u
  443. }
  444. // ClearNotes clears the value of the "notes" field.
  445. func (_u *PromoCodeUpdateOne) ClearNotes() *PromoCodeUpdateOne {
  446. _u.mutation.ClearNotes()
  447. return _u
  448. }
  449. // SetUpdatedAt sets the "updated_at" field.
  450. func (_u *PromoCodeUpdateOne) SetUpdatedAt(v time.Time) *PromoCodeUpdateOne {
  451. _u.mutation.SetUpdatedAt(v)
  452. return _u
  453. }
  454. // AddUsageRecordIDs adds the "usage_records" edge to the PromoCodeUsage entity by IDs.
  455. func (_u *PromoCodeUpdateOne) AddUsageRecordIDs(ids ...int64) *PromoCodeUpdateOne {
  456. _u.mutation.AddUsageRecordIDs(ids...)
  457. return _u
  458. }
  459. // AddUsageRecords adds the "usage_records" edges to the PromoCodeUsage entity.
  460. func (_u *PromoCodeUpdateOne) AddUsageRecords(v ...*PromoCodeUsage) *PromoCodeUpdateOne {
  461. ids := make([]int64, len(v))
  462. for i := range v {
  463. ids[i] = v[i].ID
  464. }
  465. return _u.AddUsageRecordIDs(ids...)
  466. }
  467. // Mutation returns the PromoCodeMutation object of the builder.
  468. func (_u *PromoCodeUpdateOne) Mutation() *PromoCodeMutation {
  469. return _u.mutation
  470. }
  471. // ClearUsageRecords clears all "usage_records" edges to the PromoCodeUsage entity.
  472. func (_u *PromoCodeUpdateOne) ClearUsageRecords() *PromoCodeUpdateOne {
  473. _u.mutation.ClearUsageRecords()
  474. return _u
  475. }
  476. // RemoveUsageRecordIDs removes the "usage_records" edge to PromoCodeUsage entities by IDs.
  477. func (_u *PromoCodeUpdateOne) RemoveUsageRecordIDs(ids ...int64) *PromoCodeUpdateOne {
  478. _u.mutation.RemoveUsageRecordIDs(ids...)
  479. return _u
  480. }
  481. // RemoveUsageRecords removes "usage_records" edges to PromoCodeUsage entities.
  482. func (_u *PromoCodeUpdateOne) RemoveUsageRecords(v ...*PromoCodeUsage) *PromoCodeUpdateOne {
  483. ids := make([]int64, len(v))
  484. for i := range v {
  485. ids[i] = v[i].ID
  486. }
  487. return _u.RemoveUsageRecordIDs(ids...)
  488. }
  489. // Where appends a list predicates to the PromoCodeUpdate builder.
  490. func (_u *PromoCodeUpdateOne) Where(ps ...predicate.PromoCode) *PromoCodeUpdateOne {
  491. _u.mutation.Where(ps...)
  492. return _u
  493. }
  494. // Select allows selecting one or more fields (columns) of the returned entity.
  495. // The default is selecting all fields defined in the entity schema.
  496. func (_u *PromoCodeUpdateOne) Select(field string, fields ...string) *PromoCodeUpdateOne {
  497. _u.fields = append([]string{field}, fields...)
  498. return _u
  499. }
  500. // Save executes the query and returns the updated PromoCode entity.
  501. func (_u *PromoCodeUpdateOne) Save(ctx context.Context) (*PromoCode, error) {
  502. _u.defaults()
  503. return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
  504. }
  505. // SaveX is like Save, but panics if an error occurs.
  506. func (_u *PromoCodeUpdateOne) SaveX(ctx context.Context) *PromoCode {
  507. node, err := _u.Save(ctx)
  508. if err != nil {
  509. panic(err)
  510. }
  511. return node
  512. }
  513. // Exec executes the query on the entity.
  514. func (_u *PromoCodeUpdateOne) Exec(ctx context.Context) error {
  515. _, err := _u.Save(ctx)
  516. return err
  517. }
  518. // ExecX is like Exec, but panics if an error occurs.
  519. func (_u *PromoCodeUpdateOne) ExecX(ctx context.Context) {
  520. if err := _u.Exec(ctx); err != nil {
  521. panic(err)
  522. }
  523. }
  524. // defaults sets the default values of the builder before save.
  525. func (_u *PromoCodeUpdateOne) defaults() {
  526. if _, ok := _u.mutation.UpdatedAt(); !ok {
  527. v := promocode.UpdateDefaultUpdatedAt()
  528. _u.mutation.SetUpdatedAt(v)
  529. }
  530. }
  531. // check runs all checks and user-defined validators on the builder.
  532. func (_u *PromoCodeUpdateOne) check() error {
  533. if v, ok := _u.mutation.Code(); ok {
  534. if err := promocode.CodeValidator(v); err != nil {
  535. return &ValidationError{Name: "code", err: fmt.Errorf(`ent: validator failed for field "PromoCode.code": %w`, err)}
  536. }
  537. }
  538. if v, ok := _u.mutation.Status(); ok {
  539. if err := promocode.StatusValidator(v); err != nil {
  540. return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "PromoCode.status": %w`, err)}
  541. }
  542. }
  543. return nil
  544. }
  545. func (_u *PromoCodeUpdateOne) sqlSave(ctx context.Context) (_node *PromoCode, err error) {
  546. if err := _u.check(); err != nil {
  547. return _node, err
  548. }
  549. _spec := sqlgraph.NewUpdateSpec(promocode.Table, promocode.Columns, sqlgraph.NewFieldSpec(promocode.FieldID, field.TypeInt64))
  550. id, ok := _u.mutation.ID()
  551. if !ok {
  552. return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "PromoCode.id" for update`)}
  553. }
  554. _spec.Node.ID.Value = id
  555. if fields := _u.fields; len(fields) > 0 {
  556. _spec.Node.Columns = make([]string, 0, len(fields))
  557. _spec.Node.Columns = append(_spec.Node.Columns, promocode.FieldID)
  558. for _, f := range fields {
  559. if !promocode.ValidColumn(f) {
  560. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  561. }
  562. if f != promocode.FieldID {
  563. _spec.Node.Columns = append(_spec.Node.Columns, f)
  564. }
  565. }
  566. }
  567. if ps := _u.mutation.predicates; len(ps) > 0 {
  568. _spec.Predicate = func(selector *sql.Selector) {
  569. for i := range ps {
  570. ps[i](selector)
  571. }
  572. }
  573. }
  574. if value, ok := _u.mutation.Code(); ok {
  575. _spec.SetField(promocode.FieldCode, field.TypeString, value)
  576. }
  577. if value, ok := _u.mutation.BonusAmount(); ok {
  578. _spec.SetField(promocode.FieldBonusAmount, field.TypeFloat64, value)
  579. }
  580. if value, ok := _u.mutation.AddedBonusAmount(); ok {
  581. _spec.AddField(promocode.FieldBonusAmount, field.TypeFloat64, value)
  582. }
  583. if value, ok := _u.mutation.MaxUses(); ok {
  584. _spec.SetField(promocode.FieldMaxUses, field.TypeInt, value)
  585. }
  586. if value, ok := _u.mutation.AddedMaxUses(); ok {
  587. _spec.AddField(promocode.FieldMaxUses, field.TypeInt, value)
  588. }
  589. if value, ok := _u.mutation.UsedCount(); ok {
  590. _spec.SetField(promocode.FieldUsedCount, field.TypeInt, value)
  591. }
  592. if value, ok := _u.mutation.AddedUsedCount(); ok {
  593. _spec.AddField(promocode.FieldUsedCount, field.TypeInt, value)
  594. }
  595. if value, ok := _u.mutation.Status(); ok {
  596. _spec.SetField(promocode.FieldStatus, field.TypeString, value)
  597. }
  598. if value, ok := _u.mutation.ExpiresAt(); ok {
  599. _spec.SetField(promocode.FieldExpiresAt, field.TypeTime, value)
  600. }
  601. if _u.mutation.ExpiresAtCleared() {
  602. _spec.ClearField(promocode.FieldExpiresAt, field.TypeTime)
  603. }
  604. if value, ok := _u.mutation.Notes(); ok {
  605. _spec.SetField(promocode.FieldNotes, field.TypeString, value)
  606. }
  607. if _u.mutation.NotesCleared() {
  608. _spec.ClearField(promocode.FieldNotes, field.TypeString)
  609. }
  610. if value, ok := _u.mutation.UpdatedAt(); ok {
  611. _spec.SetField(promocode.FieldUpdatedAt, field.TypeTime, value)
  612. }
  613. if _u.mutation.UsageRecordsCleared() {
  614. edge := &sqlgraph.EdgeSpec{
  615. Rel: sqlgraph.O2M,
  616. Inverse: false,
  617. Table: promocode.UsageRecordsTable,
  618. Columns: []string{promocode.UsageRecordsColumn},
  619. Bidi: false,
  620. Target: &sqlgraph.EdgeTarget{
  621. IDSpec: sqlgraph.NewFieldSpec(promocodeusage.FieldID, field.TypeInt64),
  622. },
  623. }
  624. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  625. }
  626. if nodes := _u.mutation.RemovedUsageRecordsIDs(); len(nodes) > 0 && !_u.mutation.UsageRecordsCleared() {
  627. edge := &sqlgraph.EdgeSpec{
  628. Rel: sqlgraph.O2M,
  629. Inverse: false,
  630. Table: promocode.UsageRecordsTable,
  631. Columns: []string{promocode.UsageRecordsColumn},
  632. Bidi: false,
  633. Target: &sqlgraph.EdgeTarget{
  634. IDSpec: sqlgraph.NewFieldSpec(promocodeusage.FieldID, field.TypeInt64),
  635. },
  636. }
  637. for _, k := range nodes {
  638. edge.Target.Nodes = append(edge.Target.Nodes, k)
  639. }
  640. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  641. }
  642. if nodes := _u.mutation.UsageRecordsIDs(); len(nodes) > 0 {
  643. edge := &sqlgraph.EdgeSpec{
  644. Rel: sqlgraph.O2M,
  645. Inverse: false,
  646. Table: promocode.UsageRecordsTable,
  647. Columns: []string{promocode.UsageRecordsColumn},
  648. Bidi: false,
  649. Target: &sqlgraph.EdgeTarget{
  650. IDSpec: sqlgraph.NewFieldSpec(promocodeusage.FieldID, field.TypeInt64),
  651. },
  652. }
  653. for _, k := range nodes {
  654. edge.Target.Nodes = append(edge.Target.Nodes, k)
  655. }
  656. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  657. }
  658. _node = &PromoCode{config: _u.config}
  659. _spec.Assign = _node.assignValues
  660. _spec.ScanValues = _node.scanValues
  661. if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
  662. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  663. err = &NotFoundError{promocode.Label}
  664. } else if sqlgraph.IsConstraintError(err) {
  665. err = &ConstraintError{msg: err.Error(), wrap: err}
  666. }
  667. return nil, err
  668. }
  669. _u.mutation.done = true
  670. return _node, nil
  671. }