tss-rocks/backend/ent/postcontent_create.go
CDN 05ddc1f783
Some checks failed
Build Backend / Build Docker Image (push) Successful in 3m33s
Test Backend / test (push) Failing after 31s
[feature] migrate to monorepo
2025-02-21 00:49:20 +08:00

333 lines
10 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"tss-rocks-be/ent/post"
"tss-rocks-be/ent/postcontent"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
)
// PostContentCreate is the builder for creating a PostContent entity.
type PostContentCreate struct {
config
mutation *PostContentMutation
hooks []Hook
}
// SetLanguageCode sets the "language_code" field.
func (pcc *PostContentCreate) SetLanguageCode(pc postcontent.LanguageCode) *PostContentCreate {
pcc.mutation.SetLanguageCode(pc)
return pcc
}
// SetTitle sets the "title" field.
func (pcc *PostContentCreate) SetTitle(s string) *PostContentCreate {
pcc.mutation.SetTitle(s)
return pcc
}
// SetContentMarkdown sets the "content_markdown" field.
func (pcc *PostContentCreate) SetContentMarkdown(s string) *PostContentCreate {
pcc.mutation.SetContentMarkdown(s)
return pcc
}
// SetSummary sets the "summary" field.
func (pcc *PostContentCreate) SetSummary(s string) *PostContentCreate {
pcc.mutation.SetSummary(s)
return pcc
}
// SetMetaKeywords sets the "meta_keywords" field.
func (pcc *PostContentCreate) SetMetaKeywords(s string) *PostContentCreate {
pcc.mutation.SetMetaKeywords(s)
return pcc
}
// SetNillableMetaKeywords sets the "meta_keywords" field if the given value is not nil.
func (pcc *PostContentCreate) SetNillableMetaKeywords(s *string) *PostContentCreate {
if s != nil {
pcc.SetMetaKeywords(*s)
}
return pcc
}
// SetMetaDescription sets the "meta_description" field.
func (pcc *PostContentCreate) SetMetaDescription(s string) *PostContentCreate {
pcc.mutation.SetMetaDescription(s)
return pcc
}
// SetNillableMetaDescription sets the "meta_description" field if the given value is not nil.
func (pcc *PostContentCreate) SetNillableMetaDescription(s *string) *PostContentCreate {
if s != nil {
pcc.SetMetaDescription(*s)
}
return pcc
}
// SetSlug sets the "slug" field.
func (pcc *PostContentCreate) SetSlug(s string) *PostContentCreate {
pcc.mutation.SetSlug(s)
return pcc
}
// SetPostID sets the "post" edge to the Post entity by ID.
func (pcc *PostContentCreate) SetPostID(id int) *PostContentCreate {
pcc.mutation.SetPostID(id)
return pcc
}
// SetNillablePostID sets the "post" edge to the Post entity by ID if the given value is not nil.
func (pcc *PostContentCreate) SetNillablePostID(id *int) *PostContentCreate {
if id != nil {
pcc = pcc.SetPostID(*id)
}
return pcc
}
// SetPost sets the "post" edge to the Post entity.
func (pcc *PostContentCreate) SetPost(p *Post) *PostContentCreate {
return pcc.SetPostID(p.ID)
}
// Mutation returns the PostContentMutation object of the builder.
func (pcc *PostContentCreate) Mutation() *PostContentMutation {
return pcc.mutation
}
// Save creates the PostContent in the database.
func (pcc *PostContentCreate) Save(ctx context.Context) (*PostContent, error) {
return withHooks(ctx, pcc.sqlSave, pcc.mutation, pcc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (pcc *PostContentCreate) SaveX(ctx context.Context) *PostContent {
v, err := pcc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (pcc *PostContentCreate) Exec(ctx context.Context) error {
_, err := pcc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (pcc *PostContentCreate) ExecX(ctx context.Context) {
if err := pcc.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (pcc *PostContentCreate) check() error {
if _, ok := pcc.mutation.LanguageCode(); !ok {
return &ValidationError{Name: "language_code", err: errors.New(`ent: missing required field "PostContent.language_code"`)}
}
if v, ok := pcc.mutation.LanguageCode(); ok {
if err := postcontent.LanguageCodeValidator(v); err != nil {
return &ValidationError{Name: "language_code", err: fmt.Errorf(`ent: validator failed for field "PostContent.language_code": %w`, err)}
}
}
if _, ok := pcc.mutation.Title(); !ok {
return &ValidationError{Name: "title", err: errors.New(`ent: missing required field "PostContent.title"`)}
}
if v, ok := pcc.mutation.Title(); ok {
if err := postcontent.TitleValidator(v); err != nil {
return &ValidationError{Name: "title", err: fmt.Errorf(`ent: validator failed for field "PostContent.title": %w`, err)}
}
}
if _, ok := pcc.mutation.ContentMarkdown(); !ok {
return &ValidationError{Name: "content_markdown", err: errors.New(`ent: missing required field "PostContent.content_markdown"`)}
}
if v, ok := pcc.mutation.ContentMarkdown(); ok {
if err := postcontent.ContentMarkdownValidator(v); err != nil {
return &ValidationError{Name: "content_markdown", err: fmt.Errorf(`ent: validator failed for field "PostContent.content_markdown": %w`, err)}
}
}
if _, ok := pcc.mutation.Summary(); !ok {
return &ValidationError{Name: "summary", err: errors.New(`ent: missing required field "PostContent.summary"`)}
}
if v, ok := pcc.mutation.Summary(); ok {
if err := postcontent.SummaryValidator(v); err != nil {
return &ValidationError{Name: "summary", err: fmt.Errorf(`ent: validator failed for field "PostContent.summary": %w`, err)}
}
}
if _, ok := pcc.mutation.Slug(); !ok {
return &ValidationError{Name: "slug", err: errors.New(`ent: missing required field "PostContent.slug"`)}
}
if v, ok := pcc.mutation.Slug(); ok {
if err := postcontent.SlugValidator(v); err != nil {
return &ValidationError{Name: "slug", err: fmt.Errorf(`ent: validator failed for field "PostContent.slug": %w`, err)}
}
}
return nil
}
func (pcc *PostContentCreate) sqlSave(ctx context.Context) (*PostContent, error) {
if err := pcc.check(); err != nil {
return nil, err
}
_node, _spec := pcc.createSpec()
if err := sqlgraph.CreateNode(ctx, pcc.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)
pcc.mutation.id = &_node.ID
pcc.mutation.done = true
return _node, nil
}
func (pcc *PostContentCreate) createSpec() (*PostContent, *sqlgraph.CreateSpec) {
var (
_node = &PostContent{config: pcc.config}
_spec = sqlgraph.NewCreateSpec(postcontent.Table, sqlgraph.NewFieldSpec(postcontent.FieldID, field.TypeInt))
)
if value, ok := pcc.mutation.LanguageCode(); ok {
_spec.SetField(postcontent.FieldLanguageCode, field.TypeEnum, value)
_node.LanguageCode = value
}
if value, ok := pcc.mutation.Title(); ok {
_spec.SetField(postcontent.FieldTitle, field.TypeString, value)
_node.Title = value
}
if value, ok := pcc.mutation.ContentMarkdown(); ok {
_spec.SetField(postcontent.FieldContentMarkdown, field.TypeString, value)
_node.ContentMarkdown = value
}
if value, ok := pcc.mutation.Summary(); ok {
_spec.SetField(postcontent.FieldSummary, field.TypeString, value)
_node.Summary = value
}
if value, ok := pcc.mutation.MetaKeywords(); ok {
_spec.SetField(postcontent.FieldMetaKeywords, field.TypeString, value)
_node.MetaKeywords = value
}
if value, ok := pcc.mutation.MetaDescription(); ok {
_spec.SetField(postcontent.FieldMetaDescription, field.TypeString, value)
_node.MetaDescription = value
}
if value, ok := pcc.mutation.Slug(); ok {
_spec.SetField(postcontent.FieldSlug, field.TypeString, value)
_node.Slug = value
}
if nodes := pcc.mutation.PostIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: postcontent.PostTable,
Columns: []string{postcontent.PostColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(post.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.post_contents = &nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// PostContentCreateBulk is the builder for creating many PostContent entities in bulk.
type PostContentCreateBulk struct {
config
err error
builders []*PostContentCreate
}
// Save creates the PostContent entities in the database.
func (pccb *PostContentCreateBulk) Save(ctx context.Context) ([]*PostContent, error) {
if pccb.err != nil {
return nil, pccb.err
}
specs := make([]*sqlgraph.CreateSpec, len(pccb.builders))
nodes := make([]*PostContent, len(pccb.builders))
mutators := make([]Mutator, len(pccb.builders))
for i := range pccb.builders {
func(i int, root context.Context) {
builder := pccb.builders[i]
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*PostContentMutation)
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, pccb.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, pccb.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, pccb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (pccb *PostContentCreateBulk) SaveX(ctx context.Context) []*PostContent {
v, err := pccb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (pccb *PostContentCreateBulk) Exec(ctx context.Context) error {
_, err := pccb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (pccb *PostContentCreateBulk) ExecX(ctx context.Context) {
if err := pccb.Exec(ctx); err != nil {
panic(err)
}
}