| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868 |
- // Code generated by ent, DO NOT EDIT.
- package ent
- import (
- "context"
- "errors"
- "fmt"
- "time"
- "entgo.io/ent/dialect/sql"
- "entgo.io/ent/dialect/sql/sqlgraph"
- "entgo.io/ent/schema/field"
- "github.com/Wei-Shaw/sub2api/ent/announcement"
- "github.com/Wei-Shaw/sub2api/ent/announcementread"
- "github.com/Wei-Shaw/sub2api/ent/predicate"
- "github.com/Wei-Shaw/sub2api/internal/domain"
- )
- // AnnouncementUpdate is the builder for updating Announcement entities.
- type AnnouncementUpdate struct {
- config
- hooks []Hook
- mutation *AnnouncementMutation
- }
- // Where appends a list predicates to the AnnouncementUpdate builder.
- func (_u *AnnouncementUpdate) Where(ps ...predicate.Announcement) *AnnouncementUpdate {
- _u.mutation.Where(ps...)
- return _u
- }
- // SetTitle sets the "title" field.
- func (_u *AnnouncementUpdate) SetTitle(v string) *AnnouncementUpdate {
- _u.mutation.SetTitle(v)
- return _u
- }
- // SetNillableTitle sets the "title" field if the given value is not nil.
- func (_u *AnnouncementUpdate) SetNillableTitle(v *string) *AnnouncementUpdate {
- if v != nil {
- _u.SetTitle(*v)
- }
- return _u
- }
- // SetContent sets the "content" field.
- func (_u *AnnouncementUpdate) SetContent(v string) *AnnouncementUpdate {
- _u.mutation.SetContent(v)
- return _u
- }
- // SetNillableContent sets the "content" field if the given value is not nil.
- func (_u *AnnouncementUpdate) SetNillableContent(v *string) *AnnouncementUpdate {
- if v != nil {
- _u.SetContent(*v)
- }
- return _u
- }
- // SetStatus sets the "status" field.
- func (_u *AnnouncementUpdate) SetStatus(v string) *AnnouncementUpdate {
- _u.mutation.SetStatus(v)
- return _u
- }
- // SetNillableStatus sets the "status" field if the given value is not nil.
- func (_u *AnnouncementUpdate) SetNillableStatus(v *string) *AnnouncementUpdate {
- if v != nil {
- _u.SetStatus(*v)
- }
- return _u
- }
- // SetNotifyMode sets the "notify_mode" field.
- func (_u *AnnouncementUpdate) SetNotifyMode(v string) *AnnouncementUpdate {
- _u.mutation.SetNotifyMode(v)
- return _u
- }
- // SetNillableNotifyMode sets the "notify_mode" field if the given value is not nil.
- func (_u *AnnouncementUpdate) SetNillableNotifyMode(v *string) *AnnouncementUpdate {
- if v != nil {
- _u.SetNotifyMode(*v)
- }
- return _u
- }
- // SetTargeting sets the "targeting" field.
- func (_u *AnnouncementUpdate) SetTargeting(v domain.AnnouncementTargeting) *AnnouncementUpdate {
- _u.mutation.SetTargeting(v)
- return _u
- }
- // SetNillableTargeting sets the "targeting" field if the given value is not nil.
- func (_u *AnnouncementUpdate) SetNillableTargeting(v *domain.AnnouncementTargeting) *AnnouncementUpdate {
- if v != nil {
- _u.SetTargeting(*v)
- }
- return _u
- }
- // ClearTargeting clears the value of the "targeting" field.
- func (_u *AnnouncementUpdate) ClearTargeting() *AnnouncementUpdate {
- _u.mutation.ClearTargeting()
- return _u
- }
- // SetStartsAt sets the "starts_at" field.
- func (_u *AnnouncementUpdate) SetStartsAt(v time.Time) *AnnouncementUpdate {
- _u.mutation.SetStartsAt(v)
- return _u
- }
- // SetNillableStartsAt sets the "starts_at" field if the given value is not nil.
- func (_u *AnnouncementUpdate) SetNillableStartsAt(v *time.Time) *AnnouncementUpdate {
- if v != nil {
- _u.SetStartsAt(*v)
- }
- return _u
- }
- // ClearStartsAt clears the value of the "starts_at" field.
- func (_u *AnnouncementUpdate) ClearStartsAt() *AnnouncementUpdate {
- _u.mutation.ClearStartsAt()
- return _u
- }
- // SetEndsAt sets the "ends_at" field.
- func (_u *AnnouncementUpdate) SetEndsAt(v time.Time) *AnnouncementUpdate {
- _u.mutation.SetEndsAt(v)
- return _u
- }
- // SetNillableEndsAt sets the "ends_at" field if the given value is not nil.
- func (_u *AnnouncementUpdate) SetNillableEndsAt(v *time.Time) *AnnouncementUpdate {
- if v != nil {
- _u.SetEndsAt(*v)
- }
- return _u
- }
- // ClearEndsAt clears the value of the "ends_at" field.
- func (_u *AnnouncementUpdate) ClearEndsAt() *AnnouncementUpdate {
- _u.mutation.ClearEndsAt()
- return _u
- }
- // SetCreatedBy sets the "created_by" field.
- func (_u *AnnouncementUpdate) SetCreatedBy(v int64) *AnnouncementUpdate {
- _u.mutation.ResetCreatedBy()
- _u.mutation.SetCreatedBy(v)
- return _u
- }
- // SetNillableCreatedBy sets the "created_by" field if the given value is not nil.
- func (_u *AnnouncementUpdate) SetNillableCreatedBy(v *int64) *AnnouncementUpdate {
- if v != nil {
- _u.SetCreatedBy(*v)
- }
- return _u
- }
- // AddCreatedBy adds value to the "created_by" field.
- func (_u *AnnouncementUpdate) AddCreatedBy(v int64) *AnnouncementUpdate {
- _u.mutation.AddCreatedBy(v)
- return _u
- }
- // ClearCreatedBy clears the value of the "created_by" field.
- func (_u *AnnouncementUpdate) ClearCreatedBy() *AnnouncementUpdate {
- _u.mutation.ClearCreatedBy()
- return _u
- }
- // SetUpdatedBy sets the "updated_by" field.
- func (_u *AnnouncementUpdate) SetUpdatedBy(v int64) *AnnouncementUpdate {
- _u.mutation.ResetUpdatedBy()
- _u.mutation.SetUpdatedBy(v)
- return _u
- }
- // SetNillableUpdatedBy sets the "updated_by" field if the given value is not nil.
- func (_u *AnnouncementUpdate) SetNillableUpdatedBy(v *int64) *AnnouncementUpdate {
- if v != nil {
- _u.SetUpdatedBy(*v)
- }
- return _u
- }
- // AddUpdatedBy adds value to the "updated_by" field.
- func (_u *AnnouncementUpdate) AddUpdatedBy(v int64) *AnnouncementUpdate {
- _u.mutation.AddUpdatedBy(v)
- return _u
- }
- // ClearUpdatedBy clears the value of the "updated_by" field.
- func (_u *AnnouncementUpdate) ClearUpdatedBy() *AnnouncementUpdate {
- _u.mutation.ClearUpdatedBy()
- return _u
- }
- // SetUpdatedAt sets the "updated_at" field.
- func (_u *AnnouncementUpdate) SetUpdatedAt(v time.Time) *AnnouncementUpdate {
- _u.mutation.SetUpdatedAt(v)
- return _u
- }
- // AddReadIDs adds the "reads" edge to the AnnouncementRead entity by IDs.
- func (_u *AnnouncementUpdate) AddReadIDs(ids ...int64) *AnnouncementUpdate {
- _u.mutation.AddReadIDs(ids...)
- return _u
- }
- // AddReads adds the "reads" edges to the AnnouncementRead entity.
- func (_u *AnnouncementUpdate) AddReads(v ...*AnnouncementRead) *AnnouncementUpdate {
- ids := make([]int64, len(v))
- for i := range v {
- ids[i] = v[i].ID
- }
- return _u.AddReadIDs(ids...)
- }
- // Mutation returns the AnnouncementMutation object of the builder.
- func (_u *AnnouncementUpdate) Mutation() *AnnouncementMutation {
- return _u.mutation
- }
- // ClearReads clears all "reads" edges to the AnnouncementRead entity.
- func (_u *AnnouncementUpdate) ClearReads() *AnnouncementUpdate {
- _u.mutation.ClearReads()
- return _u
- }
- // RemoveReadIDs removes the "reads" edge to AnnouncementRead entities by IDs.
- func (_u *AnnouncementUpdate) RemoveReadIDs(ids ...int64) *AnnouncementUpdate {
- _u.mutation.RemoveReadIDs(ids...)
- return _u
- }
- // RemoveReads removes "reads" edges to AnnouncementRead entities.
- func (_u *AnnouncementUpdate) RemoveReads(v ...*AnnouncementRead) *AnnouncementUpdate {
- ids := make([]int64, len(v))
- for i := range v {
- ids[i] = v[i].ID
- }
- return _u.RemoveReadIDs(ids...)
- }
- // Save executes the query and returns the number of nodes affected by the update operation.
- func (_u *AnnouncementUpdate) Save(ctx context.Context) (int, error) {
- _u.defaults()
- return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
- }
- // SaveX is like Save, but panics if an error occurs.
- func (_u *AnnouncementUpdate) SaveX(ctx context.Context) int {
- affected, err := _u.Save(ctx)
- if err != nil {
- panic(err)
- }
- return affected
- }
- // Exec executes the query.
- func (_u *AnnouncementUpdate) Exec(ctx context.Context) error {
- _, err := _u.Save(ctx)
- return err
- }
- // ExecX is like Exec, but panics if an error occurs.
- func (_u *AnnouncementUpdate) ExecX(ctx context.Context) {
- if err := _u.Exec(ctx); err != nil {
- panic(err)
- }
- }
- // defaults sets the default values of the builder before save.
- func (_u *AnnouncementUpdate) defaults() {
- if _, ok := _u.mutation.UpdatedAt(); !ok {
- v := announcement.UpdateDefaultUpdatedAt()
- _u.mutation.SetUpdatedAt(v)
- }
- }
- // check runs all checks and user-defined validators on the builder.
- func (_u *AnnouncementUpdate) check() error {
- if v, ok := _u.mutation.Title(); ok {
- if err := announcement.TitleValidator(v); err != nil {
- return &ValidationError{Name: "title", err: fmt.Errorf(`ent: validator failed for field "Announcement.title": %w`, err)}
- }
- }
- if v, ok := _u.mutation.Content(); ok {
- if err := announcement.ContentValidator(v); err != nil {
- return &ValidationError{Name: "content", err: fmt.Errorf(`ent: validator failed for field "Announcement.content": %w`, err)}
- }
- }
- if v, ok := _u.mutation.Status(); ok {
- if err := announcement.StatusValidator(v); err != nil {
- return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Announcement.status": %w`, err)}
- }
- }
- if v, ok := _u.mutation.NotifyMode(); ok {
- if err := announcement.NotifyModeValidator(v); err != nil {
- return &ValidationError{Name: "notify_mode", err: fmt.Errorf(`ent: validator failed for field "Announcement.notify_mode": %w`, err)}
- }
- }
- return nil
- }
- func (_u *AnnouncementUpdate) sqlSave(ctx context.Context) (_node int, err error) {
- if err := _u.check(); err != nil {
- return _node, err
- }
- _spec := sqlgraph.NewUpdateSpec(announcement.Table, announcement.Columns, sqlgraph.NewFieldSpec(announcement.FieldID, field.TypeInt64))
- if ps := _u.mutation.predicates; len(ps) > 0 {
- _spec.Predicate = func(selector *sql.Selector) {
- for i := range ps {
- ps[i](selector)
- }
- }
- }
- if value, ok := _u.mutation.Title(); ok {
- _spec.SetField(announcement.FieldTitle, field.TypeString, value)
- }
- if value, ok := _u.mutation.Content(); ok {
- _spec.SetField(announcement.FieldContent, field.TypeString, value)
- }
- if value, ok := _u.mutation.Status(); ok {
- _spec.SetField(announcement.FieldStatus, field.TypeString, value)
- }
- if value, ok := _u.mutation.NotifyMode(); ok {
- _spec.SetField(announcement.FieldNotifyMode, field.TypeString, value)
- }
- if value, ok := _u.mutation.Targeting(); ok {
- _spec.SetField(announcement.FieldTargeting, field.TypeJSON, value)
- }
- if _u.mutation.TargetingCleared() {
- _spec.ClearField(announcement.FieldTargeting, field.TypeJSON)
- }
- if value, ok := _u.mutation.StartsAt(); ok {
- _spec.SetField(announcement.FieldStartsAt, field.TypeTime, value)
- }
- if _u.mutation.StartsAtCleared() {
- _spec.ClearField(announcement.FieldStartsAt, field.TypeTime)
- }
- if value, ok := _u.mutation.EndsAt(); ok {
- _spec.SetField(announcement.FieldEndsAt, field.TypeTime, value)
- }
- if _u.mutation.EndsAtCleared() {
- _spec.ClearField(announcement.FieldEndsAt, field.TypeTime)
- }
- if value, ok := _u.mutation.CreatedBy(); ok {
- _spec.SetField(announcement.FieldCreatedBy, field.TypeInt64, value)
- }
- if value, ok := _u.mutation.AddedCreatedBy(); ok {
- _spec.AddField(announcement.FieldCreatedBy, field.TypeInt64, value)
- }
- if _u.mutation.CreatedByCleared() {
- _spec.ClearField(announcement.FieldCreatedBy, field.TypeInt64)
- }
- if value, ok := _u.mutation.UpdatedBy(); ok {
- _spec.SetField(announcement.FieldUpdatedBy, field.TypeInt64, value)
- }
- if value, ok := _u.mutation.AddedUpdatedBy(); ok {
- _spec.AddField(announcement.FieldUpdatedBy, field.TypeInt64, value)
- }
- if _u.mutation.UpdatedByCleared() {
- _spec.ClearField(announcement.FieldUpdatedBy, field.TypeInt64)
- }
- if value, ok := _u.mutation.UpdatedAt(); ok {
- _spec.SetField(announcement.FieldUpdatedAt, field.TypeTime, value)
- }
- if _u.mutation.ReadsCleared() {
- edge := &sqlgraph.EdgeSpec{
- Rel: sqlgraph.O2M,
- Inverse: false,
- Table: announcement.ReadsTable,
- Columns: []string{announcement.ReadsColumn},
- Bidi: false,
- Target: &sqlgraph.EdgeTarget{
- IDSpec: sqlgraph.NewFieldSpec(announcementread.FieldID, field.TypeInt64),
- },
- }
- _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
- }
- if nodes := _u.mutation.RemovedReadsIDs(); len(nodes) > 0 && !_u.mutation.ReadsCleared() {
- edge := &sqlgraph.EdgeSpec{
- Rel: sqlgraph.O2M,
- Inverse: false,
- Table: announcement.ReadsTable,
- Columns: []string{announcement.ReadsColumn},
- Bidi: false,
- Target: &sqlgraph.EdgeTarget{
- IDSpec: sqlgraph.NewFieldSpec(announcementread.FieldID, field.TypeInt64),
- },
- }
- for _, k := range nodes {
- edge.Target.Nodes = append(edge.Target.Nodes, k)
- }
- _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
- }
- if nodes := _u.mutation.ReadsIDs(); len(nodes) > 0 {
- edge := &sqlgraph.EdgeSpec{
- Rel: sqlgraph.O2M,
- Inverse: false,
- Table: announcement.ReadsTable,
- Columns: []string{announcement.ReadsColumn},
- Bidi: false,
- Target: &sqlgraph.EdgeTarget{
- IDSpec: sqlgraph.NewFieldSpec(announcementread.FieldID, field.TypeInt64),
- },
- }
- for _, k := range nodes {
- edge.Target.Nodes = append(edge.Target.Nodes, k)
- }
- _spec.Edges.Add = append(_spec.Edges.Add, edge)
- }
- if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
- if _, ok := err.(*sqlgraph.NotFoundError); ok {
- err = &NotFoundError{announcement.Label}
- } else if sqlgraph.IsConstraintError(err) {
- err = &ConstraintError{msg: err.Error(), wrap: err}
- }
- return 0, err
- }
- _u.mutation.done = true
- return _node, nil
- }
- // AnnouncementUpdateOne is the builder for updating a single Announcement entity.
- type AnnouncementUpdateOne struct {
- config
- fields []string
- hooks []Hook
- mutation *AnnouncementMutation
- }
- // SetTitle sets the "title" field.
- func (_u *AnnouncementUpdateOne) SetTitle(v string) *AnnouncementUpdateOne {
- _u.mutation.SetTitle(v)
- return _u
- }
- // SetNillableTitle sets the "title" field if the given value is not nil.
- func (_u *AnnouncementUpdateOne) SetNillableTitle(v *string) *AnnouncementUpdateOne {
- if v != nil {
- _u.SetTitle(*v)
- }
- return _u
- }
- // SetContent sets the "content" field.
- func (_u *AnnouncementUpdateOne) SetContent(v string) *AnnouncementUpdateOne {
- _u.mutation.SetContent(v)
- return _u
- }
- // SetNillableContent sets the "content" field if the given value is not nil.
- func (_u *AnnouncementUpdateOne) SetNillableContent(v *string) *AnnouncementUpdateOne {
- if v != nil {
- _u.SetContent(*v)
- }
- return _u
- }
- // SetStatus sets the "status" field.
- func (_u *AnnouncementUpdateOne) SetStatus(v string) *AnnouncementUpdateOne {
- _u.mutation.SetStatus(v)
- return _u
- }
- // SetNillableStatus sets the "status" field if the given value is not nil.
- func (_u *AnnouncementUpdateOne) SetNillableStatus(v *string) *AnnouncementUpdateOne {
- if v != nil {
- _u.SetStatus(*v)
- }
- return _u
- }
- // SetNotifyMode sets the "notify_mode" field.
- func (_u *AnnouncementUpdateOne) SetNotifyMode(v string) *AnnouncementUpdateOne {
- _u.mutation.SetNotifyMode(v)
- return _u
- }
- // SetNillableNotifyMode sets the "notify_mode" field if the given value is not nil.
- func (_u *AnnouncementUpdateOne) SetNillableNotifyMode(v *string) *AnnouncementUpdateOne {
- if v != nil {
- _u.SetNotifyMode(*v)
- }
- return _u
- }
- // SetTargeting sets the "targeting" field.
- func (_u *AnnouncementUpdateOne) SetTargeting(v domain.AnnouncementTargeting) *AnnouncementUpdateOne {
- _u.mutation.SetTargeting(v)
- return _u
- }
- // SetNillableTargeting sets the "targeting" field if the given value is not nil.
- func (_u *AnnouncementUpdateOne) SetNillableTargeting(v *domain.AnnouncementTargeting) *AnnouncementUpdateOne {
- if v != nil {
- _u.SetTargeting(*v)
- }
- return _u
- }
- // ClearTargeting clears the value of the "targeting" field.
- func (_u *AnnouncementUpdateOne) ClearTargeting() *AnnouncementUpdateOne {
- _u.mutation.ClearTargeting()
- return _u
- }
- // SetStartsAt sets the "starts_at" field.
- func (_u *AnnouncementUpdateOne) SetStartsAt(v time.Time) *AnnouncementUpdateOne {
- _u.mutation.SetStartsAt(v)
- return _u
- }
- // SetNillableStartsAt sets the "starts_at" field if the given value is not nil.
- func (_u *AnnouncementUpdateOne) SetNillableStartsAt(v *time.Time) *AnnouncementUpdateOne {
- if v != nil {
- _u.SetStartsAt(*v)
- }
- return _u
- }
- // ClearStartsAt clears the value of the "starts_at" field.
- func (_u *AnnouncementUpdateOne) ClearStartsAt() *AnnouncementUpdateOne {
- _u.mutation.ClearStartsAt()
- return _u
- }
- // SetEndsAt sets the "ends_at" field.
- func (_u *AnnouncementUpdateOne) SetEndsAt(v time.Time) *AnnouncementUpdateOne {
- _u.mutation.SetEndsAt(v)
- return _u
- }
- // SetNillableEndsAt sets the "ends_at" field if the given value is not nil.
- func (_u *AnnouncementUpdateOne) SetNillableEndsAt(v *time.Time) *AnnouncementUpdateOne {
- if v != nil {
- _u.SetEndsAt(*v)
- }
- return _u
- }
- // ClearEndsAt clears the value of the "ends_at" field.
- func (_u *AnnouncementUpdateOne) ClearEndsAt() *AnnouncementUpdateOne {
- _u.mutation.ClearEndsAt()
- return _u
- }
- // SetCreatedBy sets the "created_by" field.
- func (_u *AnnouncementUpdateOne) SetCreatedBy(v int64) *AnnouncementUpdateOne {
- _u.mutation.ResetCreatedBy()
- _u.mutation.SetCreatedBy(v)
- return _u
- }
- // SetNillableCreatedBy sets the "created_by" field if the given value is not nil.
- func (_u *AnnouncementUpdateOne) SetNillableCreatedBy(v *int64) *AnnouncementUpdateOne {
- if v != nil {
- _u.SetCreatedBy(*v)
- }
- return _u
- }
- // AddCreatedBy adds value to the "created_by" field.
- func (_u *AnnouncementUpdateOne) AddCreatedBy(v int64) *AnnouncementUpdateOne {
- _u.mutation.AddCreatedBy(v)
- return _u
- }
- // ClearCreatedBy clears the value of the "created_by" field.
- func (_u *AnnouncementUpdateOne) ClearCreatedBy() *AnnouncementUpdateOne {
- _u.mutation.ClearCreatedBy()
- return _u
- }
- // SetUpdatedBy sets the "updated_by" field.
- func (_u *AnnouncementUpdateOne) SetUpdatedBy(v int64) *AnnouncementUpdateOne {
- _u.mutation.ResetUpdatedBy()
- _u.mutation.SetUpdatedBy(v)
- return _u
- }
- // SetNillableUpdatedBy sets the "updated_by" field if the given value is not nil.
- func (_u *AnnouncementUpdateOne) SetNillableUpdatedBy(v *int64) *AnnouncementUpdateOne {
- if v != nil {
- _u.SetUpdatedBy(*v)
- }
- return _u
- }
- // AddUpdatedBy adds value to the "updated_by" field.
- func (_u *AnnouncementUpdateOne) AddUpdatedBy(v int64) *AnnouncementUpdateOne {
- _u.mutation.AddUpdatedBy(v)
- return _u
- }
- // ClearUpdatedBy clears the value of the "updated_by" field.
- func (_u *AnnouncementUpdateOne) ClearUpdatedBy() *AnnouncementUpdateOne {
- _u.mutation.ClearUpdatedBy()
- return _u
- }
- // SetUpdatedAt sets the "updated_at" field.
- func (_u *AnnouncementUpdateOne) SetUpdatedAt(v time.Time) *AnnouncementUpdateOne {
- _u.mutation.SetUpdatedAt(v)
- return _u
- }
- // AddReadIDs adds the "reads" edge to the AnnouncementRead entity by IDs.
- func (_u *AnnouncementUpdateOne) AddReadIDs(ids ...int64) *AnnouncementUpdateOne {
- _u.mutation.AddReadIDs(ids...)
- return _u
- }
- // AddReads adds the "reads" edges to the AnnouncementRead entity.
- func (_u *AnnouncementUpdateOne) AddReads(v ...*AnnouncementRead) *AnnouncementUpdateOne {
- ids := make([]int64, len(v))
- for i := range v {
- ids[i] = v[i].ID
- }
- return _u.AddReadIDs(ids...)
- }
- // Mutation returns the AnnouncementMutation object of the builder.
- func (_u *AnnouncementUpdateOne) Mutation() *AnnouncementMutation {
- return _u.mutation
- }
- // ClearReads clears all "reads" edges to the AnnouncementRead entity.
- func (_u *AnnouncementUpdateOne) ClearReads() *AnnouncementUpdateOne {
- _u.mutation.ClearReads()
- return _u
- }
- // RemoveReadIDs removes the "reads" edge to AnnouncementRead entities by IDs.
- func (_u *AnnouncementUpdateOne) RemoveReadIDs(ids ...int64) *AnnouncementUpdateOne {
- _u.mutation.RemoveReadIDs(ids...)
- return _u
- }
- // RemoveReads removes "reads" edges to AnnouncementRead entities.
- func (_u *AnnouncementUpdateOne) RemoveReads(v ...*AnnouncementRead) *AnnouncementUpdateOne {
- ids := make([]int64, len(v))
- for i := range v {
- ids[i] = v[i].ID
- }
- return _u.RemoveReadIDs(ids...)
- }
- // Where appends a list predicates to the AnnouncementUpdate builder.
- func (_u *AnnouncementUpdateOne) Where(ps ...predicate.Announcement) *AnnouncementUpdateOne {
- _u.mutation.Where(ps...)
- return _u
- }
- // Select allows selecting one or more fields (columns) of the returned entity.
- // The default is selecting all fields defined in the entity schema.
- func (_u *AnnouncementUpdateOne) Select(field string, fields ...string) *AnnouncementUpdateOne {
- _u.fields = append([]string{field}, fields...)
- return _u
- }
- // Save executes the query and returns the updated Announcement entity.
- func (_u *AnnouncementUpdateOne) Save(ctx context.Context) (*Announcement, error) {
- _u.defaults()
- return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
- }
- // SaveX is like Save, but panics if an error occurs.
- func (_u *AnnouncementUpdateOne) SaveX(ctx context.Context) *Announcement {
- node, err := _u.Save(ctx)
- if err != nil {
- panic(err)
- }
- return node
- }
- // Exec executes the query on the entity.
- func (_u *AnnouncementUpdateOne) Exec(ctx context.Context) error {
- _, err := _u.Save(ctx)
- return err
- }
- // ExecX is like Exec, but panics if an error occurs.
- func (_u *AnnouncementUpdateOne) ExecX(ctx context.Context) {
- if err := _u.Exec(ctx); err != nil {
- panic(err)
- }
- }
- // defaults sets the default values of the builder before save.
- func (_u *AnnouncementUpdateOne) defaults() {
- if _, ok := _u.mutation.UpdatedAt(); !ok {
- v := announcement.UpdateDefaultUpdatedAt()
- _u.mutation.SetUpdatedAt(v)
- }
- }
- // check runs all checks and user-defined validators on the builder.
- func (_u *AnnouncementUpdateOne) check() error {
- if v, ok := _u.mutation.Title(); ok {
- if err := announcement.TitleValidator(v); err != nil {
- return &ValidationError{Name: "title", err: fmt.Errorf(`ent: validator failed for field "Announcement.title": %w`, err)}
- }
- }
- if v, ok := _u.mutation.Content(); ok {
- if err := announcement.ContentValidator(v); err != nil {
- return &ValidationError{Name: "content", err: fmt.Errorf(`ent: validator failed for field "Announcement.content": %w`, err)}
- }
- }
- if v, ok := _u.mutation.Status(); ok {
- if err := announcement.StatusValidator(v); err != nil {
- return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Announcement.status": %w`, err)}
- }
- }
- if v, ok := _u.mutation.NotifyMode(); ok {
- if err := announcement.NotifyModeValidator(v); err != nil {
- return &ValidationError{Name: "notify_mode", err: fmt.Errorf(`ent: validator failed for field "Announcement.notify_mode": %w`, err)}
- }
- }
- return nil
- }
- func (_u *AnnouncementUpdateOne) sqlSave(ctx context.Context) (_node *Announcement, err error) {
- if err := _u.check(); err != nil {
- return _node, err
- }
- _spec := sqlgraph.NewUpdateSpec(announcement.Table, announcement.Columns, sqlgraph.NewFieldSpec(announcement.FieldID, field.TypeInt64))
- id, ok := _u.mutation.ID()
- if !ok {
- return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Announcement.id" for update`)}
- }
- _spec.Node.ID.Value = id
- if fields := _u.fields; len(fields) > 0 {
- _spec.Node.Columns = make([]string, 0, len(fields))
- _spec.Node.Columns = append(_spec.Node.Columns, announcement.FieldID)
- for _, f := range fields {
- if !announcement.ValidColumn(f) {
- return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
- }
- if f != announcement.FieldID {
- _spec.Node.Columns = append(_spec.Node.Columns, f)
- }
- }
- }
- if ps := _u.mutation.predicates; len(ps) > 0 {
- _spec.Predicate = func(selector *sql.Selector) {
- for i := range ps {
- ps[i](selector)
- }
- }
- }
- if value, ok := _u.mutation.Title(); ok {
- _spec.SetField(announcement.FieldTitle, field.TypeString, value)
- }
- if value, ok := _u.mutation.Content(); ok {
- _spec.SetField(announcement.FieldContent, field.TypeString, value)
- }
- if value, ok := _u.mutation.Status(); ok {
- _spec.SetField(announcement.FieldStatus, field.TypeString, value)
- }
- if value, ok := _u.mutation.NotifyMode(); ok {
- _spec.SetField(announcement.FieldNotifyMode, field.TypeString, value)
- }
- if value, ok := _u.mutation.Targeting(); ok {
- _spec.SetField(announcement.FieldTargeting, field.TypeJSON, value)
- }
- if _u.mutation.TargetingCleared() {
- _spec.ClearField(announcement.FieldTargeting, field.TypeJSON)
- }
- if value, ok := _u.mutation.StartsAt(); ok {
- _spec.SetField(announcement.FieldStartsAt, field.TypeTime, value)
- }
- if _u.mutation.StartsAtCleared() {
- _spec.ClearField(announcement.FieldStartsAt, field.TypeTime)
- }
- if value, ok := _u.mutation.EndsAt(); ok {
- _spec.SetField(announcement.FieldEndsAt, field.TypeTime, value)
- }
- if _u.mutation.EndsAtCleared() {
- _spec.ClearField(announcement.FieldEndsAt, field.TypeTime)
- }
- if value, ok := _u.mutation.CreatedBy(); ok {
- _spec.SetField(announcement.FieldCreatedBy, field.TypeInt64, value)
- }
- if value, ok := _u.mutation.AddedCreatedBy(); ok {
- _spec.AddField(announcement.FieldCreatedBy, field.TypeInt64, value)
- }
- if _u.mutation.CreatedByCleared() {
- _spec.ClearField(announcement.FieldCreatedBy, field.TypeInt64)
- }
- if value, ok := _u.mutation.UpdatedBy(); ok {
- _spec.SetField(announcement.FieldUpdatedBy, field.TypeInt64, value)
- }
- if value, ok := _u.mutation.AddedUpdatedBy(); ok {
- _spec.AddField(announcement.FieldUpdatedBy, field.TypeInt64, value)
- }
- if _u.mutation.UpdatedByCleared() {
- _spec.ClearField(announcement.FieldUpdatedBy, field.TypeInt64)
- }
- if value, ok := _u.mutation.UpdatedAt(); ok {
- _spec.SetField(announcement.FieldUpdatedAt, field.TypeTime, value)
- }
- if _u.mutation.ReadsCleared() {
- edge := &sqlgraph.EdgeSpec{
- Rel: sqlgraph.O2M,
- Inverse: false,
- Table: announcement.ReadsTable,
- Columns: []string{announcement.ReadsColumn},
- Bidi: false,
- Target: &sqlgraph.EdgeTarget{
- IDSpec: sqlgraph.NewFieldSpec(announcementread.FieldID, field.TypeInt64),
- },
- }
- _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
- }
- if nodes := _u.mutation.RemovedReadsIDs(); len(nodes) > 0 && !_u.mutation.ReadsCleared() {
- edge := &sqlgraph.EdgeSpec{
- Rel: sqlgraph.O2M,
- Inverse: false,
- Table: announcement.ReadsTable,
- Columns: []string{announcement.ReadsColumn},
- Bidi: false,
- Target: &sqlgraph.EdgeTarget{
- IDSpec: sqlgraph.NewFieldSpec(announcementread.FieldID, field.TypeInt64),
- },
- }
- for _, k := range nodes {
- edge.Target.Nodes = append(edge.Target.Nodes, k)
- }
- _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
- }
- if nodes := _u.mutation.ReadsIDs(); len(nodes) > 0 {
- edge := &sqlgraph.EdgeSpec{
- Rel: sqlgraph.O2M,
- Inverse: false,
- Table: announcement.ReadsTable,
- Columns: []string{announcement.ReadsColumn},
- Bidi: false,
- Target: &sqlgraph.EdgeTarget{
- IDSpec: sqlgraph.NewFieldSpec(announcementread.FieldID, field.TypeInt64),
- },
- }
- for _, k := range nodes {
- edge.Target.Nodes = append(edge.Target.Nodes, k)
- }
- _spec.Edges.Add = append(_spec.Edges.Add, edge)
- }
- _node = &Announcement{config: _u.config}
- _spec.Assign = _node.assignValues
- _spec.ScanValues = _node.scanValues
- if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
- if _, ok := err.(*sqlgraph.NotFoundError); ok {
- err = &NotFoundError{announcement.Label}
- } else if sqlgraph.IsConstraintError(err) {
- err = &ConstraintError{msg: err.Error(), wrap: err}
- }
- return nil, err
- }
- _u.mutation.done = true
- return _node, nil
- }
|