announcement_update.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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/announcement"
  12. "github.com/Wei-Shaw/sub2api/ent/announcementread"
  13. "github.com/Wei-Shaw/sub2api/ent/predicate"
  14. "github.com/Wei-Shaw/sub2api/internal/domain"
  15. )
  16. // AnnouncementUpdate is the builder for updating Announcement entities.
  17. type AnnouncementUpdate struct {
  18. config
  19. hooks []Hook
  20. mutation *AnnouncementMutation
  21. }
  22. // Where appends a list predicates to the AnnouncementUpdate builder.
  23. func (_u *AnnouncementUpdate) Where(ps ...predicate.Announcement) *AnnouncementUpdate {
  24. _u.mutation.Where(ps...)
  25. return _u
  26. }
  27. // SetTitle sets the "title" field.
  28. func (_u *AnnouncementUpdate) SetTitle(v string) *AnnouncementUpdate {
  29. _u.mutation.SetTitle(v)
  30. return _u
  31. }
  32. // SetNillableTitle sets the "title" field if the given value is not nil.
  33. func (_u *AnnouncementUpdate) SetNillableTitle(v *string) *AnnouncementUpdate {
  34. if v != nil {
  35. _u.SetTitle(*v)
  36. }
  37. return _u
  38. }
  39. // SetContent sets the "content" field.
  40. func (_u *AnnouncementUpdate) SetContent(v string) *AnnouncementUpdate {
  41. _u.mutation.SetContent(v)
  42. return _u
  43. }
  44. // SetNillableContent sets the "content" field if the given value is not nil.
  45. func (_u *AnnouncementUpdate) SetNillableContent(v *string) *AnnouncementUpdate {
  46. if v != nil {
  47. _u.SetContent(*v)
  48. }
  49. return _u
  50. }
  51. // SetStatus sets the "status" field.
  52. func (_u *AnnouncementUpdate) SetStatus(v string) *AnnouncementUpdate {
  53. _u.mutation.SetStatus(v)
  54. return _u
  55. }
  56. // SetNillableStatus sets the "status" field if the given value is not nil.
  57. func (_u *AnnouncementUpdate) SetNillableStatus(v *string) *AnnouncementUpdate {
  58. if v != nil {
  59. _u.SetStatus(*v)
  60. }
  61. return _u
  62. }
  63. // SetNotifyMode sets the "notify_mode" field.
  64. func (_u *AnnouncementUpdate) SetNotifyMode(v string) *AnnouncementUpdate {
  65. _u.mutation.SetNotifyMode(v)
  66. return _u
  67. }
  68. // SetNillableNotifyMode sets the "notify_mode" field if the given value is not nil.
  69. func (_u *AnnouncementUpdate) SetNillableNotifyMode(v *string) *AnnouncementUpdate {
  70. if v != nil {
  71. _u.SetNotifyMode(*v)
  72. }
  73. return _u
  74. }
  75. // SetTargeting sets the "targeting" field.
  76. func (_u *AnnouncementUpdate) SetTargeting(v domain.AnnouncementTargeting) *AnnouncementUpdate {
  77. _u.mutation.SetTargeting(v)
  78. return _u
  79. }
  80. // SetNillableTargeting sets the "targeting" field if the given value is not nil.
  81. func (_u *AnnouncementUpdate) SetNillableTargeting(v *domain.AnnouncementTargeting) *AnnouncementUpdate {
  82. if v != nil {
  83. _u.SetTargeting(*v)
  84. }
  85. return _u
  86. }
  87. // ClearTargeting clears the value of the "targeting" field.
  88. func (_u *AnnouncementUpdate) ClearTargeting() *AnnouncementUpdate {
  89. _u.mutation.ClearTargeting()
  90. return _u
  91. }
  92. // SetStartsAt sets the "starts_at" field.
  93. func (_u *AnnouncementUpdate) SetStartsAt(v time.Time) *AnnouncementUpdate {
  94. _u.mutation.SetStartsAt(v)
  95. return _u
  96. }
  97. // SetNillableStartsAt sets the "starts_at" field if the given value is not nil.
  98. func (_u *AnnouncementUpdate) SetNillableStartsAt(v *time.Time) *AnnouncementUpdate {
  99. if v != nil {
  100. _u.SetStartsAt(*v)
  101. }
  102. return _u
  103. }
  104. // ClearStartsAt clears the value of the "starts_at" field.
  105. func (_u *AnnouncementUpdate) ClearStartsAt() *AnnouncementUpdate {
  106. _u.mutation.ClearStartsAt()
  107. return _u
  108. }
  109. // SetEndsAt sets the "ends_at" field.
  110. func (_u *AnnouncementUpdate) SetEndsAt(v time.Time) *AnnouncementUpdate {
  111. _u.mutation.SetEndsAt(v)
  112. return _u
  113. }
  114. // SetNillableEndsAt sets the "ends_at" field if the given value is not nil.
  115. func (_u *AnnouncementUpdate) SetNillableEndsAt(v *time.Time) *AnnouncementUpdate {
  116. if v != nil {
  117. _u.SetEndsAt(*v)
  118. }
  119. return _u
  120. }
  121. // ClearEndsAt clears the value of the "ends_at" field.
  122. func (_u *AnnouncementUpdate) ClearEndsAt() *AnnouncementUpdate {
  123. _u.mutation.ClearEndsAt()
  124. return _u
  125. }
  126. // SetCreatedBy sets the "created_by" field.
  127. func (_u *AnnouncementUpdate) SetCreatedBy(v int64) *AnnouncementUpdate {
  128. _u.mutation.ResetCreatedBy()
  129. _u.mutation.SetCreatedBy(v)
  130. return _u
  131. }
  132. // SetNillableCreatedBy sets the "created_by" field if the given value is not nil.
  133. func (_u *AnnouncementUpdate) SetNillableCreatedBy(v *int64) *AnnouncementUpdate {
  134. if v != nil {
  135. _u.SetCreatedBy(*v)
  136. }
  137. return _u
  138. }
  139. // AddCreatedBy adds value to the "created_by" field.
  140. func (_u *AnnouncementUpdate) AddCreatedBy(v int64) *AnnouncementUpdate {
  141. _u.mutation.AddCreatedBy(v)
  142. return _u
  143. }
  144. // ClearCreatedBy clears the value of the "created_by" field.
  145. func (_u *AnnouncementUpdate) ClearCreatedBy() *AnnouncementUpdate {
  146. _u.mutation.ClearCreatedBy()
  147. return _u
  148. }
  149. // SetUpdatedBy sets the "updated_by" field.
  150. func (_u *AnnouncementUpdate) SetUpdatedBy(v int64) *AnnouncementUpdate {
  151. _u.mutation.ResetUpdatedBy()
  152. _u.mutation.SetUpdatedBy(v)
  153. return _u
  154. }
  155. // SetNillableUpdatedBy sets the "updated_by" field if the given value is not nil.
  156. func (_u *AnnouncementUpdate) SetNillableUpdatedBy(v *int64) *AnnouncementUpdate {
  157. if v != nil {
  158. _u.SetUpdatedBy(*v)
  159. }
  160. return _u
  161. }
  162. // AddUpdatedBy adds value to the "updated_by" field.
  163. func (_u *AnnouncementUpdate) AddUpdatedBy(v int64) *AnnouncementUpdate {
  164. _u.mutation.AddUpdatedBy(v)
  165. return _u
  166. }
  167. // ClearUpdatedBy clears the value of the "updated_by" field.
  168. func (_u *AnnouncementUpdate) ClearUpdatedBy() *AnnouncementUpdate {
  169. _u.mutation.ClearUpdatedBy()
  170. return _u
  171. }
  172. // SetUpdatedAt sets the "updated_at" field.
  173. func (_u *AnnouncementUpdate) SetUpdatedAt(v time.Time) *AnnouncementUpdate {
  174. _u.mutation.SetUpdatedAt(v)
  175. return _u
  176. }
  177. // AddReadIDs adds the "reads" edge to the AnnouncementRead entity by IDs.
  178. func (_u *AnnouncementUpdate) AddReadIDs(ids ...int64) *AnnouncementUpdate {
  179. _u.mutation.AddReadIDs(ids...)
  180. return _u
  181. }
  182. // AddReads adds the "reads" edges to the AnnouncementRead entity.
  183. func (_u *AnnouncementUpdate) AddReads(v ...*AnnouncementRead) *AnnouncementUpdate {
  184. ids := make([]int64, len(v))
  185. for i := range v {
  186. ids[i] = v[i].ID
  187. }
  188. return _u.AddReadIDs(ids...)
  189. }
  190. // Mutation returns the AnnouncementMutation object of the builder.
  191. func (_u *AnnouncementUpdate) Mutation() *AnnouncementMutation {
  192. return _u.mutation
  193. }
  194. // ClearReads clears all "reads" edges to the AnnouncementRead entity.
  195. func (_u *AnnouncementUpdate) ClearReads() *AnnouncementUpdate {
  196. _u.mutation.ClearReads()
  197. return _u
  198. }
  199. // RemoveReadIDs removes the "reads" edge to AnnouncementRead entities by IDs.
  200. func (_u *AnnouncementUpdate) RemoveReadIDs(ids ...int64) *AnnouncementUpdate {
  201. _u.mutation.RemoveReadIDs(ids...)
  202. return _u
  203. }
  204. // RemoveReads removes "reads" edges to AnnouncementRead entities.
  205. func (_u *AnnouncementUpdate) RemoveReads(v ...*AnnouncementRead) *AnnouncementUpdate {
  206. ids := make([]int64, len(v))
  207. for i := range v {
  208. ids[i] = v[i].ID
  209. }
  210. return _u.RemoveReadIDs(ids...)
  211. }
  212. // Save executes the query and returns the number of nodes affected by the update operation.
  213. func (_u *AnnouncementUpdate) Save(ctx context.Context) (int, error) {
  214. _u.defaults()
  215. return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
  216. }
  217. // SaveX is like Save, but panics if an error occurs.
  218. func (_u *AnnouncementUpdate) SaveX(ctx context.Context) int {
  219. affected, err := _u.Save(ctx)
  220. if err != nil {
  221. panic(err)
  222. }
  223. return affected
  224. }
  225. // Exec executes the query.
  226. func (_u *AnnouncementUpdate) Exec(ctx context.Context) error {
  227. _, err := _u.Save(ctx)
  228. return err
  229. }
  230. // ExecX is like Exec, but panics if an error occurs.
  231. func (_u *AnnouncementUpdate) ExecX(ctx context.Context) {
  232. if err := _u.Exec(ctx); err != nil {
  233. panic(err)
  234. }
  235. }
  236. // defaults sets the default values of the builder before save.
  237. func (_u *AnnouncementUpdate) defaults() {
  238. if _, ok := _u.mutation.UpdatedAt(); !ok {
  239. v := announcement.UpdateDefaultUpdatedAt()
  240. _u.mutation.SetUpdatedAt(v)
  241. }
  242. }
  243. // check runs all checks and user-defined validators on the builder.
  244. func (_u *AnnouncementUpdate) check() error {
  245. if v, ok := _u.mutation.Title(); ok {
  246. if err := announcement.TitleValidator(v); err != nil {
  247. return &ValidationError{Name: "title", err: fmt.Errorf(`ent: validator failed for field "Announcement.title": %w`, err)}
  248. }
  249. }
  250. if v, ok := _u.mutation.Content(); ok {
  251. if err := announcement.ContentValidator(v); err != nil {
  252. return &ValidationError{Name: "content", err: fmt.Errorf(`ent: validator failed for field "Announcement.content": %w`, err)}
  253. }
  254. }
  255. if v, ok := _u.mutation.Status(); ok {
  256. if err := announcement.StatusValidator(v); err != nil {
  257. return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Announcement.status": %w`, err)}
  258. }
  259. }
  260. if v, ok := _u.mutation.NotifyMode(); ok {
  261. if err := announcement.NotifyModeValidator(v); err != nil {
  262. return &ValidationError{Name: "notify_mode", err: fmt.Errorf(`ent: validator failed for field "Announcement.notify_mode": %w`, err)}
  263. }
  264. }
  265. return nil
  266. }
  267. func (_u *AnnouncementUpdate) sqlSave(ctx context.Context) (_node int, err error) {
  268. if err := _u.check(); err != nil {
  269. return _node, err
  270. }
  271. _spec := sqlgraph.NewUpdateSpec(announcement.Table, announcement.Columns, sqlgraph.NewFieldSpec(announcement.FieldID, field.TypeInt64))
  272. if ps := _u.mutation.predicates; len(ps) > 0 {
  273. _spec.Predicate = func(selector *sql.Selector) {
  274. for i := range ps {
  275. ps[i](selector)
  276. }
  277. }
  278. }
  279. if value, ok := _u.mutation.Title(); ok {
  280. _spec.SetField(announcement.FieldTitle, field.TypeString, value)
  281. }
  282. if value, ok := _u.mutation.Content(); ok {
  283. _spec.SetField(announcement.FieldContent, field.TypeString, value)
  284. }
  285. if value, ok := _u.mutation.Status(); ok {
  286. _spec.SetField(announcement.FieldStatus, field.TypeString, value)
  287. }
  288. if value, ok := _u.mutation.NotifyMode(); ok {
  289. _spec.SetField(announcement.FieldNotifyMode, field.TypeString, value)
  290. }
  291. if value, ok := _u.mutation.Targeting(); ok {
  292. _spec.SetField(announcement.FieldTargeting, field.TypeJSON, value)
  293. }
  294. if _u.mutation.TargetingCleared() {
  295. _spec.ClearField(announcement.FieldTargeting, field.TypeJSON)
  296. }
  297. if value, ok := _u.mutation.StartsAt(); ok {
  298. _spec.SetField(announcement.FieldStartsAt, field.TypeTime, value)
  299. }
  300. if _u.mutation.StartsAtCleared() {
  301. _spec.ClearField(announcement.FieldStartsAt, field.TypeTime)
  302. }
  303. if value, ok := _u.mutation.EndsAt(); ok {
  304. _spec.SetField(announcement.FieldEndsAt, field.TypeTime, value)
  305. }
  306. if _u.mutation.EndsAtCleared() {
  307. _spec.ClearField(announcement.FieldEndsAt, field.TypeTime)
  308. }
  309. if value, ok := _u.mutation.CreatedBy(); ok {
  310. _spec.SetField(announcement.FieldCreatedBy, field.TypeInt64, value)
  311. }
  312. if value, ok := _u.mutation.AddedCreatedBy(); ok {
  313. _spec.AddField(announcement.FieldCreatedBy, field.TypeInt64, value)
  314. }
  315. if _u.mutation.CreatedByCleared() {
  316. _spec.ClearField(announcement.FieldCreatedBy, field.TypeInt64)
  317. }
  318. if value, ok := _u.mutation.UpdatedBy(); ok {
  319. _spec.SetField(announcement.FieldUpdatedBy, field.TypeInt64, value)
  320. }
  321. if value, ok := _u.mutation.AddedUpdatedBy(); ok {
  322. _spec.AddField(announcement.FieldUpdatedBy, field.TypeInt64, value)
  323. }
  324. if _u.mutation.UpdatedByCleared() {
  325. _spec.ClearField(announcement.FieldUpdatedBy, field.TypeInt64)
  326. }
  327. if value, ok := _u.mutation.UpdatedAt(); ok {
  328. _spec.SetField(announcement.FieldUpdatedAt, field.TypeTime, value)
  329. }
  330. if _u.mutation.ReadsCleared() {
  331. edge := &sqlgraph.EdgeSpec{
  332. Rel: sqlgraph.O2M,
  333. Inverse: false,
  334. Table: announcement.ReadsTable,
  335. Columns: []string{announcement.ReadsColumn},
  336. Bidi: false,
  337. Target: &sqlgraph.EdgeTarget{
  338. IDSpec: sqlgraph.NewFieldSpec(announcementread.FieldID, field.TypeInt64),
  339. },
  340. }
  341. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  342. }
  343. if nodes := _u.mutation.RemovedReadsIDs(); len(nodes) > 0 && !_u.mutation.ReadsCleared() {
  344. edge := &sqlgraph.EdgeSpec{
  345. Rel: sqlgraph.O2M,
  346. Inverse: false,
  347. Table: announcement.ReadsTable,
  348. Columns: []string{announcement.ReadsColumn},
  349. Bidi: false,
  350. Target: &sqlgraph.EdgeTarget{
  351. IDSpec: sqlgraph.NewFieldSpec(announcementread.FieldID, field.TypeInt64),
  352. },
  353. }
  354. for _, k := range nodes {
  355. edge.Target.Nodes = append(edge.Target.Nodes, k)
  356. }
  357. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  358. }
  359. if nodes := _u.mutation.ReadsIDs(); len(nodes) > 0 {
  360. edge := &sqlgraph.EdgeSpec{
  361. Rel: sqlgraph.O2M,
  362. Inverse: false,
  363. Table: announcement.ReadsTable,
  364. Columns: []string{announcement.ReadsColumn},
  365. Bidi: false,
  366. Target: &sqlgraph.EdgeTarget{
  367. IDSpec: sqlgraph.NewFieldSpec(announcementread.FieldID, field.TypeInt64),
  368. },
  369. }
  370. for _, k := range nodes {
  371. edge.Target.Nodes = append(edge.Target.Nodes, k)
  372. }
  373. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  374. }
  375. if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
  376. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  377. err = &NotFoundError{announcement.Label}
  378. } else if sqlgraph.IsConstraintError(err) {
  379. err = &ConstraintError{msg: err.Error(), wrap: err}
  380. }
  381. return 0, err
  382. }
  383. _u.mutation.done = true
  384. return _node, nil
  385. }
  386. // AnnouncementUpdateOne is the builder for updating a single Announcement entity.
  387. type AnnouncementUpdateOne struct {
  388. config
  389. fields []string
  390. hooks []Hook
  391. mutation *AnnouncementMutation
  392. }
  393. // SetTitle sets the "title" field.
  394. func (_u *AnnouncementUpdateOne) SetTitle(v string) *AnnouncementUpdateOne {
  395. _u.mutation.SetTitle(v)
  396. return _u
  397. }
  398. // SetNillableTitle sets the "title" field if the given value is not nil.
  399. func (_u *AnnouncementUpdateOne) SetNillableTitle(v *string) *AnnouncementUpdateOne {
  400. if v != nil {
  401. _u.SetTitle(*v)
  402. }
  403. return _u
  404. }
  405. // SetContent sets the "content" field.
  406. func (_u *AnnouncementUpdateOne) SetContent(v string) *AnnouncementUpdateOne {
  407. _u.mutation.SetContent(v)
  408. return _u
  409. }
  410. // SetNillableContent sets the "content" field if the given value is not nil.
  411. func (_u *AnnouncementUpdateOne) SetNillableContent(v *string) *AnnouncementUpdateOne {
  412. if v != nil {
  413. _u.SetContent(*v)
  414. }
  415. return _u
  416. }
  417. // SetStatus sets the "status" field.
  418. func (_u *AnnouncementUpdateOne) SetStatus(v string) *AnnouncementUpdateOne {
  419. _u.mutation.SetStatus(v)
  420. return _u
  421. }
  422. // SetNillableStatus sets the "status" field if the given value is not nil.
  423. func (_u *AnnouncementUpdateOne) SetNillableStatus(v *string) *AnnouncementUpdateOne {
  424. if v != nil {
  425. _u.SetStatus(*v)
  426. }
  427. return _u
  428. }
  429. // SetNotifyMode sets the "notify_mode" field.
  430. func (_u *AnnouncementUpdateOne) SetNotifyMode(v string) *AnnouncementUpdateOne {
  431. _u.mutation.SetNotifyMode(v)
  432. return _u
  433. }
  434. // SetNillableNotifyMode sets the "notify_mode" field if the given value is not nil.
  435. func (_u *AnnouncementUpdateOne) SetNillableNotifyMode(v *string) *AnnouncementUpdateOne {
  436. if v != nil {
  437. _u.SetNotifyMode(*v)
  438. }
  439. return _u
  440. }
  441. // SetTargeting sets the "targeting" field.
  442. func (_u *AnnouncementUpdateOne) SetTargeting(v domain.AnnouncementTargeting) *AnnouncementUpdateOne {
  443. _u.mutation.SetTargeting(v)
  444. return _u
  445. }
  446. // SetNillableTargeting sets the "targeting" field if the given value is not nil.
  447. func (_u *AnnouncementUpdateOne) SetNillableTargeting(v *domain.AnnouncementTargeting) *AnnouncementUpdateOne {
  448. if v != nil {
  449. _u.SetTargeting(*v)
  450. }
  451. return _u
  452. }
  453. // ClearTargeting clears the value of the "targeting" field.
  454. func (_u *AnnouncementUpdateOne) ClearTargeting() *AnnouncementUpdateOne {
  455. _u.mutation.ClearTargeting()
  456. return _u
  457. }
  458. // SetStartsAt sets the "starts_at" field.
  459. func (_u *AnnouncementUpdateOne) SetStartsAt(v time.Time) *AnnouncementUpdateOne {
  460. _u.mutation.SetStartsAt(v)
  461. return _u
  462. }
  463. // SetNillableStartsAt sets the "starts_at" field if the given value is not nil.
  464. func (_u *AnnouncementUpdateOne) SetNillableStartsAt(v *time.Time) *AnnouncementUpdateOne {
  465. if v != nil {
  466. _u.SetStartsAt(*v)
  467. }
  468. return _u
  469. }
  470. // ClearStartsAt clears the value of the "starts_at" field.
  471. func (_u *AnnouncementUpdateOne) ClearStartsAt() *AnnouncementUpdateOne {
  472. _u.mutation.ClearStartsAt()
  473. return _u
  474. }
  475. // SetEndsAt sets the "ends_at" field.
  476. func (_u *AnnouncementUpdateOne) SetEndsAt(v time.Time) *AnnouncementUpdateOne {
  477. _u.mutation.SetEndsAt(v)
  478. return _u
  479. }
  480. // SetNillableEndsAt sets the "ends_at" field if the given value is not nil.
  481. func (_u *AnnouncementUpdateOne) SetNillableEndsAt(v *time.Time) *AnnouncementUpdateOne {
  482. if v != nil {
  483. _u.SetEndsAt(*v)
  484. }
  485. return _u
  486. }
  487. // ClearEndsAt clears the value of the "ends_at" field.
  488. func (_u *AnnouncementUpdateOne) ClearEndsAt() *AnnouncementUpdateOne {
  489. _u.mutation.ClearEndsAt()
  490. return _u
  491. }
  492. // SetCreatedBy sets the "created_by" field.
  493. func (_u *AnnouncementUpdateOne) SetCreatedBy(v int64) *AnnouncementUpdateOne {
  494. _u.mutation.ResetCreatedBy()
  495. _u.mutation.SetCreatedBy(v)
  496. return _u
  497. }
  498. // SetNillableCreatedBy sets the "created_by" field if the given value is not nil.
  499. func (_u *AnnouncementUpdateOne) SetNillableCreatedBy(v *int64) *AnnouncementUpdateOne {
  500. if v != nil {
  501. _u.SetCreatedBy(*v)
  502. }
  503. return _u
  504. }
  505. // AddCreatedBy adds value to the "created_by" field.
  506. func (_u *AnnouncementUpdateOne) AddCreatedBy(v int64) *AnnouncementUpdateOne {
  507. _u.mutation.AddCreatedBy(v)
  508. return _u
  509. }
  510. // ClearCreatedBy clears the value of the "created_by" field.
  511. func (_u *AnnouncementUpdateOne) ClearCreatedBy() *AnnouncementUpdateOne {
  512. _u.mutation.ClearCreatedBy()
  513. return _u
  514. }
  515. // SetUpdatedBy sets the "updated_by" field.
  516. func (_u *AnnouncementUpdateOne) SetUpdatedBy(v int64) *AnnouncementUpdateOne {
  517. _u.mutation.ResetUpdatedBy()
  518. _u.mutation.SetUpdatedBy(v)
  519. return _u
  520. }
  521. // SetNillableUpdatedBy sets the "updated_by" field if the given value is not nil.
  522. func (_u *AnnouncementUpdateOne) SetNillableUpdatedBy(v *int64) *AnnouncementUpdateOne {
  523. if v != nil {
  524. _u.SetUpdatedBy(*v)
  525. }
  526. return _u
  527. }
  528. // AddUpdatedBy adds value to the "updated_by" field.
  529. func (_u *AnnouncementUpdateOne) AddUpdatedBy(v int64) *AnnouncementUpdateOne {
  530. _u.mutation.AddUpdatedBy(v)
  531. return _u
  532. }
  533. // ClearUpdatedBy clears the value of the "updated_by" field.
  534. func (_u *AnnouncementUpdateOne) ClearUpdatedBy() *AnnouncementUpdateOne {
  535. _u.mutation.ClearUpdatedBy()
  536. return _u
  537. }
  538. // SetUpdatedAt sets the "updated_at" field.
  539. func (_u *AnnouncementUpdateOne) SetUpdatedAt(v time.Time) *AnnouncementUpdateOne {
  540. _u.mutation.SetUpdatedAt(v)
  541. return _u
  542. }
  543. // AddReadIDs adds the "reads" edge to the AnnouncementRead entity by IDs.
  544. func (_u *AnnouncementUpdateOne) AddReadIDs(ids ...int64) *AnnouncementUpdateOne {
  545. _u.mutation.AddReadIDs(ids...)
  546. return _u
  547. }
  548. // AddReads adds the "reads" edges to the AnnouncementRead entity.
  549. func (_u *AnnouncementUpdateOne) AddReads(v ...*AnnouncementRead) *AnnouncementUpdateOne {
  550. ids := make([]int64, len(v))
  551. for i := range v {
  552. ids[i] = v[i].ID
  553. }
  554. return _u.AddReadIDs(ids...)
  555. }
  556. // Mutation returns the AnnouncementMutation object of the builder.
  557. func (_u *AnnouncementUpdateOne) Mutation() *AnnouncementMutation {
  558. return _u.mutation
  559. }
  560. // ClearReads clears all "reads" edges to the AnnouncementRead entity.
  561. func (_u *AnnouncementUpdateOne) ClearReads() *AnnouncementUpdateOne {
  562. _u.mutation.ClearReads()
  563. return _u
  564. }
  565. // RemoveReadIDs removes the "reads" edge to AnnouncementRead entities by IDs.
  566. func (_u *AnnouncementUpdateOne) RemoveReadIDs(ids ...int64) *AnnouncementUpdateOne {
  567. _u.mutation.RemoveReadIDs(ids...)
  568. return _u
  569. }
  570. // RemoveReads removes "reads" edges to AnnouncementRead entities.
  571. func (_u *AnnouncementUpdateOne) RemoveReads(v ...*AnnouncementRead) *AnnouncementUpdateOne {
  572. ids := make([]int64, len(v))
  573. for i := range v {
  574. ids[i] = v[i].ID
  575. }
  576. return _u.RemoveReadIDs(ids...)
  577. }
  578. // Where appends a list predicates to the AnnouncementUpdate builder.
  579. func (_u *AnnouncementUpdateOne) Where(ps ...predicate.Announcement) *AnnouncementUpdateOne {
  580. _u.mutation.Where(ps...)
  581. return _u
  582. }
  583. // Select allows selecting one or more fields (columns) of the returned entity.
  584. // The default is selecting all fields defined in the entity schema.
  585. func (_u *AnnouncementUpdateOne) Select(field string, fields ...string) *AnnouncementUpdateOne {
  586. _u.fields = append([]string{field}, fields...)
  587. return _u
  588. }
  589. // Save executes the query and returns the updated Announcement entity.
  590. func (_u *AnnouncementUpdateOne) Save(ctx context.Context) (*Announcement, error) {
  591. _u.defaults()
  592. return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
  593. }
  594. // SaveX is like Save, but panics if an error occurs.
  595. func (_u *AnnouncementUpdateOne) SaveX(ctx context.Context) *Announcement {
  596. node, err := _u.Save(ctx)
  597. if err != nil {
  598. panic(err)
  599. }
  600. return node
  601. }
  602. // Exec executes the query on the entity.
  603. func (_u *AnnouncementUpdateOne) Exec(ctx context.Context) error {
  604. _, err := _u.Save(ctx)
  605. return err
  606. }
  607. // ExecX is like Exec, but panics if an error occurs.
  608. func (_u *AnnouncementUpdateOne) ExecX(ctx context.Context) {
  609. if err := _u.Exec(ctx); err != nil {
  610. panic(err)
  611. }
  612. }
  613. // defaults sets the default values of the builder before save.
  614. func (_u *AnnouncementUpdateOne) defaults() {
  615. if _, ok := _u.mutation.UpdatedAt(); !ok {
  616. v := announcement.UpdateDefaultUpdatedAt()
  617. _u.mutation.SetUpdatedAt(v)
  618. }
  619. }
  620. // check runs all checks and user-defined validators on the builder.
  621. func (_u *AnnouncementUpdateOne) check() error {
  622. if v, ok := _u.mutation.Title(); ok {
  623. if err := announcement.TitleValidator(v); err != nil {
  624. return &ValidationError{Name: "title", err: fmt.Errorf(`ent: validator failed for field "Announcement.title": %w`, err)}
  625. }
  626. }
  627. if v, ok := _u.mutation.Content(); ok {
  628. if err := announcement.ContentValidator(v); err != nil {
  629. return &ValidationError{Name: "content", err: fmt.Errorf(`ent: validator failed for field "Announcement.content": %w`, err)}
  630. }
  631. }
  632. if v, ok := _u.mutation.Status(); ok {
  633. if err := announcement.StatusValidator(v); err != nil {
  634. return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Announcement.status": %w`, err)}
  635. }
  636. }
  637. if v, ok := _u.mutation.NotifyMode(); ok {
  638. if err := announcement.NotifyModeValidator(v); err != nil {
  639. return &ValidationError{Name: "notify_mode", err: fmt.Errorf(`ent: validator failed for field "Announcement.notify_mode": %w`, err)}
  640. }
  641. }
  642. return nil
  643. }
  644. func (_u *AnnouncementUpdateOne) sqlSave(ctx context.Context) (_node *Announcement, err error) {
  645. if err := _u.check(); err != nil {
  646. return _node, err
  647. }
  648. _spec := sqlgraph.NewUpdateSpec(announcement.Table, announcement.Columns, sqlgraph.NewFieldSpec(announcement.FieldID, field.TypeInt64))
  649. id, ok := _u.mutation.ID()
  650. if !ok {
  651. return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Announcement.id" for update`)}
  652. }
  653. _spec.Node.ID.Value = id
  654. if fields := _u.fields; len(fields) > 0 {
  655. _spec.Node.Columns = make([]string, 0, len(fields))
  656. _spec.Node.Columns = append(_spec.Node.Columns, announcement.FieldID)
  657. for _, f := range fields {
  658. if !announcement.ValidColumn(f) {
  659. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  660. }
  661. if f != announcement.FieldID {
  662. _spec.Node.Columns = append(_spec.Node.Columns, f)
  663. }
  664. }
  665. }
  666. if ps := _u.mutation.predicates; len(ps) > 0 {
  667. _spec.Predicate = func(selector *sql.Selector) {
  668. for i := range ps {
  669. ps[i](selector)
  670. }
  671. }
  672. }
  673. if value, ok := _u.mutation.Title(); ok {
  674. _spec.SetField(announcement.FieldTitle, field.TypeString, value)
  675. }
  676. if value, ok := _u.mutation.Content(); ok {
  677. _spec.SetField(announcement.FieldContent, field.TypeString, value)
  678. }
  679. if value, ok := _u.mutation.Status(); ok {
  680. _spec.SetField(announcement.FieldStatus, field.TypeString, value)
  681. }
  682. if value, ok := _u.mutation.NotifyMode(); ok {
  683. _spec.SetField(announcement.FieldNotifyMode, field.TypeString, value)
  684. }
  685. if value, ok := _u.mutation.Targeting(); ok {
  686. _spec.SetField(announcement.FieldTargeting, field.TypeJSON, value)
  687. }
  688. if _u.mutation.TargetingCleared() {
  689. _spec.ClearField(announcement.FieldTargeting, field.TypeJSON)
  690. }
  691. if value, ok := _u.mutation.StartsAt(); ok {
  692. _spec.SetField(announcement.FieldStartsAt, field.TypeTime, value)
  693. }
  694. if _u.mutation.StartsAtCleared() {
  695. _spec.ClearField(announcement.FieldStartsAt, field.TypeTime)
  696. }
  697. if value, ok := _u.mutation.EndsAt(); ok {
  698. _spec.SetField(announcement.FieldEndsAt, field.TypeTime, value)
  699. }
  700. if _u.mutation.EndsAtCleared() {
  701. _spec.ClearField(announcement.FieldEndsAt, field.TypeTime)
  702. }
  703. if value, ok := _u.mutation.CreatedBy(); ok {
  704. _spec.SetField(announcement.FieldCreatedBy, field.TypeInt64, value)
  705. }
  706. if value, ok := _u.mutation.AddedCreatedBy(); ok {
  707. _spec.AddField(announcement.FieldCreatedBy, field.TypeInt64, value)
  708. }
  709. if _u.mutation.CreatedByCleared() {
  710. _spec.ClearField(announcement.FieldCreatedBy, field.TypeInt64)
  711. }
  712. if value, ok := _u.mutation.UpdatedBy(); ok {
  713. _spec.SetField(announcement.FieldUpdatedBy, field.TypeInt64, value)
  714. }
  715. if value, ok := _u.mutation.AddedUpdatedBy(); ok {
  716. _spec.AddField(announcement.FieldUpdatedBy, field.TypeInt64, value)
  717. }
  718. if _u.mutation.UpdatedByCleared() {
  719. _spec.ClearField(announcement.FieldUpdatedBy, field.TypeInt64)
  720. }
  721. if value, ok := _u.mutation.UpdatedAt(); ok {
  722. _spec.SetField(announcement.FieldUpdatedAt, field.TypeTime, value)
  723. }
  724. if _u.mutation.ReadsCleared() {
  725. edge := &sqlgraph.EdgeSpec{
  726. Rel: sqlgraph.O2M,
  727. Inverse: false,
  728. Table: announcement.ReadsTable,
  729. Columns: []string{announcement.ReadsColumn},
  730. Bidi: false,
  731. Target: &sqlgraph.EdgeTarget{
  732. IDSpec: sqlgraph.NewFieldSpec(announcementread.FieldID, field.TypeInt64),
  733. },
  734. }
  735. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  736. }
  737. if nodes := _u.mutation.RemovedReadsIDs(); len(nodes) > 0 && !_u.mutation.ReadsCleared() {
  738. edge := &sqlgraph.EdgeSpec{
  739. Rel: sqlgraph.O2M,
  740. Inverse: false,
  741. Table: announcement.ReadsTable,
  742. Columns: []string{announcement.ReadsColumn},
  743. Bidi: false,
  744. Target: &sqlgraph.EdgeTarget{
  745. IDSpec: sqlgraph.NewFieldSpec(announcementread.FieldID, field.TypeInt64),
  746. },
  747. }
  748. for _, k := range nodes {
  749. edge.Target.Nodes = append(edge.Target.Nodes, k)
  750. }
  751. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  752. }
  753. if nodes := _u.mutation.ReadsIDs(); len(nodes) > 0 {
  754. edge := &sqlgraph.EdgeSpec{
  755. Rel: sqlgraph.O2M,
  756. Inverse: false,
  757. Table: announcement.ReadsTable,
  758. Columns: []string{announcement.ReadsColumn},
  759. Bidi: false,
  760. Target: &sqlgraph.EdgeTarget{
  761. IDSpec: sqlgraph.NewFieldSpec(announcementread.FieldID, field.TypeInt64),
  762. },
  763. }
  764. for _, k := range nodes {
  765. edge.Target.Nodes = append(edge.Target.Nodes, k)
  766. }
  767. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  768. }
  769. _node = &Announcement{config: _u.config}
  770. _spec.Assign = _node.assignValues
  771. _spec.ScanValues = _node.scanValues
  772. if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
  773. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  774. err = &NotFoundError{announcement.Label}
  775. } else if sqlgraph.IsConstraintError(err) {
  776. err = &ConstraintError{msg: err.Error(), wrap: err}
  777. }
  778. return nil, err
  779. }
  780. _u.mutation.done = true
  781. return _node, nil
  782. }