tss-rocks/backend/ent/dailycategory_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

291 lines
8.3 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"tss-rocks-be/ent/daily"
"tss-rocks-be/ent/dailycategory"
"tss-rocks-be/ent/dailycategorycontent"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
)
// DailyCategoryCreate is the builder for creating a DailyCategory entity.
type DailyCategoryCreate struct {
config
mutation *DailyCategoryMutation
hooks []Hook
}
// SetCreatedAt sets the "created_at" field.
func (dcc *DailyCategoryCreate) SetCreatedAt(t time.Time) *DailyCategoryCreate {
dcc.mutation.SetCreatedAt(t)
return dcc
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (dcc *DailyCategoryCreate) SetNillableCreatedAt(t *time.Time) *DailyCategoryCreate {
if t != nil {
dcc.SetCreatedAt(*t)
}
return dcc
}
// SetUpdatedAt sets the "updated_at" field.
func (dcc *DailyCategoryCreate) SetUpdatedAt(t time.Time) *DailyCategoryCreate {
dcc.mutation.SetUpdatedAt(t)
return dcc
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (dcc *DailyCategoryCreate) SetNillableUpdatedAt(t *time.Time) *DailyCategoryCreate {
if t != nil {
dcc.SetUpdatedAt(*t)
}
return dcc
}
// AddContentIDs adds the "contents" edge to the DailyCategoryContent entity by IDs.
func (dcc *DailyCategoryCreate) AddContentIDs(ids ...int) *DailyCategoryCreate {
dcc.mutation.AddContentIDs(ids...)
return dcc
}
// AddContents adds the "contents" edges to the DailyCategoryContent entity.
func (dcc *DailyCategoryCreate) AddContents(d ...*DailyCategoryContent) *DailyCategoryCreate {
ids := make([]int, len(d))
for i := range d {
ids[i] = d[i].ID
}
return dcc.AddContentIDs(ids...)
}
// AddDailyItemIDs adds the "daily_items" edge to the Daily entity by IDs.
func (dcc *DailyCategoryCreate) AddDailyItemIDs(ids ...string) *DailyCategoryCreate {
dcc.mutation.AddDailyItemIDs(ids...)
return dcc
}
// AddDailyItems adds the "daily_items" edges to the Daily entity.
func (dcc *DailyCategoryCreate) AddDailyItems(d ...*Daily) *DailyCategoryCreate {
ids := make([]string, len(d))
for i := range d {
ids[i] = d[i].ID
}
return dcc.AddDailyItemIDs(ids...)
}
// Mutation returns the DailyCategoryMutation object of the builder.
func (dcc *DailyCategoryCreate) Mutation() *DailyCategoryMutation {
return dcc.mutation
}
// Save creates the DailyCategory in the database.
func (dcc *DailyCategoryCreate) Save(ctx context.Context) (*DailyCategory, error) {
dcc.defaults()
return withHooks(ctx, dcc.sqlSave, dcc.mutation, dcc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (dcc *DailyCategoryCreate) SaveX(ctx context.Context) *DailyCategory {
v, err := dcc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (dcc *DailyCategoryCreate) Exec(ctx context.Context) error {
_, err := dcc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (dcc *DailyCategoryCreate) ExecX(ctx context.Context) {
if err := dcc.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (dcc *DailyCategoryCreate) defaults() {
if _, ok := dcc.mutation.CreatedAt(); !ok {
v := dailycategory.DefaultCreatedAt()
dcc.mutation.SetCreatedAt(v)
}
if _, ok := dcc.mutation.UpdatedAt(); !ok {
v := dailycategory.DefaultUpdatedAt()
dcc.mutation.SetUpdatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (dcc *DailyCategoryCreate) check() error {
if _, ok := dcc.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "DailyCategory.created_at"`)}
}
if _, ok := dcc.mutation.UpdatedAt(); !ok {
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "DailyCategory.updated_at"`)}
}
return nil
}
func (dcc *DailyCategoryCreate) sqlSave(ctx context.Context) (*DailyCategory, error) {
if err := dcc.check(); err != nil {
return nil, err
}
_node, _spec := dcc.createSpec()
if err := sqlgraph.CreateNode(ctx, dcc.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)
dcc.mutation.id = &_node.ID
dcc.mutation.done = true
return _node, nil
}
func (dcc *DailyCategoryCreate) createSpec() (*DailyCategory, *sqlgraph.CreateSpec) {
var (
_node = &DailyCategory{config: dcc.config}
_spec = sqlgraph.NewCreateSpec(dailycategory.Table, sqlgraph.NewFieldSpec(dailycategory.FieldID, field.TypeInt))
)
if value, ok := dcc.mutation.CreatedAt(); ok {
_spec.SetField(dailycategory.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := dcc.mutation.UpdatedAt(); ok {
_spec.SetField(dailycategory.FieldUpdatedAt, field.TypeTime, value)
_node.UpdatedAt = value
}
if nodes := dcc.mutation.ContentsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: dailycategory.ContentsTable,
Columns: []string{dailycategory.ContentsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(dailycategorycontent.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := dcc.mutation.DailyItemsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: dailycategory.DailyItemsTable,
Columns: []string{dailycategory.DailyItemsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(daily.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// DailyCategoryCreateBulk is the builder for creating many DailyCategory entities in bulk.
type DailyCategoryCreateBulk struct {
config
err error
builders []*DailyCategoryCreate
}
// Save creates the DailyCategory entities in the database.
func (dccb *DailyCategoryCreateBulk) Save(ctx context.Context) ([]*DailyCategory, error) {
if dccb.err != nil {
return nil, dccb.err
}
specs := make([]*sqlgraph.CreateSpec, len(dccb.builders))
nodes := make([]*DailyCategory, len(dccb.builders))
mutators := make([]Mutator, len(dccb.builders))
for i := range dccb.builders {
func(i int, root context.Context) {
builder := dccb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*DailyCategoryMutation)
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, dccb.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, dccb.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, dccb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (dccb *DailyCategoryCreateBulk) SaveX(ctx context.Context) []*DailyCategory {
v, err := dccb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (dccb *DailyCategoryCreateBulk) Exec(ctx context.Context) error {
_, err := dccb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (dccb *DailyCategoryCreateBulk) ExecX(ctx context.Context) {
if err := dccb.Exec(ctx); err != nil {
panic(err)
}
}