371 lines
10 KiB
Go
371 lines
10 KiB
Go
// Code generated by ent, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
"tss-rocks-be/ent/category"
|
|
"tss-rocks-be/ent/post"
|
|
"tss-rocks-be/ent/postcontent"
|
|
"tss-rocks-be/ent/postcontributor"
|
|
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
"entgo.io/ent/schema/field"
|
|
)
|
|
|
|
// PostCreate is the builder for creating a Post entity.
|
|
type PostCreate struct {
|
|
config
|
|
mutation *PostMutation
|
|
hooks []Hook
|
|
}
|
|
|
|
// SetStatus sets the "status" field.
|
|
func (pc *PostCreate) SetStatus(po post.Status) *PostCreate {
|
|
pc.mutation.SetStatus(po)
|
|
return pc
|
|
}
|
|
|
|
// SetNillableStatus sets the "status" field if the given value is not nil.
|
|
func (pc *PostCreate) SetNillableStatus(po *post.Status) *PostCreate {
|
|
if po != nil {
|
|
pc.SetStatus(*po)
|
|
}
|
|
return pc
|
|
}
|
|
|
|
// SetSlug sets the "slug" field.
|
|
func (pc *PostCreate) SetSlug(s string) *PostCreate {
|
|
pc.mutation.SetSlug(s)
|
|
return pc
|
|
}
|
|
|
|
// SetCreatedAt sets the "created_at" field.
|
|
func (pc *PostCreate) SetCreatedAt(t time.Time) *PostCreate {
|
|
pc.mutation.SetCreatedAt(t)
|
|
return pc
|
|
}
|
|
|
|
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
|
|
func (pc *PostCreate) SetNillableCreatedAt(t *time.Time) *PostCreate {
|
|
if t != nil {
|
|
pc.SetCreatedAt(*t)
|
|
}
|
|
return pc
|
|
}
|
|
|
|
// SetUpdatedAt sets the "updated_at" field.
|
|
func (pc *PostCreate) SetUpdatedAt(t time.Time) *PostCreate {
|
|
pc.mutation.SetUpdatedAt(t)
|
|
return pc
|
|
}
|
|
|
|
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
|
|
func (pc *PostCreate) SetNillableUpdatedAt(t *time.Time) *PostCreate {
|
|
if t != nil {
|
|
pc.SetUpdatedAt(*t)
|
|
}
|
|
return pc
|
|
}
|
|
|
|
// AddContentIDs adds the "contents" edge to the PostContent entity by IDs.
|
|
func (pc *PostCreate) AddContentIDs(ids ...int) *PostCreate {
|
|
pc.mutation.AddContentIDs(ids...)
|
|
return pc
|
|
}
|
|
|
|
// AddContents adds the "contents" edges to the PostContent entity.
|
|
func (pc *PostCreate) AddContents(p ...*PostContent) *PostCreate {
|
|
ids := make([]int, len(p))
|
|
for i := range p {
|
|
ids[i] = p[i].ID
|
|
}
|
|
return pc.AddContentIDs(ids...)
|
|
}
|
|
|
|
// AddContributorIDs adds the "contributors" edge to the PostContributor entity by IDs.
|
|
func (pc *PostCreate) AddContributorIDs(ids ...int) *PostCreate {
|
|
pc.mutation.AddContributorIDs(ids...)
|
|
return pc
|
|
}
|
|
|
|
// AddContributors adds the "contributors" edges to the PostContributor entity.
|
|
func (pc *PostCreate) AddContributors(p ...*PostContributor) *PostCreate {
|
|
ids := make([]int, len(p))
|
|
for i := range p {
|
|
ids[i] = p[i].ID
|
|
}
|
|
return pc.AddContributorIDs(ids...)
|
|
}
|
|
|
|
// AddCategoryIDs adds the "categories" edge to the Category entity by IDs.
|
|
func (pc *PostCreate) AddCategoryIDs(ids ...int) *PostCreate {
|
|
pc.mutation.AddCategoryIDs(ids...)
|
|
return pc
|
|
}
|
|
|
|
// AddCategories adds the "categories" edges to the Category entity.
|
|
func (pc *PostCreate) AddCategories(c ...*Category) *PostCreate {
|
|
ids := make([]int, len(c))
|
|
for i := range c {
|
|
ids[i] = c[i].ID
|
|
}
|
|
return pc.AddCategoryIDs(ids...)
|
|
}
|
|
|
|
// Mutation returns the PostMutation object of the builder.
|
|
func (pc *PostCreate) Mutation() *PostMutation {
|
|
return pc.mutation
|
|
}
|
|
|
|
// Save creates the Post in the database.
|
|
func (pc *PostCreate) Save(ctx context.Context) (*Post, error) {
|
|
pc.defaults()
|
|
return withHooks(ctx, pc.sqlSave, pc.mutation, pc.hooks)
|
|
}
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
func (pc *PostCreate) SaveX(ctx context.Context) *Post {
|
|
v, err := pc.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (pc *PostCreate) Exec(ctx context.Context) error {
|
|
_, err := pc.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (pc *PostCreate) ExecX(ctx context.Context) {
|
|
if err := pc.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// defaults sets the default values of the builder before save.
|
|
func (pc *PostCreate) defaults() {
|
|
if _, ok := pc.mutation.Status(); !ok {
|
|
v := post.DefaultStatus
|
|
pc.mutation.SetStatus(v)
|
|
}
|
|
if _, ok := pc.mutation.CreatedAt(); !ok {
|
|
v := post.DefaultCreatedAt()
|
|
pc.mutation.SetCreatedAt(v)
|
|
}
|
|
if _, ok := pc.mutation.UpdatedAt(); !ok {
|
|
v := post.DefaultUpdatedAt()
|
|
pc.mutation.SetUpdatedAt(v)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (pc *PostCreate) check() error {
|
|
if _, ok := pc.mutation.Status(); !ok {
|
|
return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "Post.status"`)}
|
|
}
|
|
if v, ok := pc.mutation.Status(); ok {
|
|
if err := post.StatusValidator(v); err != nil {
|
|
return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Post.status": %w`, err)}
|
|
}
|
|
}
|
|
if _, ok := pc.mutation.Slug(); !ok {
|
|
return &ValidationError{Name: "slug", err: errors.New(`ent: missing required field "Post.slug"`)}
|
|
}
|
|
if v, ok := pc.mutation.Slug(); ok {
|
|
if err := post.SlugValidator(v); err != nil {
|
|
return &ValidationError{Name: "slug", err: fmt.Errorf(`ent: validator failed for field "Post.slug": %w`, err)}
|
|
}
|
|
}
|
|
if _, ok := pc.mutation.CreatedAt(); !ok {
|
|
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Post.created_at"`)}
|
|
}
|
|
if _, ok := pc.mutation.UpdatedAt(); !ok {
|
|
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Post.updated_at"`)}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (pc *PostCreate) sqlSave(ctx context.Context) (*Post, error) {
|
|
if err := pc.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
_node, _spec := pc.createSpec()
|
|
if err := sqlgraph.CreateNode(ctx, pc.driver, _spec); err != nil {
|
|
if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
return nil, err
|
|
}
|
|
id := _spec.ID.Value.(int64)
|
|
_node.ID = int(id)
|
|
pc.mutation.id = &_node.ID
|
|
pc.mutation.done = true
|
|
return _node, nil
|
|
}
|
|
|
|
func (pc *PostCreate) createSpec() (*Post, *sqlgraph.CreateSpec) {
|
|
var (
|
|
_node = &Post{config: pc.config}
|
|
_spec = sqlgraph.NewCreateSpec(post.Table, sqlgraph.NewFieldSpec(post.FieldID, field.TypeInt))
|
|
)
|
|
if value, ok := pc.mutation.Status(); ok {
|
|
_spec.SetField(post.FieldStatus, field.TypeEnum, value)
|
|
_node.Status = value
|
|
}
|
|
if value, ok := pc.mutation.Slug(); ok {
|
|
_spec.SetField(post.FieldSlug, field.TypeString, value)
|
|
_node.Slug = value
|
|
}
|
|
if value, ok := pc.mutation.CreatedAt(); ok {
|
|
_spec.SetField(post.FieldCreatedAt, field.TypeTime, value)
|
|
_node.CreatedAt = value
|
|
}
|
|
if value, ok := pc.mutation.UpdatedAt(); ok {
|
|
_spec.SetField(post.FieldUpdatedAt, field.TypeTime, value)
|
|
_node.UpdatedAt = value
|
|
}
|
|
if nodes := pc.mutation.ContentsIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: post.ContentsTable,
|
|
Columns: []string{post.ContentsColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(postcontent.FieldID, field.TypeInt),
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges = append(_spec.Edges, edge)
|
|
}
|
|
if nodes := pc.mutation.ContributorsIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: post.ContributorsTable,
|
|
Columns: []string{post.ContributorsColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(postcontributor.FieldID, field.TypeInt),
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges = append(_spec.Edges, edge)
|
|
}
|
|
if nodes := pc.mutation.CategoriesIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2M,
|
|
Inverse: true,
|
|
Table: post.CategoriesTable,
|
|
Columns: post.CategoriesPrimaryKey,
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(category.FieldID, field.TypeInt),
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges = append(_spec.Edges, edge)
|
|
}
|
|
return _node, _spec
|
|
}
|
|
|
|
// PostCreateBulk is the builder for creating many Post entities in bulk.
|
|
type PostCreateBulk struct {
|
|
config
|
|
err error
|
|
builders []*PostCreate
|
|
}
|
|
|
|
// Save creates the Post entities in the database.
|
|
func (pcb *PostCreateBulk) Save(ctx context.Context) ([]*Post, error) {
|
|
if pcb.err != nil {
|
|
return nil, pcb.err
|
|
}
|
|
specs := make([]*sqlgraph.CreateSpec, len(pcb.builders))
|
|
nodes := make([]*Post, len(pcb.builders))
|
|
mutators := make([]Mutator, len(pcb.builders))
|
|
for i := range pcb.builders {
|
|
func(i int, root context.Context) {
|
|
builder := pcb.builders[i]
|
|
builder.defaults()
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*PostMutation)
|
|
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, pcb.builders[i+1].mutation)
|
|
} else {
|
|
spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
|
|
// Invoke the actual operation on the latest mutation in the chain.
|
|
if err = sqlgraph.BatchCreate(ctx, pcb.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
|
|
if specs[i].ID.Value != nil {
|
|
id := specs[i].ID.Value.(int64)
|
|
nodes[i].ID = int(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, pcb.builders[0].mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nodes, nil
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (pcb *PostCreateBulk) SaveX(ctx context.Context) []*Post {
|
|
v, err := pcb.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (pcb *PostCreateBulk) Exec(ctx context.Context) error {
|
|
_, err := pcb.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (pcb *PostCreateBulk) ExecX(ctx context.Context) {
|
|
if err := pcb.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|