promocode_create.go 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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/promocode"
  12. "github.com/Wei-Shaw/sub2api/ent/promocodeusage"
  13. )
  14. // PromoCodeCreate is the builder for creating a PromoCode entity.
  15. type PromoCodeCreate struct {
  16. config
  17. mutation *PromoCodeMutation
  18. hooks []Hook
  19. conflict []sql.ConflictOption
  20. }
  21. // SetCode sets the "code" field.
  22. func (_c *PromoCodeCreate) SetCode(v string) *PromoCodeCreate {
  23. _c.mutation.SetCode(v)
  24. return _c
  25. }
  26. // SetBonusAmount sets the "bonus_amount" field.
  27. func (_c *PromoCodeCreate) SetBonusAmount(v float64) *PromoCodeCreate {
  28. _c.mutation.SetBonusAmount(v)
  29. return _c
  30. }
  31. // SetNillableBonusAmount sets the "bonus_amount" field if the given value is not nil.
  32. func (_c *PromoCodeCreate) SetNillableBonusAmount(v *float64) *PromoCodeCreate {
  33. if v != nil {
  34. _c.SetBonusAmount(*v)
  35. }
  36. return _c
  37. }
  38. // SetMaxUses sets the "max_uses" field.
  39. func (_c *PromoCodeCreate) SetMaxUses(v int) *PromoCodeCreate {
  40. _c.mutation.SetMaxUses(v)
  41. return _c
  42. }
  43. // SetNillableMaxUses sets the "max_uses" field if the given value is not nil.
  44. func (_c *PromoCodeCreate) SetNillableMaxUses(v *int) *PromoCodeCreate {
  45. if v != nil {
  46. _c.SetMaxUses(*v)
  47. }
  48. return _c
  49. }
  50. // SetUsedCount sets the "used_count" field.
  51. func (_c *PromoCodeCreate) SetUsedCount(v int) *PromoCodeCreate {
  52. _c.mutation.SetUsedCount(v)
  53. return _c
  54. }
  55. // SetNillableUsedCount sets the "used_count" field if the given value is not nil.
  56. func (_c *PromoCodeCreate) SetNillableUsedCount(v *int) *PromoCodeCreate {
  57. if v != nil {
  58. _c.SetUsedCount(*v)
  59. }
  60. return _c
  61. }
  62. // SetStatus sets the "status" field.
  63. func (_c *PromoCodeCreate) SetStatus(v string) *PromoCodeCreate {
  64. _c.mutation.SetStatus(v)
  65. return _c
  66. }
  67. // SetNillableStatus sets the "status" field if the given value is not nil.
  68. func (_c *PromoCodeCreate) SetNillableStatus(v *string) *PromoCodeCreate {
  69. if v != nil {
  70. _c.SetStatus(*v)
  71. }
  72. return _c
  73. }
  74. // SetExpiresAt sets the "expires_at" field.
  75. func (_c *PromoCodeCreate) SetExpiresAt(v time.Time) *PromoCodeCreate {
  76. _c.mutation.SetExpiresAt(v)
  77. return _c
  78. }
  79. // SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
  80. func (_c *PromoCodeCreate) SetNillableExpiresAt(v *time.Time) *PromoCodeCreate {
  81. if v != nil {
  82. _c.SetExpiresAt(*v)
  83. }
  84. return _c
  85. }
  86. // SetNotes sets the "notes" field.
  87. func (_c *PromoCodeCreate) SetNotes(v string) *PromoCodeCreate {
  88. _c.mutation.SetNotes(v)
  89. return _c
  90. }
  91. // SetNillableNotes sets the "notes" field if the given value is not nil.
  92. func (_c *PromoCodeCreate) SetNillableNotes(v *string) *PromoCodeCreate {
  93. if v != nil {
  94. _c.SetNotes(*v)
  95. }
  96. return _c
  97. }
  98. // SetCreatedAt sets the "created_at" field.
  99. func (_c *PromoCodeCreate) SetCreatedAt(v time.Time) *PromoCodeCreate {
  100. _c.mutation.SetCreatedAt(v)
  101. return _c
  102. }
  103. // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
  104. func (_c *PromoCodeCreate) SetNillableCreatedAt(v *time.Time) *PromoCodeCreate {
  105. if v != nil {
  106. _c.SetCreatedAt(*v)
  107. }
  108. return _c
  109. }
  110. // SetUpdatedAt sets the "updated_at" field.
  111. func (_c *PromoCodeCreate) SetUpdatedAt(v time.Time) *PromoCodeCreate {
  112. _c.mutation.SetUpdatedAt(v)
  113. return _c
  114. }
  115. // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
  116. func (_c *PromoCodeCreate) SetNillableUpdatedAt(v *time.Time) *PromoCodeCreate {
  117. if v != nil {
  118. _c.SetUpdatedAt(*v)
  119. }
  120. return _c
  121. }
  122. // AddUsageRecordIDs adds the "usage_records" edge to the PromoCodeUsage entity by IDs.
  123. func (_c *PromoCodeCreate) AddUsageRecordIDs(ids ...int64) *PromoCodeCreate {
  124. _c.mutation.AddUsageRecordIDs(ids...)
  125. return _c
  126. }
  127. // AddUsageRecords adds the "usage_records" edges to the PromoCodeUsage entity.
  128. func (_c *PromoCodeCreate) AddUsageRecords(v ...*PromoCodeUsage) *PromoCodeCreate {
  129. ids := make([]int64, len(v))
  130. for i := range v {
  131. ids[i] = v[i].ID
  132. }
  133. return _c.AddUsageRecordIDs(ids...)
  134. }
  135. // Mutation returns the PromoCodeMutation object of the builder.
  136. func (_c *PromoCodeCreate) Mutation() *PromoCodeMutation {
  137. return _c.mutation
  138. }
  139. // Save creates the PromoCode in the database.
  140. func (_c *PromoCodeCreate) Save(ctx context.Context) (*PromoCode, error) {
  141. _c.defaults()
  142. return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
  143. }
  144. // SaveX calls Save and panics if Save returns an error.
  145. func (_c *PromoCodeCreate) SaveX(ctx context.Context) *PromoCode {
  146. v, err := _c.Save(ctx)
  147. if err != nil {
  148. panic(err)
  149. }
  150. return v
  151. }
  152. // Exec executes the query.
  153. func (_c *PromoCodeCreate) Exec(ctx context.Context) error {
  154. _, err := _c.Save(ctx)
  155. return err
  156. }
  157. // ExecX is like Exec, but panics if an error occurs.
  158. func (_c *PromoCodeCreate) ExecX(ctx context.Context) {
  159. if err := _c.Exec(ctx); err != nil {
  160. panic(err)
  161. }
  162. }
  163. // defaults sets the default values of the builder before save.
  164. func (_c *PromoCodeCreate) defaults() {
  165. if _, ok := _c.mutation.BonusAmount(); !ok {
  166. v := promocode.DefaultBonusAmount
  167. _c.mutation.SetBonusAmount(v)
  168. }
  169. if _, ok := _c.mutation.MaxUses(); !ok {
  170. v := promocode.DefaultMaxUses
  171. _c.mutation.SetMaxUses(v)
  172. }
  173. if _, ok := _c.mutation.UsedCount(); !ok {
  174. v := promocode.DefaultUsedCount
  175. _c.mutation.SetUsedCount(v)
  176. }
  177. if _, ok := _c.mutation.Status(); !ok {
  178. v := promocode.DefaultStatus
  179. _c.mutation.SetStatus(v)
  180. }
  181. if _, ok := _c.mutation.CreatedAt(); !ok {
  182. v := promocode.DefaultCreatedAt()
  183. _c.mutation.SetCreatedAt(v)
  184. }
  185. if _, ok := _c.mutation.UpdatedAt(); !ok {
  186. v := promocode.DefaultUpdatedAt()
  187. _c.mutation.SetUpdatedAt(v)
  188. }
  189. }
  190. // check runs all checks and user-defined validators on the builder.
  191. func (_c *PromoCodeCreate) check() error {
  192. if _, ok := _c.mutation.Code(); !ok {
  193. return &ValidationError{Name: "code", err: errors.New(`ent: missing required field "PromoCode.code"`)}
  194. }
  195. if v, ok := _c.mutation.Code(); ok {
  196. if err := promocode.CodeValidator(v); err != nil {
  197. return &ValidationError{Name: "code", err: fmt.Errorf(`ent: validator failed for field "PromoCode.code": %w`, err)}
  198. }
  199. }
  200. if _, ok := _c.mutation.BonusAmount(); !ok {
  201. return &ValidationError{Name: "bonus_amount", err: errors.New(`ent: missing required field "PromoCode.bonus_amount"`)}
  202. }
  203. if _, ok := _c.mutation.MaxUses(); !ok {
  204. return &ValidationError{Name: "max_uses", err: errors.New(`ent: missing required field "PromoCode.max_uses"`)}
  205. }
  206. if _, ok := _c.mutation.UsedCount(); !ok {
  207. return &ValidationError{Name: "used_count", err: errors.New(`ent: missing required field "PromoCode.used_count"`)}
  208. }
  209. if _, ok := _c.mutation.Status(); !ok {
  210. return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "PromoCode.status"`)}
  211. }
  212. if v, ok := _c.mutation.Status(); ok {
  213. if err := promocode.StatusValidator(v); err != nil {
  214. return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "PromoCode.status": %w`, err)}
  215. }
  216. }
  217. if _, ok := _c.mutation.CreatedAt(); !ok {
  218. return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "PromoCode.created_at"`)}
  219. }
  220. if _, ok := _c.mutation.UpdatedAt(); !ok {
  221. return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "PromoCode.updated_at"`)}
  222. }
  223. return nil
  224. }
  225. func (_c *PromoCodeCreate) sqlSave(ctx context.Context) (*PromoCode, error) {
  226. if err := _c.check(); err != nil {
  227. return nil, err
  228. }
  229. _node, _spec := _c.createSpec()
  230. if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
  231. if sqlgraph.IsConstraintError(err) {
  232. err = &ConstraintError{msg: err.Error(), wrap: err}
  233. }
  234. return nil, err
  235. }
  236. id := _spec.ID.Value.(int64)
  237. _node.ID = int64(id)
  238. _c.mutation.id = &_node.ID
  239. _c.mutation.done = true
  240. return _node, nil
  241. }
  242. func (_c *PromoCodeCreate) createSpec() (*PromoCode, *sqlgraph.CreateSpec) {
  243. var (
  244. _node = &PromoCode{config: _c.config}
  245. _spec = sqlgraph.NewCreateSpec(promocode.Table, sqlgraph.NewFieldSpec(promocode.FieldID, field.TypeInt64))
  246. )
  247. _spec.OnConflict = _c.conflict
  248. if value, ok := _c.mutation.Code(); ok {
  249. _spec.SetField(promocode.FieldCode, field.TypeString, value)
  250. _node.Code = value
  251. }
  252. if value, ok := _c.mutation.BonusAmount(); ok {
  253. _spec.SetField(promocode.FieldBonusAmount, field.TypeFloat64, value)
  254. _node.BonusAmount = value
  255. }
  256. if value, ok := _c.mutation.MaxUses(); ok {
  257. _spec.SetField(promocode.FieldMaxUses, field.TypeInt, value)
  258. _node.MaxUses = value
  259. }
  260. if value, ok := _c.mutation.UsedCount(); ok {
  261. _spec.SetField(promocode.FieldUsedCount, field.TypeInt, value)
  262. _node.UsedCount = value
  263. }
  264. if value, ok := _c.mutation.Status(); ok {
  265. _spec.SetField(promocode.FieldStatus, field.TypeString, value)
  266. _node.Status = value
  267. }
  268. if value, ok := _c.mutation.ExpiresAt(); ok {
  269. _spec.SetField(promocode.FieldExpiresAt, field.TypeTime, value)
  270. _node.ExpiresAt = &value
  271. }
  272. if value, ok := _c.mutation.Notes(); ok {
  273. _spec.SetField(promocode.FieldNotes, field.TypeString, value)
  274. _node.Notes = &value
  275. }
  276. if value, ok := _c.mutation.CreatedAt(); ok {
  277. _spec.SetField(promocode.FieldCreatedAt, field.TypeTime, value)
  278. _node.CreatedAt = value
  279. }
  280. if value, ok := _c.mutation.UpdatedAt(); ok {
  281. _spec.SetField(promocode.FieldUpdatedAt, field.TypeTime, value)
  282. _node.UpdatedAt = value
  283. }
  284. if nodes := _c.mutation.UsageRecordsIDs(); len(nodes) > 0 {
  285. edge := &sqlgraph.EdgeSpec{
  286. Rel: sqlgraph.O2M,
  287. Inverse: false,
  288. Table: promocode.UsageRecordsTable,
  289. Columns: []string{promocode.UsageRecordsColumn},
  290. Bidi: false,
  291. Target: &sqlgraph.EdgeTarget{
  292. IDSpec: sqlgraph.NewFieldSpec(promocodeusage.FieldID, field.TypeInt64),
  293. },
  294. }
  295. for _, k := range nodes {
  296. edge.Target.Nodes = append(edge.Target.Nodes, k)
  297. }
  298. _spec.Edges = append(_spec.Edges, edge)
  299. }
  300. return _node, _spec
  301. }
  302. // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
  303. // of the `INSERT` statement. For example:
  304. //
  305. // client.PromoCode.Create().
  306. // SetCode(v).
  307. // OnConflict(
  308. // // Update the row with the new values
  309. // // the was proposed for insertion.
  310. // sql.ResolveWithNewValues(),
  311. // ).
  312. // // Override some of the fields with custom
  313. // // update values.
  314. // Update(func(u *ent.PromoCodeUpsert) {
  315. // SetCode(v+v).
  316. // }).
  317. // Exec(ctx)
  318. func (_c *PromoCodeCreate) OnConflict(opts ...sql.ConflictOption) *PromoCodeUpsertOne {
  319. _c.conflict = opts
  320. return &PromoCodeUpsertOne{
  321. create: _c,
  322. }
  323. }
  324. // OnConflictColumns calls `OnConflict` and configures the columns
  325. // as conflict target. Using this option is equivalent to using:
  326. //
  327. // client.PromoCode.Create().
  328. // OnConflict(sql.ConflictColumns(columns...)).
  329. // Exec(ctx)
  330. func (_c *PromoCodeCreate) OnConflictColumns(columns ...string) *PromoCodeUpsertOne {
  331. _c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
  332. return &PromoCodeUpsertOne{
  333. create: _c,
  334. }
  335. }
  336. type (
  337. // PromoCodeUpsertOne is the builder for "upsert"-ing
  338. // one PromoCode node.
  339. PromoCodeUpsertOne struct {
  340. create *PromoCodeCreate
  341. }
  342. // PromoCodeUpsert is the "OnConflict" setter.
  343. PromoCodeUpsert struct {
  344. *sql.UpdateSet
  345. }
  346. )
  347. // SetCode sets the "code" field.
  348. func (u *PromoCodeUpsert) SetCode(v string) *PromoCodeUpsert {
  349. u.Set(promocode.FieldCode, v)
  350. return u
  351. }
  352. // UpdateCode sets the "code" field to the value that was provided on create.
  353. func (u *PromoCodeUpsert) UpdateCode() *PromoCodeUpsert {
  354. u.SetExcluded(promocode.FieldCode)
  355. return u
  356. }
  357. // SetBonusAmount sets the "bonus_amount" field.
  358. func (u *PromoCodeUpsert) SetBonusAmount(v float64) *PromoCodeUpsert {
  359. u.Set(promocode.FieldBonusAmount, v)
  360. return u
  361. }
  362. // UpdateBonusAmount sets the "bonus_amount" field to the value that was provided on create.
  363. func (u *PromoCodeUpsert) UpdateBonusAmount() *PromoCodeUpsert {
  364. u.SetExcluded(promocode.FieldBonusAmount)
  365. return u
  366. }
  367. // AddBonusAmount adds v to the "bonus_amount" field.
  368. func (u *PromoCodeUpsert) AddBonusAmount(v float64) *PromoCodeUpsert {
  369. u.Add(promocode.FieldBonusAmount, v)
  370. return u
  371. }
  372. // SetMaxUses sets the "max_uses" field.
  373. func (u *PromoCodeUpsert) SetMaxUses(v int) *PromoCodeUpsert {
  374. u.Set(promocode.FieldMaxUses, v)
  375. return u
  376. }
  377. // UpdateMaxUses sets the "max_uses" field to the value that was provided on create.
  378. func (u *PromoCodeUpsert) UpdateMaxUses() *PromoCodeUpsert {
  379. u.SetExcluded(promocode.FieldMaxUses)
  380. return u
  381. }
  382. // AddMaxUses adds v to the "max_uses" field.
  383. func (u *PromoCodeUpsert) AddMaxUses(v int) *PromoCodeUpsert {
  384. u.Add(promocode.FieldMaxUses, v)
  385. return u
  386. }
  387. // SetUsedCount sets the "used_count" field.
  388. func (u *PromoCodeUpsert) SetUsedCount(v int) *PromoCodeUpsert {
  389. u.Set(promocode.FieldUsedCount, v)
  390. return u
  391. }
  392. // UpdateUsedCount sets the "used_count" field to the value that was provided on create.
  393. func (u *PromoCodeUpsert) UpdateUsedCount() *PromoCodeUpsert {
  394. u.SetExcluded(promocode.FieldUsedCount)
  395. return u
  396. }
  397. // AddUsedCount adds v to the "used_count" field.
  398. func (u *PromoCodeUpsert) AddUsedCount(v int) *PromoCodeUpsert {
  399. u.Add(promocode.FieldUsedCount, v)
  400. return u
  401. }
  402. // SetStatus sets the "status" field.
  403. func (u *PromoCodeUpsert) SetStatus(v string) *PromoCodeUpsert {
  404. u.Set(promocode.FieldStatus, v)
  405. return u
  406. }
  407. // UpdateStatus sets the "status" field to the value that was provided on create.
  408. func (u *PromoCodeUpsert) UpdateStatus() *PromoCodeUpsert {
  409. u.SetExcluded(promocode.FieldStatus)
  410. return u
  411. }
  412. // SetExpiresAt sets the "expires_at" field.
  413. func (u *PromoCodeUpsert) SetExpiresAt(v time.Time) *PromoCodeUpsert {
  414. u.Set(promocode.FieldExpiresAt, v)
  415. return u
  416. }
  417. // UpdateExpiresAt sets the "expires_at" field to the value that was provided on create.
  418. func (u *PromoCodeUpsert) UpdateExpiresAt() *PromoCodeUpsert {
  419. u.SetExcluded(promocode.FieldExpiresAt)
  420. return u
  421. }
  422. // ClearExpiresAt clears the value of the "expires_at" field.
  423. func (u *PromoCodeUpsert) ClearExpiresAt() *PromoCodeUpsert {
  424. u.SetNull(promocode.FieldExpiresAt)
  425. return u
  426. }
  427. // SetNotes sets the "notes" field.
  428. func (u *PromoCodeUpsert) SetNotes(v string) *PromoCodeUpsert {
  429. u.Set(promocode.FieldNotes, v)
  430. return u
  431. }
  432. // UpdateNotes sets the "notes" field to the value that was provided on create.
  433. func (u *PromoCodeUpsert) UpdateNotes() *PromoCodeUpsert {
  434. u.SetExcluded(promocode.FieldNotes)
  435. return u
  436. }
  437. // ClearNotes clears the value of the "notes" field.
  438. func (u *PromoCodeUpsert) ClearNotes() *PromoCodeUpsert {
  439. u.SetNull(promocode.FieldNotes)
  440. return u
  441. }
  442. // SetUpdatedAt sets the "updated_at" field.
  443. func (u *PromoCodeUpsert) SetUpdatedAt(v time.Time) *PromoCodeUpsert {
  444. u.Set(promocode.FieldUpdatedAt, v)
  445. return u
  446. }
  447. // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
  448. func (u *PromoCodeUpsert) UpdateUpdatedAt() *PromoCodeUpsert {
  449. u.SetExcluded(promocode.FieldUpdatedAt)
  450. return u
  451. }
  452. // UpdateNewValues updates the mutable fields using the new values that were set on create.
  453. // Using this option is equivalent to using:
  454. //
  455. // client.PromoCode.Create().
  456. // OnConflict(
  457. // sql.ResolveWithNewValues(),
  458. // ).
  459. // Exec(ctx)
  460. func (u *PromoCodeUpsertOne) UpdateNewValues() *PromoCodeUpsertOne {
  461. u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
  462. u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
  463. if _, exists := u.create.mutation.CreatedAt(); exists {
  464. s.SetIgnore(promocode.FieldCreatedAt)
  465. }
  466. }))
  467. return u
  468. }
  469. // Ignore sets each column to itself in case of conflict.
  470. // Using this option is equivalent to using:
  471. //
  472. // client.PromoCode.Create().
  473. // OnConflict(sql.ResolveWithIgnore()).
  474. // Exec(ctx)
  475. func (u *PromoCodeUpsertOne) Ignore() *PromoCodeUpsertOne {
  476. u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
  477. return u
  478. }
  479. // DoNothing configures the conflict_action to `DO NOTHING`.
  480. // Supported only by SQLite and PostgreSQL.
  481. func (u *PromoCodeUpsertOne) DoNothing() *PromoCodeUpsertOne {
  482. u.create.conflict = append(u.create.conflict, sql.DoNothing())
  483. return u
  484. }
  485. // Update allows overriding fields `UPDATE` values. See the PromoCodeCreate.OnConflict
  486. // documentation for more info.
  487. func (u *PromoCodeUpsertOne) Update(set func(*PromoCodeUpsert)) *PromoCodeUpsertOne {
  488. u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
  489. set(&PromoCodeUpsert{UpdateSet: update})
  490. }))
  491. return u
  492. }
  493. // SetCode sets the "code" field.
  494. func (u *PromoCodeUpsertOne) SetCode(v string) *PromoCodeUpsertOne {
  495. return u.Update(func(s *PromoCodeUpsert) {
  496. s.SetCode(v)
  497. })
  498. }
  499. // UpdateCode sets the "code" field to the value that was provided on create.
  500. func (u *PromoCodeUpsertOne) UpdateCode() *PromoCodeUpsertOne {
  501. return u.Update(func(s *PromoCodeUpsert) {
  502. s.UpdateCode()
  503. })
  504. }
  505. // SetBonusAmount sets the "bonus_amount" field.
  506. func (u *PromoCodeUpsertOne) SetBonusAmount(v float64) *PromoCodeUpsertOne {
  507. return u.Update(func(s *PromoCodeUpsert) {
  508. s.SetBonusAmount(v)
  509. })
  510. }
  511. // AddBonusAmount adds v to the "bonus_amount" field.
  512. func (u *PromoCodeUpsertOne) AddBonusAmount(v float64) *PromoCodeUpsertOne {
  513. return u.Update(func(s *PromoCodeUpsert) {
  514. s.AddBonusAmount(v)
  515. })
  516. }
  517. // UpdateBonusAmount sets the "bonus_amount" field to the value that was provided on create.
  518. func (u *PromoCodeUpsertOne) UpdateBonusAmount() *PromoCodeUpsertOne {
  519. return u.Update(func(s *PromoCodeUpsert) {
  520. s.UpdateBonusAmount()
  521. })
  522. }
  523. // SetMaxUses sets the "max_uses" field.
  524. func (u *PromoCodeUpsertOne) SetMaxUses(v int) *PromoCodeUpsertOne {
  525. return u.Update(func(s *PromoCodeUpsert) {
  526. s.SetMaxUses(v)
  527. })
  528. }
  529. // AddMaxUses adds v to the "max_uses" field.
  530. func (u *PromoCodeUpsertOne) AddMaxUses(v int) *PromoCodeUpsertOne {
  531. return u.Update(func(s *PromoCodeUpsert) {
  532. s.AddMaxUses(v)
  533. })
  534. }
  535. // UpdateMaxUses sets the "max_uses" field to the value that was provided on create.
  536. func (u *PromoCodeUpsertOne) UpdateMaxUses() *PromoCodeUpsertOne {
  537. return u.Update(func(s *PromoCodeUpsert) {
  538. s.UpdateMaxUses()
  539. })
  540. }
  541. // SetUsedCount sets the "used_count" field.
  542. func (u *PromoCodeUpsertOne) SetUsedCount(v int) *PromoCodeUpsertOne {
  543. return u.Update(func(s *PromoCodeUpsert) {
  544. s.SetUsedCount(v)
  545. })
  546. }
  547. // AddUsedCount adds v to the "used_count" field.
  548. func (u *PromoCodeUpsertOne) AddUsedCount(v int) *PromoCodeUpsertOne {
  549. return u.Update(func(s *PromoCodeUpsert) {
  550. s.AddUsedCount(v)
  551. })
  552. }
  553. // UpdateUsedCount sets the "used_count" field to the value that was provided on create.
  554. func (u *PromoCodeUpsertOne) UpdateUsedCount() *PromoCodeUpsertOne {
  555. return u.Update(func(s *PromoCodeUpsert) {
  556. s.UpdateUsedCount()
  557. })
  558. }
  559. // SetStatus sets the "status" field.
  560. func (u *PromoCodeUpsertOne) SetStatus(v string) *PromoCodeUpsertOne {
  561. return u.Update(func(s *PromoCodeUpsert) {
  562. s.SetStatus(v)
  563. })
  564. }
  565. // UpdateStatus sets the "status" field to the value that was provided on create.
  566. func (u *PromoCodeUpsertOne) UpdateStatus() *PromoCodeUpsertOne {
  567. return u.Update(func(s *PromoCodeUpsert) {
  568. s.UpdateStatus()
  569. })
  570. }
  571. // SetExpiresAt sets the "expires_at" field.
  572. func (u *PromoCodeUpsertOne) SetExpiresAt(v time.Time) *PromoCodeUpsertOne {
  573. return u.Update(func(s *PromoCodeUpsert) {
  574. s.SetExpiresAt(v)
  575. })
  576. }
  577. // UpdateExpiresAt sets the "expires_at" field to the value that was provided on create.
  578. func (u *PromoCodeUpsertOne) UpdateExpiresAt() *PromoCodeUpsertOne {
  579. return u.Update(func(s *PromoCodeUpsert) {
  580. s.UpdateExpiresAt()
  581. })
  582. }
  583. // ClearExpiresAt clears the value of the "expires_at" field.
  584. func (u *PromoCodeUpsertOne) ClearExpiresAt() *PromoCodeUpsertOne {
  585. return u.Update(func(s *PromoCodeUpsert) {
  586. s.ClearExpiresAt()
  587. })
  588. }
  589. // SetNotes sets the "notes" field.
  590. func (u *PromoCodeUpsertOne) SetNotes(v string) *PromoCodeUpsertOne {
  591. return u.Update(func(s *PromoCodeUpsert) {
  592. s.SetNotes(v)
  593. })
  594. }
  595. // UpdateNotes sets the "notes" field to the value that was provided on create.
  596. func (u *PromoCodeUpsertOne) UpdateNotes() *PromoCodeUpsertOne {
  597. return u.Update(func(s *PromoCodeUpsert) {
  598. s.UpdateNotes()
  599. })
  600. }
  601. // ClearNotes clears the value of the "notes" field.
  602. func (u *PromoCodeUpsertOne) ClearNotes() *PromoCodeUpsertOne {
  603. return u.Update(func(s *PromoCodeUpsert) {
  604. s.ClearNotes()
  605. })
  606. }
  607. // SetUpdatedAt sets the "updated_at" field.
  608. func (u *PromoCodeUpsertOne) SetUpdatedAt(v time.Time) *PromoCodeUpsertOne {
  609. return u.Update(func(s *PromoCodeUpsert) {
  610. s.SetUpdatedAt(v)
  611. })
  612. }
  613. // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
  614. func (u *PromoCodeUpsertOne) UpdateUpdatedAt() *PromoCodeUpsertOne {
  615. return u.Update(func(s *PromoCodeUpsert) {
  616. s.UpdateUpdatedAt()
  617. })
  618. }
  619. // Exec executes the query.
  620. func (u *PromoCodeUpsertOne) Exec(ctx context.Context) error {
  621. if len(u.create.conflict) == 0 {
  622. return errors.New("ent: missing options for PromoCodeCreate.OnConflict")
  623. }
  624. return u.create.Exec(ctx)
  625. }
  626. // ExecX is like Exec, but panics if an error occurs.
  627. func (u *PromoCodeUpsertOne) ExecX(ctx context.Context) {
  628. if err := u.create.Exec(ctx); err != nil {
  629. panic(err)
  630. }
  631. }
  632. // Exec executes the UPSERT query and returns the inserted/updated ID.
  633. func (u *PromoCodeUpsertOne) ID(ctx context.Context) (id int64, err error) {
  634. node, err := u.create.Save(ctx)
  635. if err != nil {
  636. return id, err
  637. }
  638. return node.ID, nil
  639. }
  640. // IDX is like ID, but panics if an error occurs.
  641. func (u *PromoCodeUpsertOne) IDX(ctx context.Context) int64 {
  642. id, err := u.ID(ctx)
  643. if err != nil {
  644. panic(err)
  645. }
  646. return id
  647. }
  648. // PromoCodeCreateBulk is the builder for creating many PromoCode entities in bulk.
  649. type PromoCodeCreateBulk struct {
  650. config
  651. err error
  652. builders []*PromoCodeCreate
  653. conflict []sql.ConflictOption
  654. }
  655. // Save creates the PromoCode entities in the database.
  656. func (_c *PromoCodeCreateBulk) Save(ctx context.Context) ([]*PromoCode, error) {
  657. if _c.err != nil {
  658. return nil, _c.err
  659. }
  660. specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
  661. nodes := make([]*PromoCode, len(_c.builders))
  662. mutators := make([]Mutator, len(_c.builders))
  663. for i := range _c.builders {
  664. func(i int, root context.Context) {
  665. builder := _c.builders[i]
  666. builder.defaults()
  667. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  668. mutation, ok := m.(*PromoCodeMutation)
  669. if !ok {
  670. return nil, fmt.Errorf("unexpected mutation type %T", m)
  671. }
  672. if err := builder.check(); err != nil {
  673. return nil, err
  674. }
  675. builder.mutation = mutation
  676. var err error
  677. nodes[i], specs[i] = builder.createSpec()
  678. if i < len(mutators)-1 {
  679. _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation)
  680. } else {
  681. spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
  682. spec.OnConflict = _c.conflict
  683. // Invoke the actual operation on the latest mutation in the chain.
  684. if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil {
  685. if sqlgraph.IsConstraintError(err) {
  686. err = &ConstraintError{msg: err.Error(), wrap: err}
  687. }
  688. }
  689. }
  690. if err != nil {
  691. return nil, err
  692. }
  693. mutation.id = &nodes[i].ID
  694. if specs[i].ID.Value != nil {
  695. id := specs[i].ID.Value.(int64)
  696. nodes[i].ID = int64(id)
  697. }
  698. mutation.done = true
  699. return nodes[i], nil
  700. })
  701. for i := len(builder.hooks) - 1; i >= 0; i-- {
  702. mut = builder.hooks[i](mut)
  703. }
  704. mutators[i] = mut
  705. }(i, ctx)
  706. }
  707. if len(mutators) > 0 {
  708. if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil {
  709. return nil, err
  710. }
  711. }
  712. return nodes, nil
  713. }
  714. // SaveX is like Save, but panics if an error occurs.
  715. func (_c *PromoCodeCreateBulk) SaveX(ctx context.Context) []*PromoCode {
  716. v, err := _c.Save(ctx)
  717. if err != nil {
  718. panic(err)
  719. }
  720. return v
  721. }
  722. // Exec executes the query.
  723. func (_c *PromoCodeCreateBulk) Exec(ctx context.Context) error {
  724. _, err := _c.Save(ctx)
  725. return err
  726. }
  727. // ExecX is like Exec, but panics if an error occurs.
  728. func (_c *PromoCodeCreateBulk) ExecX(ctx context.Context) {
  729. if err := _c.Exec(ctx); err != nil {
  730. panic(err)
  731. }
  732. }
  733. // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
  734. // of the `INSERT` statement. For example:
  735. //
  736. // client.PromoCode.CreateBulk(builders...).
  737. // OnConflict(
  738. // // Update the row with the new values
  739. // // the was proposed for insertion.
  740. // sql.ResolveWithNewValues(),
  741. // ).
  742. // // Override some of the fields with custom
  743. // // update values.
  744. // Update(func(u *ent.PromoCodeUpsert) {
  745. // SetCode(v+v).
  746. // }).
  747. // Exec(ctx)
  748. func (_c *PromoCodeCreateBulk) OnConflict(opts ...sql.ConflictOption) *PromoCodeUpsertBulk {
  749. _c.conflict = opts
  750. return &PromoCodeUpsertBulk{
  751. create: _c,
  752. }
  753. }
  754. // OnConflictColumns calls `OnConflict` and configures the columns
  755. // as conflict target. Using this option is equivalent to using:
  756. //
  757. // client.PromoCode.Create().
  758. // OnConflict(sql.ConflictColumns(columns...)).
  759. // Exec(ctx)
  760. func (_c *PromoCodeCreateBulk) OnConflictColumns(columns ...string) *PromoCodeUpsertBulk {
  761. _c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
  762. return &PromoCodeUpsertBulk{
  763. create: _c,
  764. }
  765. }
  766. // PromoCodeUpsertBulk is the builder for "upsert"-ing
  767. // a bulk of PromoCode nodes.
  768. type PromoCodeUpsertBulk struct {
  769. create *PromoCodeCreateBulk
  770. }
  771. // UpdateNewValues updates the mutable fields using the new values that
  772. // were set on create. Using this option is equivalent to using:
  773. //
  774. // client.PromoCode.Create().
  775. // OnConflict(
  776. // sql.ResolveWithNewValues(),
  777. // ).
  778. // Exec(ctx)
  779. func (u *PromoCodeUpsertBulk) UpdateNewValues() *PromoCodeUpsertBulk {
  780. u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
  781. u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
  782. for _, b := range u.create.builders {
  783. if _, exists := b.mutation.CreatedAt(); exists {
  784. s.SetIgnore(promocode.FieldCreatedAt)
  785. }
  786. }
  787. }))
  788. return u
  789. }
  790. // Ignore sets each column to itself in case of conflict.
  791. // Using this option is equivalent to using:
  792. //
  793. // client.PromoCode.Create().
  794. // OnConflict(sql.ResolveWithIgnore()).
  795. // Exec(ctx)
  796. func (u *PromoCodeUpsertBulk) Ignore() *PromoCodeUpsertBulk {
  797. u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
  798. return u
  799. }
  800. // DoNothing configures the conflict_action to `DO NOTHING`.
  801. // Supported only by SQLite and PostgreSQL.
  802. func (u *PromoCodeUpsertBulk) DoNothing() *PromoCodeUpsertBulk {
  803. u.create.conflict = append(u.create.conflict, sql.DoNothing())
  804. return u
  805. }
  806. // Update allows overriding fields `UPDATE` values. See the PromoCodeCreateBulk.OnConflict
  807. // documentation for more info.
  808. func (u *PromoCodeUpsertBulk) Update(set func(*PromoCodeUpsert)) *PromoCodeUpsertBulk {
  809. u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
  810. set(&PromoCodeUpsert{UpdateSet: update})
  811. }))
  812. return u
  813. }
  814. // SetCode sets the "code" field.
  815. func (u *PromoCodeUpsertBulk) SetCode(v string) *PromoCodeUpsertBulk {
  816. return u.Update(func(s *PromoCodeUpsert) {
  817. s.SetCode(v)
  818. })
  819. }
  820. // UpdateCode sets the "code" field to the value that was provided on create.
  821. func (u *PromoCodeUpsertBulk) UpdateCode() *PromoCodeUpsertBulk {
  822. return u.Update(func(s *PromoCodeUpsert) {
  823. s.UpdateCode()
  824. })
  825. }
  826. // SetBonusAmount sets the "bonus_amount" field.
  827. func (u *PromoCodeUpsertBulk) SetBonusAmount(v float64) *PromoCodeUpsertBulk {
  828. return u.Update(func(s *PromoCodeUpsert) {
  829. s.SetBonusAmount(v)
  830. })
  831. }
  832. // AddBonusAmount adds v to the "bonus_amount" field.
  833. func (u *PromoCodeUpsertBulk) AddBonusAmount(v float64) *PromoCodeUpsertBulk {
  834. return u.Update(func(s *PromoCodeUpsert) {
  835. s.AddBonusAmount(v)
  836. })
  837. }
  838. // UpdateBonusAmount sets the "bonus_amount" field to the value that was provided on create.
  839. func (u *PromoCodeUpsertBulk) UpdateBonusAmount() *PromoCodeUpsertBulk {
  840. return u.Update(func(s *PromoCodeUpsert) {
  841. s.UpdateBonusAmount()
  842. })
  843. }
  844. // SetMaxUses sets the "max_uses" field.
  845. func (u *PromoCodeUpsertBulk) SetMaxUses(v int) *PromoCodeUpsertBulk {
  846. return u.Update(func(s *PromoCodeUpsert) {
  847. s.SetMaxUses(v)
  848. })
  849. }
  850. // AddMaxUses adds v to the "max_uses" field.
  851. func (u *PromoCodeUpsertBulk) AddMaxUses(v int) *PromoCodeUpsertBulk {
  852. return u.Update(func(s *PromoCodeUpsert) {
  853. s.AddMaxUses(v)
  854. })
  855. }
  856. // UpdateMaxUses sets the "max_uses" field to the value that was provided on create.
  857. func (u *PromoCodeUpsertBulk) UpdateMaxUses() *PromoCodeUpsertBulk {
  858. return u.Update(func(s *PromoCodeUpsert) {
  859. s.UpdateMaxUses()
  860. })
  861. }
  862. // SetUsedCount sets the "used_count" field.
  863. func (u *PromoCodeUpsertBulk) SetUsedCount(v int) *PromoCodeUpsertBulk {
  864. return u.Update(func(s *PromoCodeUpsert) {
  865. s.SetUsedCount(v)
  866. })
  867. }
  868. // AddUsedCount adds v to the "used_count" field.
  869. func (u *PromoCodeUpsertBulk) AddUsedCount(v int) *PromoCodeUpsertBulk {
  870. return u.Update(func(s *PromoCodeUpsert) {
  871. s.AddUsedCount(v)
  872. })
  873. }
  874. // UpdateUsedCount sets the "used_count" field to the value that was provided on create.
  875. func (u *PromoCodeUpsertBulk) UpdateUsedCount() *PromoCodeUpsertBulk {
  876. return u.Update(func(s *PromoCodeUpsert) {
  877. s.UpdateUsedCount()
  878. })
  879. }
  880. // SetStatus sets the "status" field.
  881. func (u *PromoCodeUpsertBulk) SetStatus(v string) *PromoCodeUpsertBulk {
  882. return u.Update(func(s *PromoCodeUpsert) {
  883. s.SetStatus(v)
  884. })
  885. }
  886. // UpdateStatus sets the "status" field to the value that was provided on create.
  887. func (u *PromoCodeUpsertBulk) UpdateStatus() *PromoCodeUpsertBulk {
  888. return u.Update(func(s *PromoCodeUpsert) {
  889. s.UpdateStatus()
  890. })
  891. }
  892. // SetExpiresAt sets the "expires_at" field.
  893. func (u *PromoCodeUpsertBulk) SetExpiresAt(v time.Time) *PromoCodeUpsertBulk {
  894. return u.Update(func(s *PromoCodeUpsert) {
  895. s.SetExpiresAt(v)
  896. })
  897. }
  898. // UpdateExpiresAt sets the "expires_at" field to the value that was provided on create.
  899. func (u *PromoCodeUpsertBulk) UpdateExpiresAt() *PromoCodeUpsertBulk {
  900. return u.Update(func(s *PromoCodeUpsert) {
  901. s.UpdateExpiresAt()
  902. })
  903. }
  904. // ClearExpiresAt clears the value of the "expires_at" field.
  905. func (u *PromoCodeUpsertBulk) ClearExpiresAt() *PromoCodeUpsertBulk {
  906. return u.Update(func(s *PromoCodeUpsert) {
  907. s.ClearExpiresAt()
  908. })
  909. }
  910. // SetNotes sets the "notes" field.
  911. func (u *PromoCodeUpsertBulk) SetNotes(v string) *PromoCodeUpsertBulk {
  912. return u.Update(func(s *PromoCodeUpsert) {
  913. s.SetNotes(v)
  914. })
  915. }
  916. // UpdateNotes sets the "notes" field to the value that was provided on create.
  917. func (u *PromoCodeUpsertBulk) UpdateNotes() *PromoCodeUpsertBulk {
  918. return u.Update(func(s *PromoCodeUpsert) {
  919. s.UpdateNotes()
  920. })
  921. }
  922. // ClearNotes clears the value of the "notes" field.
  923. func (u *PromoCodeUpsertBulk) ClearNotes() *PromoCodeUpsertBulk {
  924. return u.Update(func(s *PromoCodeUpsert) {
  925. s.ClearNotes()
  926. })
  927. }
  928. // SetUpdatedAt sets the "updated_at" field.
  929. func (u *PromoCodeUpsertBulk) SetUpdatedAt(v time.Time) *PromoCodeUpsertBulk {
  930. return u.Update(func(s *PromoCodeUpsert) {
  931. s.SetUpdatedAt(v)
  932. })
  933. }
  934. // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
  935. func (u *PromoCodeUpsertBulk) UpdateUpdatedAt() *PromoCodeUpsertBulk {
  936. return u.Update(func(s *PromoCodeUpsert) {
  937. s.UpdateUpdatedAt()
  938. })
  939. }
  940. // Exec executes the query.
  941. func (u *PromoCodeUpsertBulk) Exec(ctx context.Context) error {
  942. if u.create.err != nil {
  943. return u.create.err
  944. }
  945. for i, b := range u.create.builders {
  946. if len(b.conflict) != 0 {
  947. return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the PromoCodeCreateBulk instead", i)
  948. }
  949. }
  950. if len(u.create.conflict) == 0 {
  951. return errors.New("ent: missing options for PromoCodeCreateBulk.OnConflict")
  952. }
  953. return u.create.Exec(ctx)
  954. }
  955. // ExecX is like Exec, but panics if an error occurs.
  956. func (u *PromoCodeUpsertBulk) ExecX(ctx context.Context) {
  957. if err := u.create.Exec(ctx); err != nil {
  958. panic(err)
  959. }
  960. }