collectionpost_create.go•21.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/collection"
"github.com/Southclaws/storyden/internal/ent/collectionpost"
"github.com/Southclaws/storyden/internal/ent/post"
"github.com/rs/xid"
)
// CollectionPostCreate is the builder for creating a CollectionPost entity.
type CollectionPostCreate struct {
config
mutation *CollectionPostMutation
hooks []Hook
conflict []sql.ConflictOption
}
// SetCreatedAt sets the "created_at" field.
func (_c *CollectionPostCreate) SetCreatedAt(v time.Time) *CollectionPostCreate {
_c.mutation.SetCreatedAt(v)
return _c
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *CollectionPostCreate) SetNillableCreatedAt(v *time.Time) *CollectionPostCreate {
if v != nil {
_c.SetCreatedAt(*v)
}
return _c
}
// SetCollectionID sets the "collection_id" field.
func (_c *CollectionPostCreate) SetCollectionID(v xid.ID) *CollectionPostCreate {
_c.mutation.SetCollectionID(v)
return _c
}
// SetNillableCollectionID sets the "collection_id" field if the given value is not nil.
func (_c *CollectionPostCreate) SetNillableCollectionID(v *xid.ID) *CollectionPostCreate {
if v != nil {
_c.SetCollectionID(*v)
}
return _c
}
// SetPostID sets the "post_id" field.
func (_c *CollectionPostCreate) SetPostID(v xid.ID) *CollectionPostCreate {
_c.mutation.SetPostID(v)
return _c
}
// SetNillablePostID sets the "post_id" field if the given value is not nil.
func (_c *CollectionPostCreate) SetNillablePostID(v *xid.ID) *CollectionPostCreate {
if v != nil {
_c.SetPostID(*v)
}
return _c
}
// SetMembershipType sets the "membership_type" field.
func (_c *CollectionPostCreate) SetMembershipType(v string) *CollectionPostCreate {
_c.mutation.SetMembershipType(v)
return _c
}
// SetNillableMembershipType sets the "membership_type" field if the given value is not nil.
func (_c *CollectionPostCreate) SetNillableMembershipType(v *string) *CollectionPostCreate {
if v != nil {
_c.SetMembershipType(*v)
}
return _c
}
// SetCollection sets the "collection" edge to the Collection entity.
func (_c *CollectionPostCreate) SetCollection(v *Collection) *CollectionPostCreate {
return _c.SetCollectionID(v.ID)
}
// SetPost sets the "post" edge to the Post entity.
func (_c *CollectionPostCreate) SetPost(v *Post) *CollectionPostCreate {
return _c.SetPostID(v.ID)
}
// Mutation returns the CollectionPostMutation object of the builder.
func (_c *CollectionPostCreate) Mutation() *CollectionPostMutation {
return _c.mutation
}
// Save creates the CollectionPost in the database.
func (_c *CollectionPostCreate) Save(ctx context.Context) (*CollectionPost, error) {
_c.defaults()
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *CollectionPostCreate) SaveX(ctx context.Context) *CollectionPost {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *CollectionPostCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *CollectionPostCreate) ExecX(ctx context.Context) {
if err := _c.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (_c *CollectionPostCreate) defaults() {
if _, ok := _c.mutation.CreatedAt(); !ok {
v := collectionpost.DefaultCreatedAt()
_c.mutation.SetCreatedAt(v)
}
if _, ok := _c.mutation.CollectionID(); !ok {
v := collectionpost.DefaultCollectionID()
_c.mutation.SetCollectionID(v)
}
if _, ok := _c.mutation.PostID(); !ok {
v := collectionpost.DefaultPostID()
_c.mutation.SetPostID(v)
}
if _, ok := _c.mutation.MembershipType(); !ok {
v := collectionpost.DefaultMembershipType
_c.mutation.SetMembershipType(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_c *CollectionPostCreate) check() error {
if _, ok := _c.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "CollectionPost.created_at"`)}
}
if _, ok := _c.mutation.CollectionID(); !ok {
return &ValidationError{Name: "collection_id", err: errors.New(`ent: missing required field "CollectionPost.collection_id"`)}
}
if v, ok := _c.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 _, ok := _c.mutation.PostID(); !ok {
return &ValidationError{Name: "post_id", err: errors.New(`ent: missing required field "CollectionPost.post_id"`)}
}
if v, ok := _c.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 _, ok := _c.mutation.MembershipType(); !ok {
return &ValidationError{Name: "membership_type", err: errors.New(`ent: missing required field "CollectionPost.membership_type"`)}
}
if len(_c.mutation.CollectionIDs()) == 0 {
return &ValidationError{Name: "collection", err: errors.New(`ent: missing required edge "CollectionPost.collection"`)}
}
if len(_c.mutation.PostIDs()) == 0 {
return &ValidationError{Name: "post", err: errors.New(`ent: missing required edge "CollectionPost.post"`)}
}
return nil
}
func (_c *CollectionPostCreate) sqlSave(ctx context.Context) (*CollectionPost, error) {
if err := _c.check(); err != nil {
return nil, err
}
_node, _spec := _c.createSpec()
if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
return _node, nil
}
func (_c *CollectionPostCreate) createSpec() (*CollectionPost, *sqlgraph.CreateSpec) {
var (
_node = &CollectionPost{config: _c.config}
_spec = sqlgraph.NewCreateSpec(collectionpost.Table, nil)
)
_spec.OnConflict = _c.conflict
if value, ok := _c.mutation.CreatedAt(); ok {
_spec.SetField(collectionpost.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := _c.mutation.MembershipType(); ok {
_spec.SetField(collectionpost.FieldMembershipType, field.TypeString, value)
_node.MembershipType = value
}
if nodes := _c.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)
}
_node.CollectionID = nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.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)
}
_node.PostID = nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
// client.CollectionPost.Create().
// SetCreatedAt(v).
// OnConflict(
// // Update the row with the new values
// // the was proposed for insertion.
// sql.ResolveWithNewValues(),
// ).
// // Override some of the fields with custom
// // update values.
// Update(func(u *ent.CollectionPostUpsert) {
// SetCreatedAt(v+v).
// }).
// Exec(ctx)
func (_c *CollectionPostCreate) OnConflict(opts ...sql.ConflictOption) *CollectionPostUpsertOne {
_c.conflict = opts
return &CollectionPostUpsertOne{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.CollectionPost.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *CollectionPostCreate) OnConflictColumns(columns ...string) *CollectionPostUpsertOne {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &CollectionPostUpsertOne{
create: _c,
}
}
type (
// CollectionPostUpsertOne is the builder for "upsert"-ing
// one CollectionPost node.
CollectionPostUpsertOne struct {
create *CollectionPostCreate
}
// CollectionPostUpsert is the "OnConflict" setter.
CollectionPostUpsert struct {
*sql.UpdateSet
}
)
// SetCollectionID sets the "collection_id" field.
func (u *CollectionPostUpsert) SetCollectionID(v xid.ID) *CollectionPostUpsert {
u.Set(collectionpost.FieldCollectionID, v)
return u
}
// UpdateCollectionID sets the "collection_id" field to the value that was provided on create.
func (u *CollectionPostUpsert) UpdateCollectionID() *CollectionPostUpsert {
u.SetExcluded(collectionpost.FieldCollectionID)
return u
}
// SetPostID sets the "post_id" field.
func (u *CollectionPostUpsert) SetPostID(v xid.ID) *CollectionPostUpsert {
u.Set(collectionpost.FieldPostID, v)
return u
}
// UpdatePostID sets the "post_id" field to the value that was provided on create.
func (u *CollectionPostUpsert) UpdatePostID() *CollectionPostUpsert {
u.SetExcluded(collectionpost.FieldPostID)
return u
}
// SetMembershipType sets the "membership_type" field.
func (u *CollectionPostUpsert) SetMembershipType(v string) *CollectionPostUpsert {
u.Set(collectionpost.FieldMembershipType, v)
return u
}
// UpdateMembershipType sets the "membership_type" field to the value that was provided on create.
func (u *CollectionPostUpsert) UpdateMembershipType() *CollectionPostUpsert {
u.SetExcluded(collectionpost.FieldMembershipType)
return u
}
// UpdateNewValues updates the mutable fields using the new values that were set on create.
// Using this option is equivalent to using:
//
// client.CollectionPost.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// ).
// Exec(ctx)
func (u *CollectionPostUpsertOne) UpdateNewValues() *CollectionPostUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
if _, exists := u.create.mutation.CreatedAt(); exists {
s.SetIgnore(collectionpost.FieldCreatedAt)
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.CollectionPost.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *CollectionPostUpsertOne) Ignore() *CollectionPostUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
return u
}
// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *CollectionPostUpsertOne) DoNothing() *CollectionPostUpsertOne {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the CollectionPostCreate.OnConflict
// documentation for more info.
func (u *CollectionPostUpsertOne) Update(set func(*CollectionPostUpsert)) *CollectionPostUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&CollectionPostUpsert{UpdateSet: update})
}))
return u
}
// SetCollectionID sets the "collection_id" field.
func (u *CollectionPostUpsertOne) SetCollectionID(v xid.ID) *CollectionPostUpsertOne {
return u.Update(func(s *CollectionPostUpsert) {
s.SetCollectionID(v)
})
}
// UpdateCollectionID sets the "collection_id" field to the value that was provided on create.
func (u *CollectionPostUpsertOne) UpdateCollectionID() *CollectionPostUpsertOne {
return u.Update(func(s *CollectionPostUpsert) {
s.UpdateCollectionID()
})
}
// SetPostID sets the "post_id" field.
func (u *CollectionPostUpsertOne) SetPostID(v xid.ID) *CollectionPostUpsertOne {
return u.Update(func(s *CollectionPostUpsert) {
s.SetPostID(v)
})
}
// UpdatePostID sets the "post_id" field to the value that was provided on create.
func (u *CollectionPostUpsertOne) UpdatePostID() *CollectionPostUpsertOne {
return u.Update(func(s *CollectionPostUpsert) {
s.UpdatePostID()
})
}
// SetMembershipType sets the "membership_type" field.
func (u *CollectionPostUpsertOne) SetMembershipType(v string) *CollectionPostUpsertOne {
return u.Update(func(s *CollectionPostUpsert) {
s.SetMembershipType(v)
})
}
// UpdateMembershipType sets the "membership_type" field to the value that was provided on create.
func (u *CollectionPostUpsertOne) UpdateMembershipType() *CollectionPostUpsertOne {
return u.Update(func(s *CollectionPostUpsert) {
s.UpdateMembershipType()
})
}
// Exec executes the query.
func (u *CollectionPostUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for CollectionPostCreate.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *CollectionPostUpsertOne) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}
// CollectionPostCreateBulk is the builder for creating many CollectionPost entities in bulk.
type CollectionPostCreateBulk struct {
config
err error
builders []*CollectionPostCreate
conflict []sql.ConflictOption
}
// Save creates the CollectionPost entities in the database.
func (_c *CollectionPostCreateBulk) Save(ctx context.Context) ([]*CollectionPost, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*CollectionPost, len(_c.builders))
mutators := make([]Mutator, len(_c.builders))
for i := range _c.builders {
func(i int, root context.Context) {
builder := _c.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*CollectionPostMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err := builder.check(); err != nil {
return nil, err
}
builder.mutation = mutation
var err error
nodes[i], specs[i] = builder.createSpec()
if i < len(mutators)-1 {
_, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation)
} else {
spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
spec.OnConflict = _c.conflict
// Invoke the actual operation on the latest mutation in the chain.
if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
}
}
if err != nil {
return nil, err
}
mutation.done = true
return nodes[i], nil
})
for i := len(builder.hooks) - 1; i >= 0; i-- {
mut = builder.hooks[i](mut)
}
mutators[i] = mut
}(i, ctx)
}
if len(mutators) > 0 {
if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (_c *CollectionPostCreateBulk) SaveX(ctx context.Context) []*CollectionPost {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *CollectionPostCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *CollectionPostCreateBulk) ExecX(ctx context.Context) {
if err := _c.Exec(ctx); err != nil {
panic(err)
}
}
// OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
// of the `INSERT` statement. For example:
//
// client.CollectionPost.CreateBulk(builders...).
// OnConflict(
// // Update the row with the new values
// // the was proposed for insertion.
// sql.ResolveWithNewValues(),
// ).
// // Override some of the fields with custom
// // update values.
// Update(func(u *ent.CollectionPostUpsert) {
// SetCreatedAt(v+v).
// }).
// Exec(ctx)
func (_c *CollectionPostCreateBulk) OnConflict(opts ...sql.ConflictOption) *CollectionPostUpsertBulk {
_c.conflict = opts
return &CollectionPostUpsertBulk{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.CollectionPost.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *CollectionPostCreateBulk) OnConflictColumns(columns ...string) *CollectionPostUpsertBulk {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &CollectionPostUpsertBulk{
create: _c,
}
}
// CollectionPostUpsertBulk is the builder for "upsert"-ing
// a bulk of CollectionPost nodes.
type CollectionPostUpsertBulk struct {
create *CollectionPostCreateBulk
}
// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
// client.CollectionPost.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// ).
// Exec(ctx)
func (u *CollectionPostUpsertBulk) UpdateNewValues() *CollectionPostUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
for _, b := range u.create.builders {
if _, exists := b.mutation.CreatedAt(); exists {
s.SetIgnore(collectionpost.FieldCreatedAt)
}
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.CollectionPost.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *CollectionPostUpsertBulk) Ignore() *CollectionPostUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
return u
}
// DoNothing configures the conflict_action to `DO NOTHING`.
// Supported only by SQLite and PostgreSQL.
func (u *CollectionPostUpsertBulk) DoNothing() *CollectionPostUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the CollectionPostCreateBulk.OnConflict
// documentation for more info.
func (u *CollectionPostUpsertBulk) Update(set func(*CollectionPostUpsert)) *CollectionPostUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&CollectionPostUpsert{UpdateSet: update})
}))
return u
}
// SetCollectionID sets the "collection_id" field.
func (u *CollectionPostUpsertBulk) SetCollectionID(v xid.ID) *CollectionPostUpsertBulk {
return u.Update(func(s *CollectionPostUpsert) {
s.SetCollectionID(v)
})
}
// UpdateCollectionID sets the "collection_id" field to the value that was provided on create.
func (u *CollectionPostUpsertBulk) UpdateCollectionID() *CollectionPostUpsertBulk {
return u.Update(func(s *CollectionPostUpsert) {
s.UpdateCollectionID()
})
}
// SetPostID sets the "post_id" field.
func (u *CollectionPostUpsertBulk) SetPostID(v xid.ID) *CollectionPostUpsertBulk {
return u.Update(func(s *CollectionPostUpsert) {
s.SetPostID(v)
})
}
// UpdatePostID sets the "post_id" field to the value that was provided on create.
func (u *CollectionPostUpsertBulk) UpdatePostID() *CollectionPostUpsertBulk {
return u.Update(func(s *CollectionPostUpsert) {
s.UpdatePostID()
})
}
// SetMembershipType sets the "membership_type" field.
func (u *CollectionPostUpsertBulk) SetMembershipType(v string) *CollectionPostUpsertBulk {
return u.Update(func(s *CollectionPostUpsert) {
s.SetMembershipType(v)
})
}
// UpdateMembershipType sets the "membership_type" field to the value that was provided on create.
func (u *CollectionPostUpsertBulk) UpdateMembershipType() *CollectionPostUpsertBulk {
return u.Update(func(s *CollectionPostUpsert) {
s.UpdateMembershipType()
})
}
// Exec executes the query.
func (u *CollectionPostUpsertBulk) Exec(ctx context.Context) error {
if u.create.err != nil {
return u.create.err
}
for i, b := range u.create.builders {
if len(b.conflict) != 0 {
return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the CollectionPostCreateBulk instead", i)
}
}
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for CollectionPostCreateBulk.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *CollectionPostUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}