htmgo/examples/todo-list/ent/task_update.go

390 lines
10 KiB
Go
Raw Normal View History

2024-09-19 23:13:04 +00:00
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"todolist/ent/predicate"
"todolist/ent/task"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/dialect/sql/sqljson"
"entgo.io/ent/schema/field"
)
// TaskUpdate is the builder for updating Task entities.
type TaskUpdate struct {
config
hooks []Hook
mutation *TaskMutation
}
// Where appends a list predicates to the TaskUpdate builder.
func (tu *TaskUpdate) Where(ps ...predicate.Task) *TaskUpdate {
tu.mutation.Where(ps...)
return tu
}
// SetName sets the "name" field.
func (tu *TaskUpdate) SetName(s string) *TaskUpdate {
tu.mutation.SetName(s)
return tu
}
// SetNillableName sets the "name" field if the given value is not nil.
func (tu *TaskUpdate) SetNillableName(s *string) *TaskUpdate {
if s != nil {
tu.SetName(*s)
}
return tu
}
// SetCreatedAt sets the "created_at" field.
func (tu *TaskUpdate) SetCreatedAt(t time.Time) *TaskUpdate {
tu.mutation.SetCreatedAt(t)
return tu
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (tu *TaskUpdate) SetNillableCreatedAt(t *time.Time) *TaskUpdate {
if t != nil {
tu.SetCreatedAt(*t)
}
return tu
}
// SetUpdatedAt sets the "updated_at" field.
func (tu *TaskUpdate) SetUpdatedAt(t time.Time) *TaskUpdate {
tu.mutation.SetUpdatedAt(t)
return tu
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (tu *TaskUpdate) SetNillableUpdatedAt(t *time.Time) *TaskUpdate {
if t != nil {
tu.SetUpdatedAt(*t)
}
return tu
}
// SetCompletedAt sets the "completed_at" field.
func (tu *TaskUpdate) SetCompletedAt(t time.Time) *TaskUpdate {
tu.mutation.SetCompletedAt(t)
return tu
}
// SetNillableCompletedAt sets the "completed_at" field if the given value is not nil.
func (tu *TaskUpdate) SetNillableCompletedAt(t *time.Time) *TaskUpdate {
if t != nil {
tu.SetCompletedAt(*t)
}
return tu
}
// ClearCompletedAt clears the value of the "completed_at" field.
func (tu *TaskUpdate) ClearCompletedAt() *TaskUpdate {
tu.mutation.ClearCompletedAt()
return tu
}
// SetTags sets the "tags" field.
func (tu *TaskUpdate) SetTags(s []string) *TaskUpdate {
tu.mutation.SetTags(s)
return tu
}
// AppendTags appends s to the "tags" field.
func (tu *TaskUpdate) AppendTags(s []string) *TaskUpdate {
tu.mutation.AppendTags(s)
return tu
}
// ClearTags clears the value of the "tags" field.
func (tu *TaskUpdate) ClearTags() *TaskUpdate {
tu.mutation.ClearTags()
return tu
}
// Mutation returns the TaskMutation object of the builder.
func (tu *TaskUpdate) Mutation() *TaskMutation {
return tu.mutation
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (tu *TaskUpdate) Save(ctx context.Context) (int, error) {
return withHooks(ctx, tu.sqlSave, tu.mutation, tu.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (tu *TaskUpdate) SaveX(ctx context.Context) int {
affected, err := tu.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (tu *TaskUpdate) Exec(ctx context.Context) error {
_, err := tu.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (tu *TaskUpdate) ExecX(ctx context.Context) {
if err := tu.Exec(ctx); err != nil {
panic(err)
}
}
func (tu *TaskUpdate) sqlSave(ctx context.Context) (n int, err error) {
_spec := sqlgraph.NewUpdateSpec(task.Table, task.Columns, sqlgraph.NewFieldSpec(task.FieldID, field.TypeUUID))
if ps := tu.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := tu.mutation.Name(); ok {
_spec.SetField(task.FieldName, field.TypeString, value)
}
if value, ok := tu.mutation.CreatedAt(); ok {
_spec.SetField(task.FieldCreatedAt, field.TypeTime, value)
}
if value, ok := tu.mutation.UpdatedAt(); ok {
_spec.SetField(task.FieldUpdatedAt, field.TypeTime, value)
}
if value, ok := tu.mutation.CompletedAt(); ok {
_spec.SetField(task.FieldCompletedAt, field.TypeTime, value)
}
if tu.mutation.CompletedAtCleared() {
_spec.ClearField(task.FieldCompletedAt, field.TypeTime)
}
if value, ok := tu.mutation.Tags(); ok {
_spec.SetField(task.FieldTags, field.TypeJSON, value)
}
if value, ok := tu.mutation.AppendedTags(); ok {
_spec.AddModifier(func(u *sql.UpdateBuilder) {
sqljson.Append(u, task.FieldTags, value)
})
}
if tu.mutation.TagsCleared() {
_spec.ClearField(task.FieldTags, field.TypeJSON)
}
if n, err = sqlgraph.UpdateNodes(ctx, tu.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{task.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
tu.mutation.done = true
return n, nil
}
// TaskUpdateOne is the builder for updating a single Task entity.
type TaskUpdateOne struct {
config
fields []string
hooks []Hook
mutation *TaskMutation
}
// SetName sets the "name" field.
func (tuo *TaskUpdateOne) SetName(s string) *TaskUpdateOne {
tuo.mutation.SetName(s)
return tuo
}
// SetNillableName sets the "name" field if the given value is not nil.
func (tuo *TaskUpdateOne) SetNillableName(s *string) *TaskUpdateOne {
if s != nil {
tuo.SetName(*s)
}
return tuo
}
// SetCreatedAt sets the "created_at" field.
func (tuo *TaskUpdateOne) SetCreatedAt(t time.Time) *TaskUpdateOne {
tuo.mutation.SetCreatedAt(t)
return tuo
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (tuo *TaskUpdateOne) SetNillableCreatedAt(t *time.Time) *TaskUpdateOne {
if t != nil {
tuo.SetCreatedAt(*t)
}
return tuo
}
// SetUpdatedAt sets the "updated_at" field.
func (tuo *TaskUpdateOne) SetUpdatedAt(t time.Time) *TaskUpdateOne {
tuo.mutation.SetUpdatedAt(t)
return tuo
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (tuo *TaskUpdateOne) SetNillableUpdatedAt(t *time.Time) *TaskUpdateOne {
if t != nil {
tuo.SetUpdatedAt(*t)
}
return tuo
}
// SetCompletedAt sets the "completed_at" field.
func (tuo *TaskUpdateOne) SetCompletedAt(t time.Time) *TaskUpdateOne {
tuo.mutation.SetCompletedAt(t)
return tuo
}
// SetNillableCompletedAt sets the "completed_at" field if the given value is not nil.
func (tuo *TaskUpdateOne) SetNillableCompletedAt(t *time.Time) *TaskUpdateOne {
if t != nil {
tuo.SetCompletedAt(*t)
}
return tuo
}
// ClearCompletedAt clears the value of the "completed_at" field.
func (tuo *TaskUpdateOne) ClearCompletedAt() *TaskUpdateOne {
tuo.mutation.ClearCompletedAt()
return tuo
}
// SetTags sets the "tags" field.
func (tuo *TaskUpdateOne) SetTags(s []string) *TaskUpdateOne {
tuo.mutation.SetTags(s)
return tuo
}
// AppendTags appends s to the "tags" field.
func (tuo *TaskUpdateOne) AppendTags(s []string) *TaskUpdateOne {
tuo.mutation.AppendTags(s)
return tuo
}
// ClearTags clears the value of the "tags" field.
func (tuo *TaskUpdateOne) ClearTags() *TaskUpdateOne {
tuo.mutation.ClearTags()
return tuo
}
// Mutation returns the TaskMutation object of the builder.
func (tuo *TaskUpdateOne) Mutation() *TaskMutation {
return tuo.mutation
}
// Where appends a list predicates to the TaskUpdate builder.
func (tuo *TaskUpdateOne) Where(ps ...predicate.Task) *TaskUpdateOne {
tuo.mutation.Where(ps...)
return tuo
}
// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (tuo *TaskUpdateOne) Select(field string, fields ...string) *TaskUpdateOne {
tuo.fields = append([]string{field}, fields...)
return tuo
}
// Save executes the query and returns the updated Task entity.
func (tuo *TaskUpdateOne) Save(ctx context.Context) (*Task, error) {
return withHooks(ctx, tuo.sqlSave, tuo.mutation, tuo.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (tuo *TaskUpdateOne) SaveX(ctx context.Context) *Task {
node, err := tuo.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (tuo *TaskUpdateOne) Exec(ctx context.Context) error {
_, err := tuo.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (tuo *TaskUpdateOne) ExecX(ctx context.Context) {
if err := tuo.Exec(ctx); err != nil {
panic(err)
}
}
func (tuo *TaskUpdateOne) sqlSave(ctx context.Context) (_node *Task, err error) {
_spec := sqlgraph.NewUpdateSpec(task.Table, task.Columns, sqlgraph.NewFieldSpec(task.FieldID, field.TypeUUID))
id, ok := tuo.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Task.id" for update`)}
}
_spec.Node.ID.Value = id
if fields := tuo.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, task.FieldID)
for _, f := range fields {
if !task.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != task.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, f)
}
}
}
if ps := tuo.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := tuo.mutation.Name(); ok {
_spec.SetField(task.FieldName, field.TypeString, value)
}
if value, ok := tuo.mutation.CreatedAt(); ok {
_spec.SetField(task.FieldCreatedAt, field.TypeTime, value)
}
if value, ok := tuo.mutation.UpdatedAt(); ok {
_spec.SetField(task.FieldUpdatedAt, field.TypeTime, value)
}
if value, ok := tuo.mutation.CompletedAt(); ok {
_spec.SetField(task.FieldCompletedAt, field.TypeTime, value)
}
if tuo.mutation.CompletedAtCleared() {
_spec.ClearField(task.FieldCompletedAt, field.TypeTime)
}
if value, ok := tuo.mutation.Tags(); ok {
_spec.SetField(task.FieldTags, field.TypeJSON, value)
}
if value, ok := tuo.mutation.AppendedTags(); ok {
_spec.AddModifier(func(u *sql.UpdateBuilder) {
sqljson.Append(u, task.FieldTags, value)
})
}
if tuo.mutation.TagsCleared() {
_spec.ClearField(task.FieldTags, field.TypeJSON)
}
_node = &Task{config: tuo.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues
if err = sqlgraph.UpdateNode(ctx, tuo.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{task.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
tuo.mutation.done = true
return _node, nil
}