// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "time" "tss-rocks-be/ent/category" "tss-rocks-be/ent/daily" "tss-rocks-be/ent/dailycontent" "tss-rocks-be/ent/predicate" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" ) // DailyUpdate is the builder for updating Daily entities. type DailyUpdate struct { config hooks []Hook mutation *DailyMutation } // Where appends a list predicates to the DailyUpdate builder. func (du *DailyUpdate) Where(ps ...predicate.Daily) *DailyUpdate { du.mutation.Where(ps...) return du } // SetImageURL sets the "image_url" field. func (du *DailyUpdate) SetImageURL(s string) *DailyUpdate { du.mutation.SetImageURL(s) return du } // SetNillableImageURL sets the "image_url" field if the given value is not nil. func (du *DailyUpdate) SetNillableImageURL(s *string) *DailyUpdate { if s != nil { du.SetImageURL(*s) } return du } // SetCreatedAt sets the "created_at" field. func (du *DailyUpdate) SetCreatedAt(t time.Time) *DailyUpdate { du.mutation.SetCreatedAt(t) return du } // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. func (du *DailyUpdate) SetNillableCreatedAt(t *time.Time) *DailyUpdate { if t != nil { du.SetCreatedAt(*t) } return du } // SetUpdatedAt sets the "updated_at" field. func (du *DailyUpdate) SetUpdatedAt(t time.Time) *DailyUpdate { du.mutation.SetUpdatedAt(t) return du } // SetCategoryID sets the "category" edge to the Category entity by ID. func (du *DailyUpdate) SetCategoryID(id int) *DailyUpdate { du.mutation.SetCategoryID(id) return du } // SetCategory sets the "category" edge to the Category entity. func (du *DailyUpdate) SetCategory(c *Category) *DailyUpdate { return du.SetCategoryID(c.ID) } // AddContentIDs adds the "contents" edge to the DailyContent entity by IDs. func (du *DailyUpdate) AddContentIDs(ids ...int) *DailyUpdate { du.mutation.AddContentIDs(ids...) return du } // AddContents adds the "contents" edges to the DailyContent entity. func (du *DailyUpdate) AddContents(d ...*DailyContent) *DailyUpdate { ids := make([]int, len(d)) for i := range d { ids[i] = d[i].ID } return du.AddContentIDs(ids...) } // Mutation returns the DailyMutation object of the builder. func (du *DailyUpdate) Mutation() *DailyMutation { return du.mutation } // ClearCategory clears the "category" edge to the Category entity. func (du *DailyUpdate) ClearCategory() *DailyUpdate { du.mutation.ClearCategory() return du } // ClearContents clears all "contents" edges to the DailyContent entity. func (du *DailyUpdate) ClearContents() *DailyUpdate { du.mutation.ClearContents() return du } // RemoveContentIDs removes the "contents" edge to DailyContent entities by IDs. func (du *DailyUpdate) RemoveContentIDs(ids ...int) *DailyUpdate { du.mutation.RemoveContentIDs(ids...) return du } // RemoveContents removes "contents" edges to DailyContent entities. func (du *DailyUpdate) RemoveContents(d ...*DailyContent) *DailyUpdate { ids := make([]int, len(d)) for i := range d { ids[i] = d[i].ID } return du.RemoveContentIDs(ids...) } // Save executes the query and returns the number of nodes affected by the update operation. func (du *DailyUpdate) Save(ctx context.Context) (int, error) { du.defaults() return withHooks(ctx, du.sqlSave, du.mutation, du.hooks) } // SaveX is like Save, but panics if an error occurs. func (du *DailyUpdate) SaveX(ctx context.Context) int { affected, err := du.Save(ctx) if err != nil { panic(err) } return affected } // Exec executes the query. func (du *DailyUpdate) Exec(ctx context.Context) error { _, err := du.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (du *DailyUpdate) ExecX(ctx context.Context) { if err := du.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (du *DailyUpdate) defaults() { if _, ok := du.mutation.UpdatedAt(); !ok { v := daily.UpdateDefaultUpdatedAt() du.mutation.SetUpdatedAt(v) } } // check runs all checks and user-defined validators on the builder. func (du *DailyUpdate) check() error { if v, ok := du.mutation.ImageURL(); ok { if err := daily.ImageURLValidator(v); err != nil { return &ValidationError{Name: "image_url", err: fmt.Errorf(`ent: validator failed for field "Daily.image_url": %w`, err)} } } if du.mutation.CategoryCleared() && len(du.mutation.CategoryIDs()) > 0 { return errors.New(`ent: clearing a required unique edge "Daily.category"`) } return nil } func (du *DailyUpdate) sqlSave(ctx context.Context) (n int, err error) { if err := du.check(); err != nil { return n, err } _spec := sqlgraph.NewUpdateSpec(daily.Table, daily.Columns, sqlgraph.NewFieldSpec(daily.FieldID, field.TypeString)) if ps := du.mutation.predicates; len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { for i := range ps { ps[i](selector) } } } if value, ok := du.mutation.ImageURL(); ok { _spec.SetField(daily.FieldImageURL, field.TypeString, value) } if value, ok := du.mutation.CreatedAt(); ok { _spec.SetField(daily.FieldCreatedAt, field.TypeTime, value) } if value, ok := du.mutation.UpdatedAt(); ok { _spec.SetField(daily.FieldUpdatedAt, field.TypeTime, value) } if du.mutation.CategoryCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: daily.CategoryTable, Columns: []string{daily.CategoryColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(category.FieldID, field.TypeInt), }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := du.mutation.CategoryIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: daily.CategoryTable, Columns: []string{daily.CategoryColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(category.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges.Add = append(_spec.Edges.Add, edge) } if du.mutation.ContentsCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: false, Table: daily.ContentsTable, Columns: []string{daily.ContentsColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(dailycontent.FieldID, field.TypeInt), }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := du.mutation.RemovedContentsIDs(); len(nodes) > 0 && !du.mutation.ContentsCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: false, Table: daily.ContentsTable, Columns: []string{daily.ContentsColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(dailycontent.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := du.mutation.ContentsIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: false, Table: daily.ContentsTable, Columns: []string{daily.ContentsColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(dailycontent.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges.Add = append(_spec.Edges.Add, edge) } if n, err = sqlgraph.UpdateNodes(ctx, du.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { err = &NotFoundError{daily.Label} } else if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return 0, err } du.mutation.done = true return n, nil } // DailyUpdateOne is the builder for updating a single Daily entity. type DailyUpdateOne struct { config fields []string hooks []Hook mutation *DailyMutation } // SetImageURL sets the "image_url" field. func (duo *DailyUpdateOne) SetImageURL(s string) *DailyUpdateOne { duo.mutation.SetImageURL(s) return duo } // SetNillableImageURL sets the "image_url" field if the given value is not nil. func (duo *DailyUpdateOne) SetNillableImageURL(s *string) *DailyUpdateOne { if s != nil { duo.SetImageURL(*s) } return duo } // SetCreatedAt sets the "created_at" field. func (duo *DailyUpdateOne) SetCreatedAt(t time.Time) *DailyUpdateOne { duo.mutation.SetCreatedAt(t) return duo } // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. func (duo *DailyUpdateOne) SetNillableCreatedAt(t *time.Time) *DailyUpdateOne { if t != nil { duo.SetCreatedAt(*t) } return duo } // SetUpdatedAt sets the "updated_at" field. func (duo *DailyUpdateOne) SetUpdatedAt(t time.Time) *DailyUpdateOne { duo.mutation.SetUpdatedAt(t) return duo } // SetCategoryID sets the "category" edge to the Category entity by ID. func (duo *DailyUpdateOne) SetCategoryID(id int) *DailyUpdateOne { duo.mutation.SetCategoryID(id) return duo } // SetCategory sets the "category" edge to the Category entity. func (duo *DailyUpdateOne) SetCategory(c *Category) *DailyUpdateOne { return duo.SetCategoryID(c.ID) } // AddContentIDs adds the "contents" edge to the DailyContent entity by IDs. func (duo *DailyUpdateOne) AddContentIDs(ids ...int) *DailyUpdateOne { duo.mutation.AddContentIDs(ids...) return duo } // AddContents adds the "contents" edges to the DailyContent entity. func (duo *DailyUpdateOne) AddContents(d ...*DailyContent) *DailyUpdateOne { ids := make([]int, len(d)) for i := range d { ids[i] = d[i].ID } return duo.AddContentIDs(ids...) } // Mutation returns the DailyMutation object of the builder. func (duo *DailyUpdateOne) Mutation() *DailyMutation { return duo.mutation } // ClearCategory clears the "category" edge to the Category entity. func (duo *DailyUpdateOne) ClearCategory() *DailyUpdateOne { duo.mutation.ClearCategory() return duo } // ClearContents clears all "contents" edges to the DailyContent entity. func (duo *DailyUpdateOne) ClearContents() *DailyUpdateOne { duo.mutation.ClearContents() return duo } // RemoveContentIDs removes the "contents" edge to DailyContent entities by IDs. func (duo *DailyUpdateOne) RemoveContentIDs(ids ...int) *DailyUpdateOne { duo.mutation.RemoveContentIDs(ids...) return duo } // RemoveContents removes "contents" edges to DailyContent entities. func (duo *DailyUpdateOne) RemoveContents(d ...*DailyContent) *DailyUpdateOne { ids := make([]int, len(d)) for i := range d { ids[i] = d[i].ID } return duo.RemoveContentIDs(ids...) } // Where appends a list predicates to the DailyUpdate builder. func (duo *DailyUpdateOne) Where(ps ...predicate.Daily) *DailyUpdateOne { duo.mutation.Where(ps...) return duo } // Select allows selecting one or more fields (columns) of the returned entity. // The default is selecting all fields defined in the entity schema. func (duo *DailyUpdateOne) Select(field string, fields ...string) *DailyUpdateOne { duo.fields = append([]string{field}, fields...) return duo } // Save executes the query and returns the updated Daily entity. func (duo *DailyUpdateOne) Save(ctx context.Context) (*Daily, error) { duo.defaults() return withHooks(ctx, duo.sqlSave, duo.mutation, duo.hooks) } // SaveX is like Save, but panics if an error occurs. func (duo *DailyUpdateOne) SaveX(ctx context.Context) *Daily { node, err := duo.Save(ctx) if err != nil { panic(err) } return node } // Exec executes the query on the entity. func (duo *DailyUpdateOne) Exec(ctx context.Context) error { _, err := duo.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (duo *DailyUpdateOne) ExecX(ctx context.Context) { if err := duo.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (duo *DailyUpdateOne) defaults() { if _, ok := duo.mutation.UpdatedAt(); !ok { v := daily.UpdateDefaultUpdatedAt() duo.mutation.SetUpdatedAt(v) } } // check runs all checks and user-defined validators on the builder. func (duo *DailyUpdateOne) check() error { if v, ok := duo.mutation.ImageURL(); ok { if err := daily.ImageURLValidator(v); err != nil { return &ValidationError{Name: "image_url", err: fmt.Errorf(`ent: validator failed for field "Daily.image_url": %w`, err)} } } if duo.mutation.CategoryCleared() && len(duo.mutation.CategoryIDs()) > 0 { return errors.New(`ent: clearing a required unique edge "Daily.category"`) } return nil } func (duo *DailyUpdateOne) sqlSave(ctx context.Context) (_node *Daily, err error) { if err := duo.check(); err != nil { return _node, err } _spec := sqlgraph.NewUpdateSpec(daily.Table, daily.Columns, sqlgraph.NewFieldSpec(daily.FieldID, field.TypeString)) id, ok := duo.mutation.ID() if !ok { return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Daily.id" for update`)} } _spec.Node.ID.Value = id if fields := duo.fields; len(fields) > 0 { _spec.Node.Columns = make([]string, 0, len(fields)) _spec.Node.Columns = append(_spec.Node.Columns, daily.FieldID) for _, f := range fields { if !daily.ValidColumn(f) { return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} } if f != daily.FieldID { _spec.Node.Columns = append(_spec.Node.Columns, f) } } } if ps := duo.mutation.predicates; len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { for i := range ps { ps[i](selector) } } } if value, ok := duo.mutation.ImageURL(); ok { _spec.SetField(daily.FieldImageURL, field.TypeString, value) } if value, ok := duo.mutation.CreatedAt(); ok { _spec.SetField(daily.FieldCreatedAt, field.TypeTime, value) } if value, ok := duo.mutation.UpdatedAt(); ok { _spec.SetField(daily.FieldUpdatedAt, field.TypeTime, value) } if duo.mutation.CategoryCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: daily.CategoryTable, Columns: []string{daily.CategoryColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(category.FieldID, field.TypeInt), }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := duo.mutation.CategoryIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: daily.CategoryTable, Columns: []string{daily.CategoryColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(category.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges.Add = append(_spec.Edges.Add, edge) } if duo.mutation.ContentsCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: false, Table: daily.ContentsTable, Columns: []string{daily.ContentsColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(dailycontent.FieldID, field.TypeInt), }, } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := duo.mutation.RemovedContentsIDs(); len(nodes) > 0 && !duo.mutation.ContentsCleared() { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: false, Table: daily.ContentsTable, Columns: []string{daily.ContentsColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(dailycontent.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges.Clear = append(_spec.Edges.Clear, edge) } if nodes := duo.mutation.ContentsIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2M, Inverse: false, Table: daily.ContentsTable, Columns: []string{daily.ContentsColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(dailycontent.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges.Add = append(_spec.Edges.Add, edge) } _node = &Daily{config: duo.config} _spec.Assign = _node.assignValues _spec.ScanValues = _node.scanValues if err = sqlgraph.UpdateNode(ctx, duo.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { err = &NotFoundError{daily.Label} } else if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return nil, err } duo.mutation.done = true return _node, nil }