likepost_create.go•18.7 kB
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect"
"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/likepost"
"github.com/Southclaws/storyden/internal/ent/post"
"github.com/rs/xid"
)
// LikePostCreate is the builder for creating a LikePost entity.
type LikePostCreate struct {
config
mutation *LikePostMutation
hooks []Hook
conflict []sql.ConflictOption
}
// SetCreatedAt sets the "created_at" field.
func (_c *LikePostCreate) SetCreatedAt(v time.Time) *LikePostCreate {
_c.mutation.SetCreatedAt(v)
return _c
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *LikePostCreate) SetNillableCreatedAt(v *time.Time) *LikePostCreate {
if v != nil {
_c.SetCreatedAt(*v)
}
return _c
}
// SetAccountID sets the "account_id" field.
func (_c *LikePostCreate) SetAccountID(v xid.ID) *LikePostCreate {
_c.mutation.SetAccountID(v)
return _c
}
// SetPostID sets the "post_id" field.
func (_c *LikePostCreate) SetPostID(v xid.ID) *LikePostCreate {
_c.mutation.SetPostID(v)
return _c
}
// SetID sets the "id" field.
func (_c *LikePostCreate) SetID(v xid.ID) *LikePostCreate {
_c.mutation.SetID(v)
return _c
}
// SetNillableID sets the "id" field if the given value is not nil.
func (_c *LikePostCreate) SetNillableID(v *xid.ID) *LikePostCreate {
if v != nil {
_c.SetID(*v)
}
return _c
}
// SetAccount sets the "account" edge to the Account entity.
func (_c *LikePostCreate) SetAccount(v *Account) *LikePostCreate {
return _c.SetAccountID(v.ID)
}
// SetPost sets the "Post" edge to the Post entity.
func (_c *LikePostCreate) SetPost(v *Post) *LikePostCreate {
return _c.SetPostID(v.ID)
}
// Mutation returns the LikePostMutation object of the builder.
func (_c *LikePostCreate) Mutation() *LikePostMutation {
return _c.mutation
}
// Save creates the LikePost in the database.
func (_c *LikePostCreate) Save(ctx context.Context) (*LikePost, error) {
_c.defaults()
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *LikePostCreate) SaveX(ctx context.Context) *LikePost {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *LikePostCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *LikePostCreate) 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 *LikePostCreate) defaults() {
if _, ok := _c.mutation.CreatedAt(); !ok {
v := likepost.DefaultCreatedAt()
_c.mutation.SetCreatedAt(v)
}
if _, ok := _c.mutation.ID(); !ok {
v := likepost.DefaultID()
_c.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_c *LikePostCreate) check() error {
if _, ok := _c.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "LikePost.created_at"`)}
}
if _, ok := _c.mutation.AccountID(); !ok {
return &ValidationError{Name: "account_id", err: errors.New(`ent: missing required field "LikePost.account_id"`)}
}
if _, ok := _c.mutation.PostID(); !ok {
return &ValidationError{Name: "post_id", err: errors.New(`ent: missing required field "LikePost.post_id"`)}
}
if v, ok := _c.mutation.ID(); ok {
if err := likepost.IDValidator(v.String()); err != nil {
return &ValidationError{Name: "id", err: fmt.Errorf(`ent: validator failed for field "LikePost.id": %w`, err)}
}
}
if len(_c.mutation.AccountIDs()) == 0 {
return &ValidationError{Name: "account", err: errors.New(`ent: missing required edge "LikePost.account"`)}
}
if len(_c.mutation.PostIDs()) == 0 {
return &ValidationError{Name: "Post", err: errors.New(`ent: missing required edge "LikePost.Post"`)}
}
return nil
}
func (_c *LikePostCreate) sqlSave(ctx context.Context) (*LikePost, 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
}
if _spec.ID.Value != nil {
if id, ok := _spec.ID.Value.(*xid.ID); ok {
_node.ID = *id
} else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
return nil, err
}
}
_c.mutation.id = &_node.ID
_c.mutation.done = true
return _node, nil
}
func (_c *LikePostCreate) createSpec() (*LikePost, *sqlgraph.CreateSpec) {
var (
_node = &LikePost{config: _c.config}
_spec = sqlgraph.NewCreateSpec(likepost.Table, sqlgraph.NewFieldSpec(likepost.FieldID, field.TypeString))
)
_spec.OnConflict = _c.conflict
if id, ok := _c.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = &id
}
if value, ok := _c.mutation.CreatedAt(); ok {
_spec.SetField(likepost.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if nodes := _c.mutation.AccountIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: likepost.AccountTable,
Columns: []string{likepost.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)
}
_node.AccountID = nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := _c.mutation.PostIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: likepost.PostTable,
Columns: []string{likepost.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.LikePost.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.LikePostUpsert) {
// SetCreatedAt(v+v).
// }).
// Exec(ctx)
func (_c *LikePostCreate) OnConflict(opts ...sql.ConflictOption) *LikePostUpsertOne {
_c.conflict = opts
return &LikePostUpsertOne{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.LikePost.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *LikePostCreate) OnConflictColumns(columns ...string) *LikePostUpsertOne {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &LikePostUpsertOne{
create: _c,
}
}
type (
// LikePostUpsertOne is the builder for "upsert"-ing
// one LikePost node.
LikePostUpsertOne struct {
create *LikePostCreate
}
// LikePostUpsert is the "OnConflict" setter.
LikePostUpsert struct {
*sql.UpdateSet
}
)
// SetAccountID sets the "account_id" field.
func (u *LikePostUpsert) SetAccountID(v xid.ID) *LikePostUpsert {
u.Set(likepost.FieldAccountID, v)
return u
}
// UpdateAccountID sets the "account_id" field to the value that was provided on create.
func (u *LikePostUpsert) UpdateAccountID() *LikePostUpsert {
u.SetExcluded(likepost.FieldAccountID)
return u
}
// SetPostID sets the "post_id" field.
func (u *LikePostUpsert) SetPostID(v xid.ID) *LikePostUpsert {
u.Set(likepost.FieldPostID, v)
return u
}
// UpdatePostID sets the "post_id" field to the value that was provided on create.
func (u *LikePostUpsert) UpdatePostID() *LikePostUpsert {
u.SetExcluded(likepost.FieldPostID)
return u
}
// UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
// Using this option is equivalent to using:
//
// client.LikePost.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(likepost.FieldID)
// }),
// ).
// Exec(ctx)
func (u *LikePostUpsertOne) UpdateNewValues() *LikePostUpsertOne {
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.ID(); exists {
s.SetIgnore(likepost.FieldID)
}
if _, exists := u.create.mutation.CreatedAt(); exists {
s.SetIgnore(likepost.FieldCreatedAt)
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.LikePost.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *LikePostUpsertOne) Ignore() *LikePostUpsertOne {
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 *LikePostUpsertOne) DoNothing() *LikePostUpsertOne {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the LikePostCreate.OnConflict
// documentation for more info.
func (u *LikePostUpsertOne) Update(set func(*LikePostUpsert)) *LikePostUpsertOne {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&LikePostUpsert{UpdateSet: update})
}))
return u
}
// SetAccountID sets the "account_id" field.
func (u *LikePostUpsertOne) SetAccountID(v xid.ID) *LikePostUpsertOne {
return u.Update(func(s *LikePostUpsert) {
s.SetAccountID(v)
})
}
// UpdateAccountID sets the "account_id" field to the value that was provided on create.
func (u *LikePostUpsertOne) UpdateAccountID() *LikePostUpsertOne {
return u.Update(func(s *LikePostUpsert) {
s.UpdateAccountID()
})
}
// SetPostID sets the "post_id" field.
func (u *LikePostUpsertOne) SetPostID(v xid.ID) *LikePostUpsertOne {
return u.Update(func(s *LikePostUpsert) {
s.SetPostID(v)
})
}
// UpdatePostID sets the "post_id" field to the value that was provided on create.
func (u *LikePostUpsertOne) UpdatePostID() *LikePostUpsertOne {
return u.Update(func(s *LikePostUpsert) {
s.UpdatePostID()
})
}
// Exec executes the query.
func (u *LikePostUpsertOne) Exec(ctx context.Context) error {
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for LikePostCreate.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *LikePostUpsertOne) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}
// Exec executes the UPSERT query and returns the inserted/updated ID.
func (u *LikePostUpsertOne) ID(ctx context.Context) (id xid.ID, err error) {
if u.create.driver.Dialect() == dialect.MySQL {
// In case of "ON CONFLICT", there is no way to get back non-numeric ID
// fields from the database since MySQL does not support the RETURNING clause.
return id, errors.New("ent: LikePostUpsertOne.ID is not supported by MySQL driver. Use LikePostUpsertOne.Exec instead")
}
node, err := u.create.Save(ctx)
if err != nil {
return id, err
}
return node.ID, nil
}
// IDX is like ID, but panics if an error occurs.
func (u *LikePostUpsertOne) IDX(ctx context.Context) xid.ID {
id, err := u.ID(ctx)
if err != nil {
panic(err)
}
return id
}
// LikePostCreateBulk is the builder for creating many LikePost entities in bulk.
type LikePostCreateBulk struct {
config
err error
builders []*LikePostCreate
conflict []sql.ConflictOption
}
// Save creates the LikePost entities in the database.
func (_c *LikePostCreateBulk) Save(ctx context.Context) ([]*LikePost, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*LikePost, 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.(*LikePostMutation)
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.id = &nodes[i].ID
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 *LikePostCreateBulk) SaveX(ctx context.Context) []*LikePost {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *LikePostCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *LikePostCreateBulk) 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.LikePost.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.LikePostUpsert) {
// SetCreatedAt(v+v).
// }).
// Exec(ctx)
func (_c *LikePostCreateBulk) OnConflict(opts ...sql.ConflictOption) *LikePostUpsertBulk {
_c.conflict = opts
return &LikePostUpsertBulk{
create: _c,
}
}
// OnConflictColumns calls `OnConflict` and configures the columns
// as conflict target. Using this option is equivalent to using:
//
// client.LikePost.Create().
// OnConflict(sql.ConflictColumns(columns...)).
// Exec(ctx)
func (_c *LikePostCreateBulk) OnConflictColumns(columns ...string) *LikePostUpsertBulk {
_c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
return &LikePostUpsertBulk{
create: _c,
}
}
// LikePostUpsertBulk is the builder for "upsert"-ing
// a bulk of LikePost nodes.
type LikePostUpsertBulk struct {
create *LikePostCreateBulk
}
// UpdateNewValues updates the mutable fields using the new values that
// were set on create. Using this option is equivalent to using:
//
// client.LikePost.Create().
// OnConflict(
// sql.ResolveWithNewValues(),
// sql.ResolveWith(func(u *sql.UpdateSet) {
// u.SetIgnore(likepost.FieldID)
// }),
// ).
// Exec(ctx)
func (u *LikePostUpsertBulk) UpdateNewValues() *LikePostUpsertBulk {
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.ID(); exists {
s.SetIgnore(likepost.FieldID)
}
if _, exists := b.mutation.CreatedAt(); exists {
s.SetIgnore(likepost.FieldCreatedAt)
}
}
}))
return u
}
// Ignore sets each column to itself in case of conflict.
// Using this option is equivalent to using:
//
// client.LikePost.Create().
// OnConflict(sql.ResolveWithIgnore()).
// Exec(ctx)
func (u *LikePostUpsertBulk) Ignore() *LikePostUpsertBulk {
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 *LikePostUpsertBulk) DoNothing() *LikePostUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.DoNothing())
return u
}
// Update allows overriding fields `UPDATE` values. See the LikePostCreateBulk.OnConflict
// documentation for more info.
func (u *LikePostUpsertBulk) Update(set func(*LikePostUpsert)) *LikePostUpsertBulk {
u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
set(&LikePostUpsert{UpdateSet: update})
}))
return u
}
// SetAccountID sets the "account_id" field.
func (u *LikePostUpsertBulk) SetAccountID(v xid.ID) *LikePostUpsertBulk {
return u.Update(func(s *LikePostUpsert) {
s.SetAccountID(v)
})
}
// UpdateAccountID sets the "account_id" field to the value that was provided on create.
func (u *LikePostUpsertBulk) UpdateAccountID() *LikePostUpsertBulk {
return u.Update(func(s *LikePostUpsert) {
s.UpdateAccountID()
})
}
// SetPostID sets the "post_id" field.
func (u *LikePostUpsertBulk) SetPostID(v xid.ID) *LikePostUpsertBulk {
return u.Update(func(s *LikePostUpsert) {
s.SetPostID(v)
})
}
// UpdatePostID sets the "post_id" field to the value that was provided on create.
func (u *LikePostUpsertBulk) UpdatePostID() *LikePostUpsertBulk {
return u.Update(func(s *LikePostUpsert) {
s.UpdatePostID()
})
}
// Exec executes the query.
func (u *LikePostUpsertBulk) 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 LikePostCreateBulk instead", i)
}
}
if len(u.create.conflict) == 0 {
return errors.New("ent: missing options for LikePostCreateBulk.OnConflict")
}
return u.create.Exec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (u *LikePostUpsertBulk) ExecX(ctx context.Context) {
if err := u.create.Exec(ctx); err != nil {
panic(err)
}
}