usersubscription_update.go 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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/group"
  12. "github.com/Wei-Shaw/sub2api/ent/predicate"
  13. "github.com/Wei-Shaw/sub2api/ent/usagelog"
  14. "github.com/Wei-Shaw/sub2api/ent/user"
  15. "github.com/Wei-Shaw/sub2api/ent/usersubscription"
  16. )
  17. // UserSubscriptionUpdate is the builder for updating UserSubscription entities.
  18. type UserSubscriptionUpdate struct {
  19. config
  20. hooks []Hook
  21. mutation *UserSubscriptionMutation
  22. }
  23. // Where appends a list predicates to the UserSubscriptionUpdate builder.
  24. func (_u *UserSubscriptionUpdate) Where(ps ...predicate.UserSubscription) *UserSubscriptionUpdate {
  25. _u.mutation.Where(ps...)
  26. return _u
  27. }
  28. // SetUpdatedAt sets the "updated_at" field.
  29. func (_u *UserSubscriptionUpdate) SetUpdatedAt(v time.Time) *UserSubscriptionUpdate {
  30. _u.mutation.SetUpdatedAt(v)
  31. return _u
  32. }
  33. // SetDeletedAt sets the "deleted_at" field.
  34. func (_u *UserSubscriptionUpdate) SetDeletedAt(v time.Time) *UserSubscriptionUpdate {
  35. _u.mutation.SetDeletedAt(v)
  36. return _u
  37. }
  38. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  39. func (_u *UserSubscriptionUpdate) SetNillableDeletedAt(v *time.Time) *UserSubscriptionUpdate {
  40. if v != nil {
  41. _u.SetDeletedAt(*v)
  42. }
  43. return _u
  44. }
  45. // ClearDeletedAt clears the value of the "deleted_at" field.
  46. func (_u *UserSubscriptionUpdate) ClearDeletedAt() *UserSubscriptionUpdate {
  47. _u.mutation.ClearDeletedAt()
  48. return _u
  49. }
  50. // SetUserID sets the "user_id" field.
  51. func (_u *UserSubscriptionUpdate) SetUserID(v int64) *UserSubscriptionUpdate {
  52. _u.mutation.SetUserID(v)
  53. return _u
  54. }
  55. // SetNillableUserID sets the "user_id" field if the given value is not nil.
  56. func (_u *UserSubscriptionUpdate) SetNillableUserID(v *int64) *UserSubscriptionUpdate {
  57. if v != nil {
  58. _u.SetUserID(*v)
  59. }
  60. return _u
  61. }
  62. // SetGroupID sets the "group_id" field.
  63. func (_u *UserSubscriptionUpdate) SetGroupID(v int64) *UserSubscriptionUpdate {
  64. _u.mutation.SetGroupID(v)
  65. return _u
  66. }
  67. // SetNillableGroupID sets the "group_id" field if the given value is not nil.
  68. func (_u *UserSubscriptionUpdate) SetNillableGroupID(v *int64) *UserSubscriptionUpdate {
  69. if v != nil {
  70. _u.SetGroupID(*v)
  71. }
  72. return _u
  73. }
  74. // SetStartsAt sets the "starts_at" field.
  75. func (_u *UserSubscriptionUpdate) SetStartsAt(v time.Time) *UserSubscriptionUpdate {
  76. _u.mutation.SetStartsAt(v)
  77. return _u
  78. }
  79. // SetNillableStartsAt sets the "starts_at" field if the given value is not nil.
  80. func (_u *UserSubscriptionUpdate) SetNillableStartsAt(v *time.Time) *UserSubscriptionUpdate {
  81. if v != nil {
  82. _u.SetStartsAt(*v)
  83. }
  84. return _u
  85. }
  86. // SetExpiresAt sets the "expires_at" field.
  87. func (_u *UserSubscriptionUpdate) SetExpiresAt(v time.Time) *UserSubscriptionUpdate {
  88. _u.mutation.SetExpiresAt(v)
  89. return _u
  90. }
  91. // SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
  92. func (_u *UserSubscriptionUpdate) SetNillableExpiresAt(v *time.Time) *UserSubscriptionUpdate {
  93. if v != nil {
  94. _u.SetExpiresAt(*v)
  95. }
  96. return _u
  97. }
  98. // SetStatus sets the "status" field.
  99. func (_u *UserSubscriptionUpdate) SetStatus(v string) *UserSubscriptionUpdate {
  100. _u.mutation.SetStatus(v)
  101. return _u
  102. }
  103. // SetNillableStatus sets the "status" field if the given value is not nil.
  104. func (_u *UserSubscriptionUpdate) SetNillableStatus(v *string) *UserSubscriptionUpdate {
  105. if v != nil {
  106. _u.SetStatus(*v)
  107. }
  108. return _u
  109. }
  110. // SetDailyWindowStart sets the "daily_window_start" field.
  111. func (_u *UserSubscriptionUpdate) SetDailyWindowStart(v time.Time) *UserSubscriptionUpdate {
  112. _u.mutation.SetDailyWindowStart(v)
  113. return _u
  114. }
  115. // SetNillableDailyWindowStart sets the "daily_window_start" field if the given value is not nil.
  116. func (_u *UserSubscriptionUpdate) SetNillableDailyWindowStart(v *time.Time) *UserSubscriptionUpdate {
  117. if v != nil {
  118. _u.SetDailyWindowStart(*v)
  119. }
  120. return _u
  121. }
  122. // ClearDailyWindowStart clears the value of the "daily_window_start" field.
  123. func (_u *UserSubscriptionUpdate) ClearDailyWindowStart() *UserSubscriptionUpdate {
  124. _u.mutation.ClearDailyWindowStart()
  125. return _u
  126. }
  127. // SetWeeklyWindowStart sets the "weekly_window_start" field.
  128. func (_u *UserSubscriptionUpdate) SetWeeklyWindowStart(v time.Time) *UserSubscriptionUpdate {
  129. _u.mutation.SetWeeklyWindowStart(v)
  130. return _u
  131. }
  132. // SetNillableWeeklyWindowStart sets the "weekly_window_start" field if the given value is not nil.
  133. func (_u *UserSubscriptionUpdate) SetNillableWeeklyWindowStart(v *time.Time) *UserSubscriptionUpdate {
  134. if v != nil {
  135. _u.SetWeeklyWindowStart(*v)
  136. }
  137. return _u
  138. }
  139. // ClearWeeklyWindowStart clears the value of the "weekly_window_start" field.
  140. func (_u *UserSubscriptionUpdate) ClearWeeklyWindowStart() *UserSubscriptionUpdate {
  141. _u.mutation.ClearWeeklyWindowStart()
  142. return _u
  143. }
  144. // SetMonthlyWindowStart sets the "monthly_window_start" field.
  145. func (_u *UserSubscriptionUpdate) SetMonthlyWindowStart(v time.Time) *UserSubscriptionUpdate {
  146. _u.mutation.SetMonthlyWindowStart(v)
  147. return _u
  148. }
  149. // SetNillableMonthlyWindowStart sets the "monthly_window_start" field if the given value is not nil.
  150. func (_u *UserSubscriptionUpdate) SetNillableMonthlyWindowStart(v *time.Time) *UserSubscriptionUpdate {
  151. if v != nil {
  152. _u.SetMonthlyWindowStart(*v)
  153. }
  154. return _u
  155. }
  156. // ClearMonthlyWindowStart clears the value of the "monthly_window_start" field.
  157. func (_u *UserSubscriptionUpdate) ClearMonthlyWindowStart() *UserSubscriptionUpdate {
  158. _u.mutation.ClearMonthlyWindowStart()
  159. return _u
  160. }
  161. // SetDailyUsageUsd sets the "daily_usage_usd" field.
  162. func (_u *UserSubscriptionUpdate) SetDailyUsageUsd(v float64) *UserSubscriptionUpdate {
  163. _u.mutation.ResetDailyUsageUsd()
  164. _u.mutation.SetDailyUsageUsd(v)
  165. return _u
  166. }
  167. // SetNillableDailyUsageUsd sets the "daily_usage_usd" field if the given value is not nil.
  168. func (_u *UserSubscriptionUpdate) SetNillableDailyUsageUsd(v *float64) *UserSubscriptionUpdate {
  169. if v != nil {
  170. _u.SetDailyUsageUsd(*v)
  171. }
  172. return _u
  173. }
  174. // AddDailyUsageUsd adds value to the "daily_usage_usd" field.
  175. func (_u *UserSubscriptionUpdate) AddDailyUsageUsd(v float64) *UserSubscriptionUpdate {
  176. _u.mutation.AddDailyUsageUsd(v)
  177. return _u
  178. }
  179. // SetWeeklyUsageUsd sets the "weekly_usage_usd" field.
  180. func (_u *UserSubscriptionUpdate) SetWeeklyUsageUsd(v float64) *UserSubscriptionUpdate {
  181. _u.mutation.ResetWeeklyUsageUsd()
  182. _u.mutation.SetWeeklyUsageUsd(v)
  183. return _u
  184. }
  185. // SetNillableWeeklyUsageUsd sets the "weekly_usage_usd" field if the given value is not nil.
  186. func (_u *UserSubscriptionUpdate) SetNillableWeeklyUsageUsd(v *float64) *UserSubscriptionUpdate {
  187. if v != nil {
  188. _u.SetWeeklyUsageUsd(*v)
  189. }
  190. return _u
  191. }
  192. // AddWeeklyUsageUsd adds value to the "weekly_usage_usd" field.
  193. func (_u *UserSubscriptionUpdate) AddWeeklyUsageUsd(v float64) *UserSubscriptionUpdate {
  194. _u.mutation.AddWeeklyUsageUsd(v)
  195. return _u
  196. }
  197. // SetMonthlyUsageUsd sets the "monthly_usage_usd" field.
  198. func (_u *UserSubscriptionUpdate) SetMonthlyUsageUsd(v float64) *UserSubscriptionUpdate {
  199. _u.mutation.ResetMonthlyUsageUsd()
  200. _u.mutation.SetMonthlyUsageUsd(v)
  201. return _u
  202. }
  203. // SetNillableMonthlyUsageUsd sets the "monthly_usage_usd" field if the given value is not nil.
  204. func (_u *UserSubscriptionUpdate) SetNillableMonthlyUsageUsd(v *float64) *UserSubscriptionUpdate {
  205. if v != nil {
  206. _u.SetMonthlyUsageUsd(*v)
  207. }
  208. return _u
  209. }
  210. // AddMonthlyUsageUsd adds value to the "monthly_usage_usd" field.
  211. func (_u *UserSubscriptionUpdate) AddMonthlyUsageUsd(v float64) *UserSubscriptionUpdate {
  212. _u.mutation.AddMonthlyUsageUsd(v)
  213. return _u
  214. }
  215. // SetAssignedBy sets the "assigned_by" field.
  216. func (_u *UserSubscriptionUpdate) SetAssignedBy(v int64) *UserSubscriptionUpdate {
  217. _u.mutation.SetAssignedBy(v)
  218. return _u
  219. }
  220. // SetNillableAssignedBy sets the "assigned_by" field if the given value is not nil.
  221. func (_u *UserSubscriptionUpdate) SetNillableAssignedBy(v *int64) *UserSubscriptionUpdate {
  222. if v != nil {
  223. _u.SetAssignedBy(*v)
  224. }
  225. return _u
  226. }
  227. // ClearAssignedBy clears the value of the "assigned_by" field.
  228. func (_u *UserSubscriptionUpdate) ClearAssignedBy() *UserSubscriptionUpdate {
  229. _u.mutation.ClearAssignedBy()
  230. return _u
  231. }
  232. // SetAssignedAt sets the "assigned_at" field.
  233. func (_u *UserSubscriptionUpdate) SetAssignedAt(v time.Time) *UserSubscriptionUpdate {
  234. _u.mutation.SetAssignedAt(v)
  235. return _u
  236. }
  237. // SetNillableAssignedAt sets the "assigned_at" field if the given value is not nil.
  238. func (_u *UserSubscriptionUpdate) SetNillableAssignedAt(v *time.Time) *UserSubscriptionUpdate {
  239. if v != nil {
  240. _u.SetAssignedAt(*v)
  241. }
  242. return _u
  243. }
  244. // SetNotes sets the "notes" field.
  245. func (_u *UserSubscriptionUpdate) SetNotes(v string) *UserSubscriptionUpdate {
  246. _u.mutation.SetNotes(v)
  247. return _u
  248. }
  249. // SetNillableNotes sets the "notes" field if the given value is not nil.
  250. func (_u *UserSubscriptionUpdate) SetNillableNotes(v *string) *UserSubscriptionUpdate {
  251. if v != nil {
  252. _u.SetNotes(*v)
  253. }
  254. return _u
  255. }
  256. // ClearNotes clears the value of the "notes" field.
  257. func (_u *UserSubscriptionUpdate) ClearNotes() *UserSubscriptionUpdate {
  258. _u.mutation.ClearNotes()
  259. return _u
  260. }
  261. // SetUser sets the "user" edge to the User entity.
  262. func (_u *UserSubscriptionUpdate) SetUser(v *User) *UserSubscriptionUpdate {
  263. return _u.SetUserID(v.ID)
  264. }
  265. // SetGroup sets the "group" edge to the Group entity.
  266. func (_u *UserSubscriptionUpdate) SetGroup(v *Group) *UserSubscriptionUpdate {
  267. return _u.SetGroupID(v.ID)
  268. }
  269. // SetAssignedByUserID sets the "assigned_by_user" edge to the User entity by ID.
  270. func (_u *UserSubscriptionUpdate) SetAssignedByUserID(id int64) *UserSubscriptionUpdate {
  271. _u.mutation.SetAssignedByUserID(id)
  272. return _u
  273. }
  274. // SetNillableAssignedByUserID sets the "assigned_by_user" edge to the User entity by ID if the given value is not nil.
  275. func (_u *UserSubscriptionUpdate) SetNillableAssignedByUserID(id *int64) *UserSubscriptionUpdate {
  276. if id != nil {
  277. _u = _u.SetAssignedByUserID(*id)
  278. }
  279. return _u
  280. }
  281. // SetAssignedByUser sets the "assigned_by_user" edge to the User entity.
  282. func (_u *UserSubscriptionUpdate) SetAssignedByUser(v *User) *UserSubscriptionUpdate {
  283. return _u.SetAssignedByUserID(v.ID)
  284. }
  285. // AddUsageLogIDs adds the "usage_logs" edge to the UsageLog entity by IDs.
  286. func (_u *UserSubscriptionUpdate) AddUsageLogIDs(ids ...int64) *UserSubscriptionUpdate {
  287. _u.mutation.AddUsageLogIDs(ids...)
  288. return _u
  289. }
  290. // AddUsageLogs adds the "usage_logs" edges to the UsageLog entity.
  291. func (_u *UserSubscriptionUpdate) AddUsageLogs(v ...*UsageLog) *UserSubscriptionUpdate {
  292. ids := make([]int64, len(v))
  293. for i := range v {
  294. ids[i] = v[i].ID
  295. }
  296. return _u.AddUsageLogIDs(ids...)
  297. }
  298. // Mutation returns the UserSubscriptionMutation object of the builder.
  299. func (_u *UserSubscriptionUpdate) Mutation() *UserSubscriptionMutation {
  300. return _u.mutation
  301. }
  302. // ClearUser clears the "user" edge to the User entity.
  303. func (_u *UserSubscriptionUpdate) ClearUser() *UserSubscriptionUpdate {
  304. _u.mutation.ClearUser()
  305. return _u
  306. }
  307. // ClearGroup clears the "group" edge to the Group entity.
  308. func (_u *UserSubscriptionUpdate) ClearGroup() *UserSubscriptionUpdate {
  309. _u.mutation.ClearGroup()
  310. return _u
  311. }
  312. // ClearAssignedByUser clears the "assigned_by_user" edge to the User entity.
  313. func (_u *UserSubscriptionUpdate) ClearAssignedByUser() *UserSubscriptionUpdate {
  314. _u.mutation.ClearAssignedByUser()
  315. return _u
  316. }
  317. // ClearUsageLogs clears all "usage_logs" edges to the UsageLog entity.
  318. func (_u *UserSubscriptionUpdate) ClearUsageLogs() *UserSubscriptionUpdate {
  319. _u.mutation.ClearUsageLogs()
  320. return _u
  321. }
  322. // RemoveUsageLogIDs removes the "usage_logs" edge to UsageLog entities by IDs.
  323. func (_u *UserSubscriptionUpdate) RemoveUsageLogIDs(ids ...int64) *UserSubscriptionUpdate {
  324. _u.mutation.RemoveUsageLogIDs(ids...)
  325. return _u
  326. }
  327. // RemoveUsageLogs removes "usage_logs" edges to UsageLog entities.
  328. func (_u *UserSubscriptionUpdate) RemoveUsageLogs(v ...*UsageLog) *UserSubscriptionUpdate {
  329. ids := make([]int64, len(v))
  330. for i := range v {
  331. ids[i] = v[i].ID
  332. }
  333. return _u.RemoveUsageLogIDs(ids...)
  334. }
  335. // Save executes the query and returns the number of nodes affected by the update operation.
  336. func (_u *UserSubscriptionUpdate) Save(ctx context.Context) (int, error) {
  337. if err := _u.defaults(); err != nil {
  338. return 0, err
  339. }
  340. return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
  341. }
  342. // SaveX is like Save, but panics if an error occurs.
  343. func (_u *UserSubscriptionUpdate) SaveX(ctx context.Context) int {
  344. affected, err := _u.Save(ctx)
  345. if err != nil {
  346. panic(err)
  347. }
  348. return affected
  349. }
  350. // Exec executes the query.
  351. func (_u *UserSubscriptionUpdate) Exec(ctx context.Context) error {
  352. _, err := _u.Save(ctx)
  353. return err
  354. }
  355. // ExecX is like Exec, but panics if an error occurs.
  356. func (_u *UserSubscriptionUpdate) ExecX(ctx context.Context) {
  357. if err := _u.Exec(ctx); err != nil {
  358. panic(err)
  359. }
  360. }
  361. // defaults sets the default values of the builder before save.
  362. func (_u *UserSubscriptionUpdate) defaults() error {
  363. if _, ok := _u.mutation.UpdatedAt(); !ok {
  364. if usersubscription.UpdateDefaultUpdatedAt == nil {
  365. return fmt.Errorf("ent: uninitialized usersubscription.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  366. }
  367. v := usersubscription.UpdateDefaultUpdatedAt()
  368. _u.mutation.SetUpdatedAt(v)
  369. }
  370. return nil
  371. }
  372. // check runs all checks and user-defined validators on the builder.
  373. func (_u *UserSubscriptionUpdate) check() error {
  374. if v, ok := _u.mutation.Status(); ok {
  375. if err := usersubscription.StatusValidator(v); err != nil {
  376. return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "UserSubscription.status": %w`, err)}
  377. }
  378. }
  379. if _u.mutation.UserCleared() && len(_u.mutation.UserIDs()) > 0 {
  380. return errors.New(`ent: clearing a required unique edge "UserSubscription.user"`)
  381. }
  382. if _u.mutation.GroupCleared() && len(_u.mutation.GroupIDs()) > 0 {
  383. return errors.New(`ent: clearing a required unique edge "UserSubscription.group"`)
  384. }
  385. return nil
  386. }
  387. func (_u *UserSubscriptionUpdate) sqlSave(ctx context.Context) (_node int, err error) {
  388. if err := _u.check(); err != nil {
  389. return _node, err
  390. }
  391. _spec := sqlgraph.NewUpdateSpec(usersubscription.Table, usersubscription.Columns, sqlgraph.NewFieldSpec(usersubscription.FieldID, field.TypeInt64))
  392. if ps := _u.mutation.predicates; len(ps) > 0 {
  393. _spec.Predicate = func(selector *sql.Selector) {
  394. for i := range ps {
  395. ps[i](selector)
  396. }
  397. }
  398. }
  399. if value, ok := _u.mutation.UpdatedAt(); ok {
  400. _spec.SetField(usersubscription.FieldUpdatedAt, field.TypeTime, value)
  401. }
  402. if value, ok := _u.mutation.DeletedAt(); ok {
  403. _spec.SetField(usersubscription.FieldDeletedAt, field.TypeTime, value)
  404. }
  405. if _u.mutation.DeletedAtCleared() {
  406. _spec.ClearField(usersubscription.FieldDeletedAt, field.TypeTime)
  407. }
  408. if value, ok := _u.mutation.StartsAt(); ok {
  409. _spec.SetField(usersubscription.FieldStartsAt, field.TypeTime, value)
  410. }
  411. if value, ok := _u.mutation.ExpiresAt(); ok {
  412. _spec.SetField(usersubscription.FieldExpiresAt, field.TypeTime, value)
  413. }
  414. if value, ok := _u.mutation.Status(); ok {
  415. _spec.SetField(usersubscription.FieldStatus, field.TypeString, value)
  416. }
  417. if value, ok := _u.mutation.DailyWindowStart(); ok {
  418. _spec.SetField(usersubscription.FieldDailyWindowStart, field.TypeTime, value)
  419. }
  420. if _u.mutation.DailyWindowStartCleared() {
  421. _spec.ClearField(usersubscription.FieldDailyWindowStart, field.TypeTime)
  422. }
  423. if value, ok := _u.mutation.WeeklyWindowStart(); ok {
  424. _spec.SetField(usersubscription.FieldWeeklyWindowStart, field.TypeTime, value)
  425. }
  426. if _u.mutation.WeeklyWindowStartCleared() {
  427. _spec.ClearField(usersubscription.FieldWeeklyWindowStart, field.TypeTime)
  428. }
  429. if value, ok := _u.mutation.MonthlyWindowStart(); ok {
  430. _spec.SetField(usersubscription.FieldMonthlyWindowStart, field.TypeTime, value)
  431. }
  432. if _u.mutation.MonthlyWindowStartCleared() {
  433. _spec.ClearField(usersubscription.FieldMonthlyWindowStart, field.TypeTime)
  434. }
  435. if value, ok := _u.mutation.DailyUsageUsd(); ok {
  436. _spec.SetField(usersubscription.FieldDailyUsageUsd, field.TypeFloat64, value)
  437. }
  438. if value, ok := _u.mutation.AddedDailyUsageUsd(); ok {
  439. _spec.AddField(usersubscription.FieldDailyUsageUsd, field.TypeFloat64, value)
  440. }
  441. if value, ok := _u.mutation.WeeklyUsageUsd(); ok {
  442. _spec.SetField(usersubscription.FieldWeeklyUsageUsd, field.TypeFloat64, value)
  443. }
  444. if value, ok := _u.mutation.AddedWeeklyUsageUsd(); ok {
  445. _spec.AddField(usersubscription.FieldWeeklyUsageUsd, field.TypeFloat64, value)
  446. }
  447. if value, ok := _u.mutation.MonthlyUsageUsd(); ok {
  448. _spec.SetField(usersubscription.FieldMonthlyUsageUsd, field.TypeFloat64, value)
  449. }
  450. if value, ok := _u.mutation.AddedMonthlyUsageUsd(); ok {
  451. _spec.AddField(usersubscription.FieldMonthlyUsageUsd, field.TypeFloat64, value)
  452. }
  453. if value, ok := _u.mutation.AssignedAt(); ok {
  454. _spec.SetField(usersubscription.FieldAssignedAt, field.TypeTime, value)
  455. }
  456. if value, ok := _u.mutation.Notes(); ok {
  457. _spec.SetField(usersubscription.FieldNotes, field.TypeString, value)
  458. }
  459. if _u.mutation.NotesCleared() {
  460. _spec.ClearField(usersubscription.FieldNotes, field.TypeString)
  461. }
  462. if _u.mutation.UserCleared() {
  463. edge := &sqlgraph.EdgeSpec{
  464. Rel: sqlgraph.M2O,
  465. Inverse: true,
  466. Table: usersubscription.UserTable,
  467. Columns: []string{usersubscription.UserColumn},
  468. Bidi: false,
  469. Target: &sqlgraph.EdgeTarget{
  470. IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
  471. },
  472. }
  473. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  474. }
  475. if nodes := _u.mutation.UserIDs(); len(nodes) > 0 {
  476. edge := &sqlgraph.EdgeSpec{
  477. Rel: sqlgraph.M2O,
  478. Inverse: true,
  479. Table: usersubscription.UserTable,
  480. Columns: []string{usersubscription.UserColumn},
  481. Bidi: false,
  482. Target: &sqlgraph.EdgeTarget{
  483. IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
  484. },
  485. }
  486. for _, k := range nodes {
  487. edge.Target.Nodes = append(edge.Target.Nodes, k)
  488. }
  489. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  490. }
  491. if _u.mutation.GroupCleared() {
  492. edge := &sqlgraph.EdgeSpec{
  493. Rel: sqlgraph.M2O,
  494. Inverse: true,
  495. Table: usersubscription.GroupTable,
  496. Columns: []string{usersubscription.GroupColumn},
  497. Bidi: false,
  498. Target: &sqlgraph.EdgeTarget{
  499. IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
  500. },
  501. }
  502. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  503. }
  504. if nodes := _u.mutation.GroupIDs(); len(nodes) > 0 {
  505. edge := &sqlgraph.EdgeSpec{
  506. Rel: sqlgraph.M2O,
  507. Inverse: true,
  508. Table: usersubscription.GroupTable,
  509. Columns: []string{usersubscription.GroupColumn},
  510. Bidi: false,
  511. Target: &sqlgraph.EdgeTarget{
  512. IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
  513. },
  514. }
  515. for _, k := range nodes {
  516. edge.Target.Nodes = append(edge.Target.Nodes, k)
  517. }
  518. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  519. }
  520. if _u.mutation.AssignedByUserCleared() {
  521. edge := &sqlgraph.EdgeSpec{
  522. Rel: sqlgraph.M2O,
  523. Inverse: true,
  524. Table: usersubscription.AssignedByUserTable,
  525. Columns: []string{usersubscription.AssignedByUserColumn},
  526. Bidi: false,
  527. Target: &sqlgraph.EdgeTarget{
  528. IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
  529. },
  530. }
  531. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  532. }
  533. if nodes := _u.mutation.AssignedByUserIDs(); len(nodes) > 0 {
  534. edge := &sqlgraph.EdgeSpec{
  535. Rel: sqlgraph.M2O,
  536. Inverse: true,
  537. Table: usersubscription.AssignedByUserTable,
  538. Columns: []string{usersubscription.AssignedByUserColumn},
  539. Bidi: false,
  540. Target: &sqlgraph.EdgeTarget{
  541. IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
  542. },
  543. }
  544. for _, k := range nodes {
  545. edge.Target.Nodes = append(edge.Target.Nodes, k)
  546. }
  547. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  548. }
  549. if _u.mutation.UsageLogsCleared() {
  550. edge := &sqlgraph.EdgeSpec{
  551. Rel: sqlgraph.O2M,
  552. Inverse: false,
  553. Table: usersubscription.UsageLogsTable,
  554. Columns: []string{usersubscription.UsageLogsColumn},
  555. Bidi: false,
  556. Target: &sqlgraph.EdgeTarget{
  557. IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
  558. },
  559. }
  560. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  561. }
  562. if nodes := _u.mutation.RemovedUsageLogsIDs(); len(nodes) > 0 && !_u.mutation.UsageLogsCleared() {
  563. edge := &sqlgraph.EdgeSpec{
  564. Rel: sqlgraph.O2M,
  565. Inverse: false,
  566. Table: usersubscription.UsageLogsTable,
  567. Columns: []string{usersubscription.UsageLogsColumn},
  568. Bidi: false,
  569. Target: &sqlgraph.EdgeTarget{
  570. IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
  571. },
  572. }
  573. for _, k := range nodes {
  574. edge.Target.Nodes = append(edge.Target.Nodes, k)
  575. }
  576. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  577. }
  578. if nodes := _u.mutation.UsageLogsIDs(); len(nodes) > 0 {
  579. edge := &sqlgraph.EdgeSpec{
  580. Rel: sqlgraph.O2M,
  581. Inverse: false,
  582. Table: usersubscription.UsageLogsTable,
  583. Columns: []string{usersubscription.UsageLogsColumn},
  584. Bidi: false,
  585. Target: &sqlgraph.EdgeTarget{
  586. IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
  587. },
  588. }
  589. for _, k := range nodes {
  590. edge.Target.Nodes = append(edge.Target.Nodes, k)
  591. }
  592. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  593. }
  594. if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
  595. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  596. err = &NotFoundError{usersubscription.Label}
  597. } else if sqlgraph.IsConstraintError(err) {
  598. err = &ConstraintError{msg: err.Error(), wrap: err}
  599. }
  600. return 0, err
  601. }
  602. _u.mutation.done = true
  603. return _node, nil
  604. }
  605. // UserSubscriptionUpdateOne is the builder for updating a single UserSubscription entity.
  606. type UserSubscriptionUpdateOne struct {
  607. config
  608. fields []string
  609. hooks []Hook
  610. mutation *UserSubscriptionMutation
  611. }
  612. // SetUpdatedAt sets the "updated_at" field.
  613. func (_u *UserSubscriptionUpdateOne) SetUpdatedAt(v time.Time) *UserSubscriptionUpdateOne {
  614. _u.mutation.SetUpdatedAt(v)
  615. return _u
  616. }
  617. // SetDeletedAt sets the "deleted_at" field.
  618. func (_u *UserSubscriptionUpdateOne) SetDeletedAt(v time.Time) *UserSubscriptionUpdateOne {
  619. _u.mutation.SetDeletedAt(v)
  620. return _u
  621. }
  622. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  623. func (_u *UserSubscriptionUpdateOne) SetNillableDeletedAt(v *time.Time) *UserSubscriptionUpdateOne {
  624. if v != nil {
  625. _u.SetDeletedAt(*v)
  626. }
  627. return _u
  628. }
  629. // ClearDeletedAt clears the value of the "deleted_at" field.
  630. func (_u *UserSubscriptionUpdateOne) ClearDeletedAt() *UserSubscriptionUpdateOne {
  631. _u.mutation.ClearDeletedAt()
  632. return _u
  633. }
  634. // SetUserID sets the "user_id" field.
  635. func (_u *UserSubscriptionUpdateOne) SetUserID(v int64) *UserSubscriptionUpdateOne {
  636. _u.mutation.SetUserID(v)
  637. return _u
  638. }
  639. // SetNillableUserID sets the "user_id" field if the given value is not nil.
  640. func (_u *UserSubscriptionUpdateOne) SetNillableUserID(v *int64) *UserSubscriptionUpdateOne {
  641. if v != nil {
  642. _u.SetUserID(*v)
  643. }
  644. return _u
  645. }
  646. // SetGroupID sets the "group_id" field.
  647. func (_u *UserSubscriptionUpdateOne) SetGroupID(v int64) *UserSubscriptionUpdateOne {
  648. _u.mutation.SetGroupID(v)
  649. return _u
  650. }
  651. // SetNillableGroupID sets the "group_id" field if the given value is not nil.
  652. func (_u *UserSubscriptionUpdateOne) SetNillableGroupID(v *int64) *UserSubscriptionUpdateOne {
  653. if v != nil {
  654. _u.SetGroupID(*v)
  655. }
  656. return _u
  657. }
  658. // SetStartsAt sets the "starts_at" field.
  659. func (_u *UserSubscriptionUpdateOne) SetStartsAt(v time.Time) *UserSubscriptionUpdateOne {
  660. _u.mutation.SetStartsAt(v)
  661. return _u
  662. }
  663. // SetNillableStartsAt sets the "starts_at" field if the given value is not nil.
  664. func (_u *UserSubscriptionUpdateOne) SetNillableStartsAt(v *time.Time) *UserSubscriptionUpdateOne {
  665. if v != nil {
  666. _u.SetStartsAt(*v)
  667. }
  668. return _u
  669. }
  670. // SetExpiresAt sets the "expires_at" field.
  671. func (_u *UserSubscriptionUpdateOne) SetExpiresAt(v time.Time) *UserSubscriptionUpdateOne {
  672. _u.mutation.SetExpiresAt(v)
  673. return _u
  674. }
  675. // SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
  676. func (_u *UserSubscriptionUpdateOne) SetNillableExpiresAt(v *time.Time) *UserSubscriptionUpdateOne {
  677. if v != nil {
  678. _u.SetExpiresAt(*v)
  679. }
  680. return _u
  681. }
  682. // SetStatus sets the "status" field.
  683. func (_u *UserSubscriptionUpdateOne) SetStatus(v string) *UserSubscriptionUpdateOne {
  684. _u.mutation.SetStatus(v)
  685. return _u
  686. }
  687. // SetNillableStatus sets the "status" field if the given value is not nil.
  688. func (_u *UserSubscriptionUpdateOne) SetNillableStatus(v *string) *UserSubscriptionUpdateOne {
  689. if v != nil {
  690. _u.SetStatus(*v)
  691. }
  692. return _u
  693. }
  694. // SetDailyWindowStart sets the "daily_window_start" field.
  695. func (_u *UserSubscriptionUpdateOne) SetDailyWindowStart(v time.Time) *UserSubscriptionUpdateOne {
  696. _u.mutation.SetDailyWindowStart(v)
  697. return _u
  698. }
  699. // SetNillableDailyWindowStart sets the "daily_window_start" field if the given value is not nil.
  700. func (_u *UserSubscriptionUpdateOne) SetNillableDailyWindowStart(v *time.Time) *UserSubscriptionUpdateOne {
  701. if v != nil {
  702. _u.SetDailyWindowStart(*v)
  703. }
  704. return _u
  705. }
  706. // ClearDailyWindowStart clears the value of the "daily_window_start" field.
  707. func (_u *UserSubscriptionUpdateOne) ClearDailyWindowStart() *UserSubscriptionUpdateOne {
  708. _u.mutation.ClearDailyWindowStart()
  709. return _u
  710. }
  711. // SetWeeklyWindowStart sets the "weekly_window_start" field.
  712. func (_u *UserSubscriptionUpdateOne) SetWeeklyWindowStart(v time.Time) *UserSubscriptionUpdateOne {
  713. _u.mutation.SetWeeklyWindowStart(v)
  714. return _u
  715. }
  716. // SetNillableWeeklyWindowStart sets the "weekly_window_start" field if the given value is not nil.
  717. func (_u *UserSubscriptionUpdateOne) SetNillableWeeklyWindowStart(v *time.Time) *UserSubscriptionUpdateOne {
  718. if v != nil {
  719. _u.SetWeeklyWindowStart(*v)
  720. }
  721. return _u
  722. }
  723. // ClearWeeklyWindowStart clears the value of the "weekly_window_start" field.
  724. func (_u *UserSubscriptionUpdateOne) ClearWeeklyWindowStart() *UserSubscriptionUpdateOne {
  725. _u.mutation.ClearWeeklyWindowStart()
  726. return _u
  727. }
  728. // SetMonthlyWindowStart sets the "monthly_window_start" field.
  729. func (_u *UserSubscriptionUpdateOne) SetMonthlyWindowStart(v time.Time) *UserSubscriptionUpdateOne {
  730. _u.mutation.SetMonthlyWindowStart(v)
  731. return _u
  732. }
  733. // SetNillableMonthlyWindowStart sets the "monthly_window_start" field if the given value is not nil.
  734. func (_u *UserSubscriptionUpdateOne) SetNillableMonthlyWindowStart(v *time.Time) *UserSubscriptionUpdateOne {
  735. if v != nil {
  736. _u.SetMonthlyWindowStart(*v)
  737. }
  738. return _u
  739. }
  740. // ClearMonthlyWindowStart clears the value of the "monthly_window_start" field.
  741. func (_u *UserSubscriptionUpdateOne) ClearMonthlyWindowStart() *UserSubscriptionUpdateOne {
  742. _u.mutation.ClearMonthlyWindowStart()
  743. return _u
  744. }
  745. // SetDailyUsageUsd sets the "daily_usage_usd" field.
  746. func (_u *UserSubscriptionUpdateOne) SetDailyUsageUsd(v float64) *UserSubscriptionUpdateOne {
  747. _u.mutation.ResetDailyUsageUsd()
  748. _u.mutation.SetDailyUsageUsd(v)
  749. return _u
  750. }
  751. // SetNillableDailyUsageUsd sets the "daily_usage_usd" field if the given value is not nil.
  752. func (_u *UserSubscriptionUpdateOne) SetNillableDailyUsageUsd(v *float64) *UserSubscriptionUpdateOne {
  753. if v != nil {
  754. _u.SetDailyUsageUsd(*v)
  755. }
  756. return _u
  757. }
  758. // AddDailyUsageUsd adds value to the "daily_usage_usd" field.
  759. func (_u *UserSubscriptionUpdateOne) AddDailyUsageUsd(v float64) *UserSubscriptionUpdateOne {
  760. _u.mutation.AddDailyUsageUsd(v)
  761. return _u
  762. }
  763. // SetWeeklyUsageUsd sets the "weekly_usage_usd" field.
  764. func (_u *UserSubscriptionUpdateOne) SetWeeklyUsageUsd(v float64) *UserSubscriptionUpdateOne {
  765. _u.mutation.ResetWeeklyUsageUsd()
  766. _u.mutation.SetWeeklyUsageUsd(v)
  767. return _u
  768. }
  769. // SetNillableWeeklyUsageUsd sets the "weekly_usage_usd" field if the given value is not nil.
  770. func (_u *UserSubscriptionUpdateOne) SetNillableWeeklyUsageUsd(v *float64) *UserSubscriptionUpdateOne {
  771. if v != nil {
  772. _u.SetWeeklyUsageUsd(*v)
  773. }
  774. return _u
  775. }
  776. // AddWeeklyUsageUsd adds value to the "weekly_usage_usd" field.
  777. func (_u *UserSubscriptionUpdateOne) AddWeeklyUsageUsd(v float64) *UserSubscriptionUpdateOne {
  778. _u.mutation.AddWeeklyUsageUsd(v)
  779. return _u
  780. }
  781. // SetMonthlyUsageUsd sets the "monthly_usage_usd" field.
  782. func (_u *UserSubscriptionUpdateOne) SetMonthlyUsageUsd(v float64) *UserSubscriptionUpdateOne {
  783. _u.mutation.ResetMonthlyUsageUsd()
  784. _u.mutation.SetMonthlyUsageUsd(v)
  785. return _u
  786. }
  787. // SetNillableMonthlyUsageUsd sets the "monthly_usage_usd" field if the given value is not nil.
  788. func (_u *UserSubscriptionUpdateOne) SetNillableMonthlyUsageUsd(v *float64) *UserSubscriptionUpdateOne {
  789. if v != nil {
  790. _u.SetMonthlyUsageUsd(*v)
  791. }
  792. return _u
  793. }
  794. // AddMonthlyUsageUsd adds value to the "monthly_usage_usd" field.
  795. func (_u *UserSubscriptionUpdateOne) AddMonthlyUsageUsd(v float64) *UserSubscriptionUpdateOne {
  796. _u.mutation.AddMonthlyUsageUsd(v)
  797. return _u
  798. }
  799. // SetAssignedBy sets the "assigned_by" field.
  800. func (_u *UserSubscriptionUpdateOne) SetAssignedBy(v int64) *UserSubscriptionUpdateOne {
  801. _u.mutation.SetAssignedBy(v)
  802. return _u
  803. }
  804. // SetNillableAssignedBy sets the "assigned_by" field if the given value is not nil.
  805. func (_u *UserSubscriptionUpdateOne) SetNillableAssignedBy(v *int64) *UserSubscriptionUpdateOne {
  806. if v != nil {
  807. _u.SetAssignedBy(*v)
  808. }
  809. return _u
  810. }
  811. // ClearAssignedBy clears the value of the "assigned_by" field.
  812. func (_u *UserSubscriptionUpdateOne) ClearAssignedBy() *UserSubscriptionUpdateOne {
  813. _u.mutation.ClearAssignedBy()
  814. return _u
  815. }
  816. // SetAssignedAt sets the "assigned_at" field.
  817. func (_u *UserSubscriptionUpdateOne) SetAssignedAt(v time.Time) *UserSubscriptionUpdateOne {
  818. _u.mutation.SetAssignedAt(v)
  819. return _u
  820. }
  821. // SetNillableAssignedAt sets the "assigned_at" field if the given value is not nil.
  822. func (_u *UserSubscriptionUpdateOne) SetNillableAssignedAt(v *time.Time) *UserSubscriptionUpdateOne {
  823. if v != nil {
  824. _u.SetAssignedAt(*v)
  825. }
  826. return _u
  827. }
  828. // SetNotes sets the "notes" field.
  829. func (_u *UserSubscriptionUpdateOne) SetNotes(v string) *UserSubscriptionUpdateOne {
  830. _u.mutation.SetNotes(v)
  831. return _u
  832. }
  833. // SetNillableNotes sets the "notes" field if the given value is not nil.
  834. func (_u *UserSubscriptionUpdateOne) SetNillableNotes(v *string) *UserSubscriptionUpdateOne {
  835. if v != nil {
  836. _u.SetNotes(*v)
  837. }
  838. return _u
  839. }
  840. // ClearNotes clears the value of the "notes" field.
  841. func (_u *UserSubscriptionUpdateOne) ClearNotes() *UserSubscriptionUpdateOne {
  842. _u.mutation.ClearNotes()
  843. return _u
  844. }
  845. // SetUser sets the "user" edge to the User entity.
  846. func (_u *UserSubscriptionUpdateOne) SetUser(v *User) *UserSubscriptionUpdateOne {
  847. return _u.SetUserID(v.ID)
  848. }
  849. // SetGroup sets the "group" edge to the Group entity.
  850. func (_u *UserSubscriptionUpdateOne) SetGroup(v *Group) *UserSubscriptionUpdateOne {
  851. return _u.SetGroupID(v.ID)
  852. }
  853. // SetAssignedByUserID sets the "assigned_by_user" edge to the User entity by ID.
  854. func (_u *UserSubscriptionUpdateOne) SetAssignedByUserID(id int64) *UserSubscriptionUpdateOne {
  855. _u.mutation.SetAssignedByUserID(id)
  856. return _u
  857. }
  858. // SetNillableAssignedByUserID sets the "assigned_by_user" edge to the User entity by ID if the given value is not nil.
  859. func (_u *UserSubscriptionUpdateOne) SetNillableAssignedByUserID(id *int64) *UserSubscriptionUpdateOne {
  860. if id != nil {
  861. _u = _u.SetAssignedByUserID(*id)
  862. }
  863. return _u
  864. }
  865. // SetAssignedByUser sets the "assigned_by_user" edge to the User entity.
  866. func (_u *UserSubscriptionUpdateOne) SetAssignedByUser(v *User) *UserSubscriptionUpdateOne {
  867. return _u.SetAssignedByUserID(v.ID)
  868. }
  869. // AddUsageLogIDs adds the "usage_logs" edge to the UsageLog entity by IDs.
  870. func (_u *UserSubscriptionUpdateOne) AddUsageLogIDs(ids ...int64) *UserSubscriptionUpdateOne {
  871. _u.mutation.AddUsageLogIDs(ids...)
  872. return _u
  873. }
  874. // AddUsageLogs adds the "usage_logs" edges to the UsageLog entity.
  875. func (_u *UserSubscriptionUpdateOne) AddUsageLogs(v ...*UsageLog) *UserSubscriptionUpdateOne {
  876. ids := make([]int64, len(v))
  877. for i := range v {
  878. ids[i] = v[i].ID
  879. }
  880. return _u.AddUsageLogIDs(ids...)
  881. }
  882. // Mutation returns the UserSubscriptionMutation object of the builder.
  883. func (_u *UserSubscriptionUpdateOne) Mutation() *UserSubscriptionMutation {
  884. return _u.mutation
  885. }
  886. // ClearUser clears the "user" edge to the User entity.
  887. func (_u *UserSubscriptionUpdateOne) ClearUser() *UserSubscriptionUpdateOne {
  888. _u.mutation.ClearUser()
  889. return _u
  890. }
  891. // ClearGroup clears the "group" edge to the Group entity.
  892. func (_u *UserSubscriptionUpdateOne) ClearGroup() *UserSubscriptionUpdateOne {
  893. _u.mutation.ClearGroup()
  894. return _u
  895. }
  896. // ClearAssignedByUser clears the "assigned_by_user" edge to the User entity.
  897. func (_u *UserSubscriptionUpdateOne) ClearAssignedByUser() *UserSubscriptionUpdateOne {
  898. _u.mutation.ClearAssignedByUser()
  899. return _u
  900. }
  901. // ClearUsageLogs clears all "usage_logs" edges to the UsageLog entity.
  902. func (_u *UserSubscriptionUpdateOne) ClearUsageLogs() *UserSubscriptionUpdateOne {
  903. _u.mutation.ClearUsageLogs()
  904. return _u
  905. }
  906. // RemoveUsageLogIDs removes the "usage_logs" edge to UsageLog entities by IDs.
  907. func (_u *UserSubscriptionUpdateOne) RemoveUsageLogIDs(ids ...int64) *UserSubscriptionUpdateOne {
  908. _u.mutation.RemoveUsageLogIDs(ids...)
  909. return _u
  910. }
  911. // RemoveUsageLogs removes "usage_logs" edges to UsageLog entities.
  912. func (_u *UserSubscriptionUpdateOne) RemoveUsageLogs(v ...*UsageLog) *UserSubscriptionUpdateOne {
  913. ids := make([]int64, len(v))
  914. for i := range v {
  915. ids[i] = v[i].ID
  916. }
  917. return _u.RemoveUsageLogIDs(ids...)
  918. }
  919. // Where appends a list predicates to the UserSubscriptionUpdate builder.
  920. func (_u *UserSubscriptionUpdateOne) Where(ps ...predicate.UserSubscription) *UserSubscriptionUpdateOne {
  921. _u.mutation.Where(ps...)
  922. return _u
  923. }
  924. // Select allows selecting one or more fields (columns) of the returned entity.
  925. // The default is selecting all fields defined in the entity schema.
  926. func (_u *UserSubscriptionUpdateOne) Select(field string, fields ...string) *UserSubscriptionUpdateOne {
  927. _u.fields = append([]string{field}, fields...)
  928. return _u
  929. }
  930. // Save executes the query and returns the updated UserSubscription entity.
  931. func (_u *UserSubscriptionUpdateOne) Save(ctx context.Context) (*UserSubscription, error) {
  932. if err := _u.defaults(); err != nil {
  933. return nil, err
  934. }
  935. return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
  936. }
  937. // SaveX is like Save, but panics if an error occurs.
  938. func (_u *UserSubscriptionUpdateOne) SaveX(ctx context.Context) *UserSubscription {
  939. node, err := _u.Save(ctx)
  940. if err != nil {
  941. panic(err)
  942. }
  943. return node
  944. }
  945. // Exec executes the query on the entity.
  946. func (_u *UserSubscriptionUpdateOne) Exec(ctx context.Context) error {
  947. _, err := _u.Save(ctx)
  948. return err
  949. }
  950. // ExecX is like Exec, but panics if an error occurs.
  951. func (_u *UserSubscriptionUpdateOne) ExecX(ctx context.Context) {
  952. if err := _u.Exec(ctx); err != nil {
  953. panic(err)
  954. }
  955. }
  956. // defaults sets the default values of the builder before save.
  957. func (_u *UserSubscriptionUpdateOne) defaults() error {
  958. if _, ok := _u.mutation.UpdatedAt(); !ok {
  959. if usersubscription.UpdateDefaultUpdatedAt == nil {
  960. return fmt.Errorf("ent: uninitialized usersubscription.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  961. }
  962. v := usersubscription.UpdateDefaultUpdatedAt()
  963. _u.mutation.SetUpdatedAt(v)
  964. }
  965. return nil
  966. }
  967. // check runs all checks and user-defined validators on the builder.
  968. func (_u *UserSubscriptionUpdateOne) check() error {
  969. if v, ok := _u.mutation.Status(); ok {
  970. if err := usersubscription.StatusValidator(v); err != nil {
  971. return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "UserSubscription.status": %w`, err)}
  972. }
  973. }
  974. if _u.mutation.UserCleared() && len(_u.mutation.UserIDs()) > 0 {
  975. return errors.New(`ent: clearing a required unique edge "UserSubscription.user"`)
  976. }
  977. if _u.mutation.GroupCleared() && len(_u.mutation.GroupIDs()) > 0 {
  978. return errors.New(`ent: clearing a required unique edge "UserSubscription.group"`)
  979. }
  980. return nil
  981. }
  982. func (_u *UserSubscriptionUpdateOne) sqlSave(ctx context.Context) (_node *UserSubscription, err error) {
  983. if err := _u.check(); err != nil {
  984. return _node, err
  985. }
  986. _spec := sqlgraph.NewUpdateSpec(usersubscription.Table, usersubscription.Columns, sqlgraph.NewFieldSpec(usersubscription.FieldID, field.TypeInt64))
  987. id, ok := _u.mutation.ID()
  988. if !ok {
  989. return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "UserSubscription.id" for update`)}
  990. }
  991. _spec.Node.ID.Value = id
  992. if fields := _u.fields; len(fields) > 0 {
  993. _spec.Node.Columns = make([]string, 0, len(fields))
  994. _spec.Node.Columns = append(_spec.Node.Columns, usersubscription.FieldID)
  995. for _, f := range fields {
  996. if !usersubscription.ValidColumn(f) {
  997. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  998. }
  999. if f != usersubscription.FieldID {
  1000. _spec.Node.Columns = append(_spec.Node.Columns, f)
  1001. }
  1002. }
  1003. }
  1004. if ps := _u.mutation.predicates; len(ps) > 0 {
  1005. _spec.Predicate = func(selector *sql.Selector) {
  1006. for i := range ps {
  1007. ps[i](selector)
  1008. }
  1009. }
  1010. }
  1011. if value, ok := _u.mutation.UpdatedAt(); ok {
  1012. _spec.SetField(usersubscription.FieldUpdatedAt, field.TypeTime, value)
  1013. }
  1014. if value, ok := _u.mutation.DeletedAt(); ok {
  1015. _spec.SetField(usersubscription.FieldDeletedAt, field.TypeTime, value)
  1016. }
  1017. if _u.mutation.DeletedAtCleared() {
  1018. _spec.ClearField(usersubscription.FieldDeletedAt, field.TypeTime)
  1019. }
  1020. if value, ok := _u.mutation.StartsAt(); ok {
  1021. _spec.SetField(usersubscription.FieldStartsAt, field.TypeTime, value)
  1022. }
  1023. if value, ok := _u.mutation.ExpiresAt(); ok {
  1024. _spec.SetField(usersubscription.FieldExpiresAt, field.TypeTime, value)
  1025. }
  1026. if value, ok := _u.mutation.Status(); ok {
  1027. _spec.SetField(usersubscription.FieldStatus, field.TypeString, value)
  1028. }
  1029. if value, ok := _u.mutation.DailyWindowStart(); ok {
  1030. _spec.SetField(usersubscription.FieldDailyWindowStart, field.TypeTime, value)
  1031. }
  1032. if _u.mutation.DailyWindowStartCleared() {
  1033. _spec.ClearField(usersubscription.FieldDailyWindowStart, field.TypeTime)
  1034. }
  1035. if value, ok := _u.mutation.WeeklyWindowStart(); ok {
  1036. _spec.SetField(usersubscription.FieldWeeklyWindowStart, field.TypeTime, value)
  1037. }
  1038. if _u.mutation.WeeklyWindowStartCleared() {
  1039. _spec.ClearField(usersubscription.FieldWeeklyWindowStart, field.TypeTime)
  1040. }
  1041. if value, ok := _u.mutation.MonthlyWindowStart(); ok {
  1042. _spec.SetField(usersubscription.FieldMonthlyWindowStart, field.TypeTime, value)
  1043. }
  1044. if _u.mutation.MonthlyWindowStartCleared() {
  1045. _spec.ClearField(usersubscription.FieldMonthlyWindowStart, field.TypeTime)
  1046. }
  1047. if value, ok := _u.mutation.DailyUsageUsd(); ok {
  1048. _spec.SetField(usersubscription.FieldDailyUsageUsd, field.TypeFloat64, value)
  1049. }
  1050. if value, ok := _u.mutation.AddedDailyUsageUsd(); ok {
  1051. _spec.AddField(usersubscription.FieldDailyUsageUsd, field.TypeFloat64, value)
  1052. }
  1053. if value, ok := _u.mutation.WeeklyUsageUsd(); ok {
  1054. _spec.SetField(usersubscription.FieldWeeklyUsageUsd, field.TypeFloat64, value)
  1055. }
  1056. if value, ok := _u.mutation.AddedWeeklyUsageUsd(); ok {
  1057. _spec.AddField(usersubscription.FieldWeeklyUsageUsd, field.TypeFloat64, value)
  1058. }
  1059. if value, ok := _u.mutation.MonthlyUsageUsd(); ok {
  1060. _spec.SetField(usersubscription.FieldMonthlyUsageUsd, field.TypeFloat64, value)
  1061. }
  1062. if value, ok := _u.mutation.AddedMonthlyUsageUsd(); ok {
  1063. _spec.AddField(usersubscription.FieldMonthlyUsageUsd, field.TypeFloat64, value)
  1064. }
  1065. if value, ok := _u.mutation.AssignedAt(); ok {
  1066. _spec.SetField(usersubscription.FieldAssignedAt, field.TypeTime, value)
  1067. }
  1068. if value, ok := _u.mutation.Notes(); ok {
  1069. _spec.SetField(usersubscription.FieldNotes, field.TypeString, value)
  1070. }
  1071. if _u.mutation.NotesCleared() {
  1072. _spec.ClearField(usersubscription.FieldNotes, field.TypeString)
  1073. }
  1074. if _u.mutation.UserCleared() {
  1075. edge := &sqlgraph.EdgeSpec{
  1076. Rel: sqlgraph.M2O,
  1077. Inverse: true,
  1078. Table: usersubscription.UserTable,
  1079. Columns: []string{usersubscription.UserColumn},
  1080. Bidi: false,
  1081. Target: &sqlgraph.EdgeTarget{
  1082. IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
  1083. },
  1084. }
  1085. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  1086. }
  1087. if nodes := _u.mutation.UserIDs(); len(nodes) > 0 {
  1088. edge := &sqlgraph.EdgeSpec{
  1089. Rel: sqlgraph.M2O,
  1090. Inverse: true,
  1091. Table: usersubscription.UserTable,
  1092. Columns: []string{usersubscription.UserColumn},
  1093. Bidi: false,
  1094. Target: &sqlgraph.EdgeTarget{
  1095. IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
  1096. },
  1097. }
  1098. for _, k := range nodes {
  1099. edge.Target.Nodes = append(edge.Target.Nodes, k)
  1100. }
  1101. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  1102. }
  1103. if _u.mutation.GroupCleared() {
  1104. edge := &sqlgraph.EdgeSpec{
  1105. Rel: sqlgraph.M2O,
  1106. Inverse: true,
  1107. Table: usersubscription.GroupTable,
  1108. Columns: []string{usersubscription.GroupColumn},
  1109. Bidi: false,
  1110. Target: &sqlgraph.EdgeTarget{
  1111. IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
  1112. },
  1113. }
  1114. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  1115. }
  1116. if nodes := _u.mutation.GroupIDs(); len(nodes) > 0 {
  1117. edge := &sqlgraph.EdgeSpec{
  1118. Rel: sqlgraph.M2O,
  1119. Inverse: true,
  1120. Table: usersubscription.GroupTable,
  1121. Columns: []string{usersubscription.GroupColumn},
  1122. Bidi: false,
  1123. Target: &sqlgraph.EdgeTarget{
  1124. IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
  1125. },
  1126. }
  1127. for _, k := range nodes {
  1128. edge.Target.Nodes = append(edge.Target.Nodes, k)
  1129. }
  1130. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  1131. }
  1132. if _u.mutation.AssignedByUserCleared() {
  1133. edge := &sqlgraph.EdgeSpec{
  1134. Rel: sqlgraph.M2O,
  1135. Inverse: true,
  1136. Table: usersubscription.AssignedByUserTable,
  1137. Columns: []string{usersubscription.AssignedByUserColumn},
  1138. Bidi: false,
  1139. Target: &sqlgraph.EdgeTarget{
  1140. IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
  1141. },
  1142. }
  1143. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  1144. }
  1145. if nodes := _u.mutation.AssignedByUserIDs(); len(nodes) > 0 {
  1146. edge := &sqlgraph.EdgeSpec{
  1147. Rel: sqlgraph.M2O,
  1148. Inverse: true,
  1149. Table: usersubscription.AssignedByUserTable,
  1150. Columns: []string{usersubscription.AssignedByUserColumn},
  1151. Bidi: false,
  1152. Target: &sqlgraph.EdgeTarget{
  1153. IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
  1154. },
  1155. }
  1156. for _, k := range nodes {
  1157. edge.Target.Nodes = append(edge.Target.Nodes, k)
  1158. }
  1159. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  1160. }
  1161. if _u.mutation.UsageLogsCleared() {
  1162. edge := &sqlgraph.EdgeSpec{
  1163. Rel: sqlgraph.O2M,
  1164. Inverse: false,
  1165. Table: usersubscription.UsageLogsTable,
  1166. Columns: []string{usersubscription.UsageLogsColumn},
  1167. Bidi: false,
  1168. Target: &sqlgraph.EdgeTarget{
  1169. IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
  1170. },
  1171. }
  1172. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  1173. }
  1174. if nodes := _u.mutation.RemovedUsageLogsIDs(); len(nodes) > 0 && !_u.mutation.UsageLogsCleared() {
  1175. edge := &sqlgraph.EdgeSpec{
  1176. Rel: sqlgraph.O2M,
  1177. Inverse: false,
  1178. Table: usersubscription.UsageLogsTable,
  1179. Columns: []string{usersubscription.UsageLogsColumn},
  1180. Bidi: false,
  1181. Target: &sqlgraph.EdgeTarget{
  1182. IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
  1183. },
  1184. }
  1185. for _, k := range nodes {
  1186. edge.Target.Nodes = append(edge.Target.Nodes, k)
  1187. }
  1188. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  1189. }
  1190. if nodes := _u.mutation.UsageLogsIDs(); len(nodes) > 0 {
  1191. edge := &sqlgraph.EdgeSpec{
  1192. Rel: sqlgraph.O2M,
  1193. Inverse: false,
  1194. Table: usersubscription.UsageLogsTable,
  1195. Columns: []string{usersubscription.UsageLogsColumn},
  1196. Bidi: false,
  1197. Target: &sqlgraph.EdgeTarget{
  1198. IDSpec: sqlgraph.NewFieldSpec(usagelog.FieldID, field.TypeInt64),
  1199. },
  1200. }
  1201. for _, k := range nodes {
  1202. edge.Target.Nodes = append(edge.Target.Nodes, k)
  1203. }
  1204. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  1205. }
  1206. _node = &UserSubscription{config: _u.config}
  1207. _spec.Assign = _node.assignValues
  1208. _spec.ScanValues = _node.scanValues
  1209. if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
  1210. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  1211. err = &NotFoundError{usersubscription.Label}
  1212. } else if sqlgraph.IsConstraintError(err) {
  1213. err = &ConstraintError{msg: err.Error(), wrap: err}
  1214. }
  1215. return nil, err
  1216. }
  1217. _u.mutation.done = true
  1218. return _node, nil
  1219. }