collection_update.go•33.8 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/asset"
"github.com/Southclaws/storyden/internal/ent/collection"
"github.com/Southclaws/storyden/internal/ent/node"
"github.com/Southclaws/storyden/internal/ent/post"
"github.com/Southclaws/storyden/internal/ent/predicate"
"github.com/rs/xid"
)
// CollectionUpdate is the builder for updating Collection entities.
type CollectionUpdate struct {
config
hooks []Hook
mutation *CollectionMutation
modifiers []func(*sql.UpdateBuilder)
}
// Where appends a list predicates to the CollectionUpdate builder.
func (_u *CollectionUpdate) Where(ps ...predicate.Collection) *CollectionUpdate {
_u.mutation.Where(ps...)
return _u
}
// SetUpdatedAt sets the "updated_at" field.
func (_u *CollectionUpdate) SetUpdatedAt(v time.Time) *CollectionUpdate {
_u.mutation.SetUpdatedAt(v)
return _u
}
// SetIndexedAt sets the "indexed_at" field.
func (_u *CollectionUpdate) SetIndexedAt(v time.Time) *CollectionUpdate {
_u.mutation.SetIndexedAt(v)
return _u
}
// SetNillableIndexedAt sets the "indexed_at" field if the given value is not nil.
func (_u *CollectionUpdate) SetNillableIndexedAt(v *time.Time) *CollectionUpdate {
if v != nil {
_u.SetIndexedAt(*v)
}
return _u
}
// ClearIndexedAt clears the value of the "indexed_at" field.
func (_u *CollectionUpdate) ClearIndexedAt() *CollectionUpdate {
_u.mutation.ClearIndexedAt()
return _u
}
// SetName sets the "name" field.
func (_u *CollectionUpdate) SetName(v string) *CollectionUpdate {
_u.mutation.SetName(v)
return _u
}
// SetNillableName sets the "name" field if the given value is not nil.
func (_u *CollectionUpdate) SetNillableName(v *string) *CollectionUpdate {
if v != nil {
_u.SetName(*v)
}
return _u
}
// SetSlug sets the "slug" field.
func (_u *CollectionUpdate) SetSlug(v string) *CollectionUpdate {
_u.mutation.SetSlug(v)
return _u
}
// SetNillableSlug sets the "slug" field if the given value is not nil.
func (_u *CollectionUpdate) SetNillableSlug(v *string) *CollectionUpdate {
if v != nil {
_u.SetSlug(*v)
}
return _u
}
// SetDescription sets the "description" field.
func (_u *CollectionUpdate) SetDescription(v string) *CollectionUpdate {
_u.mutation.SetDescription(v)
return _u
}
// SetNillableDescription sets the "description" field if the given value is not nil.
func (_u *CollectionUpdate) SetNillableDescription(v *string) *CollectionUpdate {
if v != nil {
_u.SetDescription(*v)
}
return _u
}
// ClearDescription clears the value of the "description" field.
func (_u *CollectionUpdate) ClearDescription() *CollectionUpdate {
_u.mutation.ClearDescription()
return _u
}
// SetCoverAssetID sets the "cover_asset_id" field.
func (_u *CollectionUpdate) SetCoverAssetID(v xid.ID) *CollectionUpdate {
_u.mutation.SetCoverAssetID(v)
return _u
}
// SetNillableCoverAssetID sets the "cover_asset_id" field if the given value is not nil.
func (_u *CollectionUpdate) SetNillableCoverAssetID(v *xid.ID) *CollectionUpdate {
if v != nil {
_u.SetCoverAssetID(*v)
}
return _u
}
// ClearCoverAssetID clears the value of the "cover_asset_id" field.
func (_u *CollectionUpdate) ClearCoverAssetID() *CollectionUpdate {
_u.mutation.ClearCoverAssetID()
return _u
}
// SetVisibility sets the "visibility" field.
func (_u *CollectionUpdate) SetVisibility(v collection.Visibility) *CollectionUpdate {
_u.mutation.SetVisibility(v)
return _u
}
// SetNillableVisibility sets the "visibility" field if the given value is not nil.
func (_u *CollectionUpdate) SetNillableVisibility(v *collection.Visibility) *CollectionUpdate {
if v != nil {
_u.SetVisibility(*v)
}
return _u
}
// SetOwnerID sets the "owner" edge to the Account entity by ID.
func (_u *CollectionUpdate) SetOwnerID(id xid.ID) *CollectionUpdate {
_u.mutation.SetOwnerID(id)
return _u
}
// SetNillableOwnerID sets the "owner" edge to the Account entity by ID if the given value is not nil.
func (_u *CollectionUpdate) SetNillableOwnerID(id *xid.ID) *CollectionUpdate {
if id != nil {
_u = _u.SetOwnerID(*id)
}
return _u
}
// SetOwner sets the "owner" edge to the Account entity.
func (_u *CollectionUpdate) SetOwner(v *Account) *CollectionUpdate {
return _u.SetOwnerID(v.ID)
}
// SetCoverImageID sets the "cover_image" edge to the Asset entity by ID.
func (_u *CollectionUpdate) SetCoverImageID(id xid.ID) *CollectionUpdate {
_u.mutation.SetCoverImageID(id)
return _u
}
// SetNillableCoverImageID sets the "cover_image" edge to the Asset entity by ID if the given value is not nil.
func (_u *CollectionUpdate) SetNillableCoverImageID(id *xid.ID) *CollectionUpdate {
if id != nil {
_u = _u.SetCoverImageID(*id)
}
return _u
}
// SetCoverImage sets the "cover_image" edge to the Asset entity.
func (_u *CollectionUpdate) SetCoverImage(v *Asset) *CollectionUpdate {
return _u.SetCoverImageID(v.ID)
}
// AddPostIDs adds the "posts" edge to the Post entity by IDs.
func (_u *CollectionUpdate) AddPostIDs(ids ...xid.ID) *CollectionUpdate {
_u.mutation.AddPostIDs(ids...)
return _u
}
// AddPosts adds the "posts" edges to the Post entity.
func (_u *CollectionUpdate) AddPosts(v ...*Post) *CollectionUpdate {
ids := make([]xid.ID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _u.AddPostIDs(ids...)
}
// AddNodeIDs adds the "nodes" edge to the Node entity by IDs.
func (_u *CollectionUpdate) AddNodeIDs(ids ...xid.ID) *CollectionUpdate {
_u.mutation.AddNodeIDs(ids...)
return _u
}
// AddNodes adds the "nodes" edges to the Node entity.
func (_u *CollectionUpdate) AddNodes(v ...*Node) *CollectionUpdate {
ids := make([]xid.ID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _u.AddNodeIDs(ids...)
}
// Mutation returns the CollectionMutation object of the builder.
func (_u *CollectionUpdate) Mutation() *CollectionMutation {
return _u.mutation
}
// ClearOwner clears the "owner" edge to the Account entity.
func (_u *CollectionUpdate) ClearOwner() *CollectionUpdate {
_u.mutation.ClearOwner()
return _u
}
// ClearCoverImage clears the "cover_image" edge to the Asset entity.
func (_u *CollectionUpdate) ClearCoverImage() *CollectionUpdate {
_u.mutation.ClearCoverImage()
return _u
}
// ClearPosts clears all "posts" edges to the Post entity.
func (_u *CollectionUpdate) ClearPosts() *CollectionUpdate {
_u.mutation.ClearPosts()
return _u
}
// RemovePostIDs removes the "posts" edge to Post entities by IDs.
func (_u *CollectionUpdate) RemovePostIDs(ids ...xid.ID) *CollectionUpdate {
_u.mutation.RemovePostIDs(ids...)
return _u
}
// RemovePosts removes "posts" edges to Post entities.
func (_u *CollectionUpdate) RemovePosts(v ...*Post) *CollectionUpdate {
ids := make([]xid.ID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _u.RemovePostIDs(ids...)
}
// ClearNodes clears all "nodes" edges to the Node entity.
func (_u *CollectionUpdate) ClearNodes() *CollectionUpdate {
_u.mutation.ClearNodes()
return _u
}
// RemoveNodeIDs removes the "nodes" edge to Node entities by IDs.
func (_u *CollectionUpdate) RemoveNodeIDs(ids ...xid.ID) *CollectionUpdate {
_u.mutation.RemoveNodeIDs(ids...)
return _u
}
// RemoveNodes removes "nodes" edges to Node entities.
func (_u *CollectionUpdate) RemoveNodes(v ...*Node) *CollectionUpdate {
ids := make([]xid.ID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _u.RemoveNodeIDs(ids...)
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *CollectionUpdate) Save(ctx context.Context) (int, error) {
_u.defaults()
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (_u *CollectionUpdate) SaveX(ctx context.Context) int {
affected, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (_u *CollectionUpdate) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *CollectionUpdate) ExecX(ctx context.Context) {
if err := _u.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (_u *CollectionUpdate) defaults() {
if _, ok := _u.mutation.UpdatedAt(); !ok {
v := collection.UpdateDefaultUpdatedAt()
_u.mutation.SetUpdatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_u *CollectionUpdate) check() error {
if v, ok := _u.mutation.Visibility(); ok {
if err := collection.VisibilityValidator(v); err != nil {
return &ValidationError{Name: "visibility", err: fmt.Errorf(`ent: validator failed for field "Collection.visibility": %w`, err)}
}
}
return nil
}
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *CollectionUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *CollectionUpdate {
_u.modifiers = append(_u.modifiers, modifiers...)
return _u
}
func (_u *CollectionUpdate) sqlSave(ctx context.Context) (_node int, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(collection.Table, collection.Columns, sqlgraph.NewFieldSpec(collection.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.UpdatedAt(); ok {
_spec.SetField(collection.FieldUpdatedAt, field.TypeTime, value)
}
if value, ok := _u.mutation.IndexedAt(); ok {
_spec.SetField(collection.FieldIndexedAt, field.TypeTime, value)
}
if _u.mutation.IndexedAtCleared() {
_spec.ClearField(collection.FieldIndexedAt, field.TypeTime)
}
if value, ok := _u.mutation.Name(); ok {
_spec.SetField(collection.FieldName, field.TypeString, value)
}
if value, ok := _u.mutation.Slug(); ok {
_spec.SetField(collection.FieldSlug, field.TypeString, value)
}
if value, ok := _u.mutation.Description(); ok {
_spec.SetField(collection.FieldDescription, field.TypeString, value)
}
if _u.mutation.DescriptionCleared() {
_spec.ClearField(collection.FieldDescription, field.TypeString)
}
if value, ok := _u.mutation.Visibility(); ok {
_spec.SetField(collection.FieldVisibility, field.TypeEnum, value)
}
if _u.mutation.OwnerCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: collection.OwnerTable,
Columns: []string{collection.OwnerColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.OwnerIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: collection.OwnerTable,
Columns: []string{collection.OwnerColumn},
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.CoverImageCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: collection.CoverImageTable,
Columns: []string{collection.CoverImageColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(asset.FieldID, field.TypeString),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.CoverImageIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: collection.CoverImageTable,
Columns: []string{collection.CoverImageColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(asset.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.PostsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: false,
Table: collection.PostsTable,
Columns: collection.PostsPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(post.FieldID, field.TypeString),
},
}
createE := &CollectionPostCreate{config: _u.config, mutation: newCollectionPostMutation(_u.config, OpCreate)}
createE.defaults()
_, specE := createE.createSpec()
edge.Target.Fields = specE.Fields
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.RemovedPostsIDs(); len(nodes) > 0 && !_u.mutation.PostsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: false,
Table: collection.PostsTable,
Columns: collection.PostsPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(post.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
createE := &CollectionPostCreate{config: _u.config, mutation: newCollectionPostMutation(_u.config, OpCreate)}
createE.defaults()
_, specE := createE.createSpec()
edge.Target.Fields = specE.Fields
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.PostsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: false,
Table: collection.PostsTable,
Columns: collection.PostsPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(post.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
createE := &CollectionPostCreate{config: _u.config, mutation: newCollectionPostMutation(_u.config, OpCreate)}
createE.defaults()
_, specE := createE.createSpec()
edge.Target.Fields = specE.Fields
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _u.mutation.NodesCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: false,
Table: collection.NodesTable,
Columns: collection.NodesPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(node.FieldID, field.TypeString),
},
}
createE := &CollectionNodeCreate{config: _u.config, mutation: newCollectionNodeMutation(_u.config, OpCreate)}
createE.defaults()
_, specE := createE.createSpec()
edge.Target.Fields = specE.Fields
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.RemovedNodesIDs(); len(nodes) > 0 && !_u.mutation.NodesCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: false,
Table: collection.NodesTable,
Columns: collection.NodesPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(node.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
createE := &CollectionNodeCreate{config: _u.config, mutation: newCollectionNodeMutation(_u.config, OpCreate)}
createE.defaults()
_, specE := createE.createSpec()
edge.Target.Fields = specE.Fields
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.NodesIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: false,
Table: collection.NodesTable,
Columns: collection.NodesPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(node.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
createE := &CollectionNodeCreate{config: _u.config, mutation: newCollectionNodeMutation(_u.config, OpCreate)}
createE.defaults()
_, specE := createE.createSpec()
edge.Target.Fields = specE.Fields
_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{collection.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
_u.mutation.done = true
return _node, nil
}
// CollectionUpdateOne is the builder for updating a single Collection entity.
type CollectionUpdateOne struct {
config
fields []string
hooks []Hook
mutation *CollectionMutation
modifiers []func(*sql.UpdateBuilder)
}
// SetUpdatedAt sets the "updated_at" field.
func (_u *CollectionUpdateOne) SetUpdatedAt(v time.Time) *CollectionUpdateOne {
_u.mutation.SetUpdatedAt(v)
return _u
}
// SetIndexedAt sets the "indexed_at" field.
func (_u *CollectionUpdateOne) SetIndexedAt(v time.Time) *CollectionUpdateOne {
_u.mutation.SetIndexedAt(v)
return _u
}
// SetNillableIndexedAt sets the "indexed_at" field if the given value is not nil.
func (_u *CollectionUpdateOne) SetNillableIndexedAt(v *time.Time) *CollectionUpdateOne {
if v != nil {
_u.SetIndexedAt(*v)
}
return _u
}
// ClearIndexedAt clears the value of the "indexed_at" field.
func (_u *CollectionUpdateOne) ClearIndexedAt() *CollectionUpdateOne {
_u.mutation.ClearIndexedAt()
return _u
}
// SetName sets the "name" field.
func (_u *CollectionUpdateOne) SetName(v string) *CollectionUpdateOne {
_u.mutation.SetName(v)
return _u
}
// SetNillableName sets the "name" field if the given value is not nil.
func (_u *CollectionUpdateOne) SetNillableName(v *string) *CollectionUpdateOne {
if v != nil {
_u.SetName(*v)
}
return _u
}
// SetSlug sets the "slug" field.
func (_u *CollectionUpdateOne) SetSlug(v string) *CollectionUpdateOne {
_u.mutation.SetSlug(v)
return _u
}
// SetNillableSlug sets the "slug" field if the given value is not nil.
func (_u *CollectionUpdateOne) SetNillableSlug(v *string) *CollectionUpdateOne {
if v != nil {
_u.SetSlug(*v)
}
return _u
}
// SetDescription sets the "description" field.
func (_u *CollectionUpdateOne) SetDescription(v string) *CollectionUpdateOne {
_u.mutation.SetDescription(v)
return _u
}
// SetNillableDescription sets the "description" field if the given value is not nil.
func (_u *CollectionUpdateOne) SetNillableDescription(v *string) *CollectionUpdateOne {
if v != nil {
_u.SetDescription(*v)
}
return _u
}
// ClearDescription clears the value of the "description" field.
func (_u *CollectionUpdateOne) ClearDescription() *CollectionUpdateOne {
_u.mutation.ClearDescription()
return _u
}
// SetCoverAssetID sets the "cover_asset_id" field.
func (_u *CollectionUpdateOne) SetCoverAssetID(v xid.ID) *CollectionUpdateOne {
_u.mutation.SetCoverAssetID(v)
return _u
}
// SetNillableCoverAssetID sets the "cover_asset_id" field if the given value is not nil.
func (_u *CollectionUpdateOne) SetNillableCoverAssetID(v *xid.ID) *CollectionUpdateOne {
if v != nil {
_u.SetCoverAssetID(*v)
}
return _u
}
// ClearCoverAssetID clears the value of the "cover_asset_id" field.
func (_u *CollectionUpdateOne) ClearCoverAssetID() *CollectionUpdateOne {
_u.mutation.ClearCoverAssetID()
return _u
}
// SetVisibility sets the "visibility" field.
func (_u *CollectionUpdateOne) SetVisibility(v collection.Visibility) *CollectionUpdateOne {
_u.mutation.SetVisibility(v)
return _u
}
// SetNillableVisibility sets the "visibility" field if the given value is not nil.
func (_u *CollectionUpdateOne) SetNillableVisibility(v *collection.Visibility) *CollectionUpdateOne {
if v != nil {
_u.SetVisibility(*v)
}
return _u
}
// SetOwnerID sets the "owner" edge to the Account entity by ID.
func (_u *CollectionUpdateOne) SetOwnerID(id xid.ID) *CollectionUpdateOne {
_u.mutation.SetOwnerID(id)
return _u
}
// SetNillableOwnerID sets the "owner" edge to the Account entity by ID if the given value is not nil.
func (_u *CollectionUpdateOne) SetNillableOwnerID(id *xid.ID) *CollectionUpdateOne {
if id != nil {
_u = _u.SetOwnerID(*id)
}
return _u
}
// SetOwner sets the "owner" edge to the Account entity.
func (_u *CollectionUpdateOne) SetOwner(v *Account) *CollectionUpdateOne {
return _u.SetOwnerID(v.ID)
}
// SetCoverImageID sets the "cover_image" edge to the Asset entity by ID.
func (_u *CollectionUpdateOne) SetCoverImageID(id xid.ID) *CollectionUpdateOne {
_u.mutation.SetCoverImageID(id)
return _u
}
// SetNillableCoverImageID sets the "cover_image" edge to the Asset entity by ID if the given value is not nil.
func (_u *CollectionUpdateOne) SetNillableCoverImageID(id *xid.ID) *CollectionUpdateOne {
if id != nil {
_u = _u.SetCoverImageID(*id)
}
return _u
}
// SetCoverImage sets the "cover_image" edge to the Asset entity.
func (_u *CollectionUpdateOne) SetCoverImage(v *Asset) *CollectionUpdateOne {
return _u.SetCoverImageID(v.ID)
}
// AddPostIDs adds the "posts" edge to the Post entity by IDs.
func (_u *CollectionUpdateOne) AddPostIDs(ids ...xid.ID) *CollectionUpdateOne {
_u.mutation.AddPostIDs(ids...)
return _u
}
// AddPosts adds the "posts" edges to the Post entity.
func (_u *CollectionUpdateOne) AddPosts(v ...*Post) *CollectionUpdateOne {
ids := make([]xid.ID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _u.AddPostIDs(ids...)
}
// AddNodeIDs adds the "nodes" edge to the Node entity by IDs.
func (_u *CollectionUpdateOne) AddNodeIDs(ids ...xid.ID) *CollectionUpdateOne {
_u.mutation.AddNodeIDs(ids...)
return _u
}
// AddNodes adds the "nodes" edges to the Node entity.
func (_u *CollectionUpdateOne) AddNodes(v ...*Node) *CollectionUpdateOne {
ids := make([]xid.ID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _u.AddNodeIDs(ids...)
}
// Mutation returns the CollectionMutation object of the builder.
func (_u *CollectionUpdateOne) Mutation() *CollectionMutation {
return _u.mutation
}
// ClearOwner clears the "owner" edge to the Account entity.
func (_u *CollectionUpdateOne) ClearOwner() *CollectionUpdateOne {
_u.mutation.ClearOwner()
return _u
}
// ClearCoverImage clears the "cover_image" edge to the Asset entity.
func (_u *CollectionUpdateOne) ClearCoverImage() *CollectionUpdateOne {
_u.mutation.ClearCoverImage()
return _u
}
// ClearPosts clears all "posts" edges to the Post entity.
func (_u *CollectionUpdateOne) ClearPosts() *CollectionUpdateOne {
_u.mutation.ClearPosts()
return _u
}
// RemovePostIDs removes the "posts" edge to Post entities by IDs.
func (_u *CollectionUpdateOne) RemovePostIDs(ids ...xid.ID) *CollectionUpdateOne {
_u.mutation.RemovePostIDs(ids...)
return _u
}
// RemovePosts removes "posts" edges to Post entities.
func (_u *CollectionUpdateOne) RemovePosts(v ...*Post) *CollectionUpdateOne {
ids := make([]xid.ID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _u.RemovePostIDs(ids...)
}
// ClearNodes clears all "nodes" edges to the Node entity.
func (_u *CollectionUpdateOne) ClearNodes() *CollectionUpdateOne {
_u.mutation.ClearNodes()
return _u
}
// RemoveNodeIDs removes the "nodes" edge to Node entities by IDs.
func (_u *CollectionUpdateOne) RemoveNodeIDs(ids ...xid.ID) *CollectionUpdateOne {
_u.mutation.RemoveNodeIDs(ids...)
return _u
}
// RemoveNodes removes "nodes" edges to Node entities.
func (_u *CollectionUpdateOne) RemoveNodes(v ...*Node) *CollectionUpdateOne {
ids := make([]xid.ID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _u.RemoveNodeIDs(ids...)
}
// Where appends a list predicates to the CollectionUpdate builder.
func (_u *CollectionUpdateOne) Where(ps ...predicate.Collection) *CollectionUpdateOne {
_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 *CollectionUpdateOne) Select(field string, fields ...string) *CollectionUpdateOne {
_u.fields = append([]string{field}, fields...)
return _u
}
// Save executes the query and returns the updated Collection entity.
func (_u *CollectionUpdateOne) Save(ctx context.Context) (*Collection, error) {
_u.defaults()
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (_u *CollectionUpdateOne) SaveX(ctx context.Context) *Collection {
node, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (_u *CollectionUpdateOne) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *CollectionUpdateOne) ExecX(ctx context.Context) {
if err := _u.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (_u *CollectionUpdateOne) defaults() {
if _, ok := _u.mutation.UpdatedAt(); !ok {
v := collection.UpdateDefaultUpdatedAt()
_u.mutation.SetUpdatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_u *CollectionUpdateOne) check() error {
if v, ok := _u.mutation.Visibility(); ok {
if err := collection.VisibilityValidator(v); err != nil {
return &ValidationError{Name: "visibility", err: fmt.Errorf(`ent: validator failed for field "Collection.visibility": %w`, err)}
}
}
return nil
}
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *CollectionUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *CollectionUpdateOne {
_u.modifiers = append(_u.modifiers, modifiers...)
return _u
}
func (_u *CollectionUpdateOne) sqlSave(ctx context.Context) (_node *Collection, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(collection.Table, collection.Columns, sqlgraph.NewFieldSpec(collection.FieldID, field.TypeString))
id, ok := _u.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Collection.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, collection.FieldID)
for _, f := range fields {
if !collection.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != collection.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.UpdatedAt(); ok {
_spec.SetField(collection.FieldUpdatedAt, field.TypeTime, value)
}
if value, ok := _u.mutation.IndexedAt(); ok {
_spec.SetField(collection.FieldIndexedAt, field.TypeTime, value)
}
if _u.mutation.IndexedAtCleared() {
_spec.ClearField(collection.FieldIndexedAt, field.TypeTime)
}
if value, ok := _u.mutation.Name(); ok {
_spec.SetField(collection.FieldName, field.TypeString, value)
}
if value, ok := _u.mutation.Slug(); ok {
_spec.SetField(collection.FieldSlug, field.TypeString, value)
}
if value, ok := _u.mutation.Description(); ok {
_spec.SetField(collection.FieldDescription, field.TypeString, value)
}
if _u.mutation.DescriptionCleared() {
_spec.ClearField(collection.FieldDescription, field.TypeString)
}
if value, ok := _u.mutation.Visibility(); ok {
_spec.SetField(collection.FieldVisibility, field.TypeEnum, value)
}
if _u.mutation.OwnerCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: collection.OwnerTable,
Columns: []string{collection.OwnerColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.OwnerIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: collection.OwnerTable,
Columns: []string{collection.OwnerColumn},
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.CoverImageCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: collection.CoverImageTable,
Columns: []string{collection.CoverImageColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(asset.FieldID, field.TypeString),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.CoverImageIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: false,
Table: collection.CoverImageTable,
Columns: []string{collection.CoverImageColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(asset.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.PostsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: false,
Table: collection.PostsTable,
Columns: collection.PostsPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(post.FieldID, field.TypeString),
},
}
createE := &CollectionPostCreate{config: _u.config, mutation: newCollectionPostMutation(_u.config, OpCreate)}
createE.defaults()
_, specE := createE.createSpec()
edge.Target.Fields = specE.Fields
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.RemovedPostsIDs(); len(nodes) > 0 && !_u.mutation.PostsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: false,
Table: collection.PostsTable,
Columns: collection.PostsPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(post.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
createE := &CollectionPostCreate{config: _u.config, mutation: newCollectionPostMutation(_u.config, OpCreate)}
createE.defaults()
_, specE := createE.createSpec()
edge.Target.Fields = specE.Fields
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.PostsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: false,
Table: collection.PostsTable,
Columns: collection.PostsPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(post.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
createE := &CollectionPostCreate{config: _u.config, mutation: newCollectionPostMutation(_u.config, OpCreate)}
createE.defaults()
_, specE := createE.createSpec()
edge.Target.Fields = specE.Fields
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _u.mutation.NodesCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: false,
Table: collection.NodesTable,
Columns: collection.NodesPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(node.FieldID, field.TypeString),
},
}
createE := &CollectionNodeCreate{config: _u.config, mutation: newCollectionNodeMutation(_u.config, OpCreate)}
createE.defaults()
_, specE := createE.createSpec()
edge.Target.Fields = specE.Fields
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.RemovedNodesIDs(); len(nodes) > 0 && !_u.mutation.NodesCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: false,
Table: collection.NodesTable,
Columns: collection.NodesPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(node.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
createE := &CollectionNodeCreate{config: _u.config, mutation: newCollectionNodeMutation(_u.config, OpCreate)}
createE.defaults()
_, specE := createE.createSpec()
edge.Target.Fields = specE.Fields
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.NodesIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: false,
Table: collection.NodesTable,
Columns: collection.NodesPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(node.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
createE := &CollectionNodeCreate{config: _u.config, mutation: newCollectionNodeMutation(_u.config, OpCreate)}
createE.defaults()
_, specE := createE.createSpec()
edge.Target.Fields = specE.Fields
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
_spec.AddModifiers(_u.modifiers...)
_node = &Collection{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{collection.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
_u.mutation.done = true
return _node, nil
}