279 lines
8.4 KiB
Go
279 lines
8.4 KiB
Go
// Code generated by ent, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"tss-rocks-be/ent/category"
|
|
"tss-rocks-be/ent/categorycontent"
|
|
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
"entgo.io/ent/schema/field"
|
|
)
|
|
|
|
// CategoryContentCreate is the builder for creating a CategoryContent entity.
|
|
type CategoryContentCreate struct {
|
|
config
|
|
mutation *CategoryContentMutation
|
|
hooks []Hook
|
|
}
|
|
|
|
// SetLanguageCode sets the "language_code" field.
|
|
func (ccc *CategoryContentCreate) SetLanguageCode(cc categorycontent.LanguageCode) *CategoryContentCreate {
|
|
ccc.mutation.SetLanguageCode(cc)
|
|
return ccc
|
|
}
|
|
|
|
// SetName sets the "name" field.
|
|
func (ccc *CategoryContentCreate) SetName(s string) *CategoryContentCreate {
|
|
ccc.mutation.SetName(s)
|
|
return ccc
|
|
}
|
|
|
|
// SetDescription sets the "description" field.
|
|
func (ccc *CategoryContentCreate) SetDescription(s string) *CategoryContentCreate {
|
|
ccc.mutation.SetDescription(s)
|
|
return ccc
|
|
}
|
|
|
|
// SetNillableDescription sets the "description" field if the given value is not nil.
|
|
func (ccc *CategoryContentCreate) SetNillableDescription(s *string) *CategoryContentCreate {
|
|
if s != nil {
|
|
ccc.SetDescription(*s)
|
|
}
|
|
return ccc
|
|
}
|
|
|
|
// SetSlug sets the "slug" field.
|
|
func (ccc *CategoryContentCreate) SetSlug(s string) *CategoryContentCreate {
|
|
ccc.mutation.SetSlug(s)
|
|
return ccc
|
|
}
|
|
|
|
// SetCategoryID sets the "category" edge to the Category entity by ID.
|
|
func (ccc *CategoryContentCreate) SetCategoryID(id int) *CategoryContentCreate {
|
|
ccc.mutation.SetCategoryID(id)
|
|
return ccc
|
|
}
|
|
|
|
// SetNillableCategoryID sets the "category" edge to the Category entity by ID if the given value is not nil.
|
|
func (ccc *CategoryContentCreate) SetNillableCategoryID(id *int) *CategoryContentCreate {
|
|
if id != nil {
|
|
ccc = ccc.SetCategoryID(*id)
|
|
}
|
|
return ccc
|
|
}
|
|
|
|
// SetCategory sets the "category" edge to the Category entity.
|
|
func (ccc *CategoryContentCreate) SetCategory(c *Category) *CategoryContentCreate {
|
|
return ccc.SetCategoryID(c.ID)
|
|
}
|
|
|
|
// Mutation returns the CategoryContentMutation object of the builder.
|
|
func (ccc *CategoryContentCreate) Mutation() *CategoryContentMutation {
|
|
return ccc.mutation
|
|
}
|
|
|
|
// Save creates the CategoryContent in the database.
|
|
func (ccc *CategoryContentCreate) Save(ctx context.Context) (*CategoryContent, error) {
|
|
return withHooks(ctx, ccc.sqlSave, ccc.mutation, ccc.hooks)
|
|
}
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
func (ccc *CategoryContentCreate) SaveX(ctx context.Context) *CategoryContent {
|
|
v, err := ccc.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (ccc *CategoryContentCreate) Exec(ctx context.Context) error {
|
|
_, err := ccc.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (ccc *CategoryContentCreate) ExecX(ctx context.Context) {
|
|
if err := ccc.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (ccc *CategoryContentCreate) check() error {
|
|
if _, ok := ccc.mutation.LanguageCode(); !ok {
|
|
return &ValidationError{Name: "language_code", err: errors.New(`ent: missing required field "CategoryContent.language_code"`)}
|
|
}
|
|
if v, ok := ccc.mutation.LanguageCode(); ok {
|
|
if err := categorycontent.LanguageCodeValidator(v); err != nil {
|
|
return &ValidationError{Name: "language_code", err: fmt.Errorf(`ent: validator failed for field "CategoryContent.language_code": %w`, err)}
|
|
}
|
|
}
|
|
if _, ok := ccc.mutation.Name(); !ok {
|
|
return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "CategoryContent.name"`)}
|
|
}
|
|
if v, ok := ccc.mutation.Name(); ok {
|
|
if err := categorycontent.NameValidator(v); err != nil {
|
|
return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "CategoryContent.name": %w`, err)}
|
|
}
|
|
}
|
|
if _, ok := ccc.mutation.Slug(); !ok {
|
|
return &ValidationError{Name: "slug", err: errors.New(`ent: missing required field "CategoryContent.slug"`)}
|
|
}
|
|
if v, ok := ccc.mutation.Slug(); ok {
|
|
if err := categorycontent.SlugValidator(v); err != nil {
|
|
return &ValidationError{Name: "slug", err: fmt.Errorf(`ent: validator failed for field "CategoryContent.slug": %w`, err)}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (ccc *CategoryContentCreate) sqlSave(ctx context.Context) (*CategoryContent, error) {
|
|
if err := ccc.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
_node, _spec := ccc.createSpec()
|
|
if err := sqlgraph.CreateNode(ctx, ccc.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)
|
|
ccc.mutation.id = &_node.ID
|
|
ccc.mutation.done = true
|
|
return _node, nil
|
|
}
|
|
|
|
func (ccc *CategoryContentCreate) createSpec() (*CategoryContent, *sqlgraph.CreateSpec) {
|
|
var (
|
|
_node = &CategoryContent{config: ccc.config}
|
|
_spec = sqlgraph.NewCreateSpec(categorycontent.Table, sqlgraph.NewFieldSpec(categorycontent.FieldID, field.TypeInt))
|
|
)
|
|
if value, ok := ccc.mutation.LanguageCode(); ok {
|
|
_spec.SetField(categorycontent.FieldLanguageCode, field.TypeEnum, value)
|
|
_node.LanguageCode = value
|
|
}
|
|
if value, ok := ccc.mutation.Name(); ok {
|
|
_spec.SetField(categorycontent.FieldName, field.TypeString, value)
|
|
_node.Name = value
|
|
}
|
|
if value, ok := ccc.mutation.Description(); ok {
|
|
_spec.SetField(categorycontent.FieldDescription, field.TypeString, value)
|
|
_node.Description = value
|
|
}
|
|
if value, ok := ccc.mutation.Slug(); ok {
|
|
_spec.SetField(categorycontent.FieldSlug, field.TypeString, value)
|
|
_node.Slug = value
|
|
}
|
|
if nodes := ccc.mutation.CategoryIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2O,
|
|
Inverse: true,
|
|
Table: categorycontent.CategoryTable,
|
|
Columns: []string{categorycontent.CategoryColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(category.FieldID, field.TypeInt),
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_node.category_contents = &nodes[0]
|
|
_spec.Edges = append(_spec.Edges, edge)
|
|
}
|
|
return _node, _spec
|
|
}
|
|
|
|
// CategoryContentCreateBulk is the builder for creating many CategoryContent entities in bulk.
|
|
type CategoryContentCreateBulk struct {
|
|
config
|
|
err error
|
|
builders []*CategoryContentCreate
|
|
}
|
|
|
|
// Save creates the CategoryContent entities in the database.
|
|
func (cccb *CategoryContentCreateBulk) Save(ctx context.Context) ([]*CategoryContent, error) {
|
|
if cccb.err != nil {
|
|
return nil, cccb.err
|
|
}
|
|
specs := make([]*sqlgraph.CreateSpec, len(cccb.builders))
|
|
nodes := make([]*CategoryContent, len(cccb.builders))
|
|
mutators := make([]Mutator, len(cccb.builders))
|
|
for i := range cccb.builders {
|
|
func(i int, root context.Context) {
|
|
builder := cccb.builders[i]
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*CategoryContentMutation)
|
|
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, cccb.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, cccb.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, cccb.builders[0].mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nodes, nil
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (cccb *CategoryContentCreateBulk) SaveX(ctx context.Context) []*CategoryContent {
|
|
v, err := cccb.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (cccb *CategoryContentCreateBulk) Exec(ctx context.Context) error {
|
|
_, err := cccb.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (cccb *CategoryContentCreateBulk) ExecX(ctx context.Context) {
|
|
if err := cccb.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|