react_update.go•13.5 kB
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"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/predicate"
"github.com/Southclaws/storyden/internal/ent/react"
"github.com/rs/xid"
)
// ReactUpdate is the builder for updating React entities.
type ReactUpdate struct {
config
hooks []Hook
mutation *ReactMutation
modifiers []func(*sql.UpdateBuilder)
}
// Where appends a list predicates to the ReactUpdate builder.
func (_u *ReactUpdate) Where(ps ...predicate.React) *ReactUpdate {
_u.mutation.Where(ps...)
return _u
}
// SetAccountID sets the "account_id" field.
func (_u *ReactUpdate) SetAccountID(v xid.ID) *ReactUpdate {
_u.mutation.SetAccountID(v)
return _u
}
// SetNillableAccountID sets the "account_id" field if the given value is not nil.
func (_u *ReactUpdate) SetNillableAccountID(v *xid.ID) *ReactUpdate {
if v != nil {
_u.SetAccountID(*v)
}
return _u
}
// SetPostID sets the "post_id" field.
func (_u *ReactUpdate) SetPostID(v xid.ID) *ReactUpdate {
_u.mutation.SetPostID(v)
return _u
}
// SetNillablePostID sets the "post_id" field if the given value is not nil.
func (_u *ReactUpdate) SetNillablePostID(v *xid.ID) *ReactUpdate {
if v != nil {
_u.SetPostID(*v)
}
return _u
}
// SetEmoji sets the "emoji" field.
func (_u *ReactUpdate) SetEmoji(v string) *ReactUpdate {
_u.mutation.SetEmoji(v)
return _u
}
// SetNillableEmoji sets the "emoji" field if the given value is not nil.
func (_u *ReactUpdate) SetNillableEmoji(v *string) *ReactUpdate {
if v != nil {
_u.SetEmoji(*v)
}
return _u
}
// SetAccount sets the "account" edge to the Account entity.
func (_u *ReactUpdate) SetAccount(v *Account) *ReactUpdate {
return _u.SetAccountID(v.ID)
}
// SetPost sets the "Post" edge to the Post entity.
func (_u *ReactUpdate) SetPost(v *Post) *ReactUpdate {
return _u.SetPostID(v.ID)
}
// Mutation returns the ReactMutation object of the builder.
func (_u *ReactUpdate) Mutation() *ReactMutation {
return _u.mutation
}
// ClearAccount clears the "account" edge to the Account entity.
func (_u *ReactUpdate) ClearAccount() *ReactUpdate {
_u.mutation.ClearAccount()
return _u
}
// ClearPost clears the "Post" edge to the Post entity.
func (_u *ReactUpdate) ClearPost() *ReactUpdate {
_u.mutation.ClearPost()
return _u
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *ReactUpdate) 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 *ReactUpdate) SaveX(ctx context.Context) int {
affected, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (_u *ReactUpdate) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *ReactUpdate) 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 *ReactUpdate) check() error {
if _u.mutation.AccountCleared() && len(_u.mutation.AccountIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "React.account"`)
}
if _u.mutation.PostCleared() && len(_u.mutation.PostIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "React.Post"`)
}
return nil
}
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *ReactUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *ReactUpdate {
_u.modifiers = append(_u.modifiers, modifiers...)
return _u
}
func (_u *ReactUpdate) sqlSave(ctx context.Context) (_node int, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(react.Table, react.Columns, sqlgraph.NewFieldSpec(react.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.Emoji(); ok {
_spec.SetField(react.FieldEmoji, field.TypeString, value)
}
if _u.mutation.AccountCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: react.AccountTable,
Columns: []string{react.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: react.AccountTable,
Columns: []string{react.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)
}
if _u.mutation.PostCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: react.PostTable,
Columns: []string{react.PostColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(post.FieldID, field.TypeString),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.PostIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: react.PostTable,
Columns: []string{react.PostColumn},
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)
}
_spec.AddModifiers(_u.modifiers...)
if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{react.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
_u.mutation.done = true
return _node, nil
}
// ReactUpdateOne is the builder for updating a single React entity.
type ReactUpdateOne struct {
config
fields []string
hooks []Hook
mutation *ReactMutation
modifiers []func(*sql.UpdateBuilder)
}
// SetAccountID sets the "account_id" field.
func (_u *ReactUpdateOne) SetAccountID(v xid.ID) *ReactUpdateOne {
_u.mutation.SetAccountID(v)
return _u
}
// SetNillableAccountID sets the "account_id" field if the given value is not nil.
func (_u *ReactUpdateOne) SetNillableAccountID(v *xid.ID) *ReactUpdateOne {
if v != nil {
_u.SetAccountID(*v)
}
return _u
}
// SetPostID sets the "post_id" field.
func (_u *ReactUpdateOne) SetPostID(v xid.ID) *ReactUpdateOne {
_u.mutation.SetPostID(v)
return _u
}
// SetNillablePostID sets the "post_id" field if the given value is not nil.
func (_u *ReactUpdateOne) SetNillablePostID(v *xid.ID) *ReactUpdateOne {
if v != nil {
_u.SetPostID(*v)
}
return _u
}
// SetEmoji sets the "emoji" field.
func (_u *ReactUpdateOne) SetEmoji(v string) *ReactUpdateOne {
_u.mutation.SetEmoji(v)
return _u
}
// SetNillableEmoji sets the "emoji" field if the given value is not nil.
func (_u *ReactUpdateOne) SetNillableEmoji(v *string) *ReactUpdateOne {
if v != nil {
_u.SetEmoji(*v)
}
return _u
}
// SetAccount sets the "account" edge to the Account entity.
func (_u *ReactUpdateOne) SetAccount(v *Account) *ReactUpdateOne {
return _u.SetAccountID(v.ID)
}
// SetPost sets the "Post" edge to the Post entity.
func (_u *ReactUpdateOne) SetPost(v *Post) *ReactUpdateOne {
return _u.SetPostID(v.ID)
}
// Mutation returns the ReactMutation object of the builder.
func (_u *ReactUpdateOne) Mutation() *ReactMutation {
return _u.mutation
}
// ClearAccount clears the "account" edge to the Account entity.
func (_u *ReactUpdateOne) ClearAccount() *ReactUpdateOne {
_u.mutation.ClearAccount()
return _u
}
// ClearPost clears the "Post" edge to the Post entity.
func (_u *ReactUpdateOne) ClearPost() *ReactUpdateOne {
_u.mutation.ClearPost()
return _u
}
// Where appends a list predicates to the ReactUpdate builder.
func (_u *ReactUpdateOne) Where(ps ...predicate.React) *ReactUpdateOne {
_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 *ReactUpdateOne) Select(field string, fields ...string) *ReactUpdateOne {
_u.fields = append([]string{field}, fields...)
return _u
}
// Save executes the query and returns the updated React entity.
func (_u *ReactUpdateOne) Save(ctx context.Context) (*React, error) {
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (_u *ReactUpdateOne) SaveX(ctx context.Context) *React {
node, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (_u *ReactUpdateOne) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *ReactUpdateOne) 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 *ReactUpdateOne) check() error {
if _u.mutation.AccountCleared() && len(_u.mutation.AccountIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "React.account"`)
}
if _u.mutation.PostCleared() && len(_u.mutation.PostIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "React.Post"`)
}
return nil
}
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *ReactUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *ReactUpdateOne {
_u.modifiers = append(_u.modifiers, modifiers...)
return _u
}
func (_u *ReactUpdateOne) sqlSave(ctx context.Context) (_node *React, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(react.Table, react.Columns, sqlgraph.NewFieldSpec(react.FieldID, field.TypeString))
id, ok := _u.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "React.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, react.FieldID)
for _, f := range fields {
if !react.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != react.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.Emoji(); ok {
_spec.SetField(react.FieldEmoji, field.TypeString, value)
}
if _u.mutation.AccountCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: react.AccountTable,
Columns: []string{react.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: react.AccountTable,
Columns: []string{react.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)
}
if _u.mutation.PostCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: react.PostTable,
Columns: []string{react.PostColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(post.FieldID, field.TypeString),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.PostIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: react.PostTable,
Columns: []string{react.PostColumn},
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)
}
_spec.AddModifiers(_u.modifiers...)
_node = &React{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{react.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
_u.mutation.done = true
return _node, nil
}