// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "time" "tss-rocks-be/ent/contributor" "tss-rocks-be/ent/contributorrole" "tss-rocks-be/ent/post" "tss-rocks-be/ent/postcontributor" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" ) // PostContributorCreate is the builder for creating a PostContributor entity. type PostContributorCreate struct { config mutation *PostContributorMutation hooks []Hook } // SetLanguageCode sets the "language_code" field. func (pcc *PostContributorCreate) SetLanguageCode(pc postcontributor.LanguageCode) *PostContributorCreate { pcc.mutation.SetLanguageCode(pc) return pcc } // SetNillableLanguageCode sets the "language_code" field if the given value is not nil. func (pcc *PostContributorCreate) SetNillableLanguageCode(pc *postcontributor.LanguageCode) *PostContributorCreate { if pc != nil { pcc.SetLanguageCode(*pc) } return pcc } // SetCreatedAt sets the "created_at" field. func (pcc *PostContributorCreate) SetCreatedAt(t time.Time) *PostContributorCreate { pcc.mutation.SetCreatedAt(t) return pcc } // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. func (pcc *PostContributorCreate) SetNillableCreatedAt(t *time.Time) *PostContributorCreate { if t != nil { pcc.SetCreatedAt(*t) } return pcc } // SetPostID sets the "post" edge to the Post entity by ID. func (pcc *PostContributorCreate) SetPostID(id int) *PostContributorCreate { 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 *PostContributorCreate) SetNillablePostID(id *int) *PostContributorCreate { if id != nil { pcc = pcc.SetPostID(*id) } return pcc } // SetPost sets the "post" edge to the Post entity. func (pcc *PostContributorCreate) SetPost(p *Post) *PostContributorCreate { return pcc.SetPostID(p.ID) } // SetContributorID sets the "contributor" edge to the Contributor entity by ID. func (pcc *PostContributorCreate) SetContributorID(id int) *PostContributorCreate { pcc.mutation.SetContributorID(id) return pcc } // SetNillableContributorID sets the "contributor" edge to the Contributor entity by ID if the given value is not nil. func (pcc *PostContributorCreate) SetNillableContributorID(id *int) *PostContributorCreate { if id != nil { pcc = pcc.SetContributorID(*id) } return pcc } // SetContributor sets the "contributor" edge to the Contributor entity. func (pcc *PostContributorCreate) SetContributor(c *Contributor) *PostContributorCreate { return pcc.SetContributorID(c.ID) } // SetRoleID sets the "role" edge to the ContributorRole entity by ID. func (pcc *PostContributorCreate) SetRoleID(id int) *PostContributorCreate { pcc.mutation.SetRoleID(id) return pcc } // SetNillableRoleID sets the "role" edge to the ContributorRole entity by ID if the given value is not nil. func (pcc *PostContributorCreate) SetNillableRoleID(id *int) *PostContributorCreate { if id != nil { pcc = pcc.SetRoleID(*id) } return pcc } // SetRole sets the "role" edge to the ContributorRole entity. func (pcc *PostContributorCreate) SetRole(c *ContributorRole) *PostContributorCreate { return pcc.SetRoleID(c.ID) } // Mutation returns the PostContributorMutation object of the builder. func (pcc *PostContributorCreate) Mutation() *PostContributorMutation { return pcc.mutation } // Save creates the PostContributor in the database. func (pcc *PostContributorCreate) Save(ctx context.Context) (*PostContributor, error) { pcc.defaults() return withHooks(ctx, pcc.sqlSave, pcc.mutation, pcc.hooks) } // SaveX calls Save and panics if Save returns an error. func (pcc *PostContributorCreate) SaveX(ctx context.Context) *PostContributor { v, err := pcc.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (pcc *PostContributorCreate) Exec(ctx context.Context) error { _, err := pcc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (pcc *PostContributorCreate) ExecX(ctx context.Context) { if err := pcc.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (pcc *PostContributorCreate) defaults() { if _, ok := pcc.mutation.CreatedAt(); !ok { v := postcontributor.DefaultCreatedAt() pcc.mutation.SetCreatedAt(v) } } // check runs all checks and user-defined validators on the builder. func (pcc *PostContributorCreate) check() error { if v, ok := pcc.mutation.LanguageCode(); ok { if err := postcontributor.LanguageCodeValidator(v); err != nil { return &ValidationError{Name: "language_code", err: fmt.Errorf(`ent: validator failed for field "PostContributor.language_code": %w`, err)} } } if _, ok := pcc.mutation.CreatedAt(); !ok { return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "PostContributor.created_at"`)} } return nil } func (pcc *PostContributorCreate) sqlSave(ctx context.Context) (*PostContributor, 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 *PostContributorCreate) createSpec() (*PostContributor, *sqlgraph.CreateSpec) { var ( _node = &PostContributor{config: pcc.config} _spec = sqlgraph.NewCreateSpec(postcontributor.Table, sqlgraph.NewFieldSpec(postcontributor.FieldID, field.TypeInt)) ) if value, ok := pcc.mutation.LanguageCode(); ok { _spec.SetField(postcontributor.FieldLanguageCode, field.TypeEnum, value) _node.LanguageCode = &value } if value, ok := pcc.mutation.CreatedAt(); ok { _spec.SetField(postcontributor.FieldCreatedAt, field.TypeTime, value) _node.CreatedAt = value } if nodes := pcc.mutation.PostIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: postcontributor.PostTable, Columns: []string{postcontributor.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_contributors = &nodes[0] _spec.Edges = append(_spec.Edges, edge) } if nodes := pcc.mutation.ContributorIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: postcontributor.ContributorTable, Columns: []string{postcontributor.ContributorColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(contributor.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.contributor_posts = &nodes[0] _spec.Edges = append(_spec.Edges, edge) } if nodes := pcc.mutation.RoleIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: postcontributor.RoleTable, Columns: []string{postcontributor.RoleColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(contributorrole.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.contributor_role_post_contributors = &nodes[0] _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // PostContributorCreateBulk is the builder for creating many PostContributor entities in bulk. type PostContributorCreateBulk struct { config err error builders []*PostContributorCreate } // Save creates the PostContributor entities in the database. func (pccb *PostContributorCreateBulk) Save(ctx context.Context) ([]*PostContributor, error) { if pccb.err != nil { return nil, pccb.err } specs := make([]*sqlgraph.CreateSpec, len(pccb.builders)) nodes := make([]*PostContributor, len(pccb.builders)) mutators := make([]Mutator, len(pccb.builders)) for i := range pccb.builders { func(i int, root context.Context) { builder := pccb.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*PostContributorMutation) 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 *PostContributorCreateBulk) SaveX(ctx context.Context) []*PostContributor { v, err := pccb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (pccb *PostContributorCreateBulk) Exec(ctx context.Context) error { _, err := pccb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (pccb *PostContributorCreateBulk) ExecX(ctx context.Context) { if err := pccb.Exec(ctx); err != nil { panic(err) } }