subscriptionplan_update.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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/subscriptionplan"
  13. )
  14. // SubscriptionPlanUpdate is the builder for updating SubscriptionPlan entities.
  15. type SubscriptionPlanUpdate struct {
  16. config
  17. hooks []Hook
  18. mutation *SubscriptionPlanMutation
  19. }
  20. // Where appends a list predicates to the SubscriptionPlanUpdate builder.
  21. func (_u *SubscriptionPlanUpdate) Where(ps ...predicate.SubscriptionPlan) *SubscriptionPlanUpdate {
  22. _u.mutation.Where(ps...)
  23. return _u
  24. }
  25. // SetGroupID sets the "group_id" field.
  26. func (_u *SubscriptionPlanUpdate) SetGroupID(v int64) *SubscriptionPlanUpdate {
  27. _u.mutation.ResetGroupID()
  28. _u.mutation.SetGroupID(v)
  29. return _u
  30. }
  31. // SetNillableGroupID sets the "group_id" field if the given value is not nil.
  32. func (_u *SubscriptionPlanUpdate) SetNillableGroupID(v *int64) *SubscriptionPlanUpdate {
  33. if v != nil {
  34. _u.SetGroupID(*v)
  35. }
  36. return _u
  37. }
  38. // AddGroupID adds value to the "group_id" field.
  39. func (_u *SubscriptionPlanUpdate) AddGroupID(v int64) *SubscriptionPlanUpdate {
  40. _u.mutation.AddGroupID(v)
  41. return _u
  42. }
  43. // SetName sets the "name" field.
  44. func (_u *SubscriptionPlanUpdate) SetName(v string) *SubscriptionPlanUpdate {
  45. _u.mutation.SetName(v)
  46. return _u
  47. }
  48. // SetNillableName sets the "name" field if the given value is not nil.
  49. func (_u *SubscriptionPlanUpdate) SetNillableName(v *string) *SubscriptionPlanUpdate {
  50. if v != nil {
  51. _u.SetName(*v)
  52. }
  53. return _u
  54. }
  55. // SetDescription sets the "description" field.
  56. func (_u *SubscriptionPlanUpdate) SetDescription(v string) *SubscriptionPlanUpdate {
  57. _u.mutation.SetDescription(v)
  58. return _u
  59. }
  60. // SetNillableDescription sets the "description" field if the given value is not nil.
  61. func (_u *SubscriptionPlanUpdate) SetNillableDescription(v *string) *SubscriptionPlanUpdate {
  62. if v != nil {
  63. _u.SetDescription(*v)
  64. }
  65. return _u
  66. }
  67. // SetPrice sets the "price" field.
  68. func (_u *SubscriptionPlanUpdate) SetPrice(v float64) *SubscriptionPlanUpdate {
  69. _u.mutation.ResetPrice()
  70. _u.mutation.SetPrice(v)
  71. return _u
  72. }
  73. // SetNillablePrice sets the "price" field if the given value is not nil.
  74. func (_u *SubscriptionPlanUpdate) SetNillablePrice(v *float64) *SubscriptionPlanUpdate {
  75. if v != nil {
  76. _u.SetPrice(*v)
  77. }
  78. return _u
  79. }
  80. // AddPrice adds value to the "price" field.
  81. func (_u *SubscriptionPlanUpdate) AddPrice(v float64) *SubscriptionPlanUpdate {
  82. _u.mutation.AddPrice(v)
  83. return _u
  84. }
  85. // SetOriginalPrice sets the "original_price" field.
  86. func (_u *SubscriptionPlanUpdate) SetOriginalPrice(v float64) *SubscriptionPlanUpdate {
  87. _u.mutation.ResetOriginalPrice()
  88. _u.mutation.SetOriginalPrice(v)
  89. return _u
  90. }
  91. // SetNillableOriginalPrice sets the "original_price" field if the given value is not nil.
  92. func (_u *SubscriptionPlanUpdate) SetNillableOriginalPrice(v *float64) *SubscriptionPlanUpdate {
  93. if v != nil {
  94. _u.SetOriginalPrice(*v)
  95. }
  96. return _u
  97. }
  98. // AddOriginalPrice adds value to the "original_price" field.
  99. func (_u *SubscriptionPlanUpdate) AddOriginalPrice(v float64) *SubscriptionPlanUpdate {
  100. _u.mutation.AddOriginalPrice(v)
  101. return _u
  102. }
  103. // ClearOriginalPrice clears the value of the "original_price" field.
  104. func (_u *SubscriptionPlanUpdate) ClearOriginalPrice() *SubscriptionPlanUpdate {
  105. _u.mutation.ClearOriginalPrice()
  106. return _u
  107. }
  108. // SetCurrency sets the "currency" field.
  109. func (_u *SubscriptionPlanUpdate) SetCurrency(v string) *SubscriptionPlanUpdate {
  110. _u.mutation.SetCurrency(v)
  111. return _u
  112. }
  113. // SetNillableCurrency sets the "currency" field if the given value is not nil.
  114. func (_u *SubscriptionPlanUpdate) SetNillableCurrency(v *string) *SubscriptionPlanUpdate {
  115. if v != nil {
  116. _u.SetCurrency(*v)
  117. }
  118. return _u
  119. }
  120. // SetValidityDays sets the "validity_days" field.
  121. func (_u *SubscriptionPlanUpdate) SetValidityDays(v int) *SubscriptionPlanUpdate {
  122. _u.mutation.ResetValidityDays()
  123. _u.mutation.SetValidityDays(v)
  124. return _u
  125. }
  126. // SetNillableValidityDays sets the "validity_days" field if the given value is not nil.
  127. func (_u *SubscriptionPlanUpdate) SetNillableValidityDays(v *int) *SubscriptionPlanUpdate {
  128. if v != nil {
  129. _u.SetValidityDays(*v)
  130. }
  131. return _u
  132. }
  133. // AddValidityDays adds value to the "validity_days" field.
  134. func (_u *SubscriptionPlanUpdate) AddValidityDays(v int) *SubscriptionPlanUpdate {
  135. _u.mutation.AddValidityDays(v)
  136. return _u
  137. }
  138. // SetValidityUnit sets the "validity_unit" field.
  139. func (_u *SubscriptionPlanUpdate) SetValidityUnit(v string) *SubscriptionPlanUpdate {
  140. _u.mutation.SetValidityUnit(v)
  141. return _u
  142. }
  143. // SetNillableValidityUnit sets the "validity_unit" field if the given value is not nil.
  144. func (_u *SubscriptionPlanUpdate) SetNillableValidityUnit(v *string) *SubscriptionPlanUpdate {
  145. if v != nil {
  146. _u.SetValidityUnit(*v)
  147. }
  148. return _u
  149. }
  150. // SetFeatures sets the "features" field.
  151. func (_u *SubscriptionPlanUpdate) SetFeatures(v string) *SubscriptionPlanUpdate {
  152. _u.mutation.SetFeatures(v)
  153. return _u
  154. }
  155. // SetNillableFeatures sets the "features" field if the given value is not nil.
  156. func (_u *SubscriptionPlanUpdate) SetNillableFeatures(v *string) *SubscriptionPlanUpdate {
  157. if v != nil {
  158. _u.SetFeatures(*v)
  159. }
  160. return _u
  161. }
  162. // SetProductName sets the "product_name" field.
  163. func (_u *SubscriptionPlanUpdate) SetProductName(v string) *SubscriptionPlanUpdate {
  164. _u.mutation.SetProductName(v)
  165. return _u
  166. }
  167. // SetNillableProductName sets the "product_name" field if the given value is not nil.
  168. func (_u *SubscriptionPlanUpdate) SetNillableProductName(v *string) *SubscriptionPlanUpdate {
  169. if v != nil {
  170. _u.SetProductName(*v)
  171. }
  172. return _u
  173. }
  174. // SetForSale sets the "for_sale" field.
  175. func (_u *SubscriptionPlanUpdate) SetForSale(v bool) *SubscriptionPlanUpdate {
  176. _u.mutation.SetForSale(v)
  177. return _u
  178. }
  179. // SetNillableForSale sets the "for_sale" field if the given value is not nil.
  180. func (_u *SubscriptionPlanUpdate) SetNillableForSale(v *bool) *SubscriptionPlanUpdate {
  181. if v != nil {
  182. _u.SetForSale(*v)
  183. }
  184. return _u
  185. }
  186. // SetSortOrder sets the "sort_order" field.
  187. func (_u *SubscriptionPlanUpdate) SetSortOrder(v int) *SubscriptionPlanUpdate {
  188. _u.mutation.ResetSortOrder()
  189. _u.mutation.SetSortOrder(v)
  190. return _u
  191. }
  192. // SetNillableSortOrder sets the "sort_order" field if the given value is not nil.
  193. func (_u *SubscriptionPlanUpdate) SetNillableSortOrder(v *int) *SubscriptionPlanUpdate {
  194. if v != nil {
  195. _u.SetSortOrder(*v)
  196. }
  197. return _u
  198. }
  199. // AddSortOrder adds value to the "sort_order" field.
  200. func (_u *SubscriptionPlanUpdate) AddSortOrder(v int) *SubscriptionPlanUpdate {
  201. _u.mutation.AddSortOrder(v)
  202. return _u
  203. }
  204. // SetUpdatedAt sets the "updated_at" field.
  205. func (_u *SubscriptionPlanUpdate) SetUpdatedAt(v time.Time) *SubscriptionPlanUpdate {
  206. _u.mutation.SetUpdatedAt(v)
  207. return _u
  208. }
  209. // Mutation returns the SubscriptionPlanMutation object of the builder.
  210. func (_u *SubscriptionPlanUpdate) Mutation() *SubscriptionPlanMutation {
  211. return _u.mutation
  212. }
  213. // Save executes the query and returns the number of nodes affected by the update operation.
  214. func (_u *SubscriptionPlanUpdate) Save(ctx context.Context) (int, error) {
  215. _u.defaults()
  216. return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
  217. }
  218. // SaveX is like Save, but panics if an error occurs.
  219. func (_u *SubscriptionPlanUpdate) SaveX(ctx context.Context) int {
  220. affected, err := _u.Save(ctx)
  221. if err != nil {
  222. panic(err)
  223. }
  224. return affected
  225. }
  226. // Exec executes the query.
  227. func (_u *SubscriptionPlanUpdate) Exec(ctx context.Context) error {
  228. _, err := _u.Save(ctx)
  229. return err
  230. }
  231. // ExecX is like Exec, but panics if an error occurs.
  232. func (_u *SubscriptionPlanUpdate) ExecX(ctx context.Context) {
  233. if err := _u.Exec(ctx); err != nil {
  234. panic(err)
  235. }
  236. }
  237. // defaults sets the default values of the builder before save.
  238. func (_u *SubscriptionPlanUpdate) defaults() {
  239. if _, ok := _u.mutation.UpdatedAt(); !ok {
  240. v := subscriptionplan.UpdateDefaultUpdatedAt()
  241. _u.mutation.SetUpdatedAt(v)
  242. }
  243. }
  244. // check runs all checks and user-defined validators on the builder.
  245. func (_u *SubscriptionPlanUpdate) check() error {
  246. if v, ok := _u.mutation.Name(); ok {
  247. if err := subscriptionplan.NameValidator(v); err != nil {
  248. return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "SubscriptionPlan.name": %w`, err)}
  249. }
  250. }
  251. if v, ok := _u.mutation.Currency(); ok {
  252. if err := subscriptionplan.CurrencyValidator(v); err != nil {
  253. return &ValidationError{Name: "currency", err: fmt.Errorf(`ent: validator failed for field "SubscriptionPlan.currency": %w`, err)}
  254. }
  255. }
  256. if v, ok := _u.mutation.ValidityUnit(); ok {
  257. if err := subscriptionplan.ValidityUnitValidator(v); err != nil {
  258. return &ValidationError{Name: "validity_unit", err: fmt.Errorf(`ent: validator failed for field "SubscriptionPlan.validity_unit": %w`, err)}
  259. }
  260. }
  261. if v, ok := _u.mutation.ProductName(); ok {
  262. if err := subscriptionplan.ProductNameValidator(v); err != nil {
  263. return &ValidationError{Name: "product_name", err: fmt.Errorf(`ent: validator failed for field "SubscriptionPlan.product_name": %w`, err)}
  264. }
  265. }
  266. return nil
  267. }
  268. func (_u *SubscriptionPlanUpdate) sqlSave(ctx context.Context) (_node int, err error) {
  269. if err := _u.check(); err != nil {
  270. return _node, err
  271. }
  272. _spec := sqlgraph.NewUpdateSpec(subscriptionplan.Table, subscriptionplan.Columns, sqlgraph.NewFieldSpec(subscriptionplan.FieldID, field.TypeInt64))
  273. if ps := _u.mutation.predicates; len(ps) > 0 {
  274. _spec.Predicate = func(selector *sql.Selector) {
  275. for i := range ps {
  276. ps[i](selector)
  277. }
  278. }
  279. }
  280. if value, ok := _u.mutation.GroupID(); ok {
  281. _spec.SetField(subscriptionplan.FieldGroupID, field.TypeInt64, value)
  282. }
  283. if value, ok := _u.mutation.AddedGroupID(); ok {
  284. _spec.AddField(subscriptionplan.FieldGroupID, field.TypeInt64, value)
  285. }
  286. if value, ok := _u.mutation.Name(); ok {
  287. _spec.SetField(subscriptionplan.FieldName, field.TypeString, value)
  288. }
  289. if value, ok := _u.mutation.Description(); ok {
  290. _spec.SetField(subscriptionplan.FieldDescription, field.TypeString, value)
  291. }
  292. if value, ok := _u.mutation.Price(); ok {
  293. _spec.SetField(subscriptionplan.FieldPrice, field.TypeFloat64, value)
  294. }
  295. if value, ok := _u.mutation.AddedPrice(); ok {
  296. _spec.AddField(subscriptionplan.FieldPrice, field.TypeFloat64, value)
  297. }
  298. if value, ok := _u.mutation.OriginalPrice(); ok {
  299. _spec.SetField(subscriptionplan.FieldOriginalPrice, field.TypeFloat64, value)
  300. }
  301. if value, ok := _u.mutation.AddedOriginalPrice(); ok {
  302. _spec.AddField(subscriptionplan.FieldOriginalPrice, field.TypeFloat64, value)
  303. }
  304. if _u.mutation.OriginalPriceCleared() {
  305. _spec.ClearField(subscriptionplan.FieldOriginalPrice, field.TypeFloat64)
  306. }
  307. if value, ok := _u.mutation.Currency(); ok {
  308. _spec.SetField(subscriptionplan.FieldCurrency, field.TypeString, value)
  309. }
  310. if value, ok := _u.mutation.ValidityDays(); ok {
  311. _spec.SetField(subscriptionplan.FieldValidityDays, field.TypeInt, value)
  312. }
  313. if value, ok := _u.mutation.AddedValidityDays(); ok {
  314. _spec.AddField(subscriptionplan.FieldValidityDays, field.TypeInt, value)
  315. }
  316. if value, ok := _u.mutation.ValidityUnit(); ok {
  317. _spec.SetField(subscriptionplan.FieldValidityUnit, field.TypeString, value)
  318. }
  319. if value, ok := _u.mutation.Features(); ok {
  320. _spec.SetField(subscriptionplan.FieldFeatures, field.TypeString, value)
  321. }
  322. if value, ok := _u.mutation.ProductName(); ok {
  323. _spec.SetField(subscriptionplan.FieldProductName, field.TypeString, value)
  324. }
  325. if value, ok := _u.mutation.ForSale(); ok {
  326. _spec.SetField(subscriptionplan.FieldForSale, field.TypeBool, value)
  327. }
  328. if value, ok := _u.mutation.SortOrder(); ok {
  329. _spec.SetField(subscriptionplan.FieldSortOrder, field.TypeInt, value)
  330. }
  331. if value, ok := _u.mutation.AddedSortOrder(); ok {
  332. _spec.AddField(subscriptionplan.FieldSortOrder, field.TypeInt, value)
  333. }
  334. if value, ok := _u.mutation.UpdatedAt(); ok {
  335. _spec.SetField(subscriptionplan.FieldUpdatedAt, field.TypeTime, value)
  336. }
  337. if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
  338. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  339. err = &NotFoundError{subscriptionplan.Label}
  340. } else if sqlgraph.IsConstraintError(err) {
  341. err = &ConstraintError{msg: err.Error(), wrap: err}
  342. }
  343. return 0, err
  344. }
  345. _u.mutation.done = true
  346. return _node, nil
  347. }
  348. // SubscriptionPlanUpdateOne is the builder for updating a single SubscriptionPlan entity.
  349. type SubscriptionPlanUpdateOne struct {
  350. config
  351. fields []string
  352. hooks []Hook
  353. mutation *SubscriptionPlanMutation
  354. }
  355. // SetGroupID sets the "group_id" field.
  356. func (_u *SubscriptionPlanUpdateOne) SetGroupID(v int64) *SubscriptionPlanUpdateOne {
  357. _u.mutation.ResetGroupID()
  358. _u.mutation.SetGroupID(v)
  359. return _u
  360. }
  361. // SetNillableGroupID sets the "group_id" field if the given value is not nil.
  362. func (_u *SubscriptionPlanUpdateOne) SetNillableGroupID(v *int64) *SubscriptionPlanUpdateOne {
  363. if v != nil {
  364. _u.SetGroupID(*v)
  365. }
  366. return _u
  367. }
  368. // AddGroupID adds value to the "group_id" field.
  369. func (_u *SubscriptionPlanUpdateOne) AddGroupID(v int64) *SubscriptionPlanUpdateOne {
  370. _u.mutation.AddGroupID(v)
  371. return _u
  372. }
  373. // SetName sets the "name" field.
  374. func (_u *SubscriptionPlanUpdateOne) SetName(v string) *SubscriptionPlanUpdateOne {
  375. _u.mutation.SetName(v)
  376. return _u
  377. }
  378. // SetNillableName sets the "name" field if the given value is not nil.
  379. func (_u *SubscriptionPlanUpdateOne) SetNillableName(v *string) *SubscriptionPlanUpdateOne {
  380. if v != nil {
  381. _u.SetName(*v)
  382. }
  383. return _u
  384. }
  385. // SetDescription sets the "description" field.
  386. func (_u *SubscriptionPlanUpdateOne) SetDescription(v string) *SubscriptionPlanUpdateOne {
  387. _u.mutation.SetDescription(v)
  388. return _u
  389. }
  390. // SetNillableDescription sets the "description" field if the given value is not nil.
  391. func (_u *SubscriptionPlanUpdateOne) SetNillableDescription(v *string) *SubscriptionPlanUpdateOne {
  392. if v != nil {
  393. _u.SetDescription(*v)
  394. }
  395. return _u
  396. }
  397. // SetPrice sets the "price" field.
  398. func (_u *SubscriptionPlanUpdateOne) SetPrice(v float64) *SubscriptionPlanUpdateOne {
  399. _u.mutation.ResetPrice()
  400. _u.mutation.SetPrice(v)
  401. return _u
  402. }
  403. // SetNillablePrice sets the "price" field if the given value is not nil.
  404. func (_u *SubscriptionPlanUpdateOne) SetNillablePrice(v *float64) *SubscriptionPlanUpdateOne {
  405. if v != nil {
  406. _u.SetPrice(*v)
  407. }
  408. return _u
  409. }
  410. // AddPrice adds value to the "price" field.
  411. func (_u *SubscriptionPlanUpdateOne) AddPrice(v float64) *SubscriptionPlanUpdateOne {
  412. _u.mutation.AddPrice(v)
  413. return _u
  414. }
  415. // SetOriginalPrice sets the "original_price" field.
  416. func (_u *SubscriptionPlanUpdateOne) SetOriginalPrice(v float64) *SubscriptionPlanUpdateOne {
  417. _u.mutation.ResetOriginalPrice()
  418. _u.mutation.SetOriginalPrice(v)
  419. return _u
  420. }
  421. // SetNillableOriginalPrice sets the "original_price" field if the given value is not nil.
  422. func (_u *SubscriptionPlanUpdateOne) SetNillableOriginalPrice(v *float64) *SubscriptionPlanUpdateOne {
  423. if v != nil {
  424. _u.SetOriginalPrice(*v)
  425. }
  426. return _u
  427. }
  428. // AddOriginalPrice adds value to the "original_price" field.
  429. func (_u *SubscriptionPlanUpdateOne) AddOriginalPrice(v float64) *SubscriptionPlanUpdateOne {
  430. _u.mutation.AddOriginalPrice(v)
  431. return _u
  432. }
  433. // ClearOriginalPrice clears the value of the "original_price" field.
  434. func (_u *SubscriptionPlanUpdateOne) ClearOriginalPrice() *SubscriptionPlanUpdateOne {
  435. _u.mutation.ClearOriginalPrice()
  436. return _u
  437. }
  438. // SetCurrency sets the "currency" field.
  439. func (_u *SubscriptionPlanUpdateOne) SetCurrency(v string) *SubscriptionPlanUpdateOne {
  440. _u.mutation.SetCurrency(v)
  441. return _u
  442. }
  443. // SetNillableCurrency sets the "currency" field if the given value is not nil.
  444. func (_u *SubscriptionPlanUpdateOne) SetNillableCurrency(v *string) *SubscriptionPlanUpdateOne {
  445. if v != nil {
  446. _u.SetCurrency(*v)
  447. }
  448. return _u
  449. }
  450. // SetValidityDays sets the "validity_days" field.
  451. func (_u *SubscriptionPlanUpdateOne) SetValidityDays(v int) *SubscriptionPlanUpdateOne {
  452. _u.mutation.ResetValidityDays()
  453. _u.mutation.SetValidityDays(v)
  454. return _u
  455. }
  456. // SetNillableValidityDays sets the "validity_days" field if the given value is not nil.
  457. func (_u *SubscriptionPlanUpdateOne) SetNillableValidityDays(v *int) *SubscriptionPlanUpdateOne {
  458. if v != nil {
  459. _u.SetValidityDays(*v)
  460. }
  461. return _u
  462. }
  463. // AddValidityDays adds value to the "validity_days" field.
  464. func (_u *SubscriptionPlanUpdateOne) AddValidityDays(v int) *SubscriptionPlanUpdateOne {
  465. _u.mutation.AddValidityDays(v)
  466. return _u
  467. }
  468. // SetValidityUnit sets the "validity_unit" field.
  469. func (_u *SubscriptionPlanUpdateOne) SetValidityUnit(v string) *SubscriptionPlanUpdateOne {
  470. _u.mutation.SetValidityUnit(v)
  471. return _u
  472. }
  473. // SetNillableValidityUnit sets the "validity_unit" field if the given value is not nil.
  474. func (_u *SubscriptionPlanUpdateOne) SetNillableValidityUnit(v *string) *SubscriptionPlanUpdateOne {
  475. if v != nil {
  476. _u.SetValidityUnit(*v)
  477. }
  478. return _u
  479. }
  480. // SetFeatures sets the "features" field.
  481. func (_u *SubscriptionPlanUpdateOne) SetFeatures(v string) *SubscriptionPlanUpdateOne {
  482. _u.mutation.SetFeatures(v)
  483. return _u
  484. }
  485. // SetNillableFeatures sets the "features" field if the given value is not nil.
  486. func (_u *SubscriptionPlanUpdateOne) SetNillableFeatures(v *string) *SubscriptionPlanUpdateOne {
  487. if v != nil {
  488. _u.SetFeatures(*v)
  489. }
  490. return _u
  491. }
  492. // SetProductName sets the "product_name" field.
  493. func (_u *SubscriptionPlanUpdateOne) SetProductName(v string) *SubscriptionPlanUpdateOne {
  494. _u.mutation.SetProductName(v)
  495. return _u
  496. }
  497. // SetNillableProductName sets the "product_name" field if the given value is not nil.
  498. func (_u *SubscriptionPlanUpdateOne) SetNillableProductName(v *string) *SubscriptionPlanUpdateOne {
  499. if v != nil {
  500. _u.SetProductName(*v)
  501. }
  502. return _u
  503. }
  504. // SetForSale sets the "for_sale" field.
  505. func (_u *SubscriptionPlanUpdateOne) SetForSale(v bool) *SubscriptionPlanUpdateOne {
  506. _u.mutation.SetForSale(v)
  507. return _u
  508. }
  509. // SetNillableForSale sets the "for_sale" field if the given value is not nil.
  510. func (_u *SubscriptionPlanUpdateOne) SetNillableForSale(v *bool) *SubscriptionPlanUpdateOne {
  511. if v != nil {
  512. _u.SetForSale(*v)
  513. }
  514. return _u
  515. }
  516. // SetSortOrder sets the "sort_order" field.
  517. func (_u *SubscriptionPlanUpdateOne) SetSortOrder(v int) *SubscriptionPlanUpdateOne {
  518. _u.mutation.ResetSortOrder()
  519. _u.mutation.SetSortOrder(v)
  520. return _u
  521. }
  522. // SetNillableSortOrder sets the "sort_order" field if the given value is not nil.
  523. func (_u *SubscriptionPlanUpdateOne) SetNillableSortOrder(v *int) *SubscriptionPlanUpdateOne {
  524. if v != nil {
  525. _u.SetSortOrder(*v)
  526. }
  527. return _u
  528. }
  529. // AddSortOrder adds value to the "sort_order" field.
  530. func (_u *SubscriptionPlanUpdateOne) AddSortOrder(v int) *SubscriptionPlanUpdateOne {
  531. _u.mutation.AddSortOrder(v)
  532. return _u
  533. }
  534. // SetUpdatedAt sets the "updated_at" field.
  535. func (_u *SubscriptionPlanUpdateOne) SetUpdatedAt(v time.Time) *SubscriptionPlanUpdateOne {
  536. _u.mutation.SetUpdatedAt(v)
  537. return _u
  538. }
  539. // Mutation returns the SubscriptionPlanMutation object of the builder.
  540. func (_u *SubscriptionPlanUpdateOne) Mutation() *SubscriptionPlanMutation {
  541. return _u.mutation
  542. }
  543. // Where appends a list predicates to the SubscriptionPlanUpdate builder.
  544. func (_u *SubscriptionPlanUpdateOne) Where(ps ...predicate.SubscriptionPlan) *SubscriptionPlanUpdateOne {
  545. _u.mutation.Where(ps...)
  546. return _u
  547. }
  548. // Select allows selecting one or more fields (columns) of the returned entity.
  549. // The default is selecting all fields defined in the entity schema.
  550. func (_u *SubscriptionPlanUpdateOne) Select(field string, fields ...string) *SubscriptionPlanUpdateOne {
  551. _u.fields = append([]string{field}, fields...)
  552. return _u
  553. }
  554. // Save executes the query and returns the updated SubscriptionPlan entity.
  555. func (_u *SubscriptionPlanUpdateOne) Save(ctx context.Context) (*SubscriptionPlan, error) {
  556. _u.defaults()
  557. return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
  558. }
  559. // SaveX is like Save, but panics if an error occurs.
  560. func (_u *SubscriptionPlanUpdateOne) SaveX(ctx context.Context) *SubscriptionPlan {
  561. node, err := _u.Save(ctx)
  562. if err != nil {
  563. panic(err)
  564. }
  565. return node
  566. }
  567. // Exec executes the query on the entity.
  568. func (_u *SubscriptionPlanUpdateOne) Exec(ctx context.Context) error {
  569. _, err := _u.Save(ctx)
  570. return err
  571. }
  572. // ExecX is like Exec, but panics if an error occurs.
  573. func (_u *SubscriptionPlanUpdateOne) ExecX(ctx context.Context) {
  574. if err := _u.Exec(ctx); err != nil {
  575. panic(err)
  576. }
  577. }
  578. // defaults sets the default values of the builder before save.
  579. func (_u *SubscriptionPlanUpdateOne) defaults() {
  580. if _, ok := _u.mutation.UpdatedAt(); !ok {
  581. v := subscriptionplan.UpdateDefaultUpdatedAt()
  582. _u.mutation.SetUpdatedAt(v)
  583. }
  584. }
  585. // check runs all checks and user-defined validators on the builder.
  586. func (_u *SubscriptionPlanUpdateOne) check() error {
  587. if v, ok := _u.mutation.Name(); ok {
  588. if err := subscriptionplan.NameValidator(v); err != nil {
  589. return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "SubscriptionPlan.name": %w`, err)}
  590. }
  591. }
  592. if v, ok := _u.mutation.Currency(); ok {
  593. if err := subscriptionplan.CurrencyValidator(v); err != nil {
  594. return &ValidationError{Name: "currency", err: fmt.Errorf(`ent: validator failed for field "SubscriptionPlan.currency": %w`, err)}
  595. }
  596. }
  597. if v, ok := _u.mutation.ValidityUnit(); ok {
  598. if err := subscriptionplan.ValidityUnitValidator(v); err != nil {
  599. return &ValidationError{Name: "validity_unit", err: fmt.Errorf(`ent: validator failed for field "SubscriptionPlan.validity_unit": %w`, err)}
  600. }
  601. }
  602. if v, ok := _u.mutation.ProductName(); ok {
  603. if err := subscriptionplan.ProductNameValidator(v); err != nil {
  604. return &ValidationError{Name: "product_name", err: fmt.Errorf(`ent: validator failed for field "SubscriptionPlan.product_name": %w`, err)}
  605. }
  606. }
  607. return nil
  608. }
  609. func (_u *SubscriptionPlanUpdateOne) sqlSave(ctx context.Context) (_node *SubscriptionPlan, err error) {
  610. if err := _u.check(); err != nil {
  611. return _node, err
  612. }
  613. _spec := sqlgraph.NewUpdateSpec(subscriptionplan.Table, subscriptionplan.Columns, sqlgraph.NewFieldSpec(subscriptionplan.FieldID, field.TypeInt64))
  614. id, ok := _u.mutation.ID()
  615. if !ok {
  616. return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "SubscriptionPlan.id" for update`)}
  617. }
  618. _spec.Node.ID.Value = id
  619. if fields := _u.fields; len(fields) > 0 {
  620. _spec.Node.Columns = make([]string, 0, len(fields))
  621. _spec.Node.Columns = append(_spec.Node.Columns, subscriptionplan.FieldID)
  622. for _, f := range fields {
  623. if !subscriptionplan.ValidColumn(f) {
  624. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  625. }
  626. if f != subscriptionplan.FieldID {
  627. _spec.Node.Columns = append(_spec.Node.Columns, f)
  628. }
  629. }
  630. }
  631. if ps := _u.mutation.predicates; len(ps) > 0 {
  632. _spec.Predicate = func(selector *sql.Selector) {
  633. for i := range ps {
  634. ps[i](selector)
  635. }
  636. }
  637. }
  638. if value, ok := _u.mutation.GroupID(); ok {
  639. _spec.SetField(subscriptionplan.FieldGroupID, field.TypeInt64, value)
  640. }
  641. if value, ok := _u.mutation.AddedGroupID(); ok {
  642. _spec.AddField(subscriptionplan.FieldGroupID, field.TypeInt64, value)
  643. }
  644. if value, ok := _u.mutation.Name(); ok {
  645. _spec.SetField(subscriptionplan.FieldName, field.TypeString, value)
  646. }
  647. if value, ok := _u.mutation.Description(); ok {
  648. _spec.SetField(subscriptionplan.FieldDescription, field.TypeString, value)
  649. }
  650. if value, ok := _u.mutation.Price(); ok {
  651. _spec.SetField(subscriptionplan.FieldPrice, field.TypeFloat64, value)
  652. }
  653. if value, ok := _u.mutation.AddedPrice(); ok {
  654. _spec.AddField(subscriptionplan.FieldPrice, field.TypeFloat64, value)
  655. }
  656. if value, ok := _u.mutation.OriginalPrice(); ok {
  657. _spec.SetField(subscriptionplan.FieldOriginalPrice, field.TypeFloat64, value)
  658. }
  659. if value, ok := _u.mutation.AddedOriginalPrice(); ok {
  660. _spec.AddField(subscriptionplan.FieldOriginalPrice, field.TypeFloat64, value)
  661. }
  662. if _u.mutation.OriginalPriceCleared() {
  663. _spec.ClearField(subscriptionplan.FieldOriginalPrice, field.TypeFloat64)
  664. }
  665. if value, ok := _u.mutation.Currency(); ok {
  666. _spec.SetField(subscriptionplan.FieldCurrency, field.TypeString, value)
  667. }
  668. if value, ok := _u.mutation.ValidityDays(); ok {
  669. _spec.SetField(subscriptionplan.FieldValidityDays, field.TypeInt, value)
  670. }
  671. if value, ok := _u.mutation.AddedValidityDays(); ok {
  672. _spec.AddField(subscriptionplan.FieldValidityDays, field.TypeInt, value)
  673. }
  674. if value, ok := _u.mutation.ValidityUnit(); ok {
  675. _spec.SetField(subscriptionplan.FieldValidityUnit, field.TypeString, value)
  676. }
  677. if value, ok := _u.mutation.Features(); ok {
  678. _spec.SetField(subscriptionplan.FieldFeatures, field.TypeString, value)
  679. }
  680. if value, ok := _u.mutation.ProductName(); ok {
  681. _spec.SetField(subscriptionplan.FieldProductName, field.TypeString, value)
  682. }
  683. if value, ok := _u.mutation.ForSale(); ok {
  684. _spec.SetField(subscriptionplan.FieldForSale, field.TypeBool, value)
  685. }
  686. if value, ok := _u.mutation.SortOrder(); ok {
  687. _spec.SetField(subscriptionplan.FieldSortOrder, field.TypeInt, value)
  688. }
  689. if value, ok := _u.mutation.AddedSortOrder(); ok {
  690. _spec.AddField(subscriptionplan.FieldSortOrder, field.TypeInt, value)
  691. }
  692. if value, ok := _u.mutation.UpdatedAt(); ok {
  693. _spec.SetField(subscriptionplan.FieldUpdatedAt, field.TypeTime, value)
  694. }
  695. _node = &SubscriptionPlan{config: _u.config}
  696. _spec.Assign = _node.assignValues
  697. _spec.ScanValues = _node.scanValues
  698. if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
  699. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  700. err = &NotFoundError{subscriptionplan.Label}
  701. } else if sqlgraph.IsConstraintError(err) {
  702. err = &ConstraintError{msg: err.Error(), wrap: err}
  703. }
  704. return nil, err
  705. }
  706. _u.mutation.done = true
  707. return _node, nil
  708. }