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