postread_update.go•14.2 kB
// 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/Southclaws/storyden/internal/ent/account"
"github.com/Southclaws/storyden/internal/ent/post"
"github.com/Southclaws/storyden/internal/ent/postread"
"github.com/Southclaws/storyden/internal/ent/predicate"
"github.com/rs/xid"
)
// PostReadUpdate is the builder for updating PostRead entities.
type PostReadUpdate struct {
config
hooks []Hook
mutation *PostReadMutation
modifiers []func(*sql.UpdateBuilder)
}
// Where appends a list predicates to the PostReadUpdate builder.
func (_u *PostReadUpdate) Where(ps ...predicate.PostRead) *PostReadUpdate {
_u.mutation.Where(ps...)
return _u
}
// SetRootPostID sets the "root_post_id" field.
func (_u *PostReadUpdate) SetRootPostID(v xid.ID) *PostReadUpdate {
_u.mutation.SetRootPostID(v)
return _u
}
// SetNillableRootPostID sets the "root_post_id" field if the given value is not nil.
func (_u *PostReadUpdate) SetNillableRootPostID(v *xid.ID) *PostReadUpdate {
if v != nil {
_u.SetRootPostID(*v)
}
return _u
}
// SetAccountID sets the "account_id" field.
func (_u *PostReadUpdate) SetAccountID(v xid.ID) *PostReadUpdate {
_u.mutation.SetAccountID(v)
return _u
}
// SetNillableAccountID sets the "account_id" field if the given value is not nil.
func (_u *PostReadUpdate) SetNillableAccountID(v *xid.ID) *PostReadUpdate {
if v != nil {
_u.SetAccountID(*v)
}
return _u
}
// SetLastSeenAt sets the "last_seen_at" field.
func (_u *PostReadUpdate) SetLastSeenAt(v time.Time) *PostReadUpdate {
_u.mutation.SetLastSeenAt(v)
return _u
}
// SetNillableLastSeenAt sets the "last_seen_at" field if the given value is not nil.
func (_u *PostReadUpdate) SetNillableLastSeenAt(v *time.Time) *PostReadUpdate {
if v != nil {
_u.SetLastSeenAt(*v)
}
return _u
}
// SetRootPost sets the "root_post" edge to the Post entity.
func (_u *PostReadUpdate) SetRootPost(v *Post) *PostReadUpdate {
return _u.SetRootPostID(v.ID)
}
// SetAccount sets the "account" edge to the Account entity.
func (_u *PostReadUpdate) SetAccount(v *Account) *PostReadUpdate {
return _u.SetAccountID(v.ID)
}
// Mutation returns the PostReadMutation object of the builder.
func (_u *PostReadUpdate) Mutation() *PostReadMutation {
return _u.mutation
}
// ClearRootPost clears the "root_post" edge to the Post entity.
func (_u *PostReadUpdate) ClearRootPost() *PostReadUpdate {
_u.mutation.ClearRootPost()
return _u
}
// ClearAccount clears the "account" edge to the Account entity.
func (_u *PostReadUpdate) ClearAccount() *PostReadUpdate {
_u.mutation.ClearAccount()
return _u
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *PostReadUpdate) Save(ctx context.Context) (int, error) {
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (_u *PostReadUpdate) SaveX(ctx context.Context) int {
affected, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (_u *PostReadUpdate) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *PostReadUpdate) ExecX(ctx context.Context) {
if err := _u.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (_u *PostReadUpdate) check() error {
if _u.mutation.RootPostCleared() && len(_u.mutation.RootPostIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "PostRead.root_post"`)
}
if _u.mutation.AccountCleared() && len(_u.mutation.AccountIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "PostRead.account"`)
}
return nil
}
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *PostReadUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *PostReadUpdate {
_u.modifiers = append(_u.modifiers, modifiers...)
return _u
}
func (_u *PostReadUpdate) sqlSave(ctx context.Context) (_node int, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(postread.Table, postread.Columns, sqlgraph.NewFieldSpec(postread.FieldID, field.TypeString))
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.LastSeenAt(); ok {
_spec.SetField(postread.FieldLastSeenAt, field.TypeTime, value)
}
if _u.mutation.RootPostCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: postread.RootPostTable,
Columns: []string{postread.RootPostColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(post.FieldID, field.TypeString),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.RootPostIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: postread.RootPostTable,
Columns: []string{postread.RootPostColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(post.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _u.mutation.AccountCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: postread.AccountTable,
Columns: []string{postread.AccountColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.AccountIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: postread.AccountTable,
Columns: []string{postread.AccountColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
_spec.AddModifiers(_u.modifiers...)
if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{postread.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
_u.mutation.done = true
return _node, nil
}
// PostReadUpdateOne is the builder for updating a single PostRead entity.
type PostReadUpdateOne struct {
config
fields []string
hooks []Hook
mutation *PostReadMutation
modifiers []func(*sql.UpdateBuilder)
}
// SetRootPostID sets the "root_post_id" field.
func (_u *PostReadUpdateOne) SetRootPostID(v xid.ID) *PostReadUpdateOne {
_u.mutation.SetRootPostID(v)
return _u
}
// SetNillableRootPostID sets the "root_post_id" field if the given value is not nil.
func (_u *PostReadUpdateOne) SetNillableRootPostID(v *xid.ID) *PostReadUpdateOne {
if v != nil {
_u.SetRootPostID(*v)
}
return _u
}
// SetAccountID sets the "account_id" field.
func (_u *PostReadUpdateOne) SetAccountID(v xid.ID) *PostReadUpdateOne {
_u.mutation.SetAccountID(v)
return _u
}
// SetNillableAccountID sets the "account_id" field if the given value is not nil.
func (_u *PostReadUpdateOne) SetNillableAccountID(v *xid.ID) *PostReadUpdateOne {
if v != nil {
_u.SetAccountID(*v)
}
return _u
}
// SetLastSeenAt sets the "last_seen_at" field.
func (_u *PostReadUpdateOne) SetLastSeenAt(v time.Time) *PostReadUpdateOne {
_u.mutation.SetLastSeenAt(v)
return _u
}
// SetNillableLastSeenAt sets the "last_seen_at" field if the given value is not nil.
func (_u *PostReadUpdateOne) SetNillableLastSeenAt(v *time.Time) *PostReadUpdateOne {
if v != nil {
_u.SetLastSeenAt(*v)
}
return _u
}
// SetRootPost sets the "root_post" edge to the Post entity.
func (_u *PostReadUpdateOne) SetRootPost(v *Post) *PostReadUpdateOne {
return _u.SetRootPostID(v.ID)
}
// SetAccount sets the "account" edge to the Account entity.
func (_u *PostReadUpdateOne) SetAccount(v *Account) *PostReadUpdateOne {
return _u.SetAccountID(v.ID)
}
// Mutation returns the PostReadMutation object of the builder.
func (_u *PostReadUpdateOne) Mutation() *PostReadMutation {
return _u.mutation
}
// ClearRootPost clears the "root_post" edge to the Post entity.
func (_u *PostReadUpdateOne) ClearRootPost() *PostReadUpdateOne {
_u.mutation.ClearRootPost()
return _u
}
// ClearAccount clears the "account" edge to the Account entity.
func (_u *PostReadUpdateOne) ClearAccount() *PostReadUpdateOne {
_u.mutation.ClearAccount()
return _u
}
// Where appends a list predicates to the PostReadUpdate builder.
func (_u *PostReadUpdateOne) Where(ps ...predicate.PostRead) *PostReadUpdateOne {
_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 *PostReadUpdateOne) Select(field string, fields ...string) *PostReadUpdateOne {
_u.fields = append([]string{field}, fields...)
return _u
}
// Save executes the query and returns the updated PostRead entity.
func (_u *PostReadUpdateOne) Save(ctx context.Context) (*PostRead, error) {
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (_u *PostReadUpdateOne) SaveX(ctx context.Context) *PostRead {
node, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (_u *PostReadUpdateOne) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *PostReadUpdateOne) ExecX(ctx context.Context) {
if err := _u.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (_u *PostReadUpdateOne) check() error {
if _u.mutation.RootPostCleared() && len(_u.mutation.RootPostIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "PostRead.root_post"`)
}
if _u.mutation.AccountCleared() && len(_u.mutation.AccountIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "PostRead.account"`)
}
return nil
}
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *PostReadUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *PostReadUpdateOne {
_u.modifiers = append(_u.modifiers, modifiers...)
return _u
}
func (_u *PostReadUpdateOne) sqlSave(ctx context.Context) (_node *PostRead, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(postread.Table, postread.Columns, sqlgraph.NewFieldSpec(postread.FieldID, field.TypeString))
id, ok := _u.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "PostRead.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, postread.FieldID)
for _, f := range fields {
if !postread.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != postread.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.LastSeenAt(); ok {
_spec.SetField(postread.FieldLastSeenAt, field.TypeTime, value)
}
if _u.mutation.RootPostCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: postread.RootPostTable,
Columns: []string{postread.RootPostColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(post.FieldID, field.TypeString),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.RootPostIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: postread.RootPostTable,
Columns: []string{postread.RootPostColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(post.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _u.mutation.AccountCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: postread.AccountTable,
Columns: []string{postread.AccountColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.AccountIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: postread.AccountTable,
Columns: []string{postread.AccountColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
_spec.AddModifiers(_u.modifiers...)
_node = &PostRead{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{postread.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
_u.mutation.done = true
return _node, nil
}