ka/ent/post_update.go

640 lines
18 KiB
Go
Raw Normal View History

2024-10-04 20:22:25 +02:00
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"code.icod.de/dalu/ka/ent/category"
"code.icod.de/dalu/ka/ent/post"
"code.icod.de/dalu/ka/ent/predicate"
"code.icod.de/dalu/ka/ent/profile"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/google/uuid"
)
// PostUpdate is the builder for updating Post entities.
type PostUpdate struct {
config
hooks []Hook
mutation *PostMutation
}
// Where appends a list predicates to the PostUpdate builder.
func (pu *PostUpdate) Where(ps ...predicate.Post) *PostUpdate {
pu.mutation.Where(ps...)
return pu
}
// SetUpdatedAt sets the "updated_at" field.
func (pu *PostUpdate) SetUpdatedAt(t time.Time) *PostUpdate {
pu.mutation.SetUpdatedAt(t)
return pu
}
// ClearUpdatedAt clears the value of the "updated_at" field.
func (pu *PostUpdate) ClearUpdatedAt() *PostUpdate {
pu.mutation.ClearUpdatedAt()
return pu
}
// SetExpires sets the "expires" field.
func (pu *PostUpdate) SetExpires(b bool) *PostUpdate {
pu.mutation.SetExpires(b)
return pu
}
// SetNillableExpires sets the "expires" field if the given value is not nil.
func (pu *PostUpdate) SetNillableExpires(b *bool) *PostUpdate {
if b != nil {
pu.SetExpires(*b)
}
return pu
}
// SetExpireTime sets the "expire_time" field.
func (pu *PostUpdate) SetExpireTime(t time.Time) *PostUpdate {
pu.mutation.SetExpireTime(t)
return pu
}
// SetNillableExpireTime sets the "expire_time" field if the given value is not nil.
func (pu *PostUpdate) SetNillableExpireTime(t *time.Time) *PostUpdate {
if t != nil {
pu.SetExpireTime(*t)
}
return pu
}
// ClearExpireTime clears the value of the "expire_time" field.
func (pu *PostUpdate) ClearExpireTime() *PostUpdate {
pu.mutation.ClearExpireTime()
return pu
}
// SetTitle sets the "title" field.
func (pu *PostUpdate) SetTitle(s string) *PostUpdate {
pu.mutation.SetTitle(s)
return pu
}
// SetNillableTitle sets the "title" field if the given value is not nil.
func (pu *PostUpdate) SetNillableTitle(s *string) *PostUpdate {
if s != nil {
pu.SetTitle(*s)
}
return pu
}
// SetBody sets the "body" field.
func (pu *PostUpdate) SetBody(s string) *PostUpdate {
pu.mutation.SetBody(s)
return pu
}
// SetNillableBody sets the "body" field if the given value is not nil.
func (pu *PostUpdate) SetNillableBody(s *string) *PostUpdate {
if s != nil {
pu.SetBody(*s)
}
return pu
}
// SetCategoryID sets the "category" edge to the Category entity by ID.
func (pu *PostUpdate) SetCategoryID(id uuid.UUID) *PostUpdate {
pu.mutation.SetCategoryID(id)
return pu
}
// SetNillableCategoryID sets the "category" edge to the Category entity by ID if the given value is not nil.
func (pu *PostUpdate) SetNillableCategoryID(id *uuid.UUID) *PostUpdate {
if id != nil {
pu = pu.SetCategoryID(*id)
}
return pu
}
// SetCategory sets the "category" edge to the Category entity.
func (pu *PostUpdate) SetCategory(c *Category) *PostUpdate {
return pu.SetCategoryID(c.ID)
}
// SetProfileID sets the "profile" edge to the Profile entity by ID.
func (pu *PostUpdate) SetProfileID(id uuid.UUID) *PostUpdate {
pu.mutation.SetProfileID(id)
return pu
}
// SetNillableProfileID sets the "profile" edge to the Profile entity by ID if the given value is not nil.
func (pu *PostUpdate) SetNillableProfileID(id *uuid.UUID) *PostUpdate {
if id != nil {
pu = pu.SetProfileID(*id)
}
return pu
}
// SetProfile sets the "profile" edge to the Profile entity.
func (pu *PostUpdate) SetProfile(p *Profile) *PostUpdate {
return pu.SetProfileID(p.ID)
}
// Mutation returns the PostMutation object of the builder.
func (pu *PostUpdate) Mutation() *PostMutation {
return pu.mutation
}
// ClearCategory clears the "category" edge to the Category entity.
func (pu *PostUpdate) ClearCategory() *PostUpdate {
pu.mutation.ClearCategory()
return pu
}
// ClearProfile clears the "profile" edge to the Profile entity.
func (pu *PostUpdate) ClearProfile() *PostUpdate {
pu.mutation.ClearProfile()
return pu
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (pu *PostUpdate) Save(ctx context.Context) (int, error) {
pu.defaults()
return withHooks(ctx, pu.sqlSave, pu.mutation, pu.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (pu *PostUpdate) SaveX(ctx context.Context) int {
affected, err := pu.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (pu *PostUpdate) Exec(ctx context.Context) error {
_, err := pu.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (pu *PostUpdate) ExecX(ctx context.Context) {
if err := pu.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (pu *PostUpdate) defaults() {
if _, ok := pu.mutation.UpdatedAt(); !ok && !pu.mutation.UpdatedAtCleared() {
v := post.UpdateDefaultUpdatedAt()
pu.mutation.SetUpdatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (pu *PostUpdate) check() error {
if v, ok := pu.mutation.Title(); ok {
if err := post.TitleValidator(v); err != nil {
return &ValidationError{Name: "title", err: fmt.Errorf(`ent: validator failed for field "Post.title": %w`, err)}
}
}
if v, ok := pu.mutation.Body(); ok {
if err := post.BodyValidator(v); err != nil {
return &ValidationError{Name: "body", err: fmt.Errorf(`ent: validator failed for field "Post.body": %w`, err)}
}
}
return nil
}
func (pu *PostUpdate) sqlSave(ctx context.Context) (n int, err error) {
if err := pu.check(); err != nil {
return n, err
}
_spec := sqlgraph.NewUpdateSpec(post.Table, post.Columns, sqlgraph.NewFieldSpec(post.FieldID, field.TypeUUID))
if ps := pu.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := pu.mutation.UpdatedAt(); ok {
_spec.SetField(post.FieldUpdatedAt, field.TypeTime, value)
}
if pu.mutation.UpdatedAtCleared() {
_spec.ClearField(post.FieldUpdatedAt, field.TypeTime)
}
if value, ok := pu.mutation.Expires(); ok {
_spec.SetField(post.FieldExpires, field.TypeBool, value)
}
if value, ok := pu.mutation.ExpireTime(); ok {
_spec.SetField(post.FieldExpireTime, field.TypeTime, value)
}
if pu.mutation.ExpireTimeCleared() {
_spec.ClearField(post.FieldExpireTime, field.TypeTime)
}
if value, ok := pu.mutation.Title(); ok {
_spec.SetField(post.FieldTitle, field.TypeString, value)
}
if value, ok := pu.mutation.Body(); ok {
_spec.SetField(post.FieldBody, field.TypeString, value)
}
if pu.mutation.CategoryCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: post.CategoryTable,
Columns: []string{post.CategoryColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(category.FieldID, field.TypeUUID),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := pu.mutation.CategoryIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: post.CategoryTable,
Columns: []string{post.CategoryColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(category.FieldID, field.TypeUUID),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if pu.mutation.ProfileCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: post.ProfileTable,
Columns: []string{post.ProfileColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(profile.FieldID, field.TypeUUID),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := pu.mutation.ProfileIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: post.ProfileTable,
Columns: []string{post.ProfileColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(profile.FieldID, field.TypeUUID),
},
}
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, pu.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{post.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
pu.mutation.done = true
return n, nil
}
// PostUpdateOne is the builder for updating a single Post entity.
type PostUpdateOne struct {
config
fields []string
hooks []Hook
mutation *PostMutation
}
// SetUpdatedAt sets the "updated_at" field.
func (puo *PostUpdateOne) SetUpdatedAt(t time.Time) *PostUpdateOne {
puo.mutation.SetUpdatedAt(t)
return puo
}
// ClearUpdatedAt clears the value of the "updated_at" field.
func (puo *PostUpdateOne) ClearUpdatedAt() *PostUpdateOne {
puo.mutation.ClearUpdatedAt()
return puo
}
// SetExpires sets the "expires" field.
func (puo *PostUpdateOne) SetExpires(b bool) *PostUpdateOne {
puo.mutation.SetExpires(b)
return puo
}
// SetNillableExpires sets the "expires" field if the given value is not nil.
func (puo *PostUpdateOne) SetNillableExpires(b *bool) *PostUpdateOne {
if b != nil {
puo.SetExpires(*b)
}
return puo
}
// SetExpireTime sets the "expire_time" field.
func (puo *PostUpdateOne) SetExpireTime(t time.Time) *PostUpdateOne {
puo.mutation.SetExpireTime(t)
return puo
}
// SetNillableExpireTime sets the "expire_time" field if the given value is not nil.
func (puo *PostUpdateOne) SetNillableExpireTime(t *time.Time) *PostUpdateOne {
if t != nil {
puo.SetExpireTime(*t)
}
return puo
}
// ClearExpireTime clears the value of the "expire_time" field.
func (puo *PostUpdateOne) ClearExpireTime() *PostUpdateOne {
puo.mutation.ClearExpireTime()
return puo
}
// SetTitle sets the "title" field.
func (puo *PostUpdateOne) SetTitle(s string) *PostUpdateOne {
puo.mutation.SetTitle(s)
return puo
}
// SetNillableTitle sets the "title" field if the given value is not nil.
func (puo *PostUpdateOne) SetNillableTitle(s *string) *PostUpdateOne {
if s != nil {
puo.SetTitle(*s)
}
return puo
}
// SetBody sets the "body" field.
func (puo *PostUpdateOne) SetBody(s string) *PostUpdateOne {
puo.mutation.SetBody(s)
return puo
}
// SetNillableBody sets the "body" field if the given value is not nil.
func (puo *PostUpdateOne) SetNillableBody(s *string) *PostUpdateOne {
if s != nil {
puo.SetBody(*s)
}
return puo
}
// SetCategoryID sets the "category" edge to the Category entity by ID.
func (puo *PostUpdateOne) SetCategoryID(id uuid.UUID) *PostUpdateOne {
puo.mutation.SetCategoryID(id)
return puo
}
// SetNillableCategoryID sets the "category" edge to the Category entity by ID if the given value is not nil.
func (puo *PostUpdateOne) SetNillableCategoryID(id *uuid.UUID) *PostUpdateOne {
if id != nil {
puo = puo.SetCategoryID(*id)
}
return puo
}
// SetCategory sets the "category" edge to the Category entity.
func (puo *PostUpdateOne) SetCategory(c *Category) *PostUpdateOne {
return puo.SetCategoryID(c.ID)
}
// SetProfileID sets the "profile" edge to the Profile entity by ID.
func (puo *PostUpdateOne) SetProfileID(id uuid.UUID) *PostUpdateOne {
puo.mutation.SetProfileID(id)
return puo
}
// SetNillableProfileID sets the "profile" edge to the Profile entity by ID if the given value is not nil.
func (puo *PostUpdateOne) SetNillableProfileID(id *uuid.UUID) *PostUpdateOne {
if id != nil {
puo = puo.SetProfileID(*id)
}
return puo
}
// SetProfile sets the "profile" edge to the Profile entity.
func (puo *PostUpdateOne) SetProfile(p *Profile) *PostUpdateOne {
return puo.SetProfileID(p.ID)
}
// Mutation returns the PostMutation object of the builder.
func (puo *PostUpdateOne) Mutation() *PostMutation {
return puo.mutation
}
// ClearCategory clears the "category" edge to the Category entity.
func (puo *PostUpdateOne) ClearCategory() *PostUpdateOne {
puo.mutation.ClearCategory()
return puo
}
// ClearProfile clears the "profile" edge to the Profile entity.
func (puo *PostUpdateOne) ClearProfile() *PostUpdateOne {
puo.mutation.ClearProfile()
return puo
}
// Where appends a list predicates to the PostUpdate builder.
func (puo *PostUpdateOne) Where(ps ...predicate.Post) *PostUpdateOne {
puo.mutation.Where(ps...)
return puo
}
// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (puo *PostUpdateOne) Select(field string, fields ...string) *PostUpdateOne {
puo.fields = append([]string{field}, fields...)
return puo
}
// Save executes the query and returns the updated Post entity.
func (puo *PostUpdateOne) Save(ctx context.Context) (*Post, error) {
puo.defaults()
return withHooks(ctx, puo.sqlSave, puo.mutation, puo.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (puo *PostUpdateOne) SaveX(ctx context.Context) *Post {
node, err := puo.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (puo *PostUpdateOne) Exec(ctx context.Context) error {
_, err := puo.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (puo *PostUpdateOne) ExecX(ctx context.Context) {
if err := puo.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (puo *PostUpdateOne) defaults() {
if _, ok := puo.mutation.UpdatedAt(); !ok && !puo.mutation.UpdatedAtCleared() {
v := post.UpdateDefaultUpdatedAt()
puo.mutation.SetUpdatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (puo *PostUpdateOne) check() error {
if v, ok := puo.mutation.Title(); ok {
if err := post.TitleValidator(v); err != nil {
return &ValidationError{Name: "title", err: fmt.Errorf(`ent: validator failed for field "Post.title": %w`, err)}
}
}
if v, ok := puo.mutation.Body(); ok {
if err := post.BodyValidator(v); err != nil {
return &ValidationError{Name: "body", err: fmt.Errorf(`ent: validator failed for field "Post.body": %w`, err)}
}
}
return nil
}
func (puo *PostUpdateOne) sqlSave(ctx context.Context) (_node *Post, err error) {
if err := puo.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(post.Table, post.Columns, sqlgraph.NewFieldSpec(post.FieldID, field.TypeUUID))
id, ok := puo.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Post.id" for update`)}
}
_spec.Node.ID.Value = id
if fields := puo.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, post.FieldID)
for _, f := range fields {
if !post.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != post.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, f)
}
}
}
if ps := puo.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := puo.mutation.UpdatedAt(); ok {
_spec.SetField(post.FieldUpdatedAt, field.TypeTime, value)
}
if puo.mutation.UpdatedAtCleared() {
_spec.ClearField(post.FieldUpdatedAt, field.TypeTime)
}
if value, ok := puo.mutation.Expires(); ok {
_spec.SetField(post.FieldExpires, field.TypeBool, value)
}
if value, ok := puo.mutation.ExpireTime(); ok {
_spec.SetField(post.FieldExpireTime, field.TypeTime, value)
}
if puo.mutation.ExpireTimeCleared() {
_spec.ClearField(post.FieldExpireTime, field.TypeTime)
}
if value, ok := puo.mutation.Title(); ok {
_spec.SetField(post.FieldTitle, field.TypeString, value)
}
if value, ok := puo.mutation.Body(); ok {
_spec.SetField(post.FieldBody, field.TypeString, value)
}
if puo.mutation.CategoryCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: post.CategoryTable,
Columns: []string{post.CategoryColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(category.FieldID, field.TypeUUID),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := puo.mutation.CategoryIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: post.CategoryTable,
Columns: []string{post.CategoryColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(category.FieldID, field.TypeUUID),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if puo.mutation.ProfileCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: post.ProfileTable,
Columns: []string{post.ProfileColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(profile.FieldID, field.TypeUUID),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := puo.mutation.ProfileIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: post.ProfileTable,
Columns: []string{post.ProfileColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(profile.FieldID, field.TypeUUID),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
_node = &Post{config: puo.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues
if err = sqlgraph.UpdateNode(ctx, puo.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{post.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
puo.mutation.done = true
return _node, nil
}