collectionpost_update.go•16.3 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/collection"
"github.com/Southclaws/storyden/internal/ent/collectionpost"
"github.com/Southclaws/storyden/internal/ent/post"
"github.com/Southclaws/storyden/internal/ent/predicate"
"github.com/rs/xid"
)
// CollectionPostUpdate is the builder for updating CollectionPost entities.
type CollectionPostUpdate struct {
config
hooks []Hook
mutation *CollectionPostMutation
modifiers []func(*sql.UpdateBuilder)
}
// Where appends a list predicates to the CollectionPostUpdate builder.
func (_u *CollectionPostUpdate) Where(ps ...predicate.CollectionPost) *CollectionPostUpdate {
_u.mutation.Where(ps...)
return _u
}
// SetCollectionID sets the "collection_id" field.
func (_u *CollectionPostUpdate) SetCollectionID(v xid.ID) *CollectionPostUpdate {
_u.mutation.SetCollectionID(v)
return _u
}
// SetNillableCollectionID sets the "collection_id" field if the given value is not nil.
func (_u *CollectionPostUpdate) SetNillableCollectionID(v *xid.ID) *CollectionPostUpdate {
if v != nil {
_u.SetCollectionID(*v)
}
return _u
}
// SetPostID sets the "post_id" field.
func (_u *CollectionPostUpdate) SetPostID(v xid.ID) *CollectionPostUpdate {
_u.mutation.SetPostID(v)
return _u
}
// SetNillablePostID sets the "post_id" field if the given value is not nil.
func (_u *CollectionPostUpdate) SetNillablePostID(v *xid.ID) *CollectionPostUpdate {
if v != nil {
_u.SetPostID(*v)
}
return _u
}
// SetMembershipType sets the "membership_type" field.
func (_u *CollectionPostUpdate) SetMembershipType(v string) *CollectionPostUpdate {
_u.mutation.SetMembershipType(v)
return _u
}
// SetNillableMembershipType sets the "membership_type" field if the given value is not nil.
func (_u *CollectionPostUpdate) SetNillableMembershipType(v *string) *CollectionPostUpdate {
if v != nil {
_u.SetMembershipType(*v)
}
return _u
}
// SetCollection sets the "collection" edge to the Collection entity.
func (_u *CollectionPostUpdate) SetCollection(v *Collection) *CollectionPostUpdate {
return _u.SetCollectionID(v.ID)
}
// SetPost sets the "post" edge to the Post entity.
func (_u *CollectionPostUpdate) SetPost(v *Post) *CollectionPostUpdate {
return _u.SetPostID(v.ID)
}
// Mutation returns the CollectionPostMutation object of the builder.
func (_u *CollectionPostUpdate) Mutation() *CollectionPostMutation {
return _u.mutation
}
// ClearCollection clears the "collection" edge to the Collection entity.
func (_u *CollectionPostUpdate) ClearCollection() *CollectionPostUpdate {
_u.mutation.ClearCollection()
return _u
}
// ClearPost clears the "post" edge to the Post entity.
func (_u *CollectionPostUpdate) ClearPost() *CollectionPostUpdate {
_u.mutation.ClearPost()
return _u
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *CollectionPostUpdate) 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 *CollectionPostUpdate) SaveX(ctx context.Context) int {
affected, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (_u *CollectionPostUpdate) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *CollectionPostUpdate) 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 *CollectionPostUpdate) check() error {
if v, ok := _u.mutation.CollectionID(); ok {
if err := collectionpost.CollectionIDValidator(v.String()); err != nil {
return &ValidationError{Name: "collection_id", err: fmt.Errorf(`ent: validator failed for field "CollectionPost.collection_id": %w`, err)}
}
}
if v, ok := _u.mutation.PostID(); ok {
if err := collectionpost.PostIDValidator(v.String()); err != nil {
return &ValidationError{Name: "post_id", err: fmt.Errorf(`ent: validator failed for field "CollectionPost.post_id": %w`, err)}
}
}
if _u.mutation.CollectionCleared() && len(_u.mutation.CollectionIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "CollectionPost.collection"`)
}
if _u.mutation.PostCleared() && len(_u.mutation.PostIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "CollectionPost.post"`)
}
return nil
}
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *CollectionPostUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *CollectionPostUpdate {
_u.modifiers = append(_u.modifiers, modifiers...)
return _u
}
func (_u *CollectionPostUpdate) sqlSave(ctx context.Context) (_node int, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(collectionpost.Table, collectionpost.Columns, sqlgraph.NewFieldSpec(collectionpost.FieldCollectionID, field.TypeString), sqlgraph.NewFieldSpec(collectionpost.FieldPostID, 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.MembershipType(); ok {
_spec.SetField(collectionpost.FieldMembershipType, field.TypeString, value)
}
if _u.mutation.CollectionCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: collectionpost.CollectionTable,
Columns: []string{collectionpost.CollectionColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(collection.FieldID, field.TypeString),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.CollectionIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: collectionpost.CollectionTable,
Columns: []string{collectionpost.CollectionColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(collection.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: false,
Table: collectionpost.PostTable,
Columns: []string{collectionpost.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: false,
Table: collectionpost.PostTable,
Columns: []string{collectionpost.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{collectionpost.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
_u.mutation.done = true
return _node, nil
}
// CollectionPostUpdateOne is the builder for updating a single CollectionPost entity.
type CollectionPostUpdateOne struct {
config
fields []string
hooks []Hook
mutation *CollectionPostMutation
modifiers []func(*sql.UpdateBuilder)
}
// SetCollectionID sets the "collection_id" field.
func (_u *CollectionPostUpdateOne) SetCollectionID(v xid.ID) *CollectionPostUpdateOne {
_u.mutation.SetCollectionID(v)
return _u
}
// SetNillableCollectionID sets the "collection_id" field if the given value is not nil.
func (_u *CollectionPostUpdateOne) SetNillableCollectionID(v *xid.ID) *CollectionPostUpdateOne {
if v != nil {
_u.SetCollectionID(*v)
}
return _u
}
// SetPostID sets the "post_id" field.
func (_u *CollectionPostUpdateOne) SetPostID(v xid.ID) *CollectionPostUpdateOne {
_u.mutation.SetPostID(v)
return _u
}
// SetNillablePostID sets the "post_id" field if the given value is not nil.
func (_u *CollectionPostUpdateOne) SetNillablePostID(v *xid.ID) *CollectionPostUpdateOne {
if v != nil {
_u.SetPostID(*v)
}
return _u
}
// SetMembershipType sets the "membership_type" field.
func (_u *CollectionPostUpdateOne) SetMembershipType(v string) *CollectionPostUpdateOne {
_u.mutation.SetMembershipType(v)
return _u
}
// SetNillableMembershipType sets the "membership_type" field if the given value is not nil.
func (_u *CollectionPostUpdateOne) SetNillableMembershipType(v *string) *CollectionPostUpdateOne {
if v != nil {
_u.SetMembershipType(*v)
}
return _u
}
// SetCollection sets the "collection" edge to the Collection entity.
func (_u *CollectionPostUpdateOne) SetCollection(v *Collection) *CollectionPostUpdateOne {
return _u.SetCollectionID(v.ID)
}
// SetPost sets the "post" edge to the Post entity.
func (_u *CollectionPostUpdateOne) SetPost(v *Post) *CollectionPostUpdateOne {
return _u.SetPostID(v.ID)
}
// Mutation returns the CollectionPostMutation object of the builder.
func (_u *CollectionPostUpdateOne) Mutation() *CollectionPostMutation {
return _u.mutation
}
// ClearCollection clears the "collection" edge to the Collection entity.
func (_u *CollectionPostUpdateOne) ClearCollection() *CollectionPostUpdateOne {
_u.mutation.ClearCollection()
return _u
}
// ClearPost clears the "post" edge to the Post entity.
func (_u *CollectionPostUpdateOne) ClearPost() *CollectionPostUpdateOne {
_u.mutation.ClearPost()
return _u
}
// Where appends a list predicates to the CollectionPostUpdate builder.
func (_u *CollectionPostUpdateOne) Where(ps ...predicate.CollectionPost) *CollectionPostUpdateOne {
_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 *CollectionPostUpdateOne) Select(field string, fields ...string) *CollectionPostUpdateOne {
_u.fields = append([]string{field}, fields...)
return _u
}
// Save executes the query and returns the updated CollectionPost entity.
func (_u *CollectionPostUpdateOne) Save(ctx context.Context) (*CollectionPost, error) {
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (_u *CollectionPostUpdateOne) SaveX(ctx context.Context) *CollectionPost {
node, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (_u *CollectionPostUpdateOne) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *CollectionPostUpdateOne) 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 *CollectionPostUpdateOne) check() error {
if v, ok := _u.mutation.CollectionID(); ok {
if err := collectionpost.CollectionIDValidator(v.String()); err != nil {
return &ValidationError{Name: "collection_id", err: fmt.Errorf(`ent: validator failed for field "CollectionPost.collection_id": %w`, err)}
}
}
if v, ok := _u.mutation.PostID(); ok {
if err := collectionpost.PostIDValidator(v.String()); err != nil {
return &ValidationError{Name: "post_id", err: fmt.Errorf(`ent: validator failed for field "CollectionPost.post_id": %w`, err)}
}
}
if _u.mutation.CollectionCleared() && len(_u.mutation.CollectionIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "CollectionPost.collection"`)
}
if _u.mutation.PostCleared() && len(_u.mutation.PostIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "CollectionPost.post"`)
}
return nil
}
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *CollectionPostUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *CollectionPostUpdateOne {
_u.modifiers = append(_u.modifiers, modifiers...)
return _u
}
func (_u *CollectionPostUpdateOne) sqlSave(ctx context.Context) (_node *CollectionPost, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(collectionpost.Table, collectionpost.Columns, sqlgraph.NewFieldSpec(collectionpost.FieldCollectionID, field.TypeString), sqlgraph.NewFieldSpec(collectionpost.FieldPostID, field.TypeString))
if id, ok := _u.mutation.CollectionID(); !ok {
return nil, &ValidationError{Name: "collection_id", err: errors.New(`ent: missing "CollectionPost.collection_id" for update`)}
} else {
_spec.Node.CompositeID[0].Value = id
}
if id, ok := _u.mutation.PostID(); !ok {
return nil, &ValidationError{Name: "post_id", err: errors.New(`ent: missing "CollectionPost.post_id" for update`)}
} else {
_spec.Node.CompositeID[1].Value = id
}
if fields := _u.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, len(fields))
for i, f := range fields {
if !collectionpost.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
_spec.Node.Columns[i] = 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.MembershipType(); ok {
_spec.SetField(collectionpost.FieldMembershipType, field.TypeString, value)
}
if _u.mutation.CollectionCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: collectionpost.CollectionTable,
Columns: []string{collectionpost.CollectionColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(collection.FieldID, field.TypeString),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.CollectionIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: collectionpost.CollectionTable,
Columns: []string{collectionpost.CollectionColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(collection.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: false,
Table: collectionpost.PostTable,
Columns: []string{collectionpost.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: false,
Table: collectionpost.PostTable,
Columns: []string{collectionpost.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 = &CollectionPost{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{collectionpost.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
_u.mutation.done = true
return _node, nil
}